diff --git a/vms/platformvm/block/builder/builder_test.go b/vms/platformvm/block/builder/builder_test.go index 0dc804ff810..22de84d02a1 100644 --- a/vms/platformvm/block/builder/builder_test.go +++ b/vms/platformvm/block/builder/builder_test.go @@ -33,10 +33,7 @@ func TestBuildBlockBasic(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() // Create a valid transaction tx, err := env.txBuilder.NewCreateChainTx( @@ -78,10 +75,7 @@ func TestBuildBlockDoesNotBuildWithEmptyMempool(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() tx, exists := env.mempool.Peek() require.False(exists) @@ -98,10 +92,7 @@ func TestBuildBlockShouldReward(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() var ( now = env.backend.Clk.Time() @@ -198,10 +189,7 @@ func TestBuildBlockAdvanceTime(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() var ( now = env.backend.Clk.Time() @@ -234,10 +222,7 @@ func TestBuildBlockForceAdvanceTime(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() // Create a valid transaction tx, err := env.txBuilder.NewCreateChainTx( @@ -291,10 +276,7 @@ func TestBuildBlockDropExpiredStakerTxs(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() // The [StartTime] in a staker tx is only validated pre-Durango. // TODO: Delete this test post-Durango activation. @@ -397,10 +379,7 @@ func TestBuildBlockInvalidStakingDurations(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() // Post-Durango, [StartTime] is no longer validated. Staking durations are // based on the current chain timestamp and must be validated. @@ -476,11 +455,7 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - env.ctx.Lock.Lock() - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() // Create a valid transaction tx, err := env.txBuilder.NewCreateChainTx( @@ -509,6 +484,7 @@ func TestPreviouslyDroppedTxsCannotBeReAddedToMempool(t *testing.T) { env.ctx.Lock.Unlock() err = env.network.IssueTx(context.Background(), tx) require.ErrorIs(err, errTestingDropped) + env.ctx.Lock.Lock() _, ok := env.mempool.Get(txID) require.False(ok) @@ -522,11 +498,9 @@ func TestNoErrorOnUnexpectedSetPreferenceDuringBootstrapping(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() + defer env.ctx.Lock.Unlock() + env.isBootstrapped.Set(false) - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() require.True(env.blkManager.SetPreference(ids.GenerateTestID())) // should not panic } diff --git a/vms/platformvm/block/builder/helpers_test.go b/vms/platformvm/block/builder/helpers_test.go index fa7339be6fd..f2d15195ad0 100644 --- a/vms/platformvm/block/builder/helpers_test.go +++ b/vms/platformvm/block/builder/helpers_test.go @@ -211,6 +211,24 @@ func newEnvironment(t *testing.T) *environment { res.blkManager.SetPreference(genesisID) addSubnet(t, res) + t.Cleanup(func() { + res.ctx.Lock.Lock() + defer res.ctx.Lock.Unlock() + + res.Builder.ShutdownBlockTimer() + + if res.isBootstrapped.Get() { + validatorIDs := res.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) + + require.NoError(res.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID)) + + require.NoError(res.state.Commit()) + } + + require.NoError(res.state.Close()) + require.NoError(res.baseDB.Close()) + }) + return res } @@ -398,23 +416,3 @@ func buildGenesisTest(t *testing.T, ctx *snow.Context) []byte { return genesisBytes } - -func shutdownEnvironment(env *environment) error { - env.Builder.ShutdownBlockTimer() - - if env.isBootstrapped.Get() { - validatorIDs := env.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) - - if err := env.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID); err != nil { - return err - } - if err := env.state.Commit(); err != nil { - return err - } - } - - return utils.Err( - env.state.Close(), - env.baseDB.Close(), - ) -} diff --git a/vms/platformvm/block/builder/standard_block_test.go b/vms/platformvm/block/builder/standard_block_test.go index 61dd0d5e2ca..6064b215311 100644 --- a/vms/platformvm/block/builder/standard_block_test.go +++ b/vms/platformvm/block/builder/standard_block_test.go @@ -24,10 +24,7 @@ func TestAtomicTxImports(t *testing.T) { env := newEnvironment(t) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() utxoID := avax.UTXOID{ TxID: ids.Empty.Prefix(1), diff --git a/vms/platformvm/block/executor/helpers_test.go b/vms/platformvm/block/executor/helpers_test.go index 5e7b5a3fce1..1a3d2993328 100644 --- a/vms/platformvm/block/executor/helpers_test.go +++ b/vms/platformvm/block/executor/helpers_test.go @@ -10,6 +10,8 @@ import ( "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" "github.com/ava-labs/avalanchego/chains" @@ -220,6 +222,31 @@ func newEnvironment(t *testing.T, ctrl *gomock.Controller) *environment { // whatever we need } + t.Cleanup(func() { + res.ctx.Lock.Lock() + defer res.ctx.Lock.Unlock() + + if res.mockedState != nil { + // state is mocked, nothing to do here + return + } + + require := require.New(t) + + if res.isBootstrapped.Get() { + validatorIDs := res.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) + + require.NoError(res.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID)) + require.NoError(res.state.Commit()) + } + + if res.state != nil { + require.NoError(res.state.Close()) + } + + require.NoError(res.baseDB.Close()) + }) + return res } @@ -424,33 +451,6 @@ func buildGenesisTest(ctx *snow.Context) []byte { return genesisBytes } -func shutdownEnvironment(t *environment) error { - if t.mockedState != nil { - // state is mocked, nothing to do here - return nil - } - - if t.isBootstrapped.Get() { - validatorIDs := t.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) - - if err := t.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID); err != nil { - return err - } - if err := t.state.Commit(); err != nil { - return err - } - } - - var err error - if t.state != nil { - err = t.state.Close() - } - return utils.Err( - err, - t.baseDB.Close(), - ) -} - func addPendingValidator( env *environment, startTime time.Time, diff --git a/vms/platformvm/block/executor/proposal_block_test.go b/vms/platformvm/block/executor/proposal_block_test.go index 549a3fbe25d..08d1ee8939e 100644 --- a/vms/platformvm/block/executor/proposal_block_test.go +++ b/vms/platformvm/block/executor/proposal_block_test.go @@ -34,9 +34,6 @@ func TestApricotProposalBlockTimeVerification(t *testing.T) { ctrl := gomock.NewController(t) env := newEnvironment(t, ctrl) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() // create apricotParentBlk. It's a standard one for simplicity parentHeight := uint64(2022) @@ -148,9 +145,6 @@ func TestBanffProposalBlockTimeVerification(t *testing.T) { ctrl := gomock.NewController(t) env := newEnvironment(t, ctrl) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.clk.Set(defaultGenesisTime) env.config.BanffTime = time.Time{} // activate Banff env.config.DurangoTime = mockable.MaxTime // deactivate Durango @@ -569,9 +563,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { t.Run(test.description, func(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -722,9 +713,6 @@ func TestBanffProposalBlockUpdateStakers(t *testing.T) { func TestBanffProposalBlockRemoveSubnetValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -865,9 +853,6 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) { t.Run(fmt.Sprintf("tracked %t", tracked), func(ts *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -971,9 +956,6 @@ func TestBanffProposalBlockTrackedSubnet(t *testing.T) { func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff // Case: Timestamp is after next validator start time @@ -1156,9 +1138,6 @@ func TestBanffProposalBlockDelegatorStakerWeight(t *testing.T) { func TestBanffProposalBlockDelegatorStakers(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff // Case: Timestamp is after next validator start time @@ -1341,9 +1320,6 @@ func TestBanffProposalBlockDelegatorStakers(t *testing.T) { func TestAddValidatorProposalBlock(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff env.config.DurangoTime = time.Time{} // activate Durango diff --git a/vms/platformvm/block/executor/standard_block_test.go b/vms/platformvm/block/executor/standard_block_test.go index 435ee6618af..3e0122c9123 100644 --- a/vms/platformvm/block/executor/standard_block_test.go +++ b/vms/platformvm/block/executor/standard_block_test.go @@ -31,9 +31,6 @@ func TestApricotStandardBlockTimeVerification(t *testing.T) { ctrl := gomock.NewController(t) env := newEnvironment(t, ctrl) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() // setup and store parent block // it's a standard block for simplicity @@ -87,9 +84,6 @@ func TestBanffStandardBlockTimeVerification(t *testing.T) { ctrl := gomock.NewController(t) env := newEnvironment(t, ctrl) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() now := env.clk.Time() env.clk.Set(now) env.config.BanffTime = time.Time{} // activate Banff @@ -298,9 +292,6 @@ func TestBanffStandardBlockUpdatePrimaryNetworkStakers(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff // Case: Timestamp is after next validator start time @@ -503,9 +494,6 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) { t.Run(test.description, func(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -605,9 +593,6 @@ func TestBanffStandardBlockUpdateStakers(t *testing.T) { func TestBanffStandardBlockRemoveSubnetValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -704,9 +689,6 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) { t.Run(fmt.Sprintf("tracked %t", tracked), func(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff subnetID := testSubnet1.ID() @@ -767,9 +749,6 @@ func TestBanffStandardBlockTrackedSubnet(t *testing.T) { func TestBanffStandardBlockDelegatorStakerWeight(t *testing.T) { require := require.New(t) env := newEnvironment(t, nil) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() env.config.BanffTime = time.Time{} // activate Banff // Case: Timestamp is after next validator start time diff --git a/vms/platformvm/txs/executor/advance_time_test.go b/vms/platformvm/txs/executor/advance_time_test.go index bb66c7ef7f8..08d5ec6ae07 100644 --- a/vms/platformvm/txs/executor/advance_time_test.go +++ b/vms/platformvm/txs/executor/advance_time_test.go @@ -26,9 +26,7 @@ func TestAdvanceTimeTxUpdatePrimaryNetworkStakers(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() dummyHeight := uint64(1) // Case: Timestamp is after next validator start time @@ -90,9 +88,6 @@ func TestAdvanceTimeTxUpdatePrimaryNetworkStakers(t *testing.T) { func TestAdvanceTimeTxTimestampTooEarly(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() tx, err := env.txBuilder.NewAdvanceTimeTx(env.state.GetTimestamp()) require.NoError(err) @@ -118,6 +113,7 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() + defer env.ctx.Lock.Unlock() // Case: Timestamp is after next validator start time // Add a pending validator @@ -147,14 +143,10 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { require.ErrorIs(err, ErrChildBlockAfterStakerChangeTime) } - require.NoError(shutdownEnvironment(env)) - // Case: Timestamp is after next validator end time env = newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() // fast forward clock to 10 seconds before genesis validators stop validating env.clk.Set(defaultValidateEndTime.Add(-10 * time.Second)) @@ -354,9 +346,7 @@ func TestAdvanceTimeTxUpdateStakers(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() dummyHeight := uint64(1) @@ -459,9 +449,7 @@ func TestAdvanceTimeTxRemoveSubnetValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() subnetID := testSubnet1.ID() env.config.TrackedSubnets.Add(subnetID) @@ -563,9 +551,7 @@ func TestTrackedSubnet(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() dummyHeight := uint64(1) subnetID := testSubnet1.ID() @@ -633,9 +619,7 @@ func TestAdvanceTimeTxDelegatorStakerWeight(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() dummyHeight := uint64(1) // Case: Timestamp is after next validator start time @@ -740,9 +724,7 @@ func TestAdvanceTimeTxDelegatorStakers(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() dummyHeight := uint64(1) // Case: Timestamp is after next validator start time @@ -837,9 +819,7 @@ func TestAdvanceTimeTxInitiallyPrefersCommit(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() now := env.clk.Time() // Proposed advancing timestamp to 1 second after sync bound @@ -867,9 +847,7 @@ func TestAdvanceTimeTxAfterBanff(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() env.clk.Set(defaultGenesisTime) // VM's clock reads the genesis time upgradeTime := env.clk.Time().Add(SyncBound) env.config.BanffTime = upgradeTime @@ -901,9 +879,7 @@ func TestAdvanceTimeTxUnmarshal(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() chainTime := env.state.GetTimestamp() tx, err := env.txBuilder.NewAdvanceTimeTx(chainTime.Add(time.Second)) diff --git a/vms/platformvm/txs/executor/create_chain_test.go b/vms/platformvm/txs/executor/create_chain_test.go index 3b050261647..0342c8dc1cf 100644 --- a/vms/platformvm/txs/executor/create_chain_test.go +++ b/vms/platformvm/txs/executor/create_chain_test.go @@ -27,9 +27,7 @@ func TestCreateChainTxInsufficientControlSigs(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() tx, err := env.txBuilder.NewCreateChainTx( testSubnet1.ID(), @@ -62,9 +60,7 @@ func TestCreateChainTxWrongControlSig(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() tx, err := env.txBuilder.NewCreateChainTx( testSubnet1.ID(), @@ -104,9 +100,7 @@ func TestCreateChainTxNoSuchSubnet(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() tx, err := env.txBuilder.NewCreateChainTx( testSubnet1.ID(), @@ -138,9 +132,7 @@ func TestCreateChainTxValid(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() tx, err := env.txBuilder.NewCreateChainTx( testSubnet1.ID(), @@ -198,9 +190,6 @@ func TestCreateChainTxAP3FeeChange(t *testing.T) { env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.config.ApricotPhase3Time = ap3Time - defer func() { - require.NoError(shutdownEnvironment(env)) - }() ins, outs, _, signers, err := env.utxosHandler.Spend(env.state, preFundedKeys, 0, test.fee, ids.ShortEmpty) require.NoError(err) diff --git a/vms/platformvm/txs/executor/create_subnet_test.go b/vms/platformvm/txs/executor/create_subnet_test.go index 158eded7486..6d968daa4df 100644 --- a/vms/platformvm/txs/executor/create_subnet_test.go +++ b/vms/platformvm/txs/executor/create_subnet_test.go @@ -52,9 +52,7 @@ func TestCreateSubnetTxAP3FeeChange(t *testing.T) { env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.config.ApricotPhase3Time = ap3Time env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() ins, outs, _, signers, err := env.utxosHandler.Spend(env.state, preFundedKeys, 0, test.fee, ids.ShortEmpty) require.NoError(err) diff --git a/vms/platformvm/txs/executor/export_test.go b/vms/platformvm/txs/executor/export_test.go index 10085f08436..d9e0ce07100 100644 --- a/vms/platformvm/txs/executor/export_test.go +++ b/vms/platformvm/txs/executor/export_test.go @@ -17,9 +17,7 @@ import ( func TestNewExportTx(t *testing.T) { env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(t, shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() type test struct { description string diff --git a/vms/platformvm/txs/executor/helpers_test.go b/vms/platformvm/txs/executor/helpers_test.go index 1ea645efcfb..b2654ec7c8c 100644 --- a/vms/platformvm/txs/executor/helpers_test.go +++ b/vms/platformvm/txs/executor/helpers_test.go @@ -176,6 +176,30 @@ func newEnvironment(t *testing.T, postBanff, postCortina, postDurango bool) *env addSubnet(t, env, txBuilder) + t.Cleanup(func() { + env.ctx.Lock.Lock() + defer env.ctx.Lock.Unlock() + + require := require.New(t) + + if env.isBootstrapped.Get() { + validatorIDs := env.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) + + require.NoError(env.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID)) + + for subnetID := range env.config.TrackedSubnets { + validatorIDs := env.config.Validators.GetValidatorIDs(subnetID) + + require.NoError(env.uptimes.StopTracking(validatorIDs, subnetID)) + } + env.state.SetHeight(math.MaxUint64) + require.NoError(env.state.Commit()) + } + + require.NoError(env.state.Close()) + require.NoError(env.baseDB.Close()) + }) + return env } @@ -396,30 +420,3 @@ func buildGenesisTest(ctx *snow.Context) []byte { return genesisBytes } - -func shutdownEnvironment(env *environment) error { - if env.isBootstrapped.Get() { - validatorIDs := env.config.Validators.GetValidatorIDs(constants.PrimaryNetworkID) - - if err := env.uptimes.StopTracking(validatorIDs, constants.PrimaryNetworkID); err != nil { - return err - } - - for subnetID := range env.config.TrackedSubnets { - validatorIDs := env.config.Validators.GetValidatorIDs(subnetID) - - if err := env.uptimes.StopTracking(validatorIDs, subnetID); err != nil { - return err - } - } - env.state.SetHeight( /*height*/ math.MaxUint64) - if err := env.state.Commit(); err != nil { - return err - } - } - - return utils.Err( - env.state.Close(), - env.baseDB.Close(), - ) -} diff --git a/vms/platformvm/txs/executor/import_test.go b/vms/platformvm/txs/executor/import_test.go index f9f270801b2..fed09bd882c 100644 --- a/vms/platformvm/txs/executor/import_test.go +++ b/vms/platformvm/txs/executor/import_test.go @@ -23,9 +23,6 @@ import ( func TestNewImportTx(t *testing.T) { env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(t, shutdownEnvironment(env)) - }() type test struct { description string diff --git a/vms/platformvm/txs/executor/proposal_tx_executor_test.go b/vms/platformvm/txs/executor/proposal_tx_executor_test.go index 930c0d9c253..e01bd267a02 100644 --- a/vms/platformvm/txs/executor/proposal_tx_executor_test.go +++ b/vms/platformvm/txs/executor/proposal_tx_executor_test.go @@ -247,9 +247,6 @@ func TestProposalTxExecuteAddDelegator(t *testing.T) { require := require.New(t) freshTH := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) freshTH.config.ApricotPhase3Time = tt.AP3Time - defer func() { - require.NoError(shutdownEnvironment(freshTH)) - }() tx, err := freshTH.txBuilder.NewAddDelegatorTx( tt.stakeAmount, @@ -288,9 +285,7 @@ func TestProposalTxExecuteAddSubnetValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() nodeID := genesisNodeIDs[0] { @@ -723,9 +718,7 @@ func TestProposalTxExecuteAddValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() nodeID := ids.GenerateTestNodeID() chainTime := env.state.GetTimestamp() diff --git a/vms/platformvm/txs/executor/reward_validator_test.go b/vms/platformvm/txs/executor/reward_validator_test.go index 973dea4de9b..3ee34aeb672 100644 --- a/vms/platformvm/txs/executor/reward_validator_test.go +++ b/vms/platformvm/txs/executor/reward_validator_test.go @@ -26,9 +26,6 @@ import ( func TestRewardValidatorTxExecuteOnCommit(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) currentStakerIterator, err := env.state.GetCurrentStakerIterator() @@ -129,9 +126,6 @@ func TestRewardValidatorTxExecuteOnCommit(t *testing.T) { func TestRewardValidatorTxExecuteOnAbort(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) currentStakerIterator, err := env.state.GetCurrentStakerIterator() @@ -226,9 +220,6 @@ func TestRewardValidatorTxExecuteOnAbort(t *testing.T) { func TestRewardDelegatorTxExecuteOnCommitPreDelegateeDeferral(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) vdrRewardAddress := ids.GenerateTestShortID() @@ -350,9 +341,6 @@ func TestRewardDelegatorTxExecuteOnCommitPreDelegateeDeferral(t *testing.T) { func TestRewardDelegatorTxExecuteOnCommitPostDelegateeDeferral(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, true /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) vdrRewardAddress := ids.GenerateTestShortID() @@ -569,9 +557,6 @@ func TestRewardDelegatorTxExecuteOnCommitPostDelegateeDeferral(t *testing.T) { func TestRewardDelegatorTxAndValidatorTxExecuteOnCommitPostDelegateeDeferral(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, true /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) vdrRewardAddress := ids.GenerateTestShortID() @@ -731,9 +716,6 @@ func TestRewardDelegatorTxAndValidatorTxExecuteOnCommitPostDelegateeDeferral(t * func TestRewardDelegatorTxExecuteOnAbort(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) - defer func() { - require.NoError(shutdownEnvironment(env)) - }() dummyHeight := uint64(1) initialSupply, err := env.state.GetCurrentSupply(constants.PrimaryNetworkID) diff --git a/vms/platformvm/txs/executor/standard_tx_executor_test.go b/vms/platformvm/txs/executor/standard_tx_executor_test.go index 2e99150e5a5..b9ad92933d3 100644 --- a/vms/platformvm/txs/executor/standard_tx_executor_test.go +++ b/vms/platformvm/txs/executor/standard_tx_executor_test.go @@ -43,9 +43,7 @@ func TestStandardTxExecutorAddValidatorTxEmptyID(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() chainTime := env.state.GetTimestamp() startTime := defaultValidateStartTime.Add(1 * time.Second) @@ -321,9 +319,6 @@ func TestStandardTxExecutorAddDelegator(t *testing.T) { require := require.New(t) freshTH := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) freshTH.config.ApricotPhase3Time = tt.AP3Time - defer func() { - require.NoError(shutdownEnvironment(freshTH)) - }() tx, err := freshTH.txBuilder.NewAddDelegatorTx( tt.stakeAmount, @@ -360,9 +355,7 @@ func TestStandardTxExecutorAddSubnetValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, false /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() nodeID := genesisNodeIDs[0] @@ -789,9 +782,7 @@ func TestStandardTxExecutorBanffAddValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, false /*=postCortina*/, false /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - }() + defer env.ctx.Lock.Unlock() nodeID := ids.GenerateTestNodeID() @@ -962,10 +953,7 @@ func TestStandardTxExecutorDurangoAddValidator(t *testing.T) { require := require.New(t) env := newEnvironment(t, true /*=postBanff*/, true /*=postCortina*/, true /*=postDurango*/) env.ctx.Lock.Lock() - defer func() { - require.NoError(shutdownEnvironment(env)) - env.ctx.Lock.Unlock() - }() + defer env.ctx.Lock.Unlock() var ( nodeID = ids.GenerateTestNodeID()