github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stakerestake_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  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  	"google.golang.org/protobuf/proto"
    14  )
    15  
    16  var (
    17  	_duration  = uint32(1000)
    18  	_autoStake = true
    19  )
    20  
    21  func TestRestake(t *testing.T) {
    22  	require := require.New(t)
    23  	stake, err := NewRestake(_nonce, _index, _duration, _autoStake, _payload, _gaslimit, _gasprice)
    24  	require.NoError(err)
    25  
    26  	ser := stake.Serialize()
    27  	require.Equal("080a10e807180122077061796c6f6164", hex.EncodeToString(ser))
    28  
    29  	require.NoError(err)
    30  	require.Equal(_gaslimit, stake.GasLimit())
    31  	require.Equal(_gasprice, stake.GasPrice())
    32  	require.Equal(_nonce, stake.Nonce())
    33  
    34  	require.True(stake.AutoStake())
    35  	require.Equal(_payload, stake.Payload())
    36  	require.Equal(_duration, stake.Duration())
    37  	require.Equal(_index, stake.BucketIndex())
    38  
    39  	gas, err := stake.IntrinsicGas()
    40  	require.NoError(err)
    41  	require.Equal(uint64(10700), gas)
    42  	cost, err := stake.Cost()
    43  	require.NoError(err)
    44  	require.Equal("107000", cost.Text(10))
    45  
    46  	proto := stake.Proto()
    47  	stake2 := &Restake{}
    48  	require.NoError(stake2.LoadProto(proto))
    49  	require.True(stake2.AutoStake())
    50  	require.Equal(_payload, stake2.Payload())
    51  	require.Equal(_duration, stake2.Duration())
    52  	require.Equal(_index, stake2.BucketIndex())
    53  }
    54  
    55  func TestRestakeSignVerify(t *testing.T) {
    56  	require := require.New(t)
    57  	require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString())
    58  	stake, err := NewRestake(_nonce, _index, _duration, _autoStake, _payload, _gaslimit, _gasprice)
    59  	require.NoError(err)
    60  
    61  	bd := &EnvelopeBuilder{}
    62  	elp := bd.SetGasLimit(_gaslimit).
    63  		SetGasPrice(_gasprice).
    64  		SetAction(stake).Build()
    65  	// sign
    66  	selp, err := Sign(elp, _senderKey)
    67  	require.NoError(err)
    68  	require.NotNil(selp)
    69  	ser, err := proto.Marshal(selp.Proto())
    70  	require.NoError(err)
    71  	require.Equal("0a1d080118c0843d22023130e20210080a10e807180122077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a41e2e763aed5b1fd1a8601de0f0ae34eb05162e34b0389ae3418eedbf762f64959634a968313a6516dba3a97b34efba4753bbed3a33d409ecbd45ac75007cd8e9101", hex.EncodeToString(ser))
    72  	hash, err := selp.Hash()
    73  	require.NoError(err)
    74  	require.Equal("8816e8f784a1fce40b54d1cd172bb6976fd9552f1570c73d1d9fcdc5635424a9", hex.EncodeToString(hash[:]))
    75  	// verify signature
    76  	require.NoError(selp.VerifySignature())
    77  }
    78  
    79  func TestRestakeABIEncodeAndDecode(t *testing.T) {
    80  	require := require.New(t)
    81  	stake, err := NewRestake(_nonce, _index, _duration, _autoStake, _payload, _gaslimit, _gasprice)
    82  	require.NoError(err)
    83  
    84  	data, err := stake.EncodeABIBinary()
    85  	require.NoError(err)
    86  	stake, err = NewRestakeFromABIBinary(data)
    87  	require.NoError(err)
    88  	require.Equal(_index, stake.bucketIndex)
    89  	require.Equal(_duration, stake.Duration())
    90  	require.Equal(_autoStake, stake.AutoStake())
    91  	require.Equal(_payload, stake.payload)
    92  }