github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stake_transferownership_test.go (about)

     1  package action
     2  
     3  import (
     4  	"encoding/hex"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  	"google.golang.org/protobuf/proto"
     9  )
    10  
    11  func TestStakingTransfer(t *testing.T) {
    12  	require := require.New(t)
    13  	stake, err := NewTransferStake(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
    14  	require.NoError(err)
    15  
    16  	ser := stake.Serialize()
    17  	require.Equal("080a1229696f3178707136326177383575717a72636367397935686e727976386c64326e6b7079636333677a611a077061796c6f6164", hex.EncodeToString(ser))
    18  
    19  	require.NoError(err)
    20  	require.Equal(_gaslimit, stake.GasLimit())
    21  	require.Equal(_gasprice, stake.GasPrice())
    22  	require.Equal(_nonce, stake.Nonce())
    23  
    24  	require.Equal(_payload, stake.Payload())
    25  	require.Equal(_canAddress, stake.VoterAddress().String())
    26  	require.Equal(_index, stake.BucketIndex())
    27  
    28  	gas, err := stake.IntrinsicGas()
    29  	require.NoError(err)
    30  	require.Equal(uint64(10700), gas)
    31  	cost, err := stake.Cost()
    32  	require.NoError(err)
    33  	require.Equal("107000", cost.Text(10))
    34  
    35  	proto := stake.Proto()
    36  	stake2 := &TransferStake{}
    37  	require.NoError(stake2.LoadProto(proto))
    38  	require.Equal(_payload, stake2.Payload())
    39  	require.Equal(_canAddress, stake2.VoterAddress().String())
    40  	require.Equal(_index, stake2.BucketIndex())
    41  }
    42  
    43  func TestStakingTransferSignVerify(t *testing.T) {
    44  	require := require.New(t)
    45  	require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString())
    46  	stake, err := NewTransferStake(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
    47  	require.NoError(err)
    48  
    49  	bd := &EnvelopeBuilder{}
    50  	elp := bd.SetGasLimit(_gaslimit).
    51  		SetGasPrice(_gasprice).
    52  		SetAction(stake).Build()
    53  	// sign
    54  	selp, err := Sign(elp, _senderKey)
    55  	require.NoError(err)
    56  	require.NotNil(selp)
    57  	ser, err := proto.Marshal(selp.Proto())
    58  	require.NoError(err)
    59  	require.Equal("0a43080118c0843d22023130f20236080a1229696f3178707136326177383575717a72636367397935686e727976386c64326e6b7079636333677a611a077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41fa26db427ab87a56a129196c1604f2e22c4dd2a1f99b2217bc916260757d00093d9e6dccdf53e3b0b64e41a69d71c238fbf9281625164694a74dfbeba075d0ce01", hex.EncodeToString(ser))
    60  	hash, err := selp.Hash()
    61  	require.NoError(err)
    62  	require.Equal("74b2e1d6a09ba5d1298fa422d5850991ae516865077282196295a38f93c78b85", hex.EncodeToString(hash[:]))
    63  	// verify signature
    64  	require.NoError(selp.VerifySignature())
    65  }
    66  
    67  func TestStakingTransferABIEncodeAndDecode(t *testing.T) {
    68  	require := require.New(t)
    69  	stake, err := NewTransferStake(_nonce, _canAddress, _index, _payload, _gaslimit, _gasprice)
    70  	require.NoError(err)
    71  
    72  	data, err := stake.EncodeABIBinary()
    73  	require.NoError(err)
    74  	stake, err = NewTransferStakeFromABIBinary(data)
    75  	require.NoError(err)
    76  	require.Equal(_canAddress, stake.voterAddress.String())
    77  	require.Equal(_index, stake.bucketIndex)
    78  	require.Equal(_payload, stake.payload)
    79  }