github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stake_changecandidate_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 18 func TestChangeCandidate(t *testing.T) { 19 require := require.New(t) 20 stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice) 21 require.NoError(err) 22 23 ser := stake.Serialize() 24 require.Equal("080a120a63616e646964617465311a077061796c6f6164", hex.EncodeToString(ser)) 25 26 require.NoError(err) 27 require.Equal(_gaslimit, stake.GasLimit()) 28 require.Equal(_gasprice, stake.GasPrice()) 29 require.Equal(_nonce, stake.Nonce()) 30 31 require.Equal(_payload, stake.Payload()) 32 require.Equal(_canName, stake.Candidate()) 33 require.Equal(_index, stake.BucketIndex()) 34 35 gas, err := stake.IntrinsicGas() 36 require.NoError(err) 37 require.Equal(uint64(10700), gas) 38 cost, err := stake.Cost() 39 require.NoError(err) 40 require.Equal("107000", cost.Text(10)) 41 42 proto := stake.Proto() 43 stake2 := &ChangeCandidate{} 44 require.NoError(stake2.LoadProto(proto)) 45 require.Equal(_payload, stake2.Payload()) 46 require.Equal(_canName, stake2.Candidate()) 47 require.Equal(_index, stake2.BucketIndex()) 48 49 t.Run("Invalid Gas Price", func(t *testing.T) { 50 cc, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, new(big.Int).Mul(_gasprice, big.NewInt(-1))) 51 require.NoError(err) 52 require.Equal(ErrNegativeValue, errors.Cause(cc.SanityCheck())) 53 }) 54 } 55 56 func TestChangeCandidateSignVerify(t *testing.T) { 57 require := require.New(t) 58 require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString()) 59 stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice) 60 require.NoError(err) 61 62 bd := &EnvelopeBuilder{} 63 elp := bd.SetGasLimit(_gaslimit). 64 SetGasPrice(_gasprice). 65 SetAction(stake).Build() 66 // sign 67 selp, err := Sign(elp, _senderKey) 68 require.NoError(err) 69 require.NotNil(selp) 70 ser, err := proto.Marshal(selp.Proto()) 71 require.NoError(err) 72 require.Equal("0a24080118c0843d22023130ea0217080a120a63616e646964617465311a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a412da6cd3ba7e830f0b669661f88a5c03307bdb44cae6fb45a4432ab69719f051f6631276467977617be2d265fdb00a3acc3a493261e2363a60acd3512aa15a89301", hex.EncodeToString(ser)) 73 hash, err := selp.Hash() 74 require.NoError(err) 75 require.Equal("bc65d832237134c6a38d6ba10637af097b432d6c83d267aa6235e5b7c953d30f", hex.EncodeToString(hash[:])) 76 // verify signature 77 require.NoError(selp.VerifySignature()) 78 } 79 80 func TestChangeCandidateABIEncodeAndDecode(t *testing.T) { 81 require := require.New(t) 82 stake, err := NewChangeCandidate(_nonce, _canName, _index, _payload, _gaslimit, _gasprice) 83 require.NoError(err) 84 85 data, err := stake.EncodeABIBinary() 86 require.NoError(err) 87 stake, err = NewChangeCandidateFromABIBinary(data) 88 require.NoError(err) 89 require.Equal(_canName, stake.candidateName) 90 require.Equal(_index, stake.bucketIndex) 91 require.Equal(_payload, stake.payload) 92 }