-
Notifications
You must be signed in to change notification settings - Fork 701
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use subnet public key diffs after Etna is activated (#3502)
- Loading branch information
1 parent
9f0ff01
commit f500dee
Showing
5 changed files
with
251 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package state_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/upgrade/upgradetest" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/state/statetest" | ||
) | ||
|
||
func TestState_GetEtnaHeight_Activation(t *testing.T) { | ||
require := require.New(t) | ||
|
||
upgrades := upgradetest.GetConfig(upgradetest.Durango) | ||
upgrades.EtnaTime = genesistest.DefaultValidatorStartTime.Add(2 * time.Second) | ||
state := statetest.New(t, statetest.Config{ | ||
Upgrades: upgrades, | ||
}) | ||
|
||
// Etna isn't initially active | ||
_, err := state.GetEtnaHeight() | ||
require.ErrorIs(err, database.ErrNotFound) | ||
|
||
// Etna still isn't active after advancing the time | ||
state.SetHeight(1) | ||
state.SetTimestamp(genesistest.DefaultValidatorStartTime.Add(time.Second)) | ||
require.NoError(state.Commit()) | ||
|
||
_, err = state.GetEtnaHeight() | ||
require.ErrorIs(err, database.ErrNotFound) | ||
|
||
// Etna was just activated | ||
const expectedEtnaHeight uint64 = 2 | ||
state.SetHeight(expectedEtnaHeight) | ||
state.SetTimestamp(genesistest.DefaultValidatorStartTime.Add(2 * time.Second)) | ||
require.NoError(state.Commit()) | ||
|
||
etnaHeight, err := state.GetEtnaHeight() | ||
require.NoError(err) | ||
require.Equal(expectedEtnaHeight, etnaHeight) | ||
|
||
// Etna was previously activated | ||
state.SetHeight(3) | ||
state.SetTimestamp(genesistest.DefaultValidatorStartTime.Add(3 * time.Second)) | ||
require.NoError(state.Commit()) | ||
|
||
etnaHeight, err = state.GetEtnaHeight() | ||
require.NoError(err) | ||
require.Equal(expectedEtnaHeight, etnaHeight) | ||
} | ||
|
||
func TestState_GetEtnaHeight_InitiallyActive(t *testing.T) { | ||
require := require.New(t) | ||
|
||
state := statetest.New(t, statetest.Config{}) | ||
|
||
// Etna is initially active | ||
etnaHeight, err := state.GetEtnaHeight() | ||
require.NoError(err) | ||
require.Zero(etnaHeight) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package validators_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/ava-labs/avalanchego/snow/validators" | ||
"github.com/ava-labs/avalanchego/upgrade/upgradetest" | ||
"github.com/ava-labs/avalanchego/utils/constants" | ||
"github.com/ava-labs/avalanchego/utils/crypto/bls" | ||
"github.com/ava-labs/avalanchego/utils/logging" | ||
"github.com/ava-labs/avalanchego/utils/timer/mockable" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/block" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/config" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/genesis/genesistest" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/metrics" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/state" | ||
"github.com/ava-labs/avalanchego/vms/platformvm/state/statetest" | ||
|
||
. "github.com/ava-labs/avalanchego/vms/platformvm/validators" | ||
) | ||
|
||
func TestGetValidatorSet_AfterEtna(t *testing.T) { | ||
require := require.New(t) | ||
|
||
vdrs := validators.NewManager() | ||
upgrades := upgradetest.GetConfig(upgradetest.Durango) | ||
upgradeTime := genesistest.DefaultValidatorStartTime.Add(2 * time.Second) | ||
upgrades.EtnaTime = upgradeTime | ||
s := statetest.New(t, statetest.Config{ | ||
Validators: vdrs, | ||
Upgrades: upgrades, | ||
}) | ||
|
||
sk, err := bls.NewSecretKey() | ||
require.NoError(err) | ||
var ( | ||
subnetID = ids.GenerateTestID() | ||
startTime = genesistest.DefaultValidatorStartTime | ||
endTime = startTime.Add(24 * time.Hour) | ||
pk = bls.PublicFromSecretKey(sk) | ||
primaryStaker = &state.Staker{ | ||
TxID: ids.GenerateTestID(), | ||
NodeID: ids.GenerateTestNodeID(), | ||
PublicKey: pk, | ||
SubnetID: constants.PrimaryNetworkID, | ||
Weight: 1, | ||
StartTime: startTime, | ||
EndTime: endTime, | ||
PotentialReward: 1, | ||
} | ||
subnetStaker = &state.Staker{ | ||
TxID: ids.GenerateTestID(), | ||
NodeID: primaryStaker.NodeID, | ||
PublicKey: nil, // inherited from primaryStaker | ||
SubnetID: subnetID, | ||
Weight: 1, | ||
StartTime: upgradeTime, | ||
EndTime: endTime, | ||
} | ||
) | ||
|
||
// Add a subnet staker during the Etna upgrade | ||
{ | ||
blk, err := block.NewBanffStandardBlock(upgradeTime, s.GetLastAccepted(), 1, nil) | ||
require.NoError(err) | ||
|
||
s.SetHeight(blk.Height()) | ||
s.SetTimestamp(blk.Timestamp()) | ||
s.AddStatelessBlock(blk) | ||
s.SetLastAccepted(blk.ID()) | ||
|
||
require.NoError(s.PutCurrentValidator(primaryStaker)) | ||
require.NoError(s.PutCurrentValidator(subnetStaker)) | ||
|
||
require.NoError(s.Commit()) | ||
} | ||
|
||
// Remove a subnet staker | ||
{ | ||
blk, err := block.NewBanffStandardBlock(s.GetTimestamp(), s.GetLastAccepted(), 2, nil) | ||
require.NoError(err) | ||
|
||
s.SetHeight(blk.Height()) | ||
s.SetTimestamp(blk.Timestamp()) | ||
s.AddStatelessBlock(blk) | ||
s.SetLastAccepted(blk.ID()) | ||
|
||
s.DeleteCurrentValidator(subnetStaker) | ||
|
||
require.NoError(s.Commit()) | ||
} | ||
|
||
m := NewManager( | ||
logging.NoLog{}, | ||
config.Config{ | ||
Validators: vdrs, | ||
}, | ||
s, | ||
metrics.Noop, | ||
new(mockable.Clock), | ||
) | ||
|
||
expectedValidators := []map[ids.NodeID]*validators.GetValidatorOutput{ | ||
{}, // Subnet staker didn't exist at genesis | ||
{ | ||
subnetStaker.NodeID: { | ||
NodeID: subnetStaker.NodeID, | ||
PublicKey: pk, | ||
Weight: subnetStaker.Weight, | ||
}, | ||
}, // Subnet staker was added at height 1 | ||
{}, // Subnet staker was removed at height 2 | ||
} | ||
for height, expected := range expectedValidators { | ||
actual, err := m.GetValidatorSet(context.Background(), uint64(height), subnetID) | ||
require.NoError(err) | ||
require.Equal(expected, actual) | ||
} | ||
} |