Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pchain - Cleanup NodeID generation in UTs #2291

Merged
merged 23 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ids/test_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ func GenerateTestShortID() ShortID {
return newShortID
}

// BuildTestNodeID is an utility to build NodeID from bytes in UTs
// It must not be used in production code. In production code we should
// use ToNodeID, which performs proper length checking.
abi87 marked this conversation as resolved.
Show resolved Hide resolved
func BuildTestShortID(src []byte) ShortID {
res := ShortID{}
copy(res[:], src)
return res
}

// GenerateTestNodeID returns a new ID that should only be used for testing
func GenerateTestNodeID() NodeID {
return NodeID(GenerateTestShortID())
Expand Down
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() {
for range preFundedKeys {
nodeID := ids.GenerateTestNodeID()
genesisNodeIDs = append(genesisNodeIDs, nodeID)
}
}
abi87 marked this conversation as resolved.
Show resolved Hide resolved

type mutableSharedMemory struct {
atomic.SharedMemory
}
Expand Down Expand Up @@ -357,9 +367,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() {
for range preFundedKeys {
nodeID := ids.GenerateTestNodeID()
genesisNodeIDs = append(genesisNodeIDs, nodeID)
}
}

abi87 marked this conversation as resolved.
Show resolved Hide resolved
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
55 changes: 19 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,46 @@ 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}
staker0 := staker{
nodeID: ids.NodeID(staker0RewardAddress),
rewardAddress: staker0RewardAddress,
nodeID: ids.BuildTestNodeID([]byte{0xf0}),
abi87 marked this conversation as resolved.
Show resolved Hide resolved
rewardAddress: ids.BuildTestShortID([]byte{0xf0}),
abi87 marked this conversation as resolved.
Show resolved Hide resolved
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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{0xf5}),
startTime: staker2.endTime,
endTime: staker2.endTime.Add(defaultMinStakingDuration),
}
Expand Down Expand Up @@ -546,10 +535,7 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) {
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,
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
staker2.nodeID: current,
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
staker3.nodeID: current,
staker4.nodeID: current,
staker5.nodeID: current,
Expand All @@ -564,7 +550,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 +706,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 +734,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 +846,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 +1128,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
32 changes: 15 additions & 17 deletions vms/platformvm/block/executor/standard_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,38 +364,38 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) {
// Staker4: |------------------------|
// Staker5: |--------------------|
staker1 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf1}),
StephenButtolph marked this conversation as resolved.
Show resolved Hide resolved
rewardAddress: ids.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{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.BuildTestShortID([]byte{0xf4}),
startTime: staker3.startTime,
endTime: staker3.endTime,
}
staker5 := staker{
nodeID: ids.GenerateTestNodeID(),
rewardAddress: ids.GenerateTestShortID(),
nodeID: ids.BuildTestNodeID([]byte{0xf5}),
rewardAddress: ids.BuildTestShortID([]byte{0xf5}),
startTime: staker2.endTime,
endTime: staker2.endTime.Add(defaultMinStakingDuration),
}
Expand Down Expand Up @@ -602,8 +602,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 +630,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 +701,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
Loading