Skip to content

Commit

Permalink
Use wallet in platformvm/txs tests (#3333)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 27, 2024
1 parent 33cc086 commit 0117ab9
Show file tree
Hide file tree
Showing 11 changed files with 532 additions and 742 deletions.
99 changes: 44 additions & 55 deletions vms/platformvm/txs/executor/advance_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package executor

import (
"context"
"fmt"
"testing"
"time"
Expand All @@ -23,8 +22,6 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"

walletsigner "github.com/ava-labs/avalanchego/wallet/chain/p/signer"
)

func newAdvanceTimeTx(t testing.TB, timestamp time.Time) (*txs.Tx, error) {
Expand All @@ -50,14 +47,14 @@ func TestAdvanceTimeTxUpdatePrimaryNetworkStakers(t *testing.T) {
pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second)
pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration)
nodeID := ids.GenerateTestNodeID()
addPendingValidatorTx, err := addPendingValidator(
addPendingValidatorTx := addPendingValidator(
t,
env,
pendingValidatorStartTime,
pendingValidatorEndTime,
nodeID,
[]*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]},
)
require.NoError(err)

tx, err := newAdvanceTimeTx(t, pendingValidatorStartTime)
require.NoError(err)
Expand Down Expand Up @@ -140,8 +137,7 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) {
pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second)
pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration)
nodeID := ids.GenerateTestNodeID()
_, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]})
require.NoError(err)
addPendingValidator(t, env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]})

{
tx, err := newAdvanceTimeTx(t, pendingValidatorStartTime.Add(1*time.Second))
Expand Down Expand Up @@ -378,19 +374,22 @@ func TestAdvanceTimeTxUpdateStakers(t *testing.T) {
env.config.TrackedSubnets.Add(subnetID)

for _, staker := range test.stakers {
_, err := addPendingValidator(
addPendingValidator(
t,
env,
staker.startTime,
staker.endTime,
staker.nodeID,
[]*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]},
)
require.NoError(err)
}

for _, staker := range test.subnetStakers {
builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...)
utx, err := builder.NewAddSubnetValidatorTx(
wallet := newWallet(t, env, walletConfig{
subnetIDs: []ids.ID{subnetID},
})

tx, err := wallet.IssueAddSubnetValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: staker.nodeID,
Expand All @@ -402,8 +401,6 @@ func TestAdvanceTimeTxUpdateStakers(t *testing.T) {
},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

staker, err := state.NewPendingStaker(
tx.ID(),
Expand Down Expand Up @@ -485,13 +482,16 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {
subnetID := testSubnet1.ID()
env.config.TrackedSubnets.Add(subnetID)

wallet := newWallet(t, env, walletConfig{
subnetIDs: []ids.ID{subnetID},
})

dummyHeight := uint64(1)
// Add a subnet validator to the staker set
subnetValidatorNodeID := genesistest.DefaultNodeIDs[0]
subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(defaultMinStakingDuration)

builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...)
utx, err := builder.NewAddSubnetValidatorTx(
tx, err := wallet.IssueAddSubnetValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: subnetValidatorNodeID,
Expand All @@ -503,8 +503,6 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {
},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

addSubnetValTx := tx.Unsigned.(*txs.AddSubnetValidatorTx)
staker, err := state.NewCurrentStaker(
Expand All @@ -524,7 +522,7 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {

// Queue a staker that joins the staker set after the above validator leaves
subnetVdr2NodeID := genesistest.DefaultNodeIDs[1]
utx, err = builder.NewAddSubnetValidatorTx(
tx, err = wallet.IssueAddSubnetValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: subnetVdr2NodeID,
Expand All @@ -536,8 +534,6 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {
},
)
require.NoError(err)
tx, err = walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

staker, err = state.NewPendingStaker(
tx.ID(),
Expand Down Expand Up @@ -601,13 +597,16 @@ func TestTrackedSubnet(t *testing.T) {
env.config.TrackedSubnets.Add(subnetID)
}

wallet := newWallet(t, env, walletConfig{
subnetIDs: []ids.ID{subnetID},
})

// Add a subnet validator to the staker set
subnetValidatorNodeID := genesistest.DefaultNodeIDs[0]

subnetVdr1StartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Minute)
subnetVdr1EndTime := genesistest.DefaultValidatorStartTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute)
builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[:2]...)
utx, err := builder.NewAddSubnetValidatorTx(
tx, err := wallet.IssueAddSubnetValidatorTx(
&txs.SubnetValidator{
Validator: txs.Validator{
NodeID: subnetValidatorNodeID,
Expand All @@ -619,8 +618,6 @@ func TestTrackedSubnet(t *testing.T) {
},
)
require.NoError(err)
tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

staker, err := state.NewPendingStaker(
tx.ID(),
Expand Down Expand Up @@ -676,14 +673,14 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) {
pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second)
pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMaxStakingDuration)
nodeID := ids.GenerateTestNodeID()
_, err := addPendingValidator(
addPendingValidator(
t,
env,
pendingValidatorStartTime,
pendingValidatorEndTime,
nodeID,
[]*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]},
)
require.NoError(err)

tx, err := newAdvanceTimeTx(t, pendingValidatorStartTime)
require.NoError(err)
Expand All @@ -709,6 +706,8 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) {
env.state.SetHeight(dummyHeight)
require.NoError(env.state.Commit())

wallet := newWallet(t, env, walletConfig{})

// Test validator weight before delegation
vdrWeight := env.config.Validators.GetWeight(constants.PrimaryNetworkID, nodeID)
require.Equal(env.config.MinValidatorStake, vdrWeight)
Expand All @@ -717,8 +716,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) {
pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second)
pendingDelegatorEndTime := pendingDelegatorStartTime.Add(1 * time.Second)

builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4])
utx, err := builder.NewAddDelegatorTx(
addDelegatorTx, err := wallet.IssueAddDelegatorTx(
&txs.Validator{
NodeID: nodeID,
Start: uint64(pendingDelegatorStartTime.Unix()),
Expand All @@ -727,12 +725,10 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) {
},
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()},
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
)
require.NoError(err)
addDelegatorTx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

staker, err := state.NewPendingStaker(
addDelegatorTx.ID(),
Expand Down Expand Up @@ -786,8 +782,7 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) {
pendingValidatorStartTime := genesistest.DefaultValidatorStartTime.Add(1 * time.Second)
pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration)
nodeID := ids.GenerateTestNodeID()
_, err := addPendingValidator(env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]})
require.NoError(err)
addPendingValidator(t, env, pendingValidatorStartTime, pendingValidatorEndTime, nodeID, []*secp256k1.PrivateKey{genesistest.DefaultFundedKeys[0]})

tx, err := newAdvanceTimeTx(t, pendingValidatorStartTime)
require.NoError(err)
Expand All @@ -813,15 +808,16 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) {
env.state.SetHeight(dummyHeight)
require.NoError(env.state.Commit())

wallet := newWallet(t, env, walletConfig{})

// Test validator weight before delegation
vdrWeight := env.config.Validators.GetWeight(constants.PrimaryNetworkID, nodeID)
require.Equal(env.config.MinValidatorStake, vdrWeight)

// Add delegator
pendingDelegatorStartTime := pendingValidatorStartTime.Add(1 * time.Second)
pendingDelegatorEndTime := pendingDelegatorStartTime.Add(defaultMinStakingDuration)
builder, signer := env.factory.NewWallet(genesistest.DefaultFundedKeys[0], genesistest.DefaultFundedKeys[1], genesistest.DefaultFundedKeys[4])
utx, err := builder.NewAddDelegatorTx(
addDelegatorTx, err := wallet.IssueAddDelegatorTx(
&txs.Validator{
NodeID: nodeID,
Start: uint64(pendingDelegatorStartTime.Unix()),
Expand All @@ -830,12 +826,10 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) {
},
&secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{genesistest.DefaultFundedKeys[0].Address()},
Addrs: []ids.ShortID{ids.GenerateTestShortID()},
},
)
require.NoError(err)
addDelegatorTx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
require.NoError(err)

staker, err := state.NewPendingStaker(
addDelegatorTx.ID(),
Expand Down Expand Up @@ -935,14 +929,19 @@ func TestAdvanceTimeTxUnmarshal(t *testing.T) {
}

func addPendingValidator(
t testing.TB,
env *environment,
startTime time.Time,
endTime time.Time,
nodeID ids.NodeID,
keys []*secp256k1.PrivateKey,
) (*txs.Tx, error) {
builder, signer := env.factory.NewWallet(keys...)
utx, err := builder.NewAddValidatorTx(
) *txs.Tx {
require := require.New(t)

wallet := newWallet(t, env, walletConfig{
keys: keys,
})
addPendingValidatorTx, err := wallet.IssueAddValidatorTx(
&txs.Validator{
NodeID: nodeID,
Start: uint64(startTime.Unix()),
Expand All @@ -955,28 +954,18 @@ func addPendingValidator(
},
reward.PercentDenominator,
)
if err != nil {
return nil, err
}
addPendingValidatorTx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
if err != nil {
return nil, err
}
require.NoError(err)

staker, err := state.NewPendingStaker(
addPendingValidatorTx.ID(),
addPendingValidatorTx.Unsigned.(*txs.AddValidatorTx),
)
if err != nil {
return nil, err
}
require.NoError(err)

env.state.PutPendingValidator(staker)
env.state.AddTx(addPendingValidatorTx, status.Committed)
dummyHeight := uint64(1)
env.state.SetHeight(dummyHeight)
if err := env.state.Commit(); err != nil {
return nil, err
}
return addPendingValidatorTx, nil
require.NoError(env.state.Commit())
return addPendingValidatorTx
}
Loading

0 comments on commit 0117ab9

Please sign in to comment.