github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stakeadddeposit_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/test/identityset" 20 ) 21 22 var stakeDepositTestParams = []struct { 23 SenderKey crypto.PrivateKey 24 Nonce uint64 25 Index uint64 26 Amount string 27 Payload []byte 28 GasLimit uint64 29 GasPrice *big.Int 30 Serialize string 31 IntrinsicGas uint64 32 Cost string 33 ElpHash string 34 Sign string 35 SelpHash string 36 Expected error 37 SanityCheck error 38 }{ 39 // valid test 40 { 41 identityset.PrivateKey(27), uint64(0), uint64(10), "10", []byte("payload"), uint64(1000000), big.NewInt(10), "080a120231301a077061796c6f6164", uint64(10700), "107010", "9089e7eb1afed64fcdbd3c7ee29a6cedab9aa59cf3f7881dfaa3d19f99f09338", "0a1c080118c0843d22023130da020f080a120231301a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41a48ab1feba8181d760de946aefed7d815a89fd9b1ab503d2392bb55e1bb75eec42dddc8bd642f89accc3a37b3cf15a103a95d66695fdf0647b202869fdd66bcb01", "ca8937d6f224a4e4bf93cb5605581de2d26fb0481e1dfc1eef384ee7ccf94b73", nil, nil, 42 }, 43 // invalid test 44 { 45 identityset.PrivateKey(27), uint64(0), uint64(10), "abci", []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", ErrInvalidAmount, nil, 46 }, 47 { 48 identityset.PrivateKey(27), uint64(0), uint64(10), "0", []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount, 49 }, 50 { 51 identityset.PrivateKey(27), uint64(0), uint64(10), "-10", []byte("payload"), uint64(1000000), big.NewInt(1000), "", uint64(10700), "", "", "", "", nil, ErrInvalidAmount, 52 }, 53 { 54 identityset.PrivateKey(27), uint64(0), uint64(10), "10", []byte("payload"), uint64(1000000), big.NewInt(-10), "080a120231301a077061796c6f6164", uint64(10700), "107010", "9089e7eb1afed64fcdbd3c7ee29a6cedab9aa59cf3f7881dfaa3d19f99f09338", "0a1c080118c0843d22023130da020f080a120231301a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41a48ab1feba8181d760de946aefed7d815a89fd9b1ab503d2392bb55e1bb75eec42dddc8bd642f89accc3a37b3cf15a103a95d66695fdf0647b202869fdd66bcb01", "ca8937d6f224a4e4bf93cb5605581de2d26fb0481e1dfc1eef384ee7ccf94b73", nil, ErrNegativeValue, 55 }, 56 } 57 58 func TestDeposit(t *testing.T) { 59 require := require.New(t) 60 for _, test := range stakeDepositTestParams { 61 stake, err := NewDepositToStake(test.Nonce, test.Index, test.Amount, test.Payload, test.GasLimit, test.GasPrice) 62 require.Equal(test.Expected, errors.Cause(err)) 63 if err != nil { 64 continue 65 } 66 err = stake.SanityCheck() 67 require.Equal(test.SanityCheck, errors.Cause(err)) 68 if err != nil { 69 continue 70 } 71 72 ser := stake.Serialize() 73 require.Equal(test.Serialize, hex.EncodeToString(ser)) 74 75 require.NoError(err) 76 require.Equal(test.GasLimit, stake.GasLimit()) 77 require.Equal(test.GasPrice, stake.GasPrice()) 78 require.Equal(test.Nonce, stake.Nonce()) 79 80 require.Equal(test.Amount, stake.Amount().String()) 81 require.Equal(test.Payload, stake.Payload()) 82 require.Equal(test.Index, stake.BucketIndex()) 83 84 gas, err := stake.IntrinsicGas() 85 require.NoError(err) 86 require.Equal(test.IntrinsicGas, gas) 87 cost, err := stake.Cost() 88 require.NoError(err) 89 require.Equal(test.Cost, cost.Text(10)) 90 91 ds2 := &DepositToStake{} 92 require.NoError(ds2.LoadProto(stake.Proto())) 93 require.Equal(test.Amount, ds2.Amount().String()) 94 require.Equal(test.Payload, ds2.Payload()) 95 require.Equal(test.Index, ds2.BucketIndex()) 96 97 // verify sign 98 bd := &EnvelopeBuilder{} 99 elp := bd.SetGasLimit(test.GasLimit). 100 SetGasPrice(test.GasPrice). 101 SetAction(stake).Build() 102 // sign 103 selp, err := Sign(elp, test.SenderKey) 104 require.NoError(err) 105 require.NotNil(selp) 106 ser, err = proto.Marshal(selp.Proto()) 107 require.NoError(err) 108 require.Equal(test.Sign, hex.EncodeToString(ser)) 109 hash, err := selp.Hash() 110 require.NoError(err) 111 require.Equal(test.SelpHash, hex.EncodeToString(hash[:])) 112 // verify signature 113 require.NoError(selp.VerifySignature()) 114 } 115 } 116 117 func TestDepositToStakeABIEncodeAndDecode(t *testing.T) { 118 require := require.New(t) 119 test := stakeDepositTestParams[0] 120 stake, err := NewDepositToStake(test.Nonce, test.Index, test.Amount, test.Payload, test.GasLimit, test.GasPrice) 121 require.NoError(err) 122 123 data, err := stake.EncodeABIBinary() 124 require.NoError(err) 125 stake, err = NewDepositToStakeFromABIBinary(data) 126 require.NoError(err) 127 require.Equal(test.Index, stake.bucketIndex) 128 require.Equal(test.Amount, stake.amount.String()) 129 require.Equal(test.Payload, stake.payload) 130 }