github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/txs/executor/export_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 executor 5 6 import ( 7 "context" 8 "testing" 9 "time" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/MetalBlockchain/metalgo/ids" 14 "github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1" 15 "github.com/MetalBlockchain/metalgo/vms/components/avax" 16 "github.com/MetalBlockchain/metalgo/vms/platformvm/state" 17 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 18 19 walletsigner "github.com/MetalBlockchain/metalgo/wallet/chain/p/signer" 20 ) 21 22 func TestNewExportTx(t *testing.T) { 23 env := newEnvironment(t, banff) 24 env.ctx.Lock.Lock() 25 defer env.ctx.Lock.Unlock() 26 27 type test struct { 28 description string 29 destinationChainID ids.ID 30 sourceKeys []*secp256k1.PrivateKey 31 timestamp time.Time 32 } 33 34 sourceKey := preFundedKeys[0] 35 36 tests := []test{ 37 { 38 description: "P->X export", 39 destinationChainID: env.ctx.XChainID, 40 sourceKeys: []*secp256k1.PrivateKey{sourceKey}, 41 timestamp: defaultValidateStartTime, 42 }, 43 { 44 description: "P->C export", 45 destinationChainID: env.ctx.CChainID, 46 sourceKeys: []*secp256k1.PrivateKey{sourceKey}, 47 timestamp: env.config.UpgradeConfig.ApricotPhase5Time, 48 }, 49 } 50 51 to := ids.GenerateTestShortID() 52 for _, tt := range tests { 53 t.Run(tt.description, func(t *testing.T) { 54 require := require.New(t) 55 56 builder, signer := env.factory.NewWallet(tt.sourceKeys...) 57 utx, err := builder.NewExportTx( 58 tt.destinationChainID, 59 []*avax.TransferableOutput{{ 60 Asset: avax.Asset{ID: env.ctx.AVAXAssetID}, 61 Out: &secp256k1fx.TransferOutput{ 62 Amt: defaultBalance - defaultTxFee, 63 OutputOwners: secp256k1fx.OutputOwners{ 64 Locktime: 0, 65 Threshold: 1, 66 Addrs: []ids.ShortID{to}, 67 }, 68 }, 69 }}, 70 ) 71 require.NoError(err) 72 tx, err := walletsigner.SignUnsigned(context.Background(), signer, utx) 73 require.NoError(err) 74 75 stateDiff, err := state.NewDiff(lastAcceptedID, env) 76 require.NoError(err) 77 78 stateDiff.SetTimestamp(tt.timestamp) 79 80 verifier := StandardTxExecutor{ 81 Backend: &env.backend, 82 State: stateDiff, 83 Tx: tx, 84 } 85 require.NoError(tx.Unsigned.Visit(&verifier)) 86 }) 87 } 88 }