Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action] simplify address generation #3913

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import (
"github.com/iotexproject/iotex-core/pkg/version"
)

var (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var is placed in front of type

_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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
23 changes: 16 additions & 7 deletions action/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,33 @@ 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).
SetGasLimit(10003).
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) {
Expand All @@ -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())
Expand All @@ -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())
Expand Down
7 changes: 1 addition & 6 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easier way of generating the address

}

// IsValidCandidateName check if a candidate name string is valid.
Expand Down
7 changes: 1 addition & 6 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 1 addition & 8 deletions action/claimreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions action/claimreward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()),
Expand All @@ -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()),
Expand Down
9 changes: 1 addition & 8 deletions action/depositreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 2 additions & 7 deletions action/depositreward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()),
Expand All @@ -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()),
Expand Down
3 changes: 3 additions & 0 deletions action/protocol/rewarding/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions action/protocol/staking/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 0 additions & 9 deletions action/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:])
)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined but not used at all

type (
// Topics are data items of a transaction, such as send/recipient address
Topics []hash.Hash256
Expand Down
9 changes: 1 addition & 8 deletions action/stake_adddeposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
9 changes: 1 addition & 8 deletions action/stake_changecandidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
9 changes: 1 addition & 8 deletions action/stake_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Loading