github.com/MetalBlockchain/metalgo@v1.11.9/vms/avm/txs/initial_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 txs 5 6 import ( 7 "errors" 8 "fmt" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/MetalBlockchain/metalgo/codec" 14 "github.com/MetalBlockchain/metalgo/codec/linearcodec" 15 "github.com/MetalBlockchain/metalgo/ids" 16 "github.com/MetalBlockchain/metalgo/vms/components/avax" 17 "github.com/MetalBlockchain/metalgo/vms/components/verify" 18 "github.com/MetalBlockchain/metalgo/vms/secp256k1fx" 19 ) 20 21 var errTest = errors.New("non-nil error") 22 23 func TestInitialStateVerifySerialization(t *testing.T) { 24 require := require.New(t) 25 26 c := linearcodec.NewDefault() 27 require.NoError(c.RegisterType(&secp256k1fx.TransferOutput{})) 28 m := codec.NewDefaultManager() 29 require.NoError(m.RegisterCodec(CodecVersion, c)) 30 31 expected := []byte{ 32 // Codec version: 33 0x00, 0x00, 34 // fxID: 35 0x00, 0x00, 0x00, 0x00, 36 // num outputs: 37 0x00, 0x00, 0x00, 0x01, 38 // output: 39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 40 0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00, 41 0x00, 0x00, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x01, 42 0x00, 0x00, 0x00, 0x02, 0x51, 0x02, 0x5c, 0x61, 43 0xfb, 0xcf, 0xc0, 0x78, 0xf6, 0x93, 0x34, 0xf8, 44 0x34, 0xbe, 0x6d, 0xd2, 0x6d, 0x55, 0xa9, 0x55, 45 0xc3, 0x34, 0x41, 0x28, 0xe0, 0x60, 0x12, 0x8e, 46 0xde, 0x35, 0x23, 0xa2, 0x4a, 0x46, 0x1c, 0x89, 47 0x43, 0xab, 0x08, 0x59, 48 } 49 50 is := &InitialState{ 51 FxIndex: 0, 52 Outs: []verify.State{ 53 &secp256k1fx.TransferOutput{ 54 Amt: 12345, 55 OutputOwners: secp256k1fx.OutputOwners{ 56 Locktime: 54321, 57 Threshold: 1, 58 Addrs: []ids.ShortID{ 59 { 60 0x51, 0x02, 0x5c, 0x61, 0xfb, 0xcf, 0xc0, 0x78, 61 0xf6, 0x93, 0x34, 0xf8, 0x34, 0xbe, 0x6d, 0xd2, 62 0x6d, 0x55, 0xa9, 0x55, 63 }, 64 { 65 0xc3, 0x34, 0x41, 0x28, 0xe0, 0x60, 0x12, 0x8e, 66 0xde, 0x35, 0x23, 0xa2, 0x4a, 0x46, 0x1c, 0x89, 67 0x43, 0xab, 0x08, 0x59, 68 }, 69 }, 70 }, 71 }, 72 }, 73 } 74 75 isBytes, err := m.Marshal(CodecVersion, is) 76 require.NoError(err) 77 require.Equal(expected, isBytes) 78 } 79 80 func TestInitialStateVerifyNil(t *testing.T) { 81 require := require.New(t) 82 83 c := linearcodec.NewDefault() 84 m := codec.NewDefaultManager() 85 require.NoError(m.RegisterCodec(CodecVersion, c)) 86 numFxs := 1 87 88 is := (*InitialState)(nil) 89 err := is.Verify(m, numFxs) 90 require.ErrorIs(err, ErrNilInitialState) 91 } 92 93 func TestInitialStateVerifyUnknownFxID(t *testing.T) { 94 require := require.New(t) 95 96 c := linearcodec.NewDefault() 97 m := codec.NewDefaultManager() 98 require.NoError(m.RegisterCodec(CodecVersion, c)) 99 numFxs := 1 100 101 is := InitialState{ 102 FxIndex: 1, 103 } 104 err := is.Verify(m, numFxs) 105 require.ErrorIs(err, ErrUnknownFx) 106 } 107 108 func TestInitialStateVerifyNilOutput(t *testing.T) { 109 require := require.New(t) 110 111 c := linearcodec.NewDefault() 112 m := codec.NewDefaultManager() 113 require.NoError(m.RegisterCodec(CodecVersion, c)) 114 numFxs := 1 115 116 is := InitialState{ 117 FxIndex: 0, 118 Outs: []verify.State{nil}, 119 } 120 err := is.Verify(m, numFxs) 121 require.ErrorIs(err, ErrNilFxOutput) 122 } 123 124 func TestInitialStateVerifyInvalidOutput(t *testing.T) { 125 require := require.New(t) 126 127 c := linearcodec.NewDefault() 128 require.NoError(c.RegisterType(&avax.TestState{})) 129 m := codec.NewDefaultManager() 130 require.NoError(m.RegisterCodec(CodecVersion, c)) 131 numFxs := 1 132 133 is := InitialState{ 134 FxIndex: 0, 135 Outs: []verify.State{&avax.TestState{Err: errTest}}, 136 } 137 err := is.Verify(m, numFxs) 138 require.ErrorIs(err, errTest) 139 } 140 141 func TestInitialStateVerifyUnsortedOutputs(t *testing.T) { 142 require := require.New(t) 143 144 c := linearcodec.NewDefault() 145 require.NoError(c.RegisterType(&avax.TestTransferable{})) 146 m := codec.NewDefaultManager() 147 require.NoError(m.RegisterCodec(CodecVersion, c)) 148 numFxs := 1 149 150 is := InitialState{ 151 FxIndex: 0, 152 Outs: []verify.State{ 153 &avax.TestTransferable{Val: 1}, 154 &avax.TestTransferable{Val: 0}, 155 }, 156 } 157 err := is.Verify(m, numFxs) 158 require.ErrorIs(err, ErrOutputsNotSorted) 159 is.Sort(m) 160 require.NoError(is.Verify(m, numFxs)) 161 } 162 163 func TestInitialStateCompare(t *testing.T) { 164 tests := []struct { 165 a *InitialState 166 b *InitialState 167 expected int 168 }{ 169 { 170 a: &InitialState{}, 171 b: &InitialState{}, 172 expected: 0, 173 }, 174 { 175 a: &InitialState{ 176 FxIndex: 1, 177 }, 178 b: &InitialState{}, 179 expected: 1, 180 }, 181 } 182 for _, test := range tests { 183 t.Run(fmt.Sprintf("%d_%d_%d", test.a.FxIndex, test.b.FxIndex, test.expected), func(t *testing.T) { 184 require := require.New(t) 185 186 require.Equal(test.expected, test.a.Compare(test.b)) 187 require.Equal(-test.expected, test.b.Compare(test.a)) 188 }) 189 } 190 }