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

     1  // Copyright (c) 2019 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  	"math/big"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/iotexproject/iotex-core/test/identityset"
    15  )
    16  
    17  var (
    18  	// addr1   = identityset.Address(27).String()
    19  	_priKey1 = identityset.PrivateKey(27)
    20  	_addr2   = identityset.Address(28).String()
    21  	// Create two candidates
    22  	_cand1PriKey  = identityset.PrivateKey(11)
    23  	_cand1Addr    = identityset.Address(12).String()
    24  	_cand2PriKey  = identityset.PrivateKey(13)
    25  	_cand2Addr    = identityset.Address(14).String()
    26  	_selfStake, _ = new(big.Int).SetString("1200000000000000000000000", 10)
    27  )
    28  
    29  var (
    30  	_gasPrice = big.NewInt(10)
    31  	_gasLimit = uint64(1000000)
    32  )
    33  
    34  const (
    35  	_candidate1Name = "candidate1"
    36  	_candidate2Name = "candidate2"
    37  )
    38  
    39  func TestSignedTransfer(t *testing.T) {
    40  	require := require.New(t)
    41  	selp, err := SignedTransfer(_addr2, _priKey1, uint64(1), big.NewInt(2), []byte{}, uint64(100000), big.NewInt(10))
    42  	require.NoError(err)
    43  
    44  	tsf := selp.Action().(*Transfer)
    45  	require.Equal(_addr2, tsf.Recipient())
    46  	require.Equal(uint64(1), tsf.Nonce())
    47  	require.Equal(big.NewInt(2), tsf.Amount())
    48  	require.Equal([]byte{}, tsf.Payload())
    49  	require.Equal(uint64(100000), tsf.GasLimit())
    50  	require.Equal(big.NewInt(10), tsf.GasPrice())
    51  	require.NotNil(selp.Signature())
    52  }
    53  
    54  func TestSignedExecution(t *testing.T) {
    55  	require := require.New(t)
    56  	selp, err := SignedExecution(EmptyAddress, _priKey1, uint64(1), big.NewInt(0), uint64(100000), big.NewInt(10), []byte{})
    57  	require.NoError(err)
    58  
    59  	exec := selp.Action().(*Execution)
    60  	require.Equal(EmptyAddress, exec.Contract())
    61  	require.Equal(uint64(1), exec.Nonce())
    62  	require.Equal(big.NewInt(0), exec.Amount())
    63  	require.Equal(uint64(100000), exec.GasLimit())
    64  	require.Equal(big.NewInt(10), exec.GasPrice())
    65  	require.Equal([]byte{}, exec.Data())
    66  	require.NotNil(selp.Signature())
    67  }
    68  
    69  func TestSignedCandidateRegister(t *testing.T) {
    70  	require := require.New(t)
    71  	selp, err := SignedCandidateRegister(1, _candidate1Name, _cand1Addr, _cand1Addr, _cand1Addr, big.NewInt(10).String(), 91, true, []byte{}, _gasLimit, _gasPrice, _cand1PriKey)
    72  	require.NoError(err)
    73  
    74  	cand := selp.Action().(*CandidateRegister)
    75  	require.Equal(uint64(1), cand.Nonce())
    76  	require.Equal(_gasLimit, cand.GasLimit())
    77  	require.Equal(_gasPrice, cand.GasPrice())
    78  	require.Equal(_candidate1Name, cand.name)
    79  	require.Equal(identityset.Address(12), cand.operatorAddress)
    80  	require.Equal(identityset.Address(12), cand.rewardAddress)
    81  	require.Equal(big.NewInt(10), cand.Amount())
    82  	require.Equal(uint32(91), cand.duration)
    83  	require.Equal(true, cand.autoStake)
    84  	require.Equal([]byte{}, cand.payload)
    85  	require.NotNil(selp.Signature())
    86  }
    87  
    88  func TestSignedCandidateUpdate(t *testing.T) {
    89  	require := require.New(t)
    90  	selp, err := SignedCandidateUpdate(1, _candidate1Name, _cand1Addr, _cand1Addr, _gasLimit, _gasPrice, _cand1PriKey)
    91  	require.NoError(err)
    92  
    93  	canu := selp.Action().(*CandidateUpdate)
    94  	require.Equal(uint64(1), canu.Nonce())
    95  	require.Equal(_gasLimit, canu.GasLimit())
    96  	require.Equal(_gasPrice, canu.GasPrice())
    97  	require.NotNil(selp.Signature())
    98  }
    99  
   100  func TestSignedCreateStake(t *testing.T) {
   101  	require := require.New(t)
   102  	selp, err := SignedCreateStake(1, _candidate1Name, big.NewInt(10).String(), 91, true, []byte{}, _gasLimit, _gasPrice, _cand1PriKey)
   103  	require.NoError(err)
   104  
   105  	exec := selp.Action().(*CreateStake)
   106  	require.Equal(_candidate1Name, exec.candName)
   107  	require.Equal(uint64(1), exec.Nonce())
   108  	require.Equal(big.NewInt(10), exec.Amount())
   109  	require.Equal(_gasLimit, exec.GasLimit())
   110  	require.Equal(_gasPrice, exec.GasPrice())
   111  	require.Equal([]byte{}, exec.payload)
   112  	require.Equal(true, exec.autoStake)
   113  	require.NotNil(selp.Signature())
   114  }
   115  
   116  func TestNewUnstakeSignedReclaimStake(t *testing.T) {
   117  	require := require.New(t)
   118  	selp, err := SignedReclaimStake(false, 1, 2, []byte{}, _gasLimit, _gasPrice, _priKey1)
   119  	require.NoError(err)
   120  
   121  	exec := selp.Action().(*Unstake)
   122  	require.Equal(uint64(1), exec.Nonce())
   123  	require.Equal(uint64(2), exec.bucketIndex)
   124  	require.Equal(_gasLimit, exec.GasLimit())
   125  	require.Equal(_gasPrice, exec.GasPrice())
   126  	require.Equal([]byte{}, exec.payload)
   127  	require.NotNil(selp.Signature())
   128  }
   129  
   130  func TestNewWithdrawStakeSignedReclaimStake(t *testing.T) {
   131  	require := require.New(t)
   132  	selp, err := SignedReclaimStake(true, 1, 2, []byte{}, _gasLimit, _gasPrice, _priKey1)
   133  	require.NoError(err)
   134  
   135  	exec := selp.Action().(*WithdrawStake)
   136  	require.Equal(uint64(1), exec.Nonce())
   137  	require.Equal(uint64(2), exec.bucketIndex)
   138  	require.Equal(_gasLimit, exec.GasLimit())
   139  	require.Equal(_gasPrice, exec.GasPrice())
   140  	require.Equal([]byte{}, exec.payload)
   141  	require.NotNil(selp.Signature())
   142  }
   143  
   144  func TestSignedChangeCandidate(t *testing.T) {
   145  	require := require.New(t)
   146  	selp, err := SignedChangeCandidate(1, _candidate1Name, 2, []byte{}, _gasLimit, _gasPrice, _priKey1)
   147  	require.NoError(err)
   148  
   149  	exec := selp.Action().(*ChangeCandidate)
   150  	require.Equal(_candidate1Name, exec.candidateName)
   151  	require.Equal(uint64(1), exec.Nonce())
   152  	require.Equal(uint64(2), exec.bucketIndex)
   153  	require.Equal(_gasLimit, exec.GasLimit())
   154  	require.Equal(_gasPrice, exec.GasPrice())
   155  	require.Equal([]byte{}, exec.payload)
   156  	require.NotNil(selp.Signature())
   157  }
   158  
   159  func TestSignedTransferStake(t *testing.T) {
   160  	require := require.New(t)
   161  	selp, err := SignedTransferStake(1, _cand1Addr, 2, []byte{}, _gasLimit, _gasPrice, _priKey1)
   162  	require.NoError(err)
   163  
   164  	exec := selp.Action().(*TransferStake)
   165  	require.Equal(identityset.Address(12), exec.voterAddress)
   166  	require.Equal(uint64(1), exec.Nonce())
   167  	require.Equal(uint64(2), exec.bucketIndex)
   168  	require.Equal(_gasLimit, exec.GasLimit())
   169  	require.Equal(_gasPrice, exec.GasPrice())
   170  	require.Equal([]byte{}, exec.payload)
   171  	require.NotNil(selp.Signature())
   172  }
   173  
   174  func TestSignedDepositToStake(t *testing.T) {
   175  	require := require.New(t)
   176  	selp, err := SignedDepositToStake(1, 2, big.NewInt(10).String(), []byte{}, _gasLimit, _gasPrice, _priKey1)
   177  	require.NoError(err)
   178  
   179  	exec := selp.Action().(*DepositToStake)
   180  	require.Equal(uint64(2), exec.bucketIndex)
   181  	require.Equal(uint64(1), exec.Nonce())
   182  	require.Equal(big.NewInt(10), exec.Amount())
   183  	require.Equal(_gasLimit, exec.GasLimit())
   184  	require.Equal(_gasPrice, exec.GasPrice())
   185  	require.Equal([]byte{}, exec.payload)
   186  	require.NotNil(selp.Signature())
   187  }
   188  
   189  func TestSignedRestake(t *testing.T) {
   190  	require := require.New(t)
   191  	selp, err := SignedRestake(1, 2, 91, true, []byte{}, _gasLimit, _gasPrice, _priKey1)
   192  	require.NoError(err)
   193  
   194  	exec := selp.Action().(*Restake)
   195  	require.Equal(uint64(1), exec.Nonce())
   196  	require.Equal(uint32(91), exec.duration)
   197  	require.Equal(true, exec.autoStake)
   198  	require.Equal(uint64(2), exec.bucketIndex)
   199  	require.Equal(_gasLimit, exec.GasLimit())
   200  	require.Equal(_gasPrice, exec.GasPrice())
   201  	require.Equal([]byte{}, exec.payload)
   202  	require.NotNil(selp.Signature())
   203  }