Skip to content

Commit

Permalink
Pchain - Cleanup NodeID generation in UTs (#2291)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Laine <[email protected]>
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
3 people authored Nov 16, 2023
1 parent 043644f commit 35fbb3a
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 192 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/static-handlers/suites.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var _ = ginkgo.Describe("[StaticHandlers]", func() {
GenesisValidator: api.GenesisValidator{
StartTime: json.Uint64(time.Date(1997, 1, 1, 0, 0, 0, 0, time.UTC).Unix()),
EndTime: json.Uint64(time.Date(1997, 1, 30, 0, 0, 0, 0, time.UTC).Unix()),
NodeID: ids.NodeID(id),
NodeID: ids.BuildTestNodeID(id[:]),
},
RewardOwner: &api.Owner{
Threshold: 1,
Expand Down
15 changes: 12 additions & 3 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,19 @@ var (
testSubnet1 *txs.Tx
testSubnet1ControlKeys = preFundedKeys[0:3]

// Node IDs of genesis validators. Initialized in init function
genesisNodeIDs []ids.NodeID

errMissing = errors.New("missing")
)

func init() {
genesisNodeIDs = make([]ids.NodeID, len(preFundedKeys))
for i := range preFundedKeys {
genesisNodeIDs[i] = ids.GenerateTestNodeID()
}
}

type mutableSharedMemory struct {
atomic.SharedMemory
}
Expand Down Expand Up @@ -365,9 +375,8 @@ func buildGenesisTest(t *testing.T, ctx *snow.Context) []byte {
}
}

genesisValidators := make([]api.GenesisPermissionlessValidator, len(preFundedKeys))
for i, key := range preFundedKeys {
nodeID := ids.NodeID(key.PublicKey().Address())
genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs))
for i, nodeID := range genesisNodeIDs {
addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes())
require.NoError(err)
genesisValidators[i] = api.GenesisPermissionlessValidator{
Expand Down
15 changes: 12 additions & 3 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,19 @@ var (
genesisBlkID ids.ID
testSubnet1 *txs.Tx

// Node IDs of genesis validators. Initialized in init function
genesisNodeIDs []ids.NodeID

errMissing = errors.New("missing")
)

func init() {
genesisNodeIDs = make([]ids.NodeID, len(preFundedKeys))
for i := range preFundedKeys {
genesisNodeIDs[i] = ids.GenerateTestNodeID()
}
}

type stakerStatus uint

type staker struct {
Expand Down Expand Up @@ -399,9 +409,8 @@ func buildGenesisTest(ctx *snow.Context) []byte {
}
}

genesisValidators := make([]api.GenesisPermissionlessValidator, len(preFundedKeys))
for i, key := range preFundedKeys {
nodeID := ids.NodeID(key.PublicKey().Address())
genesisValidators := make([]api.GenesisPermissionlessValidator, len(genesisNodeIDs))
for i, nodeID := range genesisNodeIDs {
addr, err := address.FormatBech32(constants.UnitTestHRP, nodeID.Bytes())
if err != nil {
panic(err)
Expand Down
68 changes: 32 additions & 36 deletions vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,57 +392,52 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
// Staker5: |--------------------|

// Staker0 it's here just to allow to issue a proposal block with the chosen endTime.
staker0RewardAddress := ids.ShortID{2}

// In this test multiple stakers may join and leave the staker set at the same time.
// The order in which they do it is asserted; the order may depend on the staker.TxID,
// which in turns depend on every feature of the transaction creating the staker.
// So in this test we avoid ids.GenerateTestNodeID, in favour of ids.BuildTestNodeID
// so that TxID does not depend on the order we run tests.
staker0 := staker{
nodeID: ids.NodeID(staker0RewardAddress),
rewardAddress: staker0RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf0}),
rewardAddress: ids.ShortID{0xf0},
startTime: defaultGenesisTime,
endTime: time.Time{}, // actual endTime depends on specific test
}

staker1RewardAddress := ids.GenerateTestShortID()
staker1 := staker{
nodeID: ids.NodeID(staker1RewardAddress),
rewardAddress: staker1RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf1}),
rewardAddress: ids.ShortID{0xf1},
startTime: defaultGenesisTime.Add(1 * time.Minute),
endTime: defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute),
}

staker2RewardAddress := ids.ShortID{1}
staker2 := staker{
nodeID: ids.NodeID(staker2RewardAddress),
rewardAddress: staker2RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf2}),
rewardAddress: ids.ShortID{0xf2},
startTime: staker1.startTime.Add(1 * time.Minute),
endTime: staker1.startTime.Add(1 * time.Minute).Add(defaultMinStakingDuration),
}

