github.com/iotexproject/iotex-core@v1.14.1-rc1/action/execution_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/ethereum/go-ethereum/common" 14 "github.com/ethereum/go-ethereum/core/types" 15 "github.com/pkg/errors" 16 "github.com/stretchr/testify/require" 17 18 "github.com/iotexproject/iotex-core/test/identityset" 19 ) 20 21 func TestExecutionSignVerify(t *testing.T) { 22 require := require.New(t) 23 contractAddr := identityset.Address(28) 24 executorKey := identityset.PrivateKey(27) 25 data, err := hex.DecodeString("") 26 require.NoError(err) 27 ex, err := NewExecution(contractAddr.String(), 2, big.NewInt(10), uint64(100000), big.NewInt(10), data) 28 require.NoError(err) 29 require.EqualValues(21, ex.BasicActionSize()) 30 require.EqualValues(87, ex.TotalSize()) 31 32 bd := &EnvelopeBuilder{} 33 eb := bd.SetNonce(ex.nonce). 34 SetGasLimit(ex.gasLimit). 35 SetGasPrice(ex.gasPrice). 36 SetAction(ex).Build() 37 elp, ok := eb.(*envelope) 38 require.True(ok) 39 40 w := AssembleSealedEnvelope(elp, executorKey.PublicKey(), []byte("lol")) 41 require.Error(w.VerifySignature()) 42 ex2, ok := w.Envelope.Action().(*Execution) 43 require.True(ok) 44 require.Equal(ex, ex2) 45 46 // sign the Execution 47 selp, err := Sign(elp, executorKey) 48 require.NoError(err) 49 require.NotNil(selp) 50 require.EqualValues(21, ex.BasicActionSize()) 51 require.EqualValues(87, ex.TotalSize()) 52 53 // verify signature 54 require.NoError(selp.VerifySignature()) 55 } 56 57 func TestExecutionSanityCheck(t *testing.T) { 58 require := require.New(t) 59 t.Run("Negative amount", func(t *testing.T) { 60 ex, err := NewExecution("2", uint64(1), big.NewInt(-100), uint64(0), big.NewInt(0), []byte{}) 61 require.NoError(err) 62 require.Equal(ErrInvalidAmount, errors.Cause(ex.SanityCheck())) 63 }) 64 65 t.Run("Invalid contract address", func(t *testing.T) { 66 ex, err := NewExecution( 67 identityset.Address(29).String()+"bbb", 68 uint64(1), 69 big.NewInt(0), 70 uint64(0), 71 big.NewInt(0), 72 []byte{}, 73 ) 74 require.NoError(err) 75 require.Contains(ex.SanityCheck().Error(), "error when validating contract's address") 76 }) 77 78 t.Run("Negative gas price", func(t *testing.T) { 79 ex, err := NewExecution(identityset.Address(29).String(), uint64(1), big.NewInt(100), uint64(0), big.NewInt(-1), []byte{}) 80 require.NoError(err) 81 require.Equal(ErrNegativeValue, errors.Cause(ex.SanityCheck())) 82 }) 83 } 84 85 var ( 86 _c1 = common.HexToAddress("01fc246633470cf62ae2a956d21e8d481c3a69e1") 87 _c2 = common.HexToAddress("3470cf62ae2a956d38d481c3a69e121e01fc2466") 88 _k1 = common.HexToHash("02e940dd0fd5b5df4cfb8d6bcd9c74ec433e9a5c21acb72cbcb5be9e711b678f") 89 _k2 = common.HexToHash("e7709aa7aa161246674919b2f0299e95cbb6c5482e5c348d12dfe226f71f63d6") 90 _k3 = common.HexToHash("a618ea5b489eca42f331abcb08394f581f2e9da89c8ee7e72c747204842abe8b") 91 _k4 = common.HexToHash("881d3bdf2e13b6e8b6d685d2277a48ff37141495ddd4e3d7289fcfa5570f29f1") 92 ) 93 94 func TestExecutionAccessList(t *testing.T) { 95 require := require.New(t) 96 97 ex1 := &Execution{} 98 for _, v := range []struct { 99 list types.AccessList 100 gas uint64 101 }{ 102 {nil, 10400}, 103 { 104 types.AccessList{ 105 {Address: common.Address{}, StorageKeys: nil}, 106 }, 12800, 107 }, 108 { 109 types.AccessList{ 110 {Address: _c2, StorageKeys: []common.Hash{{}, _k1}}, 111 }, 16600, 112 }, 113 { 114 types.AccessList{ 115 {Address: common.Address{}, StorageKeys: nil}, 116 {Address: _c1, StorageKeys: []common.Hash{_k1, {}, _k3}}, 117 {Address: _c2, StorageKeys: []common.Hash{_k2, _k3, _k4, _k1}}, 118 }, 30900, 119 }, 120 } { 121 ex, err := NewExecutionWithAccessList( 122 identityset.Address(29).String(), 123 1, 124 big.NewInt(20), 125 uint64(100), 126 big.NewInt(1000000), 127 []byte("test"), 128 v.list, 129 ) 130 require.NoError(err) 131 require.NoError(ex1.LoadProto(ex.Proto())) 132 ex1.AbstractAction = ex.AbstractAction 133 require.Equal(ex, ex1) 134 gas, err := ex.IntrinsicGas() 135 require.NoError(err) 136 require.Equal(v.gas, gas) 137 } 138 }