github.com/iotexproject/iotex-core@v1.14.1-rc1/action/stakereclaim_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  	"github.com/iotexproject/iotex-core/test/identityset"
    17  )
    18  
    19  var (
    20  	_gaslimit   = uint64(1000000)
    21  	_gasprice   = big.NewInt(10)
    22  	_canAddress = "io1xpq62aw85uqzrccg9y5hnryv8ld2nkpycc3gza"
    23  	_canName    = "candidate1"
    24  	_payload    = []byte("payload")
    25  	_nonce      = uint64(0)
    26  	_index      = uint64(10)
    27  	_senderKey  = identityset.PrivateKey(27)
    28  )
    29  
    30  func TestUnstake(t *testing.T) {
    31  	require := require.New(t)
    32  	stake, err := NewUnstake(_nonce, _index, _payload, _gaslimit, _gasprice)
    33  	require.NoError(err)
    34  
    35  	ser := stake.Serialize()
    36  	require.Equal("080a12077061796c6f6164", hex.EncodeToString(ser))
    37  
    38  	require.NoError(err)
    39  	require.Equal(_gaslimit, stake.GasLimit())
    40  	require.Equal(_gasprice, stake.GasPrice())
    41  	require.Equal(_nonce, stake.Nonce())
    42  
    43  	require.Equal(_payload, stake.Payload())
    44  	require.Equal(_index, stake.BucketIndex())
    45  
    46  	gas, err := stake.IntrinsicGas()
    47  	require.NoError(err)
    48  	require.Equal(uint64(10700), gas)
    49  	cost, err := stake.Cost()
    50  	require.NoError(err)
    51  	require.Equal("107000", cost.Text(10))
    52  
    53  	proto := stake.Proto()
    54  	stake2 := &Unstake{}
    55  	require.NoError(stake2.LoadProto(proto))
    56  	require.Equal(_payload, stake2.Payload())
    57  	require.Equal(_index, stake2.BucketIndex())
    58  }
    59  
    60  func TestUnstakeSignVerify(t *testing.T) {
    61  	require := require.New(t)
    62  	require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString())
    63  
    64  	stake, err := NewUnstake(_nonce, _index, _payload, _gaslimit, _gasprice)
    65  	require.NoError(err)
    66  
    67  	bd := &EnvelopeBuilder{}
    68  	elp := bd.SetGasLimit(_gaslimit).
    69  		SetGasPrice(_gasprice).
    70  		SetAction(stake).Build()
    71  	// sign
    72  	selp, err := Sign(elp, _senderKey)
    73  	require.NoError(err)
    74  	require.NotNil(selp)
    75  	ser, err := proto.Marshal(selp.Proto())
    76  	require.NoError(err)
    77  	require.Equal("0a18080118c0843d22023130ca020b080a12077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a4100adee39b48e1d3dbbd65298a57c7889709fc4df39987130da306f6997374a184b7e7c232a42f21e89b06e6e7ceab81303c6b7483152d08d19ac829b22eb81e601", hex.EncodeToString(ser))
    78  	hash, err := selp.Hash()
    79  	require.NoError(err)
    80  	require.Equal("bed58b64a6c4e959eca60a86f0b2149ce0e1dd527ac5fd26aef725ebf7c22a7d", hex.EncodeToString(hash[:]))
    81  	// verify signature
    82  	require.NoError(selp.VerifySignature())
    83  }
    84  
    85  func TestUnstakeABIEncodeAndDecode(t *testing.T) {
    86  	require := require.New(t)
    87  	stake, err := NewUnstake(_nonce, _index, _payload, _gaslimit, _gasprice)
    88  	require.NoError(err)
    89  
    90  	data, err := stake.EncodeABIBinary()
    91  	require.NoError(err)
    92  	stake, err = NewUnstakeFromABIBinary(data)
    93  	require.NoError(err)
    94  	require.Equal(_index, stake.bucketIndex)
    95  	require.Equal(_payload, stake.payload)
    96  }
    97  
    98  func TestWithdraw(t *testing.T) {
    99  	require := require.New(t)
   100  	stake, err := NewWithdrawStake(_nonce, _index, _payload, _gaslimit, _gasprice)
   101  	require.NoError(err)
   102  
   103  	ser := stake.Serialize()
   104  	require.Equal("080a12077061796c6f6164", hex.EncodeToString(ser))
   105  
   106  	require.NoError(err)
   107  	require.Equal(_gaslimit, stake.GasLimit())
   108  	require.Equal(_gasprice, stake.GasPrice())
   109  	require.Equal(_nonce, stake.Nonce())
   110  
   111  	require.Equal(_payload, stake.Payload())
   112  	require.Equal(_index, stake.BucketIndex())
   113  
   114  	gas, err := stake.IntrinsicGas()
   115  	require.NoError(err)
   116  	require.Equal(uint64(10700), gas)
   117  	cost, err := stake.Cost()
   118  	require.NoError(err)
   119  	require.Equal("107000", cost.Text(10))
   120  
   121  	proto := stake.Proto()
   122  	stake2 := &WithdrawStake{}
   123  	require.NoError(stake2.LoadProto(proto))
   124  	require.Equal(_payload, stake2.Payload())
   125  	require.Equal(_index, stake2.BucketIndex())
   126  }
   127  
   128  func TestWithdrawSignVerify(t *testing.T) {
   129  	require := require.New(t)
   130  
   131  	require.Equal("cfa6ef757dee2e50351620dca002d32b9c090cfda55fb81f37f1d26b273743f1", _senderKey.HexString())
   132  
   133  	stake, err := NewWithdrawStake(_nonce, _index, _payload, _gaslimit, _gasprice)
   134  	require.NoError(err)
   135  
   136  	bd := &EnvelopeBuilder{}
   137  	elp := bd.SetGasLimit(_gaslimit).
   138  		SetGasPrice(_gasprice).
   139  		SetAction(stake).Build()
   140  	// sign
   141  	selp, err := Sign(elp, _senderKey)
   142  	require.NoError(err)
   143  	require.NotNil(selp)
   144  	ser, err := proto.Marshal(selp.Proto())
   145  	require.NoError(err)
   146  	require.Equal("0a18080118c0843d22023130d2020b080a12077061796c6f6164124104755ce6d8903f6b3793bddb4ea5d3589d637de2d209ae0ea930815c82db564ee8cc448886f639e8a0c7e94e99a5c1335b583c0bc76ef30dd6a1038ed9da8daf331a4152644d102186be6640d46b517331f3402e24424b0d85129595421d28503d75340b2922f5a0d4f667bbd6f576d9816770286b2ce032ba22eaec3952e24da4756b00", hex.EncodeToString(ser))
   147  	hash, err := selp.Hash()
   148  	require.NoError(err)
   149  	require.Equal("28049348cf34f1aa927caa250e7a1b08778c44efaf73b565b6fa9abe843871b4", hex.EncodeToString(hash[:]))
   150  	// verify signature
   151  	require.NoError(selp.VerifySignature())
   152  }
   153  
   154  func TestWithdrawABIEncodeAndDecode(t *testing.T) {
   155  	require := require.New(t)
   156  	stake, err := NewWithdrawStake(_nonce, _index, _payload, _gaslimit, _gasprice)
   157  	require.NoError(err)
   158  
   159  	data, err := stake.EncodeABIBinary()
   160  	require.NoError(err)
   161  	stake, err = NewWithdrawStakeFromABIBinary(data)
   162  	require.NoError(err)
   163  	require.Equal(_index, stake.bucketIndex)
   164  	require.Equal(_payload, stake.payload)
   165  }