Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into blobbasefee
Browse files Browse the repository at this point in the history
  • Loading branch information
envestcc committed Sep 10, 2024
2 parents 1de0beb + 49376b6 commit 8865cde
Show file tree
Hide file tree
Showing 71 changed files with 827 additions and 755 deletions.
22 changes: 15 additions & 7 deletions action/actctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ package action
import (
"math/big"

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

// AbstractAction is an abstract implementation of Action interface
type AbstractAction struct {
version uint32
chainID uint32
nonce uint64
gasLimit uint64
gasPrice *big.Int
gasTipCap *big.Int
gasFeeCap *big.Int
version uint32
chainID uint32
nonce uint64
gasLimit uint64
gasPrice *big.Int
gasTipCap *big.Int
gasFeeCap *big.Int
accessList types.AccessList
}

// Version returns the version
Expand Down Expand Up @@ -134,6 +136,9 @@ func (act *AbstractAction) toProto() *iotextypes.ActionCore {
if act.gasFeeCap != nil {
actCore.GasFeeCap = act.gasFeeCap.String()
}
if act.accessList != nil {
actCore.AccessList = toAccessListProto(act.accessList)
}
return &actCore
}

Expand Down Expand Up @@ -165,5 +170,8 @@ func (act *AbstractAction) fromProto(pb *iotextypes.ActionCore) error {
return errors.Errorf("invalid gasFeeCap %s", gasFee)
}
}
if acl := pb.GetAccessList(); acl != nil {
act.accessList = fromAccessListProto(acl)
}
return nil
}
4 changes: 4 additions & 0 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type (
hasDestination interface {
Destination() string
}

hasSize interface {
Size() uint32
}
)

// Sign signs the action using sender's private key
Expand Down
8 changes: 7 additions & 1 deletion action/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ func (b *EnvelopeBuilder) SetChainID(chainID uint32) *EnvelopeBuilder {
return b
}

func (b *EnvelopeBuilder) SetAccessList(acl types.AccessList) *EnvelopeBuilder {
b.elp.accessList = acl
return b
}

// Build builds a new action.
func (b *EnvelopeBuilder) Build() Envelope {
return b.build()
Expand Down Expand Up @@ -168,6 +173,7 @@ func (b *EnvelopeBuilder) setEnvelopeCommonFields(tx *types.Transaction) {
b.elp.nonce = tx.Nonce()
b.elp.gasPrice = new(big.Int).Set(tx.GasPrice())
b.elp.gasLimit = tx.Gas()
b.elp.accessList = tx.AccessList()
}

func getRecipientAddr(addr *common.Address) string {
Expand All @@ -181,7 +187,7 @@ func getRecipientAddr(addr *common.Address) string {
// BuildExecution loads executino action into envelope
func (b *EnvelopeBuilder) BuildExecution(tx *types.Transaction) (Envelope, error) {
b.setEnvelopeCommonFields(tx)
exec, err := NewExecutionWithAccessList(getRecipientAddr(tx.To()), tx.Nonce(), tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data(), tx.AccessList())
exec, err := NewExecution(getRecipientAddr(tx.To()), tx.Nonce(), tx.Value(), tx.Gas(), tx.GasPrice(), tx.Data())
if err != nil {
return nil, err
}
Expand Down
10 changes: 10 additions & 0 deletions action/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
Destination() (string, bool)
Cost() (*big.Int, error)
IntrinsicGas() (uint64, error)
Size() uint32
Action() Action
ToEthTx(uint32, iotextypes.Encoding) (*types.Transaction, error)
Proto() *iotextypes.ActionCore
Expand Down Expand Up @@ -63,6 +64,15 @@ func (elp *envelope) IntrinsicGas() (uint64, error) {
return elp.payload.IntrinsicGas()
}

// Size returns the size of envelope
func (elp *envelope) Size() uint32 {
size := elp.BasicActionSize()
if s, ok := elp.payload.(hasSize); ok {
size += s.Size()
}
return size
}

// Action returns the action payload.
func (elp *envelope) Action() Action { return elp.payload }

Expand Down
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 {
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
74 changes: 20 additions & 54 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 All @@ -31,6 +30,7 @@ const (

var (
_ hasDestination = (*Execution)(nil)
_ hasSize = (*Execution)(nil)
_ EthCompatibleAction = (*Execution)(nil)
_ TxData = (*Execution)(nil)
)
Expand All @@ -39,10 +39,9 @@ var (
type Execution struct {
AbstractAction

contract string
amount *big.Int
data []byte
accessList types.AccessList
contract string
amount *big.Int
data []byte
}

// NewExecution returns an Execution instance (w/o access list)
Expand Down Expand Up @@ -79,15 +78,15 @@ func NewExecutionWithAccessList(
) (*Execution, error) {
return &Execution{
AbstractAction: AbstractAction{
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
version: version.ProtocolVersion,
nonce: nonce,
gasLimit: gasLimit,
gasPrice: gasPrice,
accessList: list,
},
contract: contractAddress,
amount: amount,
data: data,
accessList: list,
contract: contractAddress,
amount: amount,
data: data,
}, nil
}

Expand All @@ -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 @@ -124,46 +127,11 @@ func (ex *Execution) Data() []byte { return ex.data }
func (ex *Execution) Payload() []byte { return ex.data }

// AccessList returns the access list
func (ex *Execution) AccessList() types.AccessList { return ex.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
}
func (ex *Execution) AccessList() types.AccessList { return ex.AbstractAction.accessList }

// TotalSize returns the total size of this Execution
func (ex *Execution) TotalSize() uint32 {
size := ex.BasicActionSize()
// Size returns the size of this Execution
func (ex *Execution) Size() uint32 {
var size uint32
if ex.amount != nil && len(ex.amount.Bytes()) > 0 {
size += uint32(len(ex.amount.Bytes()))
}
Expand All @@ -185,7 +153,6 @@ func (ex *Execution) Proto() *iotextypes.Execution {
if ex.amount != nil && len(ex.amount.String()) > 0 {
act.Amount = ex.amount.String()
}
act.AccessList = toAccessListProto(ex.accessList)
return act
}

Expand All @@ -210,7 +177,6 @@ func (ex *Execution) LoadProto(pbAct *iotextypes.Execution) error {
ex.amount = amount
}
ex.data = pbAct.GetData()
ex.accessList = fromAccessListProto(pbAct.AccessList)
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions action/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestExecutionSignVerify(t *testing.T) {
ex, err := NewExecution(contractAddr.String(), 2, big.NewInt(10), uint64(100000), big.NewInt(10), data)
require.NoError(err)
require.EqualValues(21, ex.BasicActionSize())
require.EqualValues(87, ex.TotalSize())
require.EqualValues(66, ex.Size())

bd := &EnvelopeBuilder{}
eb := bd.SetNonce(ex.nonce).
Expand All @@ -37,6 +37,7 @@ func TestExecutionSignVerify(t *testing.T) {
SetAction(ex).Build()
elp, ok := eb.(*envelope)
require.True(ok)
require.EqualValues(87, eb.Size())

w := AssembleSealedEnvelope(elp, executorKey.PublicKey(), []byte("lol"))
require.Error(w.VerifySignature())
Expand All @@ -48,9 +49,6 @@ func TestExecutionSignVerify(t *testing.T) {
selp, err := Sign(elp, executorKey)
require.NoError(err)
require.NotNil(selp)
require.EqualValues(21, ex.BasicActionSize())
require.EqualValues(87, ex.TotalSize())

// verify signature
require.NoError(selp.VerifySignature())
}
Expand Down
Loading

0 comments on commit 8865cde

Please sign in to comment.