Skip to content

Commit

Permalink
[action] pass tx type to ToEthTx() method
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Aug 15, 2024
1 parent 851b9bb commit 437b572
Show file tree
Hide file tree
Showing 29 changed files with 288 additions and 214 deletions.
3 changes: 2 additions & 1 deletion action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
)

Expand All @@ -24,7 +25,7 @@ type (

// EthCompatibleAction is the action which is compatible to be converted to eth tx
EthCompatibleAction interface {
ToEthTx(uint32) (*types.Transaction, error)
ToEthTx(uint32, iotextypes.Encoding) (*types.Transaction, error)
}

TxContainer interface {
Expand Down
2 changes: 1 addition & 1 deletion action/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestEthTxUtils(t *testing.T) {
SetAddress(addr).
SetData([]byte("any")).
SetAmount(big.NewInt(1)).Build()
tx, _ := act.ToEthTx(chainID)
tx, _ := act.ToEthTx(chainID, iotextypes.Encoding_ETHEREUM_EIP155)

var (
signer1, _ = NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, chainID)
Expand Down
22 changes: 13 additions & 9 deletions action/candidate_activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -37,6 +38,8 @@ const (

var (
candidateActivateMethod abi.Method
_ EthCompatibleAction = (*CandidateActivate)(nil)
_ TxDataBasic = (*CandidateActivate)(nil)
)

// CandidateActivate is the action to update a candidate's bucket
Expand Down Expand Up @@ -102,19 +105,20 @@ func (cr *CandidateActivate) encodeABIBinary() ([]byte, error) {
}

// ToEthTx returns an Ethereum transaction which corresponds to this action
func (cr *CandidateActivate) ToEthTx(_ uint32) (*types.Transaction, error) {
func (cr *CandidateActivate) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := cr.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: cr.Nonce(),
GasPrice: cr.GasPrice(),
Gas: cr.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:

Check warning on line 114 in action/candidate_activate.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_activate.go#L114

Added line #L114 was not covered by tests
// treat native tx as EVM LegacyTx
fallthrough

Check warning on line 116 in action/candidate_activate.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_activate.go#L116

Added line #L116 was not covered by tests
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return stakingToLegacyTx(cr, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}

// NewCandidateActivate returns a CandidateActivate action
Expand Down
22 changes: 13 additions & 9 deletions action/candidate_endorsement.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -95,6 +96,8 @@ var (
candidateEndorsementEndorseMethod abi.Method
caniddateEndorsementIntentToRevokeMethod abi.Method
candidateEndorsementRevokeMethod abi.Method
_ EthCompatibleAction = (*CandidateEndorsement)(nil)
_ TxDataBasic = (*CandidateEndorsement)(nil)
)

type (
Expand Down Expand Up @@ -220,19 +223,20 @@ func (act *CandidateEndorsement) encodeABIBinary() ([]byte, error) {
}

// ToEthTx returns an Ethereum transaction which corresponds to this action
func (act *CandidateEndorsement) ToEthTx(_ uint32) (*types.Transaction, error) {
func (act *CandidateEndorsement) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := act.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: act.Nonce(),
GasPrice: act.GasPrice(),
Gas: act.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:

Check warning on line 232 in action/candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_endorsement.go#L232

Added line #L232 was not covered by tests
// treat native tx as EVM LegacyTx
fallthrough

Check warning on line 234 in action/candidate_endorsement.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_endorsement.go#L234

Added line #L234 was not covered by tests
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return stakingToLegacyTx(act, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}

// NewCandidateEndorsementLegacy returns a CandidateEndorsement action
Expand Down
21 changes: 12 additions & 9 deletions action/candidate_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -94,6 +95,7 @@ var (
ErrInvalidOwner = errors.New("invalid owner address")

_ EthCompatibleAction = (*CandidateRegister)(nil)
_ TxDataBasic = (*CandidateRegister)(nil)
)

// CandidateRegister is the action to register a candidate
Expand Down Expand Up @@ -384,19 +386,20 @@ func ethAddrToNativeAddr(in interface{}) (address.Address, error) {
}

// ToEthTx converts action to eth-compatible tx
func (cr *CandidateRegister) ToEthTx(_ uint32) (*types.Transaction, error) {
func (cr *CandidateRegister) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := cr.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: cr.Nonce(),
GasPrice: cr.GasPrice(),
Gas: cr.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:
// treat native tx as EVM LegacyTx
fallthrough
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return stakingToLegacyTx(cr, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}

// IsValidCandidateName check if a candidate name string is valid.
Expand Down
22 changes: 13 additions & 9 deletions action/candidate_transfer_ownership.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -47,6 +48,8 @@ const (
var (
// _candidateTransferOwnershipMethod is the interface of the abi encoding of candidate transfer ownership action
_candidateTransferOwnershipMethod abi.Method
_ EthCompatibleAction = (*CandidateTransferOwnership)(nil)
_ TxDataBasic = (*CandidateTransferOwnership)(nil)
)

func init() {
Expand Down Expand Up @@ -190,17 +193,18 @@ func (act *CandidateTransferOwnership) SanityCheck() error {
}

// ToEthTx returns an Ethereum transaction which corresponds to this action
func (act *CandidateTransferOwnership) ToEthTx(_ uint32) (*types.Transaction, error) {
func (act *CandidateTransferOwnership) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := act.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: act.Nonce(),
GasPrice: act.GasPrice(),
Gas: act.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:

Check warning on line 202 in action/candidate_transfer_ownership.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_transfer_ownership.go#L202

Added line #L202 was not covered by tests
// treat native tx as EVM LegacyTx
fallthrough

Check warning on line 204 in action/candidate_transfer_ownership.go

View check run for this annotation

Codecov / codecov/patch

action/candidate_transfer_ownership.go#L204

Added line #L204 was not covered by tests
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return stakingToLegacyTx(act, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}
6 changes: 3 additions & 3 deletions action/candidate_transfer_ownership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"math/big"
"testing"

"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-address/address"

"github.com/iotexproject/iotex-core/pkg/util/byteutil"
)

Expand Down Expand Up @@ -133,7 +133,7 @@ func TestCandidateTransferOwnershipToEthTx(t *testing.T) {
require := require.New(t)
cr, err := NewCandidateTransferOwnership(1, 1000000, big.NewInt(1000), "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", []byte("payload"))
require.NoError(err)
ethTx, err := cr.ToEthTx(0)
ethTx, err := cr.ToEthTx(0, iotextypes.Encoding_ETHEREUM_EIP155)
require.NoError(err)
require.NotNil(ethTx)
require.Equal(byteutil.Must(cr.EncodeABIBinary()), ethTx.Data())
Expand Down
21 changes: 12 additions & 9 deletions action/candidate_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -57,6 +58,7 @@ var (
// _candidateUpdateMethod is the interface of the abi encoding of stake action
_candidateUpdateMethod abi.Method
_ EthCompatibleAction = (*CandidateUpdate)(nil)
_ TxDataBasic = (*CandidateUpdate)(nil)
)

// CandidateUpdate is the action to update a candidate
Expand Down Expand Up @@ -244,17 +246,18 @@ func NewCandidateUpdateFromABIBinary(data []byte) (*CandidateUpdate, error) {
}

// ToEthTx converts action to eth-compatible tx
func (cu *CandidateUpdate) ToEthTx(_ uint32) (*types.Transaction, error) {
func (cu *CandidateUpdate) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := cu.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: cu.Nonce(),
GasPrice: cu.GasPrice(),
Gas: cu.GasLimit(),
To: &_stakingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:
// treat native tx as EVM LegacyTx
fallthrough
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return stakingToLegacyTx(cu, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}
21 changes: 12 additions & 9 deletions action/claimreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -73,6 +74,7 @@ var (
_claimRewardingMethodV1 abi.Method
_claimRewardingMethodV2 abi.Method
_ EthCompatibleAction = (*ClaimFromRewardingFund)(nil)
_ TxDataBasic = (*ClaimFromRewardingFund)(nil)
errWrongMethodSig = errors.New("wrong method signature")
)

Expand Down Expand Up @@ -225,19 +227,20 @@ func (c *ClaimFromRewardingFund) encodeABIBinary() ([]byte, error) {
}

// ToEthTx converts action to eth-compatible tx
func (c *ClaimFromRewardingFund) ToEthTx(_ uint32) (*types.Transaction, error) {
func (c *ClaimFromRewardingFund) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := c.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: c.Nonce(),
GasPrice: c.GasPrice(),
Gas: c.GasLimit(),
To: &_rewardingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:
// treat native tx as EVM LegacyTx
fallthrough
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return rewardingToLegacyTx(c, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}

// NewClaimFromRewardingFundFromABIBinary decodes data into action
Expand Down
4 changes: 2 additions & 2 deletions action/claimreward_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestClaimRewardToEthTx(t *testing.T) {

builder.SetAmount(big.NewInt(101))
rc := builder.Build()
tx, err := rc.ToEthTx(0)
tx, err := rc.ToEthTx(0, 0)
r.NoError(err)
r.Equal(tx.To().String(), _rewardingProtocolEthAddr.String())
r.Equal(tx.Data()[:4], _claimRewardingMethodV1.ID)
Expand All @@ -105,7 +105,7 @@ func TestClaimRewardToEthTx(t *testing.T) {
builder.SetData([]byte{1, 2, 3})
builder.SetAddress(addr)
rc = builder.Build()
tx, err = rc.ToEthTx(0)
tx, err = rc.ToEthTx(0, iotextypes.Encoding_ETHEREUM_EIP155)
r.NoError(err)
r.Equal(tx.Data()[:4], _claimRewardingMethodV2.ID)
r.Equal(tx.Value().String(), "0")
Expand Down
21 changes: 12 additions & 9 deletions action/depositreward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package action

import (
"bytes"
"fmt"
"math/big"
"strings"

Expand Down Expand Up @@ -48,6 +49,7 @@ var (

_depositRewardMethod abi.Method
_ EthCompatibleAction = (*DepositToRewardingFund)(nil)
_ TxDataBasic = (*DepositToRewardingFund)(nil)
)

func init() {
Expand Down Expand Up @@ -159,19 +161,20 @@ func (d *DepositToRewardingFund) encodeABIBinary() ([]byte, error) {
}

// ToEthTx converts action to eth-compatible tx
func (d *DepositToRewardingFund) ToEthTx(_ uint32) (*types.Transaction, error) {
func (d *DepositToRewardingFund) ToEthTx(evmNetworkID uint32, encoding iotextypes.Encoding) (*types.Transaction, error) {
data, err := d.encodeABIBinary()
if err != nil {
return nil, err
}
return types.NewTx(&types.LegacyTx{
Nonce: d.Nonce(),
GasPrice: d.GasPrice(),
Gas: d.GasLimit(),
To: &_rewardingProtocolEthAddr,
Value: big.NewInt(0),
Data: data,
}), nil
switch encoding {
case iotextypes.Encoding_IOTEX_PROTOBUF:
// treat native tx as EVM LegacyTx
fallthrough
case iotextypes.Encoding_ETHEREUM_EIP155, iotextypes.Encoding_ETHEREUM_UNPROTECTED:
return rewardingToLegacyTx(d, data), nil
default:
panic(fmt.Sprintf("unsupported encoding %d", encoding))
}
}

// NewDepositToRewardingFundFromABIBinary decodes data into action
Expand Down
Loading

0 comments on commit 437b572

Please sign in to comment.