github.com/iotexproject/iotex-core@v1.14.1-rc1/action/builder_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  	"encoding/hex"
    10  	"math/big"
    11  	"testing"
    12  
    13  	"github.com/ethereum/go-ethereum/common"
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/iotexproject/iotex-address/address"
    16  	"github.com/stretchr/testify/require"
    17  
    18  	"github.com/iotexproject/iotex-core/pkg/version"
    19  )
    20  
    21  func TestBuilderEthAddr(t *testing.T) {
    22  	r := require.New(t)
    23  
    24  	r.Equal(address.StakingProtocolAddrHash[:], _stakingProtocolEthAddr.Bytes())
    25  	r.Equal(address.RewardingProtocolAddrHash[:], _rewardingProtocolEthAddr.Bytes())
    26  }
    27  
    28  func TestActionBuilder(t *testing.T) {
    29  	r := require.New(t)
    30  
    31  	bd := &Builder{}
    32  	act := bd.SetVersion(version.ProtocolVersion).
    33  		SetNonce(2).
    34  		SetGasLimit(10003).
    35  		SetGasPrice(big.NewInt(10004)).
    36  		Build()
    37  
    38  	r.Equal(uint32(version.ProtocolVersion), act.Version())
    39  	r.Equal(uint64(2), act.Nonce())
    40  	r.Equal(uint64(10003), act.GasLimit())
    41  	r.Equal(big.NewInt(10004), act.GasPrice())
    42  }
    43  
    44  func TestBuildRewardingAction(t *testing.T) {
    45  	r := require.New(t)
    46  
    47  	eb := &EnvelopeBuilder{}
    48  	eb.SetChainID(2)
    49  
    50  	claimData, _ := hex.DecodeString("2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000")
    51  	to := common.HexToAddress("0x0000000000000000000000000000000000000001")
    52  	env, err := eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
    53  		Nonce:    1,
    54  		GasPrice: big.NewInt(10004),
    55  		Gas:      10000,
    56  		To:       &to,
    57  		Value:    big.NewInt(100),
    58  		Data:     claimData,
    59  	}))
    60  	r.Nil(env)
    61  	r.EqualValues("invalid action type", err.Error())
    62  
    63  	env, err = eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
    64  		Nonce:    1,
    65  		GasPrice: big.NewInt(10004),
    66  		Gas:      10000,
    67  		To:       &_rewardingProtocolEthAddr,
    68  		Value:    big.NewInt(100),
    69  		Data:     claimData,
    70  	}))
    71  	r.Nil(err)
    72  	r.IsType(&ClaimFromRewardingFund{}, env.Action())
    73  	r.EqualValues(big.NewInt(10004), env.GasPrice())
    74  	r.EqualValues(10000, env.GasLimit())
    75  	r.EqualValues(big.NewInt(101), env.Action().(*ClaimFromRewardingFund).Amount())
    76  
    77  	depositData, _ := hex.DecodeString("27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003")
    78  	to = common.HexToAddress("0x0000000000000000000000000000000000000001")
    79  	env, err = eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
    80  		Nonce:    1,
    81  		GasPrice: big.NewInt(10004),
    82  		Gas:      10000,
    83  		To:       &to,
    84  		Value:    big.NewInt(100),
    85  		Data:     depositData,
    86  	}))
    87  	r.Nil(env)
    88  	r.EqualValues("invalid action type", err.Error())
    89  
    90  	env, err = eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
    91  		Nonce:    1,
    92  		GasPrice: big.NewInt(10004),
    93  		Gas:      10000,
    94  		To:       &_rewardingProtocolEthAddr,
    95  		Value:    big.NewInt(100),
    96  		Data:     depositData,
    97  	}))
    98  	r.Nil(err)
    99  	r.IsType(&DepositToRewardingFund{}, env.Action())
   100  	r.EqualValues(big.NewInt(10004), env.GasPrice())
   101  	r.EqualValues(10000, env.GasLimit())
   102  	r.EqualValues(big.NewInt(101), env.Action().(*DepositToRewardingFund).Amount())
   103  }