github.com/iotexproject/iotex-core@v1.14.1-rc1/action/transfer_test.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package action
     7  
     8  import (
     9  	"math/big"
    10  	"testing"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/require"
    14  
    15  	"github.com/iotexproject/iotex-core/test/identityset"
    16  )
    17  
    18  func TestTransferSignVerify(t *testing.T) {
    19  	require := require.New(t)
    20  	recipientAddr := identityset.Address(28)
    21  	senderKey := identityset.PrivateKey(27)
    22  
    23  	tsf, err := NewTransfer(1, big.NewInt(10), recipientAddr.String(), []byte{}, uint64(100000), big.NewInt(10))
    24  	require.NoError(err)
    25  	require.EqualValues(21, tsf.BasicActionSize())
    26  	require.EqualValues(87, tsf.TotalSize())
    27  
    28  	bd := &EnvelopeBuilder{}
    29  	eb := bd.SetNonce(tsf.nonce).
    30  		SetGasLimit(tsf.gasLimit).
    31  		SetGasPrice(tsf.gasPrice).
    32  		SetAction(tsf).Build()
    33  	elp, ok := eb.(*envelope)
    34  	require.True(ok)
    35  
    36  	w := AssembleSealedEnvelope(elp, senderKey.PublicKey(), []byte("lol"))
    37  	require.Error(w.VerifySignature())
    38  	tsf2, ok := w.Envelope.Action().(*Transfer)
    39  	require.True(ok)
    40  	require.Equal(tsf, tsf2)
    41  
    42  	// sign the transfer
    43  	selp, err := Sign(elp, senderKey)
    44  	require.NoError(err)
    45  	require.NotNil(selp)
    46  	require.EqualValues(21, tsf.BasicActionSize())
    47  	require.EqualValues(87, tsf.TotalSize())
    48  
    49  	// verify signature
    50  	require.NoError(selp.VerifySignature())
    51  }
    52  
    53  func TestTransfer(t *testing.T) {
    54  	require := require.New(t)
    55  	recipientAddr := identityset.Address(28)
    56  	senderKey := identityset.PrivateKey(27)
    57  
    58  	tsf, err := NewTransfer(0, big.NewInt(10), recipientAddr.String(), []byte{}, uint64(100000), big.NewInt(10))
    59  	require.NoError(err)
    60  
    61  	tsf.Proto()
    62  
    63  	bd := &EnvelopeBuilder{}
    64  	eb := bd.SetGasLimit(uint64(100000)).
    65  		SetGasPrice(big.NewInt(10)).
    66  		SetAction(tsf).Build()
    67  	elp, ok := eb.(*envelope)
    68  	require.True(ok)
    69  
    70  	w := AssembleSealedEnvelope(elp, senderKey.PublicKey(), []byte("lol"))
    71  	require.Error(w.VerifySignature())
    72  
    73  	require.NoError(err)
    74  	require.Equal("10", tsf.Amount().Text(10))
    75  	require.Equal([]byte{}, tsf.Payload())
    76  	require.Equal(uint64(100000), tsf.GasLimit())
    77  	require.Equal("10", tsf.GasPrice().Text(10))
    78  	require.Equal(uint64(0), tsf.Nonce())
    79  	require.Equal(senderKey.PublicKey().HexString(), w.SrcPubkey().HexString())
    80  	require.Equal(recipientAddr.String(), tsf.Recipient())
    81  	require.Equal(recipientAddr.String(), tsf.Destination())
    82  	require.Equal(uint32(87), tsf.TotalSize())
    83  
    84  	gas, err := tsf.IntrinsicGas()
    85  	require.NoError(err)
    86  	require.Equal(uint64(10000), gas)
    87  	cs, err := tsf.Cost()
    88  	require.NoError(err)
    89  	require.Equal("100010", cs.Text(10))
    90  
    91  	proto := tsf.Proto()
    92  	tsf2 := &Transfer{}
    93  	require.NoError(tsf2.LoadProto(proto))
    94  	require.Equal("10", tsf2.Amount().Text(10))
    95  	require.Equal([]byte{}, tsf2.Payload())
    96  	require.Equal(recipientAddr.String(), tsf2.Recipient())
    97  	require.Equal(recipientAddr.String(), tsf2.Destination())
    98  
    99  	t.Run("Negative amount", func(t *testing.T) {
   100  		tsf, err := NewTransfer(uint64(1), big.NewInt(-100), "2", nil,
   101  			uint64(100000), big.NewInt(0))
   102  		require.NoError(err)
   103  		require.Equal(ErrNegativeValue, errors.Cause(tsf.SanityCheck()))
   104  	})
   105  	t.Run("Negative gas fee", func(t *testing.T) {
   106  		tsf, err := NewTransfer(uint64(1), big.NewInt(100), identityset.Address(28).String(), nil,
   107  			uint64(100000), big.NewInt(-1))
   108  		require.NoError(err)
   109  		require.Equal(ErrNegativeValue, errors.Cause(tsf.SanityCheck()))
   110  	})
   111  }