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

     1  package action
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/iotexproject/iotex-proto/golang/iotextypes"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/iotexproject/iotex-core/pkg/unit"
    11  	"github.com/iotexproject/iotex-core/state"
    12  	"github.com/iotexproject/iotex-core/test/identityset"
    13  )
    14  
    15  func TestEnvelope_Basic(t *testing.T) {
    16  	req := require.New(t)
    17  	evlp, tsf := createEnvelope()
    18  	req.Equal(uint32(1), evlp.Version())
    19  	req.Equal(uint64(10), evlp.Nonce())
    20  	req.Equal(uint64(20010), evlp.GasLimit())
    21  	req.Equal("11000000000000000000", evlp.GasPrice().String())
    22  	c, err := evlp.Cost()
    23  	req.NoError(err)
    24  	req.Equal("111010000000000000000000", c.String())
    25  	g, err := evlp.IntrinsicGas()
    26  	req.NoError(err)
    27  	req.Equal(uint64(10000), g)
    28  	d, ok := evlp.Destination()
    29  	req.True(ok)
    30  	req.Equal("io1jh0ekmccywfkmj7e8qsuzsupnlk3w5337hjjg2", d)
    31  	tsf2, ok := evlp.Action().(*Transfer)
    32  	req.True(ok)
    33  	req.Equal(tsf, tsf2)
    34  
    35  	evlp.SetNonce(_nonce)
    36  	req.Equal(_nonce, evlp.Nonce())
    37  	evlp.SetChainID(tsf.chainID)
    38  	req.Equal(tsf.chainID, evlp.ChainID())
    39  }
    40  
    41  func TestEnvelope_Proto(t *testing.T) {
    42  	req := require.New(t)
    43  	eb, tsf := createEnvelope()
    44  	evlp, ok := eb.(*envelope)
    45  	req.True(ok)
    46  
    47  	proto := evlp.Proto()
    48  	actCore := &iotextypes.ActionCore{
    49  		Version:  evlp.version,
    50  		Nonce:    evlp.nonce,
    51  		GasLimit: evlp.gasLimit,
    52  		ChainID:  evlp.chainID,
    53  	}
    54  	actCore.GasPrice = evlp.gasPrice.String()
    55  	actCore.Action = &iotextypes.ActionCore_Transfer{Transfer: tsf.Proto()}
    56  	req.Equal(actCore, proto)
    57  
    58  	evlp2 := &envelope{}
    59  	req.NoError(evlp2.LoadProto(proto))
    60  	req.Equal(evlp.version, evlp2.version)
    61  	req.Equal(evlp.chainID, evlp2.chainID)
    62  	req.Equal(evlp.nonce, evlp2.nonce)
    63  	req.Equal(evlp.gasLimit, evlp2.gasLimit)
    64  	req.Equal(evlp.gasPrice, evlp2.gasPrice)
    65  	tsf2, ok := evlp2.Action().(*Transfer)
    66  	req.True(ok)
    67  	req.Equal(tsf.amount, tsf2.amount)
    68  	req.Equal(tsf.recipient, tsf2.recipient)
    69  	req.Equal(tsf.payload, tsf2.payload)
    70  }
    71  
    72  func TestEnvelope_Actions(t *testing.T) {
    73  	require := require.New(t)
    74  	candidates := state.CandidateList{}
    75  	putPollResult := NewPutPollResult(1, 10001, candidates)
    76  
    77  	createStake, err := NewCreateStake(uint64(10), _addr2, "100", uint32(10000), true, _payload, _gasLimit, _gasPrice)
    78  	require.NoError(err)
    79  
    80  	depositToStake, err := NewDepositToStake(1, 2, big.NewInt(10).String(), _payload, _gasLimit, _gasPrice)
    81  	require.NoError(err)
    82  
    83  	changeCandidate, err := NewChangeCandidate(1, _candidate1Name, 2, _payload, _gasLimit, _gasPrice)
    84  	require.NoError(err)
    85  
    86  	unstake, err := NewUnstake(_nonce, 2, _payload, _gasLimit, _gasPrice)
    87  	require.NoError(err)
    88  
    89  	withdrawStake, err := NewWithdrawStake(_nonce, 2, _payload, _gasLimit, _gasPrice)
    90  	require.NoError(err)
    91  
    92  	restake, err := NewRestake(_nonce, _index, _duration, _autoStake, _payload, _gasLimit, _gasPrice)
    93  	require.NoError(err)
    94  
    95  	transferStake, err := NewTransferStake(_nonce, _cand1Addr, 2, _payload, _gasLimit, _gasPrice)
    96  	require.NoError(err)
    97  
    98  	candidateRegister, err := NewCandidateRegister(_nonce, _candidate1Name, _cand1Addr, _cand1Addr, _cand1Addr, big.NewInt(10).String(), 91, true, _payload, _gasLimit, _gasPrice)
    99  	require.NoError(err)
   100  
   101  	candidateUpdate, err := NewCandidateUpdate(_nonce, _candidate1Name, _cand1Addr, _cand1Addr, _gasLimit, _gasPrice)
   102  	require.NoError(err)
   103  
   104  	gb := GrantRewardBuilder{}
   105  	grantReward := gb.Build()
   106  
   107  	cb := ClaimFromRewardingFundBuilder{}
   108  	claimFromRewardingFund := cb.SetAmount(big.NewInt(1)).Build()
   109  
   110  	cf := DepositToRewardingFundBuilder{}
   111  	depositToRewardingFund := cf.SetAmount(big.NewInt(1)).Build()
   112  
   113  	tests := []actionPayload{
   114  		putPollResult,
   115  		createStake,
   116  		depositToStake,
   117  		changeCandidate,
   118  		unstake,
   119  		withdrawStake,
   120  		restake,
   121  		transferStake,
   122  		candidateRegister,
   123  		candidateUpdate,
   124  		&grantReward,
   125  		&claimFromRewardingFund,
   126  		&depositToRewardingFund,
   127  	}
   128  
   129  	for _, test := range tests {
   130  		bd := &EnvelopeBuilder{}
   131  		elp := bd.SetNonce(1).
   132  			SetAction(test).
   133  			SetGasLimit(100000).
   134  			SetChainID(1).Build()
   135  		evlp, ok := elp.(*envelope)
   136  		require.True(ok)
   137  		require.NoError(evlp.LoadProto(evlp.Proto()))
   138  		require.Equal(elp.Version(), evlp.Version())
   139  		require.Equal(elp.Nonce(), evlp.Nonce())
   140  		require.Equal(elp.ChainID(), evlp.ChainID())
   141  		require.Equal(elp.GasPrice(), evlp.GasPrice())
   142  		require.Equal(elp.GasLimit(), evlp.GasLimit())
   143  	}
   144  }
   145  
   146  func createEnvelope() (Envelope, *Transfer) {
   147  	tsf, _ := NewTransfer(
   148  		uint64(10),
   149  		unit.ConvertIotxToRau(1000+int64(10)),
   150  		identityset.Address(10%identityset.Size()).String(),
   151  		nil,
   152  		20000+uint64(10),
   153  		unit.ConvertIotxToRau(1+int64(10)),
   154  	)
   155  	eb := EnvelopeBuilder{}
   156  	evlp := eb.
   157  		SetAction(tsf).
   158  		SetGasLimit(tsf.GasLimit()).
   159  		SetGasPrice(tsf.GasPrice()).
   160  		SetNonce(tsf.Nonce()).
   161  		SetVersion(1).
   162  		SetChainID(1).
   163  		Build()
   164  	return evlp, tsf
   165  }