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] deprecate EvmTransaction{} struct, to be replace by Envelope #4386

Merged
merged 2 commits into from
Sep 9, 2024
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
95 changes: 41 additions & 54 deletions action/evm_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,28 @@
package action

import (
"encoding/hex"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
)

type (
// EvmTransaction represents an action to be executed by EVM protocol
// as of now 3 types of transactions are supported:
// 1. Legacy transaction
// 2. EIP-2930 access list transaction
// 3. EIP-4844 shard blob transaction
EvmTransaction struct {
inner TxData
// TxData is the interface required to execute a transaction by EVM
// It follows the same-name interface in go-ethereum
TxData interface {
TxCommon
Value() *big.Int
To() *common.Address
Data() []byte
}

TxData interface {
TxCommon interface {
Nonce() uint64
GasLimit() uint64
Gas() uint64
GasPrice() *big.Int
Amount() *big.Int
To() *common.Address
Data() []byte
TxDynamicGas
AccessList() types.AccessList
}
Expand All @@ -39,51 +38,39 @@ type (
}
)

func NewEvmTx(a Action) *EvmTransaction {
Copy link
Member Author

Choose a reason for hiding this comment

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

no longer needed
Envelope will become the struct/interface to pass to EVM protocol

tx := new(EvmTransaction)
switch act := a.(type) {
case *Execution:
tx.inner = act
default:
panic("unsupported action type")
func toAccessListProto(list types.AccessList) []*iotextypes.AccessTuple {
if len(list) == 0 {
return nil
}
return tx
}

func (tx *EvmTransaction) Nonce() uint64 {
return tx.inner.Nonce()
}

func (tx *EvmTransaction) Gas() uint64 {
return tx.inner.GasLimit()
}

func (tx *EvmTransaction) GasPrice() *big.Int {
return tx.inner.GasPrice()
}

func (tx *EvmTransaction) GasTipCap() *big.Int {
return tx.inner.GasTipCap()
}

func (tx *EvmTransaction) GasFeeCap() *big.Int {
return tx.inner.GasFeeCap()
}

func (tx *EvmTransaction) Value() *big.Int {
return tx.inner.Amount()
}

func (tx *EvmTransaction) To() *common.Address {
return tx.inner.To()
}

func (tx *EvmTransaction) Data() []byte {
return tx.inner.Data()
proto := make([]*iotextypes.AccessTuple, len(list))
for i, v := range list {
proto[i] = &iotextypes.AccessTuple{}
proto[i].Address = hex.EncodeToString(v.Address.Bytes())
if numKey := len(v.StorageKeys); numKey > 0 {
proto[i].StorageKeys = make([]string, numKey)
for j, key := range v.StorageKeys {
proto[i].StorageKeys[j] = hex.EncodeToString(key.Bytes())
}
}
}
return proto
}

func (tx *EvmTransaction) AccessList() types.AccessList {
return tx.inner.AccessList()
func fromAccessListProto(list []*iotextypes.AccessTuple) types.AccessList {
if len(list) == 0 {
return nil
}
accessList := make(types.AccessList, len(list))
for i, v := range list {
accessList[i].Address = common.HexToAddress(v.Address)
if numKey := len(v.StorageKeys); numKey > 0 {
accessList[i].StorageKeys = make([]common.Hash, numKey)
for j, key := range v.StorageKeys {
accessList[i].StorageKeys[j] = common.HexToHash(key)
}
}
}
return accessList
}

// EffectiveGas returns the effective gas
Expand Down
40 changes: 4 additions & 36 deletions action/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package action

import (
"encoding/hex"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -105,6 +104,10 @@ func (ex *Execution) To() *common.Address {
return &evmAddr
}

func (ex *Execution) Gas() uint64 {
return ex.gasLimit
}

// Contract returns a contract address
func (ex *Execution) Contract() string { return ex.contract }

Expand All @@ -126,41 +129,6 @@ func (ex *Execution) Payload() []byte { return ex.data }
// AccessList returns the access list
func (ex *Execution) AccessList() types.AccessList { return ex.AbstractAction.accessList }

func toAccessListProto(list types.AccessList) []*iotextypes.AccessTuple {
if len(list) == 0 {
return nil
}
proto := make([]*iotextypes.AccessTuple, len(list))
for i, v := range list {
proto[i] = &iotextypes.AccessTuple{}
proto[i].Address = hex.EncodeToString(v.Address.Bytes())
if numKey := len(v.StorageKeys); numKey > 0 {
proto[i].StorageKeys = make([]string, numKey)
for j, key := range v.StorageKeys {
proto[i].StorageKeys[j] = hex.EncodeToString(key.Bytes())
}
}
}
return proto
}

func fromAccessListProto(list []*iotextypes.AccessTuple) types.AccessList {
if len(list) == 0 {
return nil
}
accessList := make(types.AccessList, len(list))
for i, v := range list {
accessList[i].Address = common.HexToAddress(v.Address)
if numKey := len(v.StorageKeys); numKey > 0 {
accessList[i].StorageKeys = make([]common.Hash, numKey)
for j, key := range v.StorageKeys {
accessList[i].StorageKeys[j] = common.HexToHash(key)
}
}
}
return accessList
}

// Size returns the size of this Execution
func (ex *Execution) Size() uint32 {
var size uint32
Expand Down
10 changes: 3 additions & 7 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type (
// newParams creates a new context for use in the EVM.
func newParams(
ctx context.Context,
execution *action.EvmTransaction,
execution action.TxData,
stateDB *StateDBAdapter,
) (*Params, error) {
var (
Expand Down Expand Up @@ -219,7 +219,7 @@ func securityDeposit(ps *Params, stateDB vm.StateDB, gasLimit uint64) error {
func ExecuteContract(
ctx context.Context,
sm protocol.StateManager,
execution *action.EvmTransaction,
execution action.TxData,
Copy link
Member Author

Choose a reason for hiding this comment

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

TxData is the interface for EVM protocol execution

) ([]byte, *action.Receipt, error) {
ctx, span := tracer.NewSpan(ctx, "evm.ExecuteContract")
defer span.End()
Expand Down Expand Up @@ -620,9 +620,5 @@ func SimulateExecution(
)

ctx = protocol.WithFeatureCtx(ctx)
return ExecuteContract(
ctx,
sm,
action.NewEvmTx(ex),
)
return ExecuteContract(ctx, sm, ex)
}
4 changes: 2 additions & 2 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestExecuteContractFailure(t *testing.T) {
return nil, nil
},
})
retval, receipt, err := ExecuteContract(ctx, sm, action.NewEvmTx(e))
retval, receipt, err := ExecuteContract(ctx, sm, e)
require.Nil(t, retval)
require.Nil(t, receipt)
require.Error(t, err)
Expand Down Expand Up @@ -300,7 +300,7 @@ func TestConstantinople(t *testing.T) {
})
stateDB, err := prepareStateDB(fCtx, sm)
require.NoError(err)
ps, err := newParams(fCtx, action.NewEvmTx(ex), stateDB)
ps, err := newParams(fCtx, ex, stateDB)
require.NoError(err)

evm := vm.NewEVM(ps.context, ps.txCtx, stateDB, ps.chainConfig, ps.evmConfig)
Expand Down
2 changes: 1 addition & 1 deletion action/protocol/execution/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (p *Protocol) Handle(ctx context.Context, elp action.Envelope, sm protocol.
GetBlockTime: p.getBlockTime,
DepositGasFunc: p.depositGas,
})
_, receipt, err := evm.ExecuteContract(ctx, sm, action.NewEvmTx(exec))
_, receipt, err := evm.ExecuteContract(ctx, sm, exec)

if err != nil {
return nil, errors.Wrap(err, "failed to execute contract")
Expand Down
6 changes: 1 addition & 5 deletions action/protocol/poll/consortium.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm proto
})

// deploy consortiumCommittee contract
_, receipt, err := evm.ExecuteContract(
ctx,
sm,
action.NewEvmTx(execution),
)
_, receipt, err := evm.ExecuteContract(ctx, sm, execution)
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions action/protocol/poll/staking_committee.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,7 @@ func (sc *stakingCommittee) CreateGenesisStates(ctx context.Context, sm protocol
},
})
// deploy native staking contract
_, receipt, err := evm.ExecuteContract(
ctx,
sm,
action.NewEvmTx(execution),
)
_, receipt, err := evm.ExecuteContract(ctx, sm, execution)
if err != nil {
return err
}
Expand Down
Loading