github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/state_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 avm 5 6 import ( 7 "math" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/MetalBlockchain/metalgo/ids" 13 "github.com/MetalBlockchain/metalgo/snow/engine/common" 14 "github.com/MetalBlockchain/metalgo/utils/constants" 15 "github.com/MetalBlockchain/metalgo/utils/crypto/secp256k1" 16 "github.com/MetalBlockchain/metalgo/utils/units" 17 "github.com/MetalBlockchain/metalgo/vms/avm/txs" 18 "github.com/MetalBlockchain/metalgo/vms/components/avax" 19 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 20 ) 21 22 func TestSetsAndGets(t *testing.T) { 23 require := require.New(t) 24 25 env := setup(t, &envConfig{ 26 fork: latest, 27 additionalFxs: []*common.Fx{{ 28 ID: ids.GenerateTestID(), 29 Fx: &FxTest{ 30 InitializeF: func(vmIntf interface{}) error { 31 vm := vmIntf.(secp256k1fx.VM) 32 return vm.CodecRegistry().RegisterType(&avax.TestState{}) 33 }, 34 }, 35 }}, 36 }) 37 defer env.vm.ctx.Lock.Unlock() 38 39 utxo := &avax.UTXO{ 40 UTXOID: avax.UTXOID{ 41 TxID: ids.Empty, 42 OutputIndex: 1, 43 }, 44 Asset: avax.Asset{ID: ids.Empty}, 45 Out: &avax.TestState{}, 46 } 47 utxoID := utxo.InputID() 48 49 tx := &txs.Tx{Unsigned: &txs.BaseTx{BaseTx: avax.BaseTx{ 50 NetworkID: constants.UnitTestID, 51 BlockchainID: env.vm.ctx.XChainID, 52 Ins: []*avax.TransferableInput{{ 53 UTXOID: avax.UTXOID{ 54 TxID: ids.Empty, 55 OutputIndex: 0, 56 }, 57 Asset: avax.Asset{ID: assetID}, 58 In: &secp256k1fx.TransferInput{ 59 Amt: 20 * units.KiloAvax, 60 Input: secp256k1fx.Input{ 61 SigIndices: []uint32{ 62 0, 63 }, 64 }, 65 }, 66 }}, 67 }}} 68 require.NoError(tx.SignSECP256K1Fx(env.vm.parser.Codec(), [][]*secp256k1.PrivateKey{{keys[0]}})) 69 70 txID := tx.ID() 71 72 env.vm.state.AddUTXO(utxo) 73 env.vm.state.AddTx(tx) 74 75 resultUTXO, err := env.vm.state.GetUTXO(utxoID) 76 require.NoError(err) 77 resultTx, err := env.vm.state.GetTx(txID) 78 require.NoError(err) 79 80 require.Equal(uint32(1), resultUTXO.OutputIndex) 81 require.Equal(tx.ID(), resultTx.ID()) 82 } 83 84 func TestFundingNoAddresses(t *testing.T) { 85 env := setup(t, &envConfig{ 86 fork: latest, 87 additionalFxs: []*common.Fx{{ 88 ID: ids.GenerateTestID(), 89 Fx: &FxTest{ 90 InitializeF: func(vmIntf interface{}) error { 91 vm := vmIntf.(secp256k1fx.VM) 92 return vm.CodecRegistry().RegisterType(&avax.TestState{}) 93 }, 94 }, 95 }}, 96 }) 97 defer env.vm.ctx.Lock.Unlock() 98 99 utxo := &avax.UTXO{ 100 UTXOID: avax.UTXOID{ 101 TxID: ids.Empty, 102 OutputIndex: 1, 103 }, 104 Asset: avax.Asset{ID: ids.Empty}, 105 Out: &avax.TestState{}, 106 } 107 108 env.vm.state.AddUTXO(utxo) 109 env.vm.state.DeleteUTXO(utxo.InputID()) 110 } 111 112 func TestFundingAddresses(t *testing.T) { 113 require := require.New(t) 114 115 env := setup(t, &envConfig{ 116 fork: latest, 117 additionalFxs: []*common.Fx{{ 118 ID: ids.GenerateTestID(), 119 Fx: &FxTest{ 120 InitializeF: func(vmIntf interface{}) error { 121 vm := vmIntf.(secp256k1fx.VM) 122 return vm.CodecRegistry().RegisterType(&avax.TestAddressable{}) 123 }, 124 }, 125 }}, 126 }) 127 defer env.vm.ctx.Lock.Unlock() 128 129 utxo := &avax.UTXO{ 130 UTXOID: avax.UTXOID{ 131 TxID: ids.Empty, 132 OutputIndex: 1, 133 }, 134 Asset: avax.Asset{ID: ids.Empty}, 135 Out: &avax.TestAddressable{ 136 Addrs: [][]byte{{0}}, 137 }, 138 } 139 140 env.vm.state.AddUTXO(utxo) 141 require.NoError(env.vm.state.Commit()) 142 143 utxos, err := env.vm.state.UTXOIDs([]byte{0}, ids.Empty, math.MaxInt32) 144 require.NoError(err) 145 require.Len(utxos, 1) 146 require.Equal(utxo.InputID(), utxos[0]) 147 148 env.vm.state.DeleteUTXO(utxo.InputID()) 149 require.NoError(env.vm.state.Commit()) 150 151 utxos, err = env.vm.state.UTXOIDs([]byte{0}, ids.Empty, math.MaxInt32) 152 require.NoError(err) 153 require.Empty(utxos) 154 }