diff --git a/action/builder.go b/action/builder.go index 495208683f..e7c2d1a25d 100644 --- a/action/builder.go +++ b/action/builder.go @@ -16,16 +16,16 @@ import ( "github.com/iotexproject/iotex-core/pkg/version" ) +var ( + _stakingProtocolEthAddr = common.BytesToAddress(address.StakingProtocolAddrHash[:]) + _rewardingProtocolEthAddr = common.BytesToAddress(address.RewardingProtocolAddrHash[:]) +) + // Builder is used to build an action. type Builder struct { act AbstractAction } -var ( - _stakingProtocolAddr, _ = address.FromString(address.StakingProtocolAddr) - _rewardingProtocolAddr, _ = address.FromString(address.RewardingProtocol) -) - // SetVersion sets action's version. func (b *Builder) SetVersion(v uint32) *Builder { b.act.version = v @@ -191,7 +191,7 @@ func (b *EnvelopeBuilder) BuildExecution(tx *types.Transaction) (Envelope, error // BuildStakingAction loads staking action into envelope from abi-encoded data func (b *EnvelopeBuilder) BuildStakingAction(tx *types.Transaction) (Envelope, error) { - if !bytes.Equal(tx.To().Bytes(), _stakingProtocolAddr.Bytes()) { + if !bytes.Equal(tx.To().Bytes(), _stakingProtocolEthAddr.Bytes()) { return nil, ErrInvalidAct } b.setEnvelopeCommonFields(tx) @@ -205,7 +205,7 @@ func (b *EnvelopeBuilder) BuildStakingAction(tx *types.Transaction) (Envelope, e // BuildRewardingAction loads rewarding action into envelope from abi-encoded data func (b *EnvelopeBuilder) BuildRewardingAction(tx *types.Transaction) (Envelope, error) { - if !bytes.Equal(tx.To().Bytes(), _rewardingProtocolAddr.Bytes()) { + if !bytes.Equal(tx.To().Bytes(), _rewardingProtocolEthAddr.Bytes()) { return nil, ErrInvalidAct } b.setEnvelopeCommonFields(tx) diff --git a/action/builder_test.go b/action/builder_test.go index 8bd7dacee2..01a0a223ef 100644 --- a/action/builder_test.go +++ b/action/builder_test.go @@ -12,13 +12,22 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/stretchr/testify/assert" + "github.com/iotexproject/iotex-address/address" "github.com/stretchr/testify/require" "github.com/iotexproject/iotex-core/pkg/version" ) +func TestBuilderEthAddr(t *testing.T) { + r := require.New(t) + + r.Equal(address.StakingProtocolAddrHash[:], _stakingProtocolEthAddr.Bytes()) + r.Equal(address.RewardingProtocolAddrHash[:], _rewardingProtocolEthAddr.Bytes()) +} + func TestActionBuilder(t *testing.T) { + r := require.New(t) + bd := &Builder{} act := bd.SetVersion(version.ProtocolVersion). SetNonce(2). @@ -26,10 +35,10 @@ func TestActionBuilder(t *testing.T) { SetGasPrice(big.NewInt(10004)). Build() - assert.Equal(t, uint32(version.ProtocolVersion), act.Version()) - assert.Equal(t, uint64(2), act.Nonce()) - assert.Equal(t, uint64(10003), act.GasLimit()) - assert.Equal(t, big.NewInt(10004), act.GasPrice()) + r.Equal(uint32(version.ProtocolVersion), act.Version()) + r.Equal(uint64(2), act.Nonce()) + r.Equal(uint64(10003), act.GasLimit()) + r.Equal(big.NewInt(10004), act.GasPrice()) } func TestBuildRewardingAction(t *testing.T) { @@ -45,7 +54,7 @@ func TestBuildRewardingAction(t *testing.T) { r.Nil(env) r.EqualValues("invalid action type", err.Error()) - tx = types.NewTransaction(1, common.HexToAddress(_rewardingProtocolAddr.Hex()), big.NewInt(100), 10000, big.NewInt(10004), claimData) + tx = types.NewTransaction(1, _rewardingProtocolEthAddr, big.NewInt(100), 10000, big.NewInt(10004), claimData) env, err = eb.BuildRewardingAction(tx) r.Nil(err) r.IsType(&ClaimFromRewardingFund{}, env.Action()) @@ -60,7 +69,7 @@ func TestBuildRewardingAction(t *testing.T) { r.Nil(env) r.EqualValues("invalid action type", err.Error()) - tx = types.NewTransaction(1, common.HexToAddress(_rewardingProtocolAddr.Hex()), big.NewInt(100), 10000, big.NewInt(10004), depositData) + tx = types.NewTransaction(1, _rewardingProtocolEthAddr, big.NewInt(100), 10000, big.NewInt(10004), depositData) env, err = eb.BuildRewardingAction(tx) r.Nil(err) r.IsType(&DepositToRewardingFund{}, env.Action()) diff --git a/action/candidate_register.go b/action/candidate_register.go index 08a37b128d..65e5e18d87 100644 --- a/action/candidate_register.go +++ b/action/candidate_register.go @@ -380,16 +380,11 @@ func ethAddrToNativeAddr(in interface{}) (address.Address, error) { // ToEthTx converts action to eth-compatible tx func (cr *CandidateRegister) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := cr.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(cr.Nonce(), ethAddr, big.NewInt(0), cr.GasLimit(), cr.GasPrice(), data), nil + return types.NewTransaction(cr.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), cr.GasLimit(), cr.GasPrice(), data), nil } // IsValidCandidateName check if a candidate name string is valid. diff --git a/action/candidate_update.go b/action/candidate_update.go index e860379d4d..2e40953f6a 100644 --- a/action/candidate_update.go +++ b/action/candidate_update.go @@ -244,14 +244,9 @@ func NewCandidateUpdateFromABIBinary(data []byte) (*CandidateUpdate, error) { // ToEthTx converts action to eth-compatible tx func (cu *CandidateUpdate) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := cu.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(cu.Nonce(), ethAddr, big.NewInt(0), cu.GasLimit(), cu.GasPrice(), data), nil + return types.NewTransaction(cu.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), cu.GasLimit(), cu.GasPrice(), data), nil } diff --git a/action/claimreward.go b/action/claimreward.go index 365350660d..ae09151f76 100644 --- a/action/claimreward.go +++ b/action/claimreward.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-proto/golang/iotextypes" ) @@ -161,16 +159,11 @@ func (c *ClaimFromRewardingFund) encodeABIBinary() ([]byte, error) { // ToEthTx converts action to eth-compatible tx func (c *ClaimFromRewardingFund) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.RewardingProtocol) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := c.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(c.Nonce(), ethAddr, big.NewInt(0), c.GasLimit(), c.GasPrice(), data), nil + return types.NewTransaction(c.Nonce(), _rewardingProtocolEthAddr, big.NewInt(0), c.GasLimit(), c.GasPrice(), data), nil } // NewClaimFromRewardingFundFromABIBinary decodes data into action diff --git a/action/claimreward_test.go b/action/claimreward_test.go index 22adbca18f..055a98c06c 100644 --- a/action/claimreward_test.go +++ b/action/claimreward_test.go @@ -5,8 +5,6 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/common" - "github.com/iotexproject/iotex-address/address" "github.com/stretchr/testify/require" ) @@ -107,15 +105,11 @@ func TestClaimRewardEncodeABIBinary(t *testing.T) { func TestClaimRewardToEthTx(t *testing.T) { r := require.New(t) - rewardingPool, _ := address.FromString(address.RewardingProtocol) - rewardEthAddr := common.BytesToAddress(rewardingPool.Bytes()) - rc := &ClaimFromRewardingFund{} - rc.amount = big.NewInt(101) tx, err := rc.ToEthTx() r.Nil(err) - r.EqualValues(rewardEthAddr, *tx.To()) + r.EqualValues(_rewardingProtocolEthAddr, *tx.To()) r.EqualValues( "2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(tx.Data()), @@ -125,7 +119,7 @@ func TestClaimRewardToEthTx(t *testing.T) { rc.data = []byte{1, 2, 3} tx, err = rc.ToEthTx() r.Nil(err) - r.EqualValues(rewardEthAddr, *tx.To()) + r.EqualValues(_rewardingProtocolEthAddr, *tx.To()) r.EqualValues( "2df163ef000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", hex.EncodeToString(tx.Data()), diff --git a/action/depositreward.go b/action/depositreward.go index 8bf394ed7d..c85ce8a2c2 100644 --- a/action/depositreward.go +++ b/action/depositreward.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-core/pkg/util/byteutil" "github.com/iotexproject/iotex-proto/golang/iotextypes" ) @@ -161,16 +159,11 @@ func (d *DepositToRewardingFund) encodeABIBinary() ([]byte, error) { // ToEthTx converts action to eth-compatible tx func (d *DepositToRewardingFund) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.RewardingProtocol) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := d.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(d.Nonce(), ethAddr, big.NewInt(0), d.GasLimit(), d.GasPrice(), data), nil + return types.NewTransaction(d.Nonce(), _rewardingProtocolEthAddr, big.NewInt(0), d.GasLimit(), d.GasPrice(), data), nil } // NewDepositToRewardingFundFromABIBinary decodes data into action diff --git a/action/depositreward_test.go b/action/depositreward_test.go index a08822dc63..21e86c0dab 100644 --- a/action/depositreward_test.go +++ b/action/depositreward_test.go @@ -5,8 +5,6 @@ import ( "math/big" "testing" - "github.com/ethereum/go-ethereum/common" - "github.com/iotexproject/iotex-address/address" "github.com/stretchr/testify/require" ) @@ -99,14 +97,11 @@ func TestDepositRewardEncodeABIBinary(t *testing.T) { func TestDepositRewardToEthTx(t *testing.T) { r := require.New(t) - rewardingPool, _ := address.FromString(address.RewardingProtocol) - rewardEthAddr := common.BytesToAddress(rewardingPool.Bytes()) - rp := &DepositToRewardingFund{} rp.amount = big.NewInt(101) tx, err := rp.ToEthTx() r.NoError(err) - r.EqualValues(rewardEthAddr, *tx.To()) + r.EqualValues(_rewardingProtocolEthAddr, *tx.To()) r.EqualValues( "27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000", hex.EncodeToString(tx.Data()), @@ -116,7 +111,7 @@ func TestDepositRewardToEthTx(t *testing.T) { rp.data = []byte{1, 2, 3} tx, err = rp.ToEthTx() r.NoError(err) - r.EqualValues(rewardEthAddr, *tx.To()) + r.EqualValues(_rewardingProtocolEthAddr, *tx.To()) r.EqualValues( "27852a6b000000000000000000000000000000000000000000000000000000000000006500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003", hex.EncodeToString(tx.Data()), diff --git a/action/protocol/rewarding/protocol_test.go b/action/protocol/rewarding/protocol_test.go index 18aeb7c108..53610306c2 100644 --- a/action/protocol/rewarding/protocol_test.go +++ b/action/protocol/rewarding/protocol_test.go @@ -335,6 +335,9 @@ func TestProtocol_Handle(t *testing.T) { // address package also defined protocol address, make sure they match require.Equal(t, p.addr.Bytes(), address.RewardingProtocolAddrHash[:]) + rwdAddr, err := address.FromString(address.RewardingProtocol) + require.NoError(t, err) + require.Equal(t, p.addr.Bytes(), rwdAddr.Bytes()) ctx := protocol.WithBlockCtx( context.Background(), diff --git a/action/protocol/staking/protocol_test.go b/action/protocol/staking/protocol_test.go index 1167bce3a3..168634da0a 100644 --- a/action/protocol/staking/protocol_test.go +++ b/action/protocol/staking/protocol_test.go @@ -101,6 +101,9 @@ func TestProtocol(t *testing.T) { // address package also defined protocol address, make sure they match r.Equal(stk.addr.Bytes(), address.StakingProtocolAddrHash[:]) + stkAddr, err := address.FromString(address.StakingProtocolAddr) + r.NoError(err) + r.Equal(stk.addr.Bytes(), stkAddr.Bytes()) // write a number of buckets into stateDB for _, e := range tests { diff --git a/action/receipt.go b/action/receipt.go index 2fad38e5e8..f5d90b388e 100644 --- a/action/receipt.go +++ b/action/receipt.go @@ -11,20 +11,11 @@ import ( "google.golang.org/protobuf/proto" "github.com/iotexproject/go-pkgs/hash" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/iotexproject/iotex-core/pkg/log" ) -var ( - // StakingBucketPoolTopic is topic for staking bucket pool - StakingBucketPoolTopic = hash.BytesToHash256(address.StakingProtocolAddrHash[:]) - - // RewardingPoolTopic is topic for rewarding pool - RewardingPoolTopic = hash.BytesToHash256(address.RewardingProtocolAddrHash[:]) -) - type ( // Topics are data items of a transaction, such as send/recipient address Topics []hash.Hash256 diff --git a/action/stake_adddeposit.go b/action/stake_adddeposit.go index 245d7b448b..85abaaee56 100644 --- a/action/stake_adddeposit.go +++ b/action/stake_adddeposit.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/iotexproject/iotex-core/pkg/util/byteutil" @@ -222,14 +220,9 @@ func NewDepositToStakeFromABIBinary(data []byte) (*DepositToStake, error) { // ToEthTx converts action to eth-compatible tx func (ds *DepositToStake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := ds.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(ds.Nonce(), ethAddr, big.NewInt(0), ds.GasLimit(), ds.GasPrice(), data), nil + return types.NewTransaction(ds.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), ds.GasLimit(), ds.GasPrice(), data), nil } diff --git a/action/stake_changecandidate.go b/action/stake_changecandidate.go index de67b582b7..f6992259bf 100644 --- a/action/stake_changecandidate.go +++ b/action/stake_changecandidate.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/iotexproject/iotex-core/pkg/util/byteutil" @@ -207,14 +205,9 @@ func NewChangeCandidateFromABIBinary(data []byte) (*ChangeCandidate, error) { // ToEthTx converts action to eth-compatible tx func (cc *ChangeCandidate) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := cc.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(cc.Nonce(), ethAddr, big.NewInt(0), cc.GasLimit(), cc.GasPrice(), data), nil + return types.NewTransaction(cc.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), cc.GasLimit(), cc.GasPrice(), data), nil } diff --git a/action/stake_create.go b/action/stake_create.go index 6c9dfaf109..fc8df0035c 100644 --- a/action/stake_create.go +++ b/action/stake_create.go @@ -11,9 +11,7 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/pkg/errors" "google.golang.org/protobuf/proto" @@ -263,14 +261,9 @@ func NewCreateStakeFromABIBinary(data []byte) (*CreateStake, error) { // ToEthTx converts action to eth-compatible tx func (cs *CreateStake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := cs.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(cs.Nonce(), ethAddr, big.NewInt(0), cs.GasLimit(), cs.GasPrice(), data), nil + return types.NewTransaction(cs.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), cs.GasLimit(), cs.GasPrice(), data), nil } diff --git a/action/stake_reclaim.go b/action/stake_reclaim.go index 3d2e64e24c..ed559c0b14 100644 --- a/action/stake_reclaim.go +++ b/action/stake_reclaim.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/iotexproject/iotex-core/pkg/util/byteutil" @@ -213,16 +211,11 @@ func NewUnstakeFromABIBinary(data []byte) (*Unstake, error) { // ToEthTx converts action to eth-compatible tx func (su *Unstake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := su.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(su.Nonce(), ethAddr, big.NewInt(0), su.GasLimit(), su.GasPrice(), data), nil + return types.NewTransaction(su.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), su.GasLimit(), su.GasPrice(), data), nil } // WithdrawStake defines the action of stake withdraw @@ -306,14 +299,9 @@ func NewWithdrawStakeFromABIBinary(data []byte) (*WithdrawStake, error) { // ToEthTx converts action to eth-compatible tx func (sw *WithdrawStake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := sw.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(sw.Nonce(), ethAddr, big.NewInt(0), sw.GasLimit(), sw.GasPrice(), data), nil + return types.NewTransaction(sw.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), sw.GasLimit(), sw.GasPrice(), data), nil } diff --git a/action/stake_restake.go b/action/stake_restake.go index 0ab83b0ff4..3515079c99 100644 --- a/action/stake_restake.go +++ b/action/stake_restake.go @@ -11,12 +11,10 @@ import ( "strings" "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/pkg/errors" "google.golang.org/protobuf/proto" - "github.com/iotexproject/iotex-address/address" "github.com/iotexproject/iotex-proto/golang/iotextypes" "github.com/iotexproject/iotex-core/pkg/util/byteutil" @@ -214,14 +212,9 @@ func NewRestakeFromABIBinary(data []byte) (*Restake, error) { // ToEthTx converts action to eth-compatible tx func (rs *Restake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := rs.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(rs.Nonce(), ethAddr, big.NewInt(0), rs.GasLimit(), rs.GasPrice(), data), nil + return types.NewTransaction(rs.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), rs.GasLimit(), rs.GasPrice(), data), nil } diff --git a/action/stake_transferownership.go b/action/stake_transferownership.go index 0636b9fb1b..51a63b64f6 100644 --- a/action/stake_transferownership.go +++ b/action/stake_transferownership.go @@ -202,14 +202,9 @@ func NewTransferStakeFromABIBinary(data []byte) (*TransferStake, error) { // ToEthTx converts action to eth-compatible tx func (ts *TransferStake) ToEthTx() (*types.Transaction, error) { - addr, err := address.FromString(address.StakingProtocolAddr) - if err != nil { - return nil, err - } - ethAddr := common.BytesToAddress(addr.Bytes()) data, err := ts.encodeABIBinary() if err != nil { return nil, err } - return types.NewTransaction(ts.Nonce(), ethAddr, big.NewInt(0), ts.GasLimit(), ts.GasPrice(), data), nil + return types.NewTransaction(ts.Nonce(), _stakingProtocolEthAddr, big.NewInt(0), ts.GasLimit(), ts.GasPrice(), data), nil }