From 8dd5f21847750740e508acd6fb185c5ec9b0bb8e Mon Sep 17 00:00:00 2001 From: StephenButtolph Date: Sun, 10 May 2020 17:03:12 -0400 Subject: [PATCH] clean up locking in the AVM / platformVM tests --- vms/avm/base_tx_test.go | 76 +++++++---- vms/avm/import_tx_test.go | 7 +- vms/avm/prefixed_state_test.go | 26 ++-- vms/avm/service_test.go | 48 ++++--- vms/avm/state_test.go | 32 ++--- vms/avm/vm_test.go | 59 +++++---- .../add_default_subnet_delegator_tx_test.go | 12 +- .../add_default_subnet_validator_tx_test.go | 18 ++- ...add_nondefault_subnet_validator_tx_test.go | 19 ++- vms/platformvm/advance_time_tx_test.go | 39 +++++- vms/platformvm/create_chain_tx_test.go | 36 +++++- vms/platformvm/event_heap_test.go | 24 +++- vms/platformvm/reward_validator_tx_test.go | 18 ++- vms/platformvm/vm_test.go | 122 +++++++++++------- 14 files changed, 356 insertions(+), 180 deletions(-) diff --git a/vms/avm/base_tx_test.go b/vms/avm/base_tx_test.go index 78ebfa6..197b7fe 100644 --- a/vms/avm/base_tx_test.go +++ b/vms/avm/base_tx_test.go @@ -623,8 +623,10 @@ func TestBaseTxSyntacticVerifyUninitialized(t *testing.T) { func TestBaseTxSemanticVerify(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -688,8 +690,10 @@ func TestBaseTxSemanticVerify(t *testing.T) { func TestBaseTxSemanticVerifyUnknownFx(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.codec.RegisterType(&ava.TestVerifiable{}) @@ -738,8 +742,10 @@ func TestBaseTxSemanticVerifyUnknownFx(t *testing.T) { func TestBaseTxSemanticVerifyWrongAssetID(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.codec.RegisterType(&ava.TestVerifiable{}) @@ -804,15 +810,15 @@ func TestBaseTxSemanticVerifyWrongAssetID(t *testing.T) { } func TestBaseTxSemanticVerifyUnauthorizedFx(t *testing.T) { - genesisBytes := BuildGenesisTest(t) - - issuer := make(chan common.Message, 1) - - ctx.Lock.Lock() - defer ctx.Lock.Unlock() - vm := &VM{} - defer vm.Shutdown() + ctx.Lock.Lock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() + + genesisBytes := BuildGenesisTest(t) + issuer := make(chan common.Message, 1) err := vm.Initialize( ctx, memdb.New(), @@ -897,8 +903,10 @@ func TestBaseTxSemanticVerifyUnauthorizedFx(t *testing.T) { func TestBaseTxSemanticVerifyInvalidSignature(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -949,8 +957,10 @@ func TestBaseTxSemanticVerifyInvalidSignature(t *testing.T) { func TestBaseTxSemanticVerifyMissingUTXO(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -1014,8 +1024,10 @@ func TestBaseTxSemanticVerifyMissingUTXO(t *testing.T) { func TestBaseTxSemanticVerifyInvalidUTXO(t *testing.T) { genesisBytes, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -1147,8 +1159,10 @@ func TestBaseTxSemanticVerifyPendingInvalidUTXO(t *testing.T) { <-issuer ctx.Lock.Lock() - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.PendingTxs() @@ -1279,8 +1293,10 @@ func TestBaseTxSemanticVerifyPendingWrongAssetID(t *testing.T) { <-issuer ctx.Lock.Lock() - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.PendingTxs() @@ -1445,8 +1461,10 @@ func TestBaseTxSemanticVerifyPendingUnauthorizedFx(t *testing.T) { <-issuer ctx.Lock.Lock() - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.PendingTxs() @@ -1595,8 +1613,10 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { <-issuer ctx.Lock.Lock() - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() vm.PendingTxs() diff --git a/vms/avm/import_tx_test.go b/vms/avm/import_tx_test.go index 2228977..9cdecdb 100644 --- a/vms/avm/import_tx_test.go +++ b/vms/avm/import_tx_test.go @@ -220,16 +220,17 @@ func TestIssueImportTx(t *testing.T) { if _, err := vm.IssueTx(tx.Bytes(), nil); err != nil { t.Fatalf("should have issued the transaction correctly but errored: %s", err) } - ctx.Lock.Unlock() - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() msg := <-issuer if msg != common.PendingTxs { t.Fatalf("Wrong message") } - // FIXME?: Is it safe to call vm.PendingTXs() called without the lock? + ctx.Lock.Lock() + defer ctx.Lock.Unlock() + defer vm.Shutdown() + txs := vm.PendingTxs() if len(txs) != 1 { t.Fatalf("Should have returned %d tx(s)", 1) diff --git a/vms/avm/prefixed_state_test.go b/vms/avm/prefixed_state_test.go index ad160e9..4eea404 100644 --- a/vms/avm/prefixed_state_test.go +++ b/vms/avm/prefixed_state_test.go @@ -17,13 +17,13 @@ import ( func TestPrefixedSetsAndGets(t *testing.T) { _, _, vm := GenesisVM(t) - ctx.Lock.Unlock() - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state - // FIXME? is it safe to call vm.codec.RegisterType() without the lock? vm.codec.RegisterType(&ava.TestVerifiable{}) utxo := &ava.UTXO{ @@ -55,7 +55,6 @@ func TestPrefixedSetsAndGets(t *testing.T) { }}, }} - // FIXME? Is it safe to call vm.codec.Marshal() without the lock? unsignedBytes, err := vm.codec.Marshal(tx.UnsignedTx) if err != nil { t.Fatal(err) @@ -75,7 +74,6 @@ func TestPrefixedSetsAndGets(t *testing.T) { }, }) - // FIXME? Is it safe to call vm.codec.Marshal() without the lock? b, err := vm.codec.Marshal(tx) if err != nil { t.Fatal(err) @@ -118,13 +116,13 @@ func TestPrefixedSetsAndGets(t *testing.T) { func TestPrefixedFundingNoAddresses(t *testing.T) { _, _, vm := GenesisVM(t) - ctx.Lock.Unlock() - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state - // FIXME? is it safe to call vm.codec.RegisterType() without the lock? vm.codec.RegisterType(&ava.TestVerifiable{}) utxo := &ava.UTXO{ @@ -146,13 +144,13 @@ func TestPrefixedFundingNoAddresses(t *testing.T) { func TestPrefixedFundingAddresses(t *testing.T) { _, _, vm := GenesisVM(t) - ctx.Lock.Unlock() - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state - // FIXME? is it safe to call vm.codec.RegisterType() without the lock? vm.codec.RegisterType(&testAddressable{}) utxo := &ava.UTXO{ diff --git a/vms/avm/service_test.go b/vms/avm/service_test.go index b42f8e5..f36e962 100644 --- a/vms/avm/service_test.go +++ b/vms/avm/service_test.go @@ -21,8 +21,10 @@ func setup(t *testing.T) ([]byte, *VM, *Service) { func TestServiceIssueTx(t *testing.T) { genesisBytes, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() txArgs := &IssueTxArgs{} txReply := &IssueTxReply{} @@ -44,8 +46,10 @@ func TestServiceIssueTx(t *testing.T) { func TestServiceGetTxStatus(t *testing.T) { genesisBytes, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() statusArgs := &GetTxStatusArgs{} statusReply := &GetTxStatusReply{} @@ -85,8 +89,10 @@ func TestServiceGetTxStatus(t *testing.T) { func TestServiceGetUTXOsInvalidAddress(t *testing.T) { _, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() addr0 := keys[0].PublicKey().Address() tests := []struct { @@ -113,8 +119,10 @@ func TestServiceGetUTXOsInvalidAddress(t *testing.T) { func TestServiceGetUTXOs(t *testing.T) { _, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() addr0 := keys[0].PublicKey().Address() tests := []struct { @@ -163,8 +171,10 @@ func TestServiceGetUTXOs(t *testing.T) { func TestGetAssetDescription(t *testing.T) { genesisBytes, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -188,8 +198,10 @@ func TestGetAssetDescription(t *testing.T) { func TestGetBalance(t *testing.T) { genesisBytes, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -211,8 +223,10 @@ func TestGetBalance(t *testing.T) { func TestCreateFixedCapAsset(t *testing.T) { _, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() reply := CreateFixedCapAssetReply{} err := s.CreateFixedCapAsset(nil, &CreateFixedCapAssetArgs{ @@ -235,8 +249,10 @@ func TestCreateFixedCapAsset(t *testing.T) { func TestCreateVariableCapAsset(t *testing.T) { _, vm, s := setup(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() reply := CreateVariableCapAssetReply{} err := s.CreateVariableCapAsset(nil, &CreateVariableCapAssetArgs{ diff --git a/vms/avm/state_test.go b/vms/avm/state_test.go index 335292c..e0598cc 100644 --- a/vms/avm/state_test.go +++ b/vms/avm/state_test.go @@ -16,10 +16,11 @@ import ( func TestStateIDs(t *testing.T) { _, _, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() - ctx.Lock.Unlock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state.state id0 := ids.NewID([32]byte{0xff, 0}) @@ -129,10 +130,11 @@ func TestStateIDs(t *testing.T) { func TestStateStatuses(t *testing.T) { _, _, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() - ctx.Lock.Unlock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state.state if _, err := state.Status(ids.Empty); err == nil { @@ -181,13 +183,13 @@ func TestStateStatuses(t *testing.T) { func TestStateUTXOs(t *testing.T) { _, _, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() - ctx.Lock.Unlock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state.state - // FIXME? Is it safe to call vm.codec.RegisterType() without the lock? vm.codec.RegisterType(&ava.TestVerifiable{}) if _, err := state.UTXO(ids.Empty); err == nil { @@ -256,13 +258,13 @@ func TestStateUTXOs(t *testing.T) { func TestStateTXs(t *testing.T) { _, _, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() - ctx.Lock.Unlock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() - // FIXME? is it safe to access vm.state.state without the lock? state := vm.state.state - // FIXME? Is it safe to call vm.codec.RegisterType() without the lock? vm.codec.RegisterType(&ava.TestTransferable{}) if _, err := state.Tx(ids.Empty); err == nil { @@ -289,7 +291,6 @@ func TestStateTXs(t *testing.T) { }}, }} - // FIXME? Is it safe to call vm.codec.Marshal() without the lock? unsignedBytes, err := vm.codec.Marshal(tx.UnsignedTx) if err != nil { t.Fatal(err) @@ -309,7 +310,6 @@ func TestStateTXs(t *testing.T) { }, }) - // FIXME? Is it safe to call vm.codec.Marshal() without the lock? b, err := vm.codec.Marshal(tx) if err != nil { t.Fatal(err) diff --git a/vms/avm/vm_test.go b/vms/avm/vm_test.go index f495e87..c397f4e 100644 --- a/vms/avm/vm_test.go +++ b/vms/avm/vm_test.go @@ -460,7 +460,10 @@ func (tx *testTxBytes) UnsignedBytes() []byte { return tx.unsignedBytes } func TestIssueTx(t *testing.T) { genesisBytes, issuer, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() newTx := NewTx(t, genesisBytes, vm) @@ -477,8 +480,8 @@ func TestIssueTx(t *testing.T) { if msg != common.PendingTxs { t.Fatalf("Wrong message") } + ctx.Lock.Lock() - // FIXME? vm.PendingTxs called after lock released. if txs := vm.PendingTxs(); len(txs) != 1 { t.Fatalf("Should have returned %d tx(s)", 1) } @@ -508,7 +511,10 @@ func TestGenesisGetUTXOs(t *testing.T) { // transaction should be issued successfully. func TestIssueDependentTx(t *testing.T) { genesisBytes, issuer, vm := GenesisVM(t) - defer func() { ctx.Lock.Lock(); vm.Shutdown(); ctx.Lock.Unlock() }() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() genesisTx := GetFirstTxFromGenesisTest(genesisBytes, t) @@ -621,15 +627,14 @@ func TestIssueDependentTx(t *testing.T) { if err != nil { t.Fatal(err) } - ctx.Lock.Unlock() msg := <-issuer if msg != common.PendingTxs { t.Fatalf("Wrong message") } + ctx.Lock.Lock() - // FIXME? vm.PendingTxs called after lock released. if txs := vm.PendingTxs(); len(txs) != 2 { t.Fatalf("Should have returned %d tx(s)", 2) } @@ -637,15 +642,15 @@ func TestIssueDependentTx(t *testing.T) { // Test issuing a transaction that creates an NFT family func TestIssueNFT(t *testing.T) { - genesisBytes := BuildGenesisTest(t) - - issuer := make(chan common.Message, 1) - - ctx.Lock.Lock() - defer ctx.Lock.Unlock() - vm := &VM{} - defer vm.Shutdown() + ctx.Lock.Lock() + defer func() { + ctx.Lock.Unlock() + vm.Shutdown() + }() + + genesisBytes := BuildGenesisTest(t) + issuer := make(chan common.Message, 1) err := vm.Initialize( ctx, memdb.New(), @@ -796,15 +801,15 @@ func TestIssueNFT(t *testing.T) { // Test issuing a transaction that creates an Property family func TestIssueProperty(t *testing.T) { - genesisBytes := BuildGenesisTest(t) - - issuer := make(chan common.Message, 1) - - ctx.Lock.Lock() - defer ctx.Lock.Unlock() - vm := &VM{} - defer vm.Shutdown() + ctx.Lock.Lock() + defer func() { + ctx.Lock.Unlock() + vm.Shutdown() + }() + + genesisBytes := BuildGenesisTest(t) + issuer := make(chan common.Message, 1) err := vm.Initialize( ctx, memdb.New(), @@ -946,8 +951,10 @@ func TestIssueProperty(t *testing.T) { func TestVMFormat(t *testing.T) { _, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + ctx.Lock.Unlock() + vm.Shutdown() + }() tests := []struct { in string @@ -966,8 +973,10 @@ func TestVMFormat(t *testing.T) { func TestVMFormatAliased(t *testing.T) { _, _, vm := GenesisVM(t) - defer ctx.Lock.Unlock() - defer vm.Shutdown() + defer func() { + ctx.Lock.Unlock() + vm.Shutdown() + }() origAliases := ctx.BCLookup defer func() { ctx.BCLookup = origAliases }() diff --git a/vms/platformvm/add_default_subnet_delegator_tx_test.go b/vms/platformvm/add_default_subnet_delegator_tx_test.go index eda9049..65a0a71 100644 --- a/vms/platformvm/add_default_subnet_delegator_tx_test.go +++ b/vms/platformvm/add_default_subnet_delegator_tx_test.go @@ -13,7 +13,11 @@ import ( func TestAddDefaultSubnetDelegatorTxSyntacticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: tx is nil var tx *addDefaultSubnetDelegatorTx @@ -154,7 +158,11 @@ func TestAddDefaultSubnetDelegatorTxSyntacticVerify(t *testing.T) { func TestAddDefaultSubnetDelegatorTxSemanticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: Proposed validator currently validating default subnet // but stops validating non-default subnet after stops validating default subnet diff --git a/vms/platformvm/add_default_subnet_validator_tx_test.go b/vms/platformvm/add_default_subnet_validator_tx_test.go index ee863e8..34ff7ef 100644 --- a/vms/platformvm/add_default_subnet_validator_tx_test.go +++ b/vms/platformvm/add_default_subnet_validator_tx_test.go @@ -12,7 +12,11 @@ import ( func TestAddDefaultSubnetValidatorTxSyntacticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: tx is nil var tx *addDefaultSubnetValidatorTx @@ -217,7 +221,11 @@ func TestAddDefaultSubnetValidatorTxSyntacticVerify(t *testing.T) { // Test AddDefaultSubnetValidatorTx.SemanticVerify func TestAddDefaultSubnetValidatorTxSemanticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: Validator's start time too early tx, err := vm.newAddDefaultSubnetValidatorTx( @@ -283,9 +291,9 @@ func TestAddDefaultSubnetValidatorTxSemanticVerify(t *testing.T) { } startTime := defaultGenesisTime.Add(1 * time.Second) tx, err = vm.newAddDefaultSubnetValidatorTx( - defaultNonce+1, // nonce - defaultStakeAmount, // stake amount - uint64(startTime.Unix()), // start time + defaultNonce+1, // nonce + defaultStakeAmount, // stake amount + uint64(startTime.Unix()), // start time uint64(startTime.Add(MinimumStakingDuration).Unix()), // end time key.PublicKey().Address(), // node ID defaultKey.PublicKey().Address(), // destination diff --git a/vms/platformvm/add_nondefault_subnet_validator_tx_test.go b/vms/platformvm/add_nondefault_subnet_validator_tx_test.go index 43069cf..df012c7 100644 --- a/vms/platformvm/add_nondefault_subnet_validator_tx_test.go +++ b/vms/platformvm/add_nondefault_subnet_validator_tx_test.go @@ -14,7 +14,11 @@ import ( func TestAddNonDefaultSubnetValidatorTxSyntacticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: tx is nil var tx *addNonDefaultSubnetValidatorTx @@ -203,7 +207,11 @@ func TestAddNonDefaultSubnetValidatorTxSyntacticVerify(t *testing.T) { func TestAddNonDefaultSubnetValidatorTxSemanticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: Proposed validator currently validating default subnet // but stops validating non-default subnet after stops validating default subnet @@ -592,13 +600,16 @@ func TestAddNonDefaultSubnetValidatorTxSemanticVerify(t *testing.T) { if err == nil { t.Fatal("should have failed verification because validator already in pending validator set of the specified subnet") } - } // Test that marshalling/unmarshalling works func TestAddNonDefaultSubnetValidatorMarshal(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // valid tx tx, err := vm.newAddNonDefaultSubnetValidatorTx( diff --git a/vms/platformvm/advance_time_tx_test.go b/vms/platformvm/advance_time_tx_test.go index d051869..0874b09 100644 --- a/vms/platformvm/advance_time_tx_test.go +++ b/vms/platformvm/advance_time_tx_test.go @@ -17,7 +17,11 @@ func TestAdvanceTimeTxSyntacticVerify(t *testing.T) { // Case 2: Timestamp is ahead of synchrony bound vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() tx = &advanceTimeTx{ Time: uint64(defaultGenesisTime.Add(Delta).Add(1 * time.Second).Unix()), @@ -40,7 +44,11 @@ func TestAdvanceTimeTxSyntacticVerify(t *testing.T) { // Ensure semantic verification fails when proposed timestamp is at or before current timestamp func TestAdvanceTimeTxTimestampTooEarly(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() tx := &advanceTimeTx{ Time: uint64(defaultGenesisTime.Unix()), @@ -55,7 +63,7 @@ func TestAdvanceTimeTxTimestampTooEarly(t *testing.T) { // Ensure semantic verification fails when proposed timestamp is after next validator set change time func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() // Case 1: Timestamp is after next validator start time // Add a pending validator @@ -98,9 +106,16 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { if err == nil { t.Fatal("should've failed verification because proposed timestamp is after pending validator start time") } + vm.Shutdown() + vm.Ctx.Lock.Unlock() // Case 2: Timestamp is after next validator end time vm = defaultVM() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // fast forward clock to 10 seconds before genesis validators stop validating vm.clock.Set(defaultValidateEndTime.Add(-10 * time.Second)) @@ -121,7 +136,11 @@ func TestAdvanceTimeTxTimestampTooLate(t *testing.T) { // Ensure semantic verification updates the current and pending validator sets correctly func TestAdvanceTimeTxUpdateValidators(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: Timestamp is after next validator start time // Add a pending validator @@ -201,7 +220,11 @@ func TestAdvanceTimeTxUpdateValidators(t *testing.T) { // Test method InitiallyPrefersCommit func TestAdvanceTimeTxInitiallyPrefersCommit(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Proposed advancing timestamp to 1 second after current timestamp tx, err := vm.newAdvanceTimeTx(defaultGenesisTime.Add(1 * time.Second)) @@ -223,7 +246,11 @@ func TestAdvanceTimeTxInitiallyPrefersCommit(t *testing.T) { // Ensure marshaling/unmarshaling works func TestAdvanceTimeTxUnmarshal(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() tx, err := vm.newAdvanceTimeTx(defaultGenesisTime) if err != nil { diff --git a/vms/platformvm/create_chain_tx_test.go b/vms/platformvm/create_chain_tx_test.go index ae8694d..ef66bc2 100644 --- a/vms/platformvm/create_chain_tx_test.go +++ b/vms/platformvm/create_chain_tx_test.go @@ -14,7 +14,11 @@ import ( // test method SyntacticVerify func TestCreateChainTxSyntacticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: tx is nil var tx *CreateChainTx @@ -143,7 +147,11 @@ func TestCreateChainTxSyntacticVerify(t *testing.T) { // Ensure SemanticVerify fails when there are not enough control sigs func TestCreateChainTxInsufficientControlSigs(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Case 1: No control sigs (2 are needed) tx, err := vm.newCreateChainTx( @@ -191,7 +199,11 @@ func TestCreateChainTxInsufficientControlSigs(t *testing.T) { // Ensure SemanticVerify fails when an incorrect control signature is given func TestCreateChainTxWrongControlSig(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Generate new, random key to sign tx with factory := crypto.FactorySECP256K1R{} @@ -225,7 +237,11 @@ func TestCreateChainTxWrongControlSig(t *testing.T) { // its validator set doesn't exist func TestCreateChainTxNoSuchSubnet(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() tx, err := vm.newCreateChainTx( defaultNonce+1, @@ -249,7 +265,11 @@ func TestCreateChainTxNoSuchSubnet(t *testing.T) { func TestCreateChainTxAlreadyExists(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // create a tx tx, err := vm.newCreateChainTx( @@ -281,7 +301,11 @@ func TestCreateChainTxAlreadyExists(t *testing.T) { // Ensure valid tx passes semanticVerify func TestCreateChainTxValid(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // create a valid tx tx, err := vm.newCreateChainTx( diff --git a/vms/platformvm/event_heap_test.go b/vms/platformvm/event_heap_test.go index 91570bd..a9d3775 100644 --- a/vms/platformvm/event_heap_test.go +++ b/vms/platformvm/event_heap_test.go @@ -11,7 +11,11 @@ import ( func TestTxHeapStart(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() txHeap := EventHeap{SortByStartTime: true} @@ -80,7 +84,11 @@ func TestTxHeapStart(t *testing.T) { func TestTxHeapStop(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() txHeap := EventHeap{} @@ -149,7 +157,11 @@ func TestTxHeapStop(t *testing.T) { func TestTxHeapStartValidatorVsDelegatorOrdering(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() txHeap := EventHeap{SortByStartTime: true} @@ -192,7 +204,11 @@ func TestTxHeapStartValidatorVsDelegatorOrdering(t *testing.T) { func TestTxHeapStopValidatorVsDelegatorOrdering(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() txHeap := EventHeap{} diff --git a/vms/platformvm/reward_validator_tx_test.go b/vms/platformvm/reward_validator_tx_test.go index 3de1989..c9cca73 100644 --- a/vms/platformvm/reward_validator_tx_test.go +++ b/vms/platformvm/reward_validator_tx_test.go @@ -18,7 +18,11 @@ func TestRewardValidatorTxSyntacticVerify(t *testing.T) { } vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() txID := ids.NewID([32]byte{1, 2, 3, 4, 5, 6, 7}) @@ -56,7 +60,11 @@ func TestRewardValidatorTxSyntacticVerify(t *testing.T) { func TestRewardValidatorTxSemanticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() var nextToRemove *addDefaultSubnetValidatorTx currentValidators, err := vm.getCurrentValidators(vm.DB, DefaultSubnetID) @@ -134,7 +142,11 @@ func TestRewardValidatorTxSemanticVerify(t *testing.T) { func TestRewardDelegatorTxSemanticVerify(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() keyIntf1, err := vm.factory.NewPrivateKey() if err != nil { diff --git a/vms/platformvm/vm_test.go b/vms/platformvm/vm_test.go index ad51e9e..70174e7 100644 --- a/vms/platformvm/vm_test.go +++ b/vms/platformvm/vm_test.go @@ -235,10 +235,13 @@ func GenesisCurrentValidators() *EventHeap { // Ensure genesis state is parsed from bytes and stored correctly func TestGenesis(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Ensure the genesis block has been accepted and stored - // FIXME? Calling vm.LastAccepted() without the lock genesisBlockID := vm.LastAccepted() // lastAccepted should be ID of genesis block genesisBlock, err := vm.getBlock(genesisBlockID) if err != nil { @@ -306,7 +309,11 @@ func TestGenesis(t *testing.T) { // accept proposal to add validator to default subnet func TestAddDefaultSubnetValidatorCommit(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() startTime := defaultGenesisTime.Add(Delta).Add(1 * time.Second) endTime := startTime.Add(MinimumStakingDuration) @@ -331,12 +338,10 @@ func TestAddDefaultSubnetValidatorCommit(t *testing.T) { // trigger block creation vm.unissuedEvents.Add(tx) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -375,7 +380,11 @@ func TestAddDefaultSubnetValidatorCommit(t *testing.T) { // Reject proposal to add validator to default subnet func TestAddDefaultSubnetValidatorReject(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() startTime := defaultGenesisTime.Add(Delta).Add(1 * time.Second) endTime := startTime.Add(MinimumStakingDuration) @@ -400,12 +409,10 @@ func TestAddDefaultSubnetValidatorReject(t *testing.T) { // trigger block creation vm.unissuedEvents.Add(tx) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -448,7 +455,11 @@ func TestAddDefaultSubnetValidatorReject(t *testing.T) { // Accept proposal to add validator to non-default subnet func TestAddNonDefaultSubnetValidatorAccept(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() startTime := defaultValidateStartTime.Add(Delta).Add(1 * time.Second) endTime := startTime.Add(MinimumStakingDuration) @@ -473,12 +484,10 @@ func TestAddNonDefaultSubnetValidatorAccept(t *testing.T) { // trigger block creation vm.unissuedEvents.Add(tx) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -521,7 +530,11 @@ func TestAddNonDefaultSubnetValidatorAccept(t *testing.T) { // Reject proposal to add validator to non-default subnet func TestAddNonDefaultSubnetValidatorReject(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() startTime := defaultValidateStartTime.Add(Delta).Add(1 * time.Second) endTime := startTime.Add(MinimumStakingDuration) @@ -548,12 +561,10 @@ func TestAddNonDefaultSubnetValidatorReject(t *testing.T) { // trigger block creation vm.unissuedEvents.Add(tx) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -596,17 +607,19 @@ func TestAddNonDefaultSubnetValidatorReject(t *testing.T) { // Test case where default subnet validator rewarded func TestRewardValidatorAccept(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Fast forward clock to time for genesis validators to leave vm.clock.Set(defaultValidateEndTime) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() // should contain proposal to advance time if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -643,12 +656,10 @@ func TestRewardValidatorAccept(t *testing.T) { t.Fatal("expected timestamp to have advanced") } - vm.Ctx.Lock.Lock() blk, err = vm.BuildBlock() // should contain proposal to reward genesis validator if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block = blk.(*ProposalBlock) @@ -689,17 +700,19 @@ func TestRewardValidatorAccept(t *testing.T) { // Test case where default subnet validator not rewarded func TestRewardValidatorReject(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() // Fast forward clock to time for genesis validators to leave vm.clock.Set(defaultValidateEndTime) - vm.Ctx.Lock.Lock() blk, err := vm.BuildBlock() // should contain proposal to advance time if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block := blk.(*ProposalBlock) @@ -736,12 +749,10 @@ func TestRewardValidatorReject(t *testing.T) { t.Fatal("expected timestamp to have advanced") } - vm.Ctx.Lock.Lock() blk, err = vm.BuildBlock() // should contain proposal to reward genesis validator if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct block = blk.(*ProposalBlock) @@ -782,9 +793,12 @@ func TestRewardValidatorReject(t *testing.T) { // Ensure BuildBlock errors when there is no block to build func TestUnneededBuildBlock(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() - // FIXME? Calling vm.BuildBlock without the lock if _, err := vm.BuildBlock(); err == nil { t.Fatalf("Should have errored on BuildBlock") } @@ -793,7 +807,11 @@ func TestUnneededBuildBlock(t *testing.T) { // test acceptance of proposal to create a new chain func TestCreateChain(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() tx, err := vm.newCreateChainTx( defaultNonce+1, @@ -810,13 +828,11 @@ func TestCreateChain(t *testing.T) { t.Fatal(err) } - vm.Ctx.Lock.Lock() vm.unissuedDecisionTxs = append(vm.unissuedDecisionTxs, tx) blk, err := vm.BuildBlock() // should contain proposal to create chain if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() if err := blk.Verify(); err != nil { t.Fatal(err) @@ -856,7 +872,11 @@ func TestCreateChain(t *testing.T) { // 4) Advance timestamp to validator's end time (removing validator from current) func TestCreateSubnet(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() createSubnetTx, err := vm.newCreateSubnetTx( testNetworkID, @@ -872,13 +892,11 @@ func TestCreateSubnet(t *testing.T) { t.Fatal(err) } - vm.Ctx.Lock.Lock() vm.unissuedDecisionTxs = append(vm.unissuedDecisionTxs, createSubnetTx) blk, err := vm.BuildBlock() // should contain proposal to create subnet if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() if err := blk.Verify(); err != nil { t.Fatal(err) @@ -935,13 +953,11 @@ func TestCreateSubnet(t *testing.T) { t.Fatal(err) } - vm.Ctx.Lock.Lock() vm.unissuedEvents.Push(addValidatorTx) blk, err = vm.BuildBlock() // should add validator to the new subnet if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct // and accept the proposal/commit @@ -989,12 +1005,10 @@ func TestCreateSubnet(t *testing.T) { // from pending to current validator set vm.clock.Set(startTime) - vm.Ctx.Lock.Lock() blk, err = vm.BuildBlock() // should be advance time tx if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct // and accept the proposal/commit @@ -1049,12 +1063,10 @@ func TestCreateSubnet(t *testing.T) { // fast forward clock to time validator should stop validating vm.clock.Set(endTime) - vm.Ctx.Lock.Lock() blk, err = vm.BuildBlock() // should be advance time tx if err != nil { t.Fatal(err) } - vm.Ctx.Lock.Unlock() // Assert preferences are correct // and accept the proposal/commit @@ -1102,7 +1114,11 @@ func TestCreateSubnet(t *testing.T) { // test asset import func TestAtomicImport(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() avmID := ids.Empty.Prefix(0) utxoID := ava.UTXOID{ @@ -1136,9 +1152,6 @@ func TestAtomicImport(t *testing.T) { t.Fatal(err) } - vm.Ctx.Lock.Lock() - defer vm.Ctx.Lock.Unlock() - vm.ava = assetID vm.avm = avmID @@ -1194,7 +1207,11 @@ func TestAtomicImport(t *testing.T) { // test optimistic asset import func TestOptimisticAtomicImport(t *testing.T) { vm := defaultVM() - defer func() { vm.Ctx.Lock.Lock(); vm.Shutdown(); vm.Ctx.Lock.Unlock() }() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() avmID := ids.Empty.Prefix(0) utxoID := ava.UTXOID{ @@ -1228,9 +1245,6 @@ func TestOptimisticAtomicImport(t *testing.T) { t.Fatal(err) } - vm.Ctx.Lock.Lock() - defer vm.Ctx.Lock.Unlock() - vm.ava = assetID vm.avm = avmID @@ -1354,7 +1368,10 @@ func TestRestartPartiallyAccepted(t *testing.T) { secondVM.clock.Set(defaultGenesisTime) secondCtx := defaultContext() secondCtx.Lock.Lock() - defer secondCtx.Lock.Unlock() + defer func() { + secondVM.Shutdown() + secondCtx.Lock.Unlock() + }() secondMsgChan := make(chan common.Message, 1) if err := secondVM.Initialize(secondCtx, db, genesisBytes, secondMsgChan, nil); err != nil { @@ -1460,7 +1477,10 @@ func TestRestartFullyAccepted(t *testing.T) { secondVM.clock.Set(defaultGenesisTime) secondCtx := defaultContext() secondCtx.Lock.Lock() - defer secondCtx.Lock.Unlock() + defer func() { + secondVM.Shutdown() + secondCtx.Lock.Unlock() + }() secondMsgChan := make(chan common.Message, 1) if err := secondVM.Initialize(secondCtx, db, genesisBytes, secondMsgChan, nil); err != nil { @@ -1658,6 +1678,12 @@ func TestUnverifiedParent(t *testing.T) { vm.clock.Set(defaultGenesisTime) ctx := defaultContext() + ctx.Lock.Lock() + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() + msgChan := make(chan common.Message, 1) if err := vm.Initialize(ctx, db, genesisBytes, msgChan, nil); err != nil { t.Fatal(err)