github.com/iotexproject/iotex-core@v1.14.1-rc1/action/action_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  	"encoding/hex"
    10  	"math/big"
    11  	"testing"
    12  
    13  	"github.com/pkg/errors"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/iotex-core/test/identityset"
    17  	"github.com/iotexproject/iotex-core/testutil"
    18  )
    19  
    20  func TestActionProtoAndVerify(t *testing.T) {
    21  	require := require.New(t)
    22  	data, err := hex.DecodeString("")
    23  	require.NoError(err)
    24  	v, err := NewExecution("", 0, big.NewInt(10), uint64(10), big.NewInt(10), data)
    25  	require.NoError(err)
    26  	t.Run("no error", func(t *testing.T) {
    27  		bd := &EnvelopeBuilder{}
    28  		elp := bd.SetGasPrice(big.NewInt(10)).
    29  			SetGasLimit(uint64(100000)).
    30  			SetAction(v).Build()
    31  
    32  		selp, err := Sign(elp, identityset.PrivateKey(28))
    33  		require.NoError(err)
    34  		require.Equal(65, len(selp.SrcPubkey().Bytes()))
    35  		require.NoError(selp.VerifySignature())
    36  
    37  		nselp := &SealedEnvelope{}
    38  		require.NoError(nselp.loadProto(selp.Proto(), _evmNetworkID))
    39  
    40  		selpHash, err := selp.Hash()
    41  		require.NoError(err)
    42  		nselpHash, err := nselp.Hash()
    43  		require.NoError(err)
    44  		require.Equal(selpHash, nselpHash)
    45  	})
    46  	t.Run("empty public key", func(t *testing.T) {
    47  		bd := &EnvelopeBuilder{}
    48  		elp := bd.SetGasPrice(big.NewInt(10)).
    49  			SetGasLimit(uint64(100000)).
    50  			SetAction(v).Build()
    51  
    52  		selp, err := Sign(elp, identityset.PrivateKey(28))
    53  		require.NoError(err)
    54  
    55  		selp.srcPubkey = nil
    56  
    57  		require.EqualError(selp.VerifySignature(), "empty public key")
    58  	})
    59  	t.Run("invalid signature", func(t *testing.T) {
    60  		bd := &EnvelopeBuilder{}
    61  		elp := bd.SetGasPrice(big.NewInt(10)).
    62  			SetGasLimit(uint64(100000)).
    63  			SetAction(v).Build()
    64  
    65  		selp, err := Sign(elp, identityset.PrivateKey(28))
    66  		require.NoError(err)
    67  		selp.signature = []byte("invalid signature")
    68  		require.Equal(ErrInvalidSender, errors.Cause(selp.VerifySignature()))
    69  	})
    70  }
    71  
    72  func TestActionFakeSeal(t *testing.T) {
    73  	require := require.New(t)
    74  	priKey := identityset.PrivateKey(27)
    75  	selp1, err := SignedExecution(identityset.Address(31).String(), identityset.PrivateKey(28), 2,
    76  		big.NewInt(1), testutil.TestGasLimit, big.NewInt(testutil.TestGasPriceInt64), []byte{1})
    77  	require.NoError(err)
    78  	selp := FakeSeal(selp1.Envelope, priKey.PublicKey())
    79  	require.Equal(selp.srcPubkey, priKey.PublicKey())
    80  }
    81  
    82  func TestAbstractActionSetter(t *testing.T) {
    83  	require := require.New(t)
    84  	t.Run("set nonce", func(t *testing.T) {
    85  		ex, err := NewExecution("2", uint64(1), big.NewInt(-100), uint64(0), big.NewInt(0), []byte{})
    86  		require.NoError(err)
    87  		require.Equal(uint64(1), ex.nonce)
    88  		ex.SetNonce(2)
    89  		require.Equal(uint64(2), ex.nonce)
    90  	})
    91  
    92  	t.Run("set gaslimit", func(t *testing.T) {
    93  		ex, err := NewExecution("2", uint64(1), big.NewInt(-100), uint64(0), big.NewInt(0), []byte{})
    94  		require.NoError(err)
    95  		require.Equal(uint64(0), ex.gasLimit)
    96  		ex.SetGasLimit(10000)
    97  		require.Equal(uint64(10000), ex.gasLimit)
    98  	})
    99  
   100  	t.Run("set gasPrice", func(t *testing.T) {
   101  		ex, err := NewExecution("2", uint64(1), big.NewInt(-100), uint64(0), big.NewInt(10), []byte{})
   102  		require.NoError(err)
   103  		require.Equal(big.NewInt(10), ex.gasPrice)
   104  		ex.SetGasPrice(big.NewInt(0))
   105  		require.Equal(big.NewInt(0), ex.gasPrice)
   106  	})
   107  }
   108  
   109  func TestIsSystemAction(t *testing.T) {
   110  	require := require.New(t)
   111  	builder := EnvelopeBuilder{}
   112  	cf := ClaimFromRewardingFundBuilder{}
   113  	actClaimFromRewarding := cf.Build()
   114  	act := builder.SetAction(&actClaimFromRewarding).Build()
   115  	sel, err := Sign(act, identityset.PrivateKey(1))
   116  	require.NoError(err)
   117  	require.False(IsSystemAction(sel))
   118  
   119  	gb := GrantRewardBuilder{}
   120  	actGrantReward := gb.Build()
   121  	act = builder.SetAction(&actGrantReward).Build()
   122  	sel, err = Sign(act, identityset.PrivateKey(1))
   123  	require.NoError(err)
   124  	require.True(IsSystemAction(sel))
   125  
   126  	actPollResult := NewPutPollResult(1, 1, nil)
   127  	act = builder.SetAction(actPollResult).Build()
   128  	sel, err = Sign(act, identityset.PrivateKey(1))
   129  	require.NoError(err)
   130  	require.True(IsSystemAction(sel))
   131  }