github.com/MetalBlockchain/metalgo@v1.11.9/vms/secp256k1fx/transfer_input_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 secp256k1fx
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/MetalBlockchain/metalgo/codec"
    12  	"github.com/MetalBlockchain/metalgo/codec/linearcodec"
    13  	"github.com/MetalBlockchain/metalgo/vms/components/verify"
    14  )
    15  
    16  func TestTransferInputAmount(t *testing.T) {
    17  	require := require.New(t)
    18  	in := TransferInput{
    19  		Amt: 1,
    20  		Input: Input{
    21  			SigIndices: []uint32{0, 1},
    22  		},
    23  	}
    24  	require.Equal(uint64(1), in.Amount())
    25  }
    26  
    27  func TestTransferInputVerify(t *testing.T) {
    28  	require := require.New(t)
    29  	in := TransferInput{
    30  		Amt: 1,
    31  		Input: Input{
    32  			SigIndices: []uint32{0, 1},
    33  		},
    34  	}
    35  	require.NoError(in.Verify())
    36  }
    37  
    38  func TestTransferInputVerifyNil(t *testing.T) {
    39  	require := require.New(t)
    40  	in := (*TransferInput)(nil)
    41  	err := in.Verify()
    42  	require.ErrorIs(err, ErrNilInput)
    43  }
    44  
    45  func TestTransferInputVerifyNoValue(t *testing.T) {
    46  	require := require.New(t)
    47  	in := TransferInput{
    48  		Amt: 0,
    49  		Input: Input{
    50  			SigIndices: []uint32{0, 1},
    51  		},
    52  	}
    53  	err := in.Verify()
    54  	require.ErrorIs(err, ErrNoValueInput)
    55  }
    56  
    57  func TestTransferInputVerifyDuplicated(t *testing.T) {
    58  	require := require.New(t)
    59  	in := TransferInput{
    60  		Amt: 1,
    61  		Input: Input{
    62  			SigIndices: []uint32{0, 0},
    63  		},
    64  	}
    65  	err := in.Verify()
    66  	require.ErrorIs(err, ErrInputIndicesNotSortedUnique)
    67  }
    68  
    69  func TestTransferInputVerifyUnsorted(t *testing.T) {
    70  	require := require.New(t)
    71  	in := TransferInput{
    72  		Amt: 1,
    73  		Input: Input{
    74  			SigIndices: []uint32{1, 0},
    75  		},
    76  	}
    77  	err := in.Verify()
    78  	require.ErrorIs(err, ErrInputIndicesNotSortedUnique)
    79  }
    80  
    81  func TestTransferInputSerialize(t *testing.T) {
    82  	require := require.New(t)
    83  	c := linearcodec.NewDefault()
    84  	m := codec.NewDefaultManager()
    85  	require.NoError(m.RegisterCodec(0, c))
    86  
    87  	expected := []byte{
    88  		// Codec version
    89  		0x00, 0x00,
    90  		// amount:
    91  		0x00, 0x00, 0x00, 0x00, 0x07, 0x5b, 0xcd, 0x15,
    92  		// length:
    93  		0x00, 0x00, 0x00, 0x02,
    94  		// sig[0]
    95  		0x00, 0x00, 0x00, 0x03,
    96  		// sig[1]
    97  		0x00, 0x00, 0x00, 0x07,
    98  	}
    99  	in := TransferInput{
   100  		Amt: 123456789,
   101  		Input: Input{
   102  			SigIndices: []uint32{3, 7},
   103  		},
   104  	}
   105  	require.NoError(in.Verify())
   106  
   107  	result, err := m.Marshal(0, &in)
   108  	require.NoError(err)
   109  	require.Equal(expected, result)
   110  }
   111  
   112  func TestTransferInputNotState(t *testing.T) {
   113  	require := require.New(t)
   114  	intf := interface{}(&TransferInput{})
   115  	_, ok := intf.(verify.State)
   116  	require.False(ok)
   117  }