github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/block/builder/standard_block_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package builder
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/MetalBlockchain/metalgo/chains/atomic"
    13  	"github.com/MetalBlockchain/metalgo/database/prefixdb"
    14  	"github.com/MetalBlockchain/metalgo/ids"
    15  	"github.com/MetalBlockchain/metalgo/vms/components/avax"
    16  	"github.com/MetalBlockchain/metalgo/vms/platformvm/status"
    17  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    18  	"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
    19  
    20  	walletsigner "github.com/MetalBlockchain/metalgo/wallet/chain/p/signer"
    21  )
    22  
    23  func TestAtomicTxImports(t *testing.T) {
    24  	require := require.New(t)
    25  
    26  	env := newEnvironment(t, latestFork)
    27  	env.ctx.Lock.Lock()
    28  	defer env.ctx.Lock.Unlock()
    29  
    30  	utxoID := avax.UTXOID{
    31  		TxID:        ids.Empty.Prefix(1),
    32  		OutputIndex: 1,
    33  	}
    34  	amount := uint64(70000)
    35  	recipientKey := preFundedKeys[1]
    36  
    37  	m := atomic.NewMemory(prefixdb.New([]byte{5}, env.baseDB))
    38  
    39  	env.msm.SharedMemory = m.NewSharedMemory(env.ctx.ChainID)
    40  	peerSharedMemory := m.NewSharedMemory(env.ctx.XChainID)
    41  	utxo := &avax.UTXO{
    42  		UTXOID: utxoID,
    43  		Asset:  avax.Asset{ID: env.ctx.AVAXAssetID},
    44  		Out: &secp256k1fx.TransferOutput{
    45  			Amt: amount,
    46  			OutputOwners: secp256k1fx.OutputOwners{
    47  				Threshold: 1,
    48  				Addrs:     []ids.ShortID{recipientKey.PublicKey().Address()},
    49  			},
    50  		},
    51  	}
    52  	utxoBytes, err := txs.Codec.Marshal(txs.CodecVersion, utxo)
    53  	require.NoError(err)
    54  
    55  	inputID := utxo.InputID()
    56  	require.NoError(peerSharedMemory.Apply(map[ids.ID]*atomic.Requests{
    57  		env.ctx.ChainID: {PutRequests: []*atomic.Element{{
    58  			Key:   inputID[:],
    59  			Value: utxoBytes,
    60  			Traits: [][]byte{
    61  				recipientKey.PublicKey().Address().Bytes(),
    62  			},
    63  		}}},
    64  	}))
    65  
    66  	builder, signer := env.factory.NewWallet(recipientKey)
    67  	utx, err := builder.NewImportTx(
    68  		env.ctx.XChainID,
    69  		&secp256k1fx.OutputOwners{
    70  			Threshold: 1,
    71  			Addrs:     []ids.ShortID{recipientKey.PublicKey().Address()},
    72  		},
    73  	)
    74  	require.NoError(err)
    75  	tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx)
    76  	require.NoError(err)
    77  
    78  	require.NoError(env.Builder.Add(tx))
    79  	b, err := env.Builder.BuildBlock(context.Background())
    80  	require.NoError(err)
    81  	// Test multiple verify calls work
    82  	require.NoError(b.Verify(context.Background()))
    83  	require.NoError(b.Accept(context.Background()))
    84  	_, txStatus, err := env.state.GetTx(tx.ID())
    85  	require.NoError(err)
    86  	// Ensure transaction is in the committed state
    87  	require.Equal(status.Committed, txStatus)
    88  }