Skip to content

Commit

Permalink
X-chain - consolidate tx creation in unit tests (#2736)
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Benegiamo <[email protected]>
Co-authored-by: Stephen Buttolph <[email protected]>
  • Loading branch information
abi87 and StephenButtolph authored Jun 10, 2024
1 parent c28af7d commit 504766e
Show file tree
Hide file tree
Showing 11 changed files with 1,340 additions and 988 deletions.
37 changes: 16 additions & 21 deletions vms/avm/environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"github.com/ava-labs/avalanchego/utils/crypto/secp256k1"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/utils/formatting/address"
"github.com/ava-labs/avalanchego/utils/linked"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
"github.com/ava-labs/avalanchego/vms/avm/block/executor"
"github.com/ava-labs/avalanchego/vms/avm/config"
"github.com/ava-labs/avalanchego/vms/avm/fxs"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/avm/txs/txstest"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/nftfx"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
Expand Down Expand Up @@ -77,12 +77,6 @@ var (

keys = secp256k1.TestKeys()[:3] // TODO: Remove [:3]
addrs []ids.ShortID // addrs[i] corresponds to keys[i]

noFeesTestConfig = &config.Config{
EUpgradeTime: mockable.MaxTime,
TxFee: 0,
CreateAssetTxFee: 0,
}
)

func init() {
Expand Down Expand Up @@ -110,13 +104,12 @@ type envConfig struct {
}

type environment struct {
genesisBytes []byte
genesisTx *txs.Tx
sharedMemory *atomic.Memory
issuer chan common.Message
vm *VM
service *Service
walletService *WalletService
genesisBytes []byte
genesisTx *txs.Tx
sharedMemory *atomic.Memory
issuer chan common.Message
vm *VM
txBuilder *txstest.Builder
}

// setup the testing environment
Expand Down Expand Up @@ -210,13 +203,7 @@ func setup(tb testing.TB, c *envConfig) *environment {
sharedMemory: m,
issuer: issuer,
vm: vm,
service: &Service{
vm: vm,
},
walletService: &WalletService{
vm: vm,
pendingTxs: linked.NewHashmap[ids.ID, *txs.Tx](),
},
txBuilder: txstest.New(vm.parser.Codec(), vm.ctx, &vm.Config, vm.feeAssetID, vm.state),
}

require.NoError(vm.SetState(context.Background(), snow.Bootstrapping))
Expand All @@ -230,6 +217,14 @@ func setup(tb testing.TB, c *envConfig) *environment {
}

require.NoError(vm.SetState(context.Background(), snow.NormalOp))

tb.Cleanup(func() {
env.vm.ctx.Lock.Lock()
defer env.vm.ctx.Lock.Unlock()

require.NoError(env.vm.Shutdown(context.Background()))
})

return env
}

Expand Down
43 changes: 11 additions & 32 deletions vms/avm/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package avm

import (
"context"
"testing"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -28,13 +27,8 @@ import (
func TestIndexTransaction_Ordered(t *testing.T) {
require := require.New(t)

env := setup(t, &envConfig{
vmStaticConfig: noFeesTestConfig,
})
defer func() {
require.NoError(env.vm.Shutdown(context.Background()))
env.vm.ctx.Lock.Unlock()
}()
env := setup(t, &envConfig{fork: durango})
defer env.vm.ctx.Lock.Unlock()

key := keys[0]
addr := key.PublicKey().Address()
Expand Down Expand Up @@ -72,13 +66,8 @@ func TestIndexTransaction_Ordered(t *testing.T) {
func TestIndexTransaction_MultipleTransactions(t *testing.T) {
require := require.New(t)

env := setup(t, &envConfig{
vmStaticConfig: noFeesTestConfig,
})
defer func() {
require.NoError(env.vm.Shutdown(context.Background()))
env.vm.ctx.Lock.Unlock()
}()
env := setup(t, &envConfig{fork: durango})
defer env.vm.ctx.Lock.Unlock()

addressTxMap := map[ids.ShortID]*txs.Tx{}
txAssetID := avax.Asset{ID: env.genesisTx.ID()}
Expand Down Expand Up @@ -120,13 +109,8 @@ func TestIndexTransaction_MultipleTransactions(t *testing.T) {
func TestIndexTransaction_MultipleAddresses(t *testing.T) {
require := require.New(t)

env := setup(t, &envConfig{
vmStaticConfig: noFeesTestConfig,
})
defer func() {
require.NoError(env.vm.Shutdown(context.Background()))
env.vm.ctx.Lock.Unlock()
}()
env := setup(t, &envConfig{fork: durango})
defer env.vm.ctx.Lock.Unlock()

addrs := make([]ids.ShortID, len(keys))
for i, key := range keys {
Expand Down Expand Up @@ -163,13 +147,8 @@ func TestIndexTransaction_MultipleAddresses(t *testing.T) {
func TestIndexer_Read(t *testing.T) {
require := require.New(t)

env := setup(t, &envConfig{
vmStaticConfig: noFeesTestConfig,
})
defer func() {
require.NoError(env.vm.Shutdown(context.Background()))
env.vm.ctx.Lock.Unlock()
}()
env := setup(t, &envConfig{fork: durango})
defer env.vm.ctx.Lock.Unlock()

// generate test address and asset IDs
assetID := ids.GenerateTestID()
Expand Down Expand Up @@ -259,7 +238,7 @@ func buildUTXO(utxoID avax.UTXOID, txAssetID avax.Asset, addr ids.ShortID) *avax
UTXOID: utxoID,
Asset: txAssetID,
Out: &secp256k1fx.TransferOutput{
Amt: 1000,
Amt: startBalance,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{addr},
Expand All @@ -277,14 +256,14 @@ func buildTX(chainID ids.ID, utxoID avax.UTXOID, txAssetID avax.Asset, address .
UTXOID: utxoID,
Asset: txAssetID,
In: &secp256k1fx.TransferInput{
Amt: 1000,
Amt: startBalance,
Input: secp256k1fx.Input{SigIndices: []uint32{0}},
},
}},
Outs: []*avax.TransferableOutput{{
Asset: txAssetID,
Out: &secp256k1fx.TransferOutput{
Amt: 1000,
Amt: startBalance - testTxFee,
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: address,
Expand Down
Loading

0 comments on commit 504766e

Please sign in to comment.