github.com/iotexproject/iotex-core@v1.14.1-rc1/action/candidateupdate_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/stretchr/testify/require"
    14  	"google.golang.org/protobuf/proto"
    15  )
    16  
    17  var (
    18  	_cuNonce           = uint64(20)
    19  	_cuName            = "test"
    20  	_cuOperatorAddrStr = "io1cl6rl2ev5dfa988qmgzg2x4hfazmp9vn2g66ng"
    21  	_cuRewardAddrStr   = "io1juvx5g063eu4ts832nukp4vgcwk2gnc5cu9ayd"
    22  	_cuGasLimit        = uint64(200000)
    23  	_cuGasPrice        = big.NewInt(2000)
    24  )
    25  
    26  func TestCandidateUpdate(t *testing.T) {
    27  	require := require.New(t)
    28  	cu, err := NewCandidateUpdate(_cuNonce, _cuName, _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
    29  	require.NoError(err)
    30  
    31  	ser := cu.Serialize()
    32  	require.Equal("0a04746573741229696f31636c36726c32657635646661393838716d677a673278346866617a6d7039766e326736366e671a29696f316a757678356730363365753474733833326e756b7034766763776b32676e6335637539617964", hex.EncodeToString(ser))
    33  
    34  	require.NoError(err)
    35  	require.Equal(_cuGasLimit, cu.GasLimit())
    36  	require.Equal(_cuGasPrice, cu.GasPrice())
    37  	require.Equal(_cuNonce, cu.Nonce())
    38  
    39  	require.Equal(_cuName, cu.Name())
    40  	require.Equal(_cuOperatorAddrStr, cu.OperatorAddress().String())
    41  	require.Equal(_cuRewardAddrStr, cu.RewardAddress().String())
    42  
    43  	gas, err := cu.IntrinsicGas()
    44  	require.NoError(err)
    45  	require.Equal(uint64(10000), gas)
    46  	cost, err := cu.Cost()
    47  	require.NoError(err)
    48  	require.Equal("20000000", cost.Text(10))
    49  
    50  	proto := cu.Proto()
    51  	cu2 := &CandidateUpdate{}
    52  	require.NoError(cu2.LoadProto(proto))
    53  	require.Equal(_cuName, cu2.Name())
    54  	require.Equal(_cuOperatorAddrStr, cu2.OperatorAddress().String())
    55  	require.Equal(_cuRewardAddrStr, cu2.RewardAddress().String())
    56  }
    57  
    58  func TestCandidateUpdateSignVerify(t *testing.T) {
    59  	require := require.New(t)
    60  	cu, err := NewCandidateUpdate(_cuNonce, _cuName, _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
    61  	require.NoError(err)
    62  
    63  	bd := &EnvelopeBuilder{}
    64  	elp := bd.SetGasLimit(_gaslimit).
    65  		SetGasPrice(_gasprice).
    66  		SetAction(cu).Build()
    67  	// sign
    68  	selp, err := Sign(elp, _senderKey)
    69  	require.NoError(err)
    70  	require.NotNil(selp)
    71  	ser, err := proto.Marshal(selp.Proto())
    72  	require.NoError(err)
    73  	require.Equal("0a69080118c0843d2202313082035c0a04746573741229696f31636c36726c32657635646661393838716d677a673278346866617a6d7039766e326736366e671a29696f316a757678356730363365753474733833326e756b7034766763776b32676e6335637539617964124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a4101885c9c6684a4a8f2f5bf11f8326f27be48658f292e8f55ec8a11a604bb0c563a11ebf12d995ca1c152e00f8e0f0edf288db711aa10dbdfd5b7d73b4a28e1f701", hex.EncodeToString(ser))
    74  	hash, err := selp.Hash()
    75  	require.NoError(err)
    76  	require.Equal("ca1a28f0e9a58ffc67037cc75066dbdd8e024aa2b2e416e4d6ce16c3d86282e5", hex.EncodeToString(hash[:]))
    77  	// verify signature
    78  	require.NoError(selp.VerifySignature())
    79  }
    80  
    81  func TestCandidateUpdateABIEncodeAndDecode(t *testing.T) {
    82  	require := require.New(t)
    83  	stake, err := NewCandidateUpdate(_cuNonce, _cuName, _cuOperatorAddrStr, _cuRewardAddrStr, _cuGasLimit, _cuGasPrice)
    84  	require.NoError(err)
    85  
    86  	data, err := stake.EncodeABIBinary()
    87  	require.NoError(err)
    88  	stake, err = NewCandidateUpdateFromABIBinary(data)
    89  	require.NoError(err)
    90  	require.Equal(_cuName, stake.Name())
    91  	require.Equal(_cuOperatorAddrStr, stake.OperatorAddress().String())
    92  	require.Equal(_cuRewardAddrStr, stake.RewardAddress().String())
    93  
    94  	stake.rewardAddress = nil
    95  	_, err = stake.EncodeABIBinary()
    96  	require.Equal(ErrAddress, err)
    97  	stake.operatorAddress = nil
    98  	_, err = stake.EncodeABIBinary()
    99  	require.Equal(ErrAddress, err)
   100  }