Skip to content

Commit

Permalink
Add statetest to replace common test state initialization (#3319)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Aug 21, 2024
1 parent 35c66e3 commit 5c67c8f
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 179 deletions.
39 changes: 8 additions & 31 deletions vms/platformvm/block/builder/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
Expand Down Expand Up @@ -43,6 +42,7 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/network"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/state/statetest"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/fee"
Expand Down Expand Up @@ -139,7 +139,13 @@ func newEnvironment(t *testing.T, f upgradetest.Fork) *environment { //nolint:un
res.fx = defaultFx(t, res.clk, res.ctx.Log, res.isBootstrapped.Get())

rewardsCalc := reward.NewCalculator(res.config.RewardConfig)
res.state = defaultState(t, res.config, res.ctx, res.baseDB, rewardsCalc)
res.state = statetest.New(t, statetest.Config{
DB: res.baseDB,
Genesis: buildGenesisTest(t, res.ctx),
Validators: res.config.Validators,
Context: res.ctx,
Rewards: rewardsCalc,
})

res.uptimes = uptime.NewManager(res.state, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)
Expand Down Expand Up @@ -263,35 +269,6 @@ func addSubnet(t *testing.T, env *environment) {
require.NoError(env.state.Commit())
}

func defaultState(
t *testing.T,
cfg *config.Config,
ctx *snow.Context,
db database.Database,
rewards reward.Calculator,
) state.State {
require := require.New(t)

execCfg, _ := config.GetExecutionConfig([]byte(`{}`))
genesisBytes := buildGenesisTest(t, ctx)
state, err := state.New(
db,
genesisBytes,
prometheus.NewRegistry(),
cfg,
execCfg,
ctx,
metrics.Noop,
rewards,
)
require.NoError(err)

// persist and reload to init a bunch of in-memory stuff
state.SetHeight(0)
require.NoError(state.Commit())
return state
}

func defaultConfig(f upgradetest.Fork) *config.Config {
upgrades := upgradetest.GetConfigWithUpgradeTime(f, time.Time{})
// This package neglects fork ordering
Expand Down
48 changes: 11 additions & 37 deletions vms/platformvm/block/executor/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/codec"
"github.com/ava-labs/avalanchego/codec/linearcodec"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/database/prefixdb"
"github.com/ava-labs/avalanchego/database/versiondb"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/ava-labs/avalanchego/vms/platformvm/metrics"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/state/statetest"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
Expand Down Expand Up @@ -77,8 +77,7 @@ var (
avaxAssetID = ids.ID{'y', 'e', 'e', 't'}
defaultTxFee = uint64(100)

genesisBlkID ids.ID
testSubnet1 *txs.Tx
testSubnet1 *txs.Tx

// Node IDs of genesis validators. Initialized in init function
genesisNodeIDs []ids.NodeID
Expand Down Expand Up @@ -148,7 +147,14 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) *
rewardsCalc := reward.NewCalculator(res.config.RewardConfig)

if ctrl == nil {
res.state = defaultState(res.config, res.ctx, res.baseDB, rewardsCalc)
res.state = statetest.New(t, statetest.Config{
DB: res.baseDB,
Genesis: buildGenesisTest(res.ctx),
Validators: res.config.Validators,
Context: res.ctx,
Rewards: rewardsCalc,
})

res.uptimes = uptime.NewManager(res.state, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)
res.factory = txstest.NewWalletFactory(
Expand All @@ -157,7 +163,6 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) *
res.state,
)
} else {
genesisBlkID = ids.GenerateTestID()
res.mockedState = state.NewMockState(ctrl)
res.uptimes = uptime.NewManager(res.mockedState, res.clk)
res.utxosVerifier = utxo.NewVerifier(res.ctx, res.clk, res.fx)
Expand All @@ -168,7 +173,7 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller, f upgradetest.Fork) *
)

// setup expectations strictly needed for environment creation
res.mockedState.EXPECT().GetLastAccepted().Return(genesisBlkID).Times(1)
res.mockedState.EXPECT().GetLastAccepted().Return(ids.GenerateTestID()).Times(1)
}

res.backend = &executor.Backend{
Expand Down Expand Up @@ -292,37 +297,6 @@ func addSubnet(env *environment) {
}
}

func defaultState(
cfg *config.Config,
ctx *snow.Context,
db database.Database,
rewards reward.Calculator,
) state.State {
genesisBytes := buildGenesisTest(ctx)
execCfg, _ := config.GetExecutionConfig([]byte(`{}`))
state, err := state.New(
db,
genesisBytes,
prometheus.NewRegistry(),
cfg,
execCfg,
ctx,
metrics.Noop,
rewards,
)
if err != nil {
panic(err)
}

// persist and reload to init a bunch of in-memory stuff
state.SetHeight(0)
if err := state.Commit(); err != nil {
panic(err)
}
genesisBlkID = state.GetLastAccepted()
return state
}

