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

Rework X-chain locking in tests #2526

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 8 additions & 9 deletions vms/avm/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func buildGenesisTestWithArgs(tb testing.TB, args *BuildGenesisArgs) []byte {
return b
}

func newTx(tb testing.TB, genesisBytes []byte, vm *VM, assetName string) *txs.Tx {
func newTx(tb testing.TB, genesisBytes []byte, parser txs.Parser, assetName string) *txs.Tx {
require := require.New(tb)

createTx := getCreateTxFromGenesisTest(tb, genesisBytes, assetName)
Expand All @@ -338,14 +338,14 @@ func newTx(tb testing.TB, genesisBytes []byte, vm *VM, assetName string) *txs.Tx
}},
},
}}
require.NoError(tx.SignSECP256K1Fx(vm.parser.Codec(), [][]*secp256k1.PrivateKey{{keys[0]}}))
require.NoError(tx.SignSECP256K1Fx(parser.Codec(), [][]*secp256k1.PrivateKey{{keys[0]}}))
return tx
}

// Sample from a set of addresses and return them raw and formatted as strings.
// The size of the sample is between 1 and len(addrs)
// If len(addrs) == 0, returns nil
func sampleAddrs(tb testing.TB, vm *VM, addrs []ids.ShortID) ([]ids.ShortID, []string) {
func sampleAddrs(tb testing.TB, addressFormatter avax.AddressManager, addrs []ids.ShortID) ([]ids.ShortID, []string) {
require := require.New(tb)

sampledAddrs := []ids.ShortID{}
Expand All @@ -359,7 +359,7 @@ func sampleAddrs(tb testing.TB, vm *VM, addrs []ids.ShortID) ([]ids.ShortID, []s
require.NoError(err)
for _, index := range indices {
addr := addrs[index]
addrStr, err := vm.FormatLocalAddress(addr)
addrStr, err := addressFormatter.FormatLocalAddress(addr)
require.NoError(err)

sampledAddrs = append(sampledAddrs, addr)
Expand Down Expand Up @@ -517,32 +517,31 @@ func makeCustomAssetGenesis(tb testing.TB) *BuildGenesisArgs {
}
}

// issueAndAccept expects the context lock to be held
// issueAndAccept expects the context lock not to be held
func issueAndAccept(
require *require.Assertions,
vm *VM,
issuer <-chan common.Message,
tx *txs.Tx,
) {
vm.ctx.Lock.Unlock()
txID, err := vm.issueTx(tx)
vm.ctx.Lock.Lock()
require.NoError(err)
require.Equal(tx.ID(), txID)

buildAndAccept(require, vm, issuer, txID)
}

// buildAndAccept expects the context lock to be held
// buildAndAccept expects the context lock not to be held
func buildAndAccept(
require *require.Assertions,
vm *VM,
issuer <-chan common.Message,
txID ids.ID,
) {
vm.ctx.Lock.Unlock()
require.Equal(common.PendingTxs, <-issuer)

vm.ctx.Lock.Lock()
defer vm.ctx.Lock.Unlock()

blkIntf, err := vm.BuildBlock(context.Background())
require.NoError(err)
Expand Down
14 changes: 12 additions & 2 deletions vms/avm/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ func TestIndexTransaction_Ordered(t *testing.T) {
tx := buildTX(utxoID, txAssetID, addr)
require.NoError(tx.SignSECP256K1Fx(env.vm.parser.Codec(), [][]*secp256k1.PrivateKey{{key}}))

// issue transaction
env.vm.ctx.Lock.Unlock()

issueAndAccept(require, env.vm, env.issuer, tx)

env.vm.ctx.Lock.Lock()

txs = append(txs, tx)
}

Expand Down Expand Up @@ -96,9 +99,13 @@ func TestIndexTransaction_MultipleTransactions(t *testing.T) {
tx := buildTX(utxoID, txAssetID, addr)
require.NoError(tx.SignSECP256K1Fx(env.vm.parser.Codec(), [][]*secp256k1.PrivateKey{{key}}))

env.vm.ctx.Lock.Unlock()

// issue transaction
issueAndAccept(require, env.vm, env.issuer, tx)

env.vm.ctx.Lock.Lock()

addressTxMap[addr] = tx
}

Expand Down Expand Up @@ -145,9 +152,12 @@ func TestIndexTransaction_MultipleAddresses(t *testing.T) {
tx := buildTX(utxoID, txAssetID, addrs...)
require.NoError(tx.SignSECP256K1Fx(env.vm.parser.Codec(), [][]*secp256k1.PrivateKey{{key}}))

// issue transaction
env.vm.ctx.Lock.Unlock()

issueAndAccept(require, env.vm, env.issuer, tx)

env.vm.ctx.Lock.Lock()

assertIndexedTX(t, env.vm.db, 0, addr, txAssetID.ID, tx.ID())
assertLatestIdx(t, env.vm.db, addr, txAssetID.ID, 1)
}
Expand Down
Loading
Loading