github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stakecreate_test.go (about) 1 // Copyright (c) 2020 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 "google.golang.org/protobuf/proto" 16 17 "github.com/iotexproject/go-pkgs/crypto" 18 19 "github.com/iotexproject/iotex-core/pkg/unit" 20 "github.com/iotexproject/iotex-core/test/identityset" 21 ) 22 23 var stakeCreateTestParams = []struct { 24 SenderKey crypto.PrivateKey 25 Nonce uint64 26 CanAddress string 27 AmountStr string 28 Duration uint32 29 AutoStake bool 30 Payload []byte 31 GasLimit uint64 32 GasPrice *big.Int 33 Serialize string 34 IntrinsicGas uint64 35 Cost string 36 ElpHash string 37 Sign string 38 SelpHash string 39 Expected error 40 SanityCheck error 41 }{ 42 // valid test 43 { 44 identityset.PrivateKey(27), uint64(10), "test", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(10), "0a0474657374120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a26080118c0843d22023130c202190a0474657374120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41563785be9d7e2d796a8aaca41dbe1a53a0bce3614ede09718e72c75cb40cdb48355964b69156008f2319e20db4a4023730c3a1664ac35dfc10a7ceff26be8ebe00", "ebb26b08e824e18cb6d38918411749351c065198603e4626bbdc10b900dde270", nil, nil, 45 }, 46 // invalid test 47 { 48 identityset.PrivateKey(27), uint64(10), "test", "ae-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", ErrInvalidAmount, nil, 49 }, 50 { 51 identityset.PrivateKey(27), uint64(10), "test", "-10", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount, 52 }, 53 { 54 identityset.PrivateKey(27), uint64(10), "test", "0", uint32(10000), false, []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount, 55 }, 56 { 57 identityset.PrivateKey(27), uint64(10), "test", "100", uint32(10000), true, []byte("payload"), uint64(1000000), big.NewInt(-unit.Qev), "0a0474657374120331303018904e20012a077061796c6f6164", uint64(10700), "107100", "18d76ff9f3cfed0fe84f3fd4831f11379edc5b3d689d646187520b3fe74ab44c", "0a26080118c0843d22023130c202190a0474657374120331303018904e20012a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41563785be9d7e2d796a8aaca41dbe1a53a0bce3614ede09718e72c75cb40cdb48355964b69156008f2319e20db4a4023730c3a1664ac35dfc10a7ceff26be8ebe00", "ebb26b08e824e18cb6d38918411749351c065198603e4626bbdc10b900dde270", nil, ErrNegativeValue, 58 }, 59 } 60 61 func TestCreateStake(t *testing.T) { 62 require := require.New(t) 63 for _, test := range stakeCreateTestParams { 64 stake, err := NewCreateStake(test.Nonce, test.CanAddress, test.AmountStr, test.Duration, test.AutoStake, test.Payload, test.GasLimit, test.GasPrice) 65 require.Equal(test.Expected, errors.Cause(err)) 66 if err != nil { 67 continue 68 } 69 err = stake.SanityCheck() 70 require.Equal(test.SanityCheck, errors.Cause(err)) 71 if err != nil { 72 continue 73 } 74 75 ser := stake.Serialize() 76 require.Equal(test.Serialize, hex.EncodeToString(ser)) 77 78 require.NoError(err) 79 require.Equal(test.GasLimit, stake.GasLimit()) 80 require.Equal(test.GasPrice, stake.GasPrice()) 81 require.Equal(test.Nonce, stake.Nonce()) 82 83 require.Equal(test.AmountStr, stake.Amount().String()) 84 require.Equal(test.Payload, stake.Payload()) 85 require.Equal(test.CanAddress, stake.Candidate()) 86 require.Equal(test.Duration, stake.Duration()) 87 require.True(stake.AutoStake()) 88 89 gas, err := stake.IntrinsicGas() 90 require.NoError(err) 91 require.Equal(test.IntrinsicGas, gas) 92 cost, err := stake.Cost() 93 require.NoError(err) 94 require.Equal(test.Cost, cost.Text(10)) 95 96 cs2 := &CreateStake{} 97 require.NoError(cs2.LoadProto(stake.Proto())) 98 require.Equal(test.AmountStr, cs2.Amount().String()) 99 require.Equal(test.Payload, cs2.Payload()) 100 require.Equal(test.CanAddress, cs2.Candidate()) 101 require.Equal(test.Duration, cs2.Duration()) 102 require.True(cs2.AutoStake()) 103 104 // verify sign 105 bd := &EnvelopeBuilder{} 106 elp := bd.SetGasLimit(test.GasLimit). 107 SetGasPrice(test.GasPrice). 108 SetAction(stake).Build() 109 // sign 110 selp, err := Sign(elp, test.SenderKey) 111 require.NoError(err) 112 require.NotNil(selp) 113 ser, err = proto.Marshal(selp.Proto()) 114 require.NoError(err) 115 require.Equal(test.Sign, hex.EncodeToString(ser)) 116 hash, err := selp.Hash() 117 require.NoError(err) 118 require.Equal(test.SelpHash, hex.EncodeToString(hash[:])) 119 // verify signature 120 require.NoError(selp.VerifySignature()) 121 } 122 123 } 124 125 func TestCreateStakeABIEncodeAndDecode(t *testing.T) { 126 require := require.New(t) 127 test := stakeCreateTestParams[0] 128 stake, err := NewCreateStake(test.Nonce, test.CanAddress, test.AmountStr, test.Duration, test.AutoStake, test.Payload, test.GasLimit, test.GasPrice) 129 require.NoError(err) 130 131 data, err := stake.EncodeABIBinary() 132 require.NoError(err) 133 stake, err = NewCreateStakeFromABIBinary(data) 134 require.NoError(err) 135 require.Equal(test.CanAddress, stake.candName) 136 require.Equal(test.AmountStr, stake.amount.String()) 137 require.Equal(test.Duration, stake.duration) 138 require.Equal(test.AutoStake, stake.autoStake) 139 require.Equal(test.Payload, stake.payload) 140 }