func defaultConfig(f upgradetest.Fork) *config.Config {
upgrades := upgradetest.GetConfigWithUpgradeTime(f, time.Time{})
// This package neglects fork ordering
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/block/executor/proposal_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) {
parentHeight := uint64(2022)

banffParentBlk, err := block.NewApricotStandardBlock(
genesisBlkID, // does not matter
ids.GenerateTestID(), // does not matter
parentHeight,
nil, // txs do not matter in this test
)
Expand Down
12 changes: 7 additions & 5 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/snow/uptime"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/upgrade"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/hashing"
Expand Down Expand Up @@ -285,7 +286,7 @@ type state struct {

validators validators.Manager
ctx *snow.Context
cfg *config.Config
upgrades upgrade.Config
metrics metrics.Metrics
rewards reward.Calculator

Expand Down Expand Up @@ -452,7 +453,8 @@ func New(
db database.Database,
genesisBytes []byte,
metricsReg prometheus.Registerer,
cfg *config.Config,
validators validators.Manager,
upgrades upgrade.Config,
execCfg *config.ExecutionConfig,
ctx *snow.Context,
metrics metrics.Metrics,
Expand Down Expand Up @@ -585,9 +587,9 @@ func New(
s := &state{
validatorState: newValidatorState(),

validators: cfg.Validators,
validators: validators,
ctx: ctx,
cfg: cfg,
upgrades: upgrades,
metrics: metrics,
rewards: rewards,
baseDB: baseDB,
Expand Down Expand Up @@ -1670,7 +1672,7 @@ func (s *state) initValidatorSets() error {

func (s *state) write(updateValidators bool, height uint64) error {
codecVersion := CodecVersion1
if !s.cfg.UpgradeConfig.IsDurangoActivated(s.GetTimestamp()) {
if !s.upgrades.IsDurangoActivated(s.GetTimestamp()) {
codecVersion = CodecVersion0
}

Expand Down
22 changes: 11 additions & 11 deletions vms/platformvm/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/snow/choices"
"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"
Expand Down Expand Up @@ -50,9 +51,8 @@ func newTestState(t testing.TB, db database.Database) *state {
db,
genesistest.NewBytes(t),
prometheus.NewRegistry(),
&config.Config{
Validators: validators.NewManager(),
},
validators.NewManager(),
upgradetest.GetConfig(upgradetest.Latest),
&config.DefaultExecutionConfig,
&snow.Context{
Log: logging.NoLog{},
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestPersistStakers(t *testing.T) {
r.Equal(staker, retrievedStaker)
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.Contains(valsMap, staker.NodeID)
r.Equal(
&validators.GetValidatorOutput{
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestPersistStakers(t *testing.T) {
val, err := s.GetCurrentValidator(staker.SubnetID, staker.NodeID)
r.NoError(err)

valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.Contains(valsMap, staker.NodeID)
valOut := valsMap[staker.NodeID]
r.Equal(valOut.NodeID, staker.NodeID)
Expand Down Expand Up @@ -313,7 +313,7 @@ func TestPersistStakers(t *testing.T) {
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
// pending validators are not showed in validators set
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.NotContains(valsMap, staker.NodeID)
},
checkValidatorUptimes: func(r *require.Assertions, s *state, staker *Staker) {
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestPersistStakers(t *testing.T) {
r.Equal(staker, retrievedDelegator)
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.NotContains(valsMap, staker.NodeID)
},
checkValidatorUptimes: func(*require.Assertions, *state, *Staker) {},
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestPersistStakers(t *testing.T) {
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
// deleted validators are not showed in the validators set anymore
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.NotContains(valsMap, staker.NodeID)
},
checkValidatorUptimes: func(r *require.Assertions, s *state, staker *Staker) {
Expand Down Expand Up @@ -532,7 +532,7 @@ func TestPersistStakers(t *testing.T) {
val, err := s.GetCurrentValidator(staker.SubnetID, staker.NodeID)
r.NoError(err)

valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.Contains(valsMap, staker.NodeID)
valOut := valsMap[staker.NodeID]
r.Equal(valOut.NodeID, staker.NodeID)
Expand Down Expand Up @@ -589,7 +589,7 @@ func TestPersistStakers(t *testing.T) {
r.ErrorIs(err, database.ErrNotFound)
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.NotContains(valsMap, staker.NodeID)
},
checkValidatorUptimes: func(r *require.Assertions, s *state, staker *Staker) {
Expand Down Expand Up @@ -660,7 +660,7 @@ func TestPersistStakers(t *testing.T) {
delIt.Release()
},
checkValidatorsSet: func(r *require.Assertions, s *state, staker *Staker) {
valsMap := s.cfg.Validators.GetMap(staker.SubnetID)
valsMap := s.validators.GetMap(staker.SubnetID)
r.NotContains(valsMap, staker.NodeID)
},
checkValidatorUptimes: func(*require.Assertions, *state, *Staker) {},
Expand Down
Loading

0 comments on commit 5c67c8f

Please sign in to comment.