staker3RewardAddress := ids.GenerateTestShortID()
staker3 := staker{
nodeID: ids.NodeID(staker3RewardAddress),
rewardAddress: staker3RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf3}),
rewardAddress: ids.ShortID{0xf3},
startTime: staker2.startTime.Add(1 * time.Minute),
endTime: staker2.endTime.Add(1 * time.Minute),
}

staker3Sub := staker{
nodeID: staker3.nodeID,
rewardAddress: staker3.rewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf3}),
rewardAddress: ids.ShortID{0xff},
startTime: staker3.startTime.Add(1 * time.Minute),
endTime: staker3.endTime.Add(-1 * time.Minute),
}

staker4RewardAddress := ids.GenerateTestShortID()
staker4 := staker{
nodeID: ids.NodeID(staker4RewardAddress),
rewardAddress: staker4RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf4}),
rewardAddress: ids.ShortID{0xf4},
startTime: staker3.startTime,
endTime: staker3.endTime,
}

staker5RewardAddress := ids.GenerateTestShortID()
staker5 := staker{
nodeID: ids.NodeID(staker5RewardAddress),
rewardAddress: staker5RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf5}),
rewardAddress: ids.ShortID{0xf5},
startTime: staker2.endTime,
endTime: staker2.endTime.Add(defaultMinStakingDuration),
}
Expand Down Expand Up @@ -541,15 +536,19 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
},
},
{
description: "advance time to staker5 end",
description: "advance time to staker5 start",
stakers: []staker{staker1, staker2, staker3, staker4, staker5},
advanceTimeTo: []time.Time{staker1.startTime, staker2.startTime, staker3.startTime, staker5.startTime},
expectedStakers: map[ids.NodeID]stakerStatus{
staker1.nodeID: current,

// given its txID, staker2 will be
// rewarded and moved out of current stakers set
// staker2.nodeID: current,
// Staker2's end time matches staker5's start time, so typically
// the block builder would produce a ProposalBlock to remove
// staker2 when advancing the time. However, this test injects
// staker0 into the staker set artificially to advance the time.
// This means that staker2 is not removed by the ProposalBlock
// when advancing the time.
staker2.nodeID: current,
staker3.nodeID: current,
staker4.nodeID: current,
staker5.nodeID: current,
Expand All @@ -564,7 +563,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
defer func() {
require.NoError(shutdownEnvironment(env))
}()

env.config.BanffTime = time.Time{} // activate Banff

subnetID := testSubnet1.ID()
Expand Down Expand Up @@ -721,8 +719,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
env.config.TrackedSubnets.Add(subnetID)

// Add a subnet validator to the staker set
subnetValidatorNodeID := ids.NodeID(preFundedKeys[0].PublicKey().Address())
// Starts after the corre
subnetValidatorNodeID := genesisNodeIDs[0]
subnetVdr1StartTime := defaultValidateStartTime
subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
Expand Down Expand Up @@ -750,7 +747,7 @@ func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) {
// The above validator is now part of the staking set

// Queue a staker that joins the staker set after the above validator leaves
subnetVdr2NodeID := ids.NodeID(preFundedKeys[1].PublicKey().Address())
subnetVdr2NodeID := genesisNodeIDs[1]
tx, err = env.txBuilder.NewAddSubnetValidatorTx(
1, // Weight
uint64(subnetVdr1EndTime.Add(time.Second).Unix()), // Start time
Expand Down Expand Up @@ -862,8 +859,7 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) {
}

// Add a subnet validator to the staker set
subnetValidatorNodeID := ids.NodeID(preFundedKeys[0].PublicKey().Address())

subnetValidatorNodeID := genesisNodeIDs[0]
subnetVdr1StartTime := defaultGenesisTime.Add(1 * time.Minute)
subnetVdr1EndTime := defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
Expand Down Expand Up @@ -1145,7 +1141,7 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) {
pendingValidatorEndTime := pendingValidatorStartTime.Add(defaultMinStakingDuration)
nodeIDKey, _ := secp256k1.NewPrivateKey()
rewardAddress := nodeIDKey.PublicKey().Address()
nodeID := ids.NodeID(rewardAddress)
nodeID := ids.BuildTestNodeID(rewardAddress[:])

_, err := addPendingValidator(
env,
Expand Down
46 changes: 28 additions & 18 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,39 +363,45 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
// Staker3sub: |----------------|
// Staker4: |------------------------|
// Staker5: |--------------------|

// In this test multiple stakers may join and leave the staker set at the same time.
// The order in which they do it is asserted; the order may depend on the staker.TxID,
// which in turns depend on every feature of the transaction creating the staker.
// So in this test we avoid ids.GenerateTestNodeID, in favour of ids.BuildTestNodeID
// so that TxID does not depend on the order we run tests.
staker1 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf1}),
rewardAddress: ids.ShortID{0xf1},
startTime: defaultGenesisTime.Add(1 * time.Minute),
endTime: defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute),
}
staker2 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf2}),
rewardAddress: ids.ShortID{0xf2},
startTime: staker1.startTime.Add(1 * time.Minute),
endTime: staker1.startTime.Add(1 * time.Minute).Add(defaultMinStakingDuration),
}
staker3 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf3}),
rewardAddress: ids.ShortID{0xf3},
startTime: staker2.startTime.Add(1 * time.Minute),
endTime: staker2.endTime.Add(1 * time.Minute),
}
staker3Sub := staker{
nodeID: staker3.nodeID,
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf3}),
rewardAddress: ids.ShortID{0xff},
startTime: staker3.startTime.Add(1 * time.Minute),
endTime: staker3.endTime.Add(-1 * time.Minute),
}
staker4 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf4}),
rewardAddress: ids.ShortID{0xf4},
startTime: staker3.startTime,
endTime: staker3.endTime,
}
staker5 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf5}),
rewardAddress: ids.ShortID{0xf5},
startTime: staker2.endTime,
endTime: staker2.endTime.Add(defaultMinStakingDuration),
}
Expand Down Expand Up @@ -474,11 +480,17 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
},
},
{
description: "advance time to staker5 end",
description: "advance time to staker5 start",
stakers: []staker{staker1, staker2, staker3, staker4, staker5},
advanceTimeTo: []time.Time{staker1.startTime, staker2.startTime, staker3.startTime, staker5.startTime},
expectedStakers: map[ids.NodeID]stakerStatus{
staker1.nodeID: current,

// Staker2's end time matches staker5's start time, so typically
// the block builder would produce a ProposalBlock to remove
// staker2 when advancing the time. However, it is valid to only
// advance the time with a StandardBlock and not remove staker2,
// which is what this test does.
staker2.nodeID: current,
staker3.nodeID: current,
staker4.nodeID: current,
Expand Down Expand Up @@ -602,8 +614,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
env.config.TrackedSubnets.Add(subnetID)

// Add a subnet validator to the staker set
subnetValidatorNodeID := ids.NodeID(preFundedKeys[0].PublicKey().Address())
// Starts after the corre
subnetValidatorNodeID := genesisNodeIDs[0]
subnetVdr1StartTime := defaultValidateStartTime
subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
Expand Down Expand Up @@ -631,7 +642,7 @@ func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) {
// The above validator is now part of the staking set

// Queue a staker that joins the staker set after the above validator leaves
subnetVdr2NodeID := ids.NodeID(preFundedKeys[1].PublicKey().Address())
subnetVdr2NodeID := genesisNodeIDs[1]
tx, err = env.txBuilder.NewAddSubnetValidatorTx(
1, // Weight
uint64(subnetVdr1EndTime.Add(time.Second).Unix()), // Start time
Expand Down Expand Up @@ -702,8 +713,7 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) {
}

// Add a subnet validator to the staker set
subnetValidatorNodeID := ids.NodeID(preFundedKeys[0].PublicKey().Address())

subnetValidatorNodeID := genesisNodeIDs[0]
subnetVdr1StartTime := defaultGenesisTime.Add(1 * time.Minute)
subnetVdr1EndTime := defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ func TestGetStake(t *testing.T) {

// Add a delegator
stakeAmount := service.vm.MinDelegatorStake + 12345
delegatorNodeID := ids.NodeID(keys[0].PublicKey().Address())
delegatorNodeID := genesisNodeIDs[0]
delegatorEndTime := uint64(defaultGenesisTime.Add(defaultMinStakingDuration).Unix())
tx, err := service.vm.txBuilder.NewAddDelegatorTx(
stakeAmount,
Expand Down Expand Up @@ -626,7 +626,7 @@ func TestGetCurrentValidators(t *testing.T) {

// Add a delegator
stakeAmount := service.vm.MinDelegatorStake + 12345
validatorNodeID := ids.NodeID(keys[1].PublicKey().Address())
validatorNodeID := genesisNodeIDs[1]
delegatorStartTime := uint64(defaultValidateStartTime.Unix())
delegatorEndTime := uint64(defaultValidateStartTime.Add(defaultMinStakingDuration).Unix())

Expand Down
13 changes: 6 additions & 7 deletions vms/platformvm/txs/executor/advance_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {

dummyHeight := uint64(1)
// Add a subnet validator to the staker set
subnetValidatorNodeID := ids.NodeID(preFundedKeys[0].PublicKey().Address())
// Starts after the corre
subnetValidatorNodeID := genesisNodeIDs[0]
subnetVdr1StartTime := defaultValidateStartTime
subnetVdr1EndTime := defaultValidateStartTime.Add(defaultMinStakingDuration)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
Expand Down Expand Up @@ -492,7 +491,7 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) {
// The above validator is now part of the staking set

// Queue a staker that joins the staker set after the above validator leaves
subnetVdr2NodeID := ids.NodeID(preFundedKeys[1].PublicKey().Address())
subnetVdr2NodeID := genesisNodeIDs[1]
tx, err = env.txBuilder.NewAddSubnetValidatorTx(
1, // Weight
uint64(subnetVdr1EndTime.Add(time.Second).Unix()), // Start time
Expand Down Expand Up @@ -567,15 +566,15 @@ func TestTrackedSubnet(t *testing.T) {
}

// Add a subnet validator to the staker set
subnetValidatorNodeID := preFundedKeys[0].PublicKey().Address()
subnetValidatorNodeID := genesisNodeIDs[0]

subnetVdr1StartTime := defaultGenesisTime.Add(1 * time.Minute)
subnetVdr1EndTime := defaultGenesisTime.Add(10 * defaultMinStakingDuration).Add(1 * time.Minute)
tx, err := env.txBuilder.NewAddSubnetValidatorTx(
1, // Weight
uint64(subnetVdr1StartTime.Unix()), // Start time
uint64(subnetVdr1EndTime.Unix()), // end time
ids.NodeID(subnetValidatorNodeID), // Node ID
subnetValidatorNodeID, // Node ID
subnetID, // Subnet ID
[]*secp256k1.PrivateKey{preFundedKeys[0], preFundedKeys[1]},
ids.ShortEmpty,
Expand Down Expand Up @@ -616,7 +615,7 @@ func TestTrackedSubnet(t *testing.T) {

env.state.SetHeight(dummyHeight)
require.NoError(env.state.Commit())
_, ok := env.config.Validators.GetValidator(subnetID, ids.NodeID(subnetValidatorNodeID))
_, ok := env.config.Validators.GetValidator(subnetID, subnetValidatorNodeID)
require.True(ok)
})
}
Expand Down Expand Up @@ -923,7 +922,7 @@ func addPendingValidator(
uint64(startTime.Unix()),
uint64(endTime.Unix()),
nodeID,
ids.ShortID(nodeID),
ids.GenerateTestShortID(),
reward.PercentDenominator,
keys,
ids.ShortEmpty,
Expand Down
Loading

0 comments on commit 35fbb3a

Please sign in to comment.