github.com/ava-labs/avalanchego@v1.11.11/vms/components/avax/utxo_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 avax
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/ava-labs/avalanchego/codec"
    12  	"github.com/ava-labs/avalanchego/codec/linearcodec"
    13  	"github.com/ava-labs/avalanchego/ids"
    14  	"github.com/ava-labs/avalanchego/vms/secp256k1fx"
    15  )
    16  
    17  func TestUTXOVerifyNil(t *testing.T) {
    18  	utxo := (*UTXO)(nil)
    19  	err := utxo.Verify()
    20  	require.ErrorIs(t, err, errNilUTXO)
    21  }
    22  
    23  func TestUTXOVerifyEmpty(t *testing.T) {
    24  	utxo := &UTXO{
    25  		UTXOID: UTXOID{TxID: ids.Empty},
    26  		Asset:  Asset{ID: ids.Empty},
    27  	}
    28  	err := utxo.Verify()
    29  	require.ErrorIs(t, err, errEmptyUTXO)
    30  }
    31  
    32  func TestUTXOSerialize(t *testing.T) {
    33  	require := require.New(t)
    34  
    35  	c := linearcodec.NewDefault()
    36  	manager := codec.NewDefaultManager()
    37  
    38  	require.NoError(c.RegisterType(&secp256k1fx.MintOutput{}))
    39  	require.NoError(c.RegisterType(&secp256k1fx.TransferOutput{}))
    40  	require.NoError(c.RegisterType(&secp256k1fx.Input{}))
    41  	require.NoError(c.RegisterType(&secp256k1fx.TransferInput{}))
    42  	require.NoError(c.RegisterType(&secp256k1fx.Credential{}))
    43  	require.NoError(manager.RegisterCodec(codecVersion, c))
    44  
    45  	expected := []byte{
    46  		// Codec version
    47  		0x00, 0x00,
    48  		// txID:
    49  		0xf9, 0x66, 0x75, 0x0f, 0x43, 0x88, 0x67, 0xc3,
    50  		0xc9, 0x82, 0x8d, 0xdc, 0xdb, 0xe6, 0x60, 0xe2,
    51  		0x1c, 0xcd, 0xbb, 0x36, 0xa9, 0x27, 0x69, 0x58,
    52  		0xf0, 0x11, 0xba, 0x47, 0x2f, 0x75, 0xd4, 0xe7,
    53  		// utxo index:
    54  		0x00, 0x00, 0x00, 0x00,
    55  		// assetID:
    56  		0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    57  		0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    58  		0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    59  		0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
    60  		// output:
    61  		0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
    62  		0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x00,
    63  		0x00, 0x00, 0xd4, 0x31, 0x00, 0x00, 0x00, 0x01,
    64  		0x00, 0x00, 0x00, 0x02, 0x00, 0x01, 0x02, 0x03,
    65  		0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
    66  		0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
    67  		0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
    68  		0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
    69  		0x24, 0x25, 0x26, 0x27,
    70  	}
    71  	utxo := &UTXO{
    72  		UTXOID: UTXOID{
    73  			TxID: ids.ID{
    74  				0xf9, 0x66, 0x75, 0x0f, 0x43, 0x88, 0x67, 0xc3,
    75  				0xc9, 0x82, 0x8d, 0xdc, 0xdb, 0xe6, 0x60, 0xe2,
    76  				0x1c, 0xcd, 0xbb, 0x36, 0xa9, 0x27, 0x69, 0x58,
    77  				0xf0, 0x11, 0xba, 0x47, 0x2f, 0x75, 0xd4, 0xe7,
    78  			},
    79  			OutputIndex: 0,
    80  		},
    81  		Asset: Asset{
    82  			ID: ids.ID{
    83  				0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    84  				0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    85  				0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
    86  				0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
    87  			},
    88  		},
    89  		Out: &secp256k1fx.TransferOutput{
    90  			Amt: 12345,
    91  			OutputOwners: secp256k1fx.OutputOwners{
    92  				Locktime:  54321,
    93  				Threshold: 1,
    94  				Addrs: []ids.ShortID{
    95  					{
    96  						0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
    97  						0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
    98  						0x10, 0x11, 0x12, 0x13,
    99  					},
   100  					{
   101  						0x14, 0x15, 0x16, 0x17,
   102  						0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
   103  						0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
   104  					},
   105  				},
   106  			},
   107  		},
   108  	}
   109  
   110  	utxoBytes, err := manager.Marshal(codecVersion, utxo)
   111  	require.NoError(err)
   112  	require.Equal(expected, utxoBytes)
   113  }