core/types: un-ssz blob txs, add json marshalling and tweaks (#27256)

This commit is contained in:
Péter Szilágyi 2023-05-12 20:14:29 +03:00 committed by GitHub
parent dffd804ca2
commit 1982437259
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 138 additions and 62 deletions

View File

@ -12,8 +12,8 @@ import (
// MarshalJSON marshals as JSON. // MarshalJSON marshals as JSON.
func (a AccessTuple) MarshalJSON() ([]byte, error) { func (a AccessTuple) MarshalJSON() ([]byte, error) {
type AccessTuple struct { type AccessTuple struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"` StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"`
} }
var enc AccessTuple var enc AccessTuple
enc.Address = a.Address enc.Address = a.Address
@ -24,8 +24,8 @@ func (a AccessTuple) MarshalJSON() ([]byte, error) {
// UnmarshalJSON unmarshals from JSON. // UnmarshalJSON unmarshals from JSON.
func (a *AccessTuple) UnmarshalJSON(input []byte) error { func (a *AccessTuple) UnmarshalJSON(input []byte) error {
type AccessTuple struct { type AccessTuple struct {
Address *common.Address `json:"address" gencodec:"required"` Address *common.Address `json:"address" gencodec:"required"`
StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"` StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"`
} }
var dec AccessTuple var dec AccessTuple
if err := json.Unmarshal(input, &dec); err != nil { if err := json.Unmarshal(input, &dec); err != nil {

View File

@ -198,7 +198,7 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
return &inner, err return &inner, err
case BlobTxType: case BlobTxType:
var inner BlobTx var inner BlobTx
err := rlp.DecodeBytes(b[1:], &inner) // TODO(karalabe): This needs to be ssz err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err return &inner, err
default: default:
return nil, ErrTxTypeNotSupported return nil, ErrTxTypeNotSupported
@ -417,11 +417,7 @@ func (tx *Transaction) Size() uint64 {
return size.(uint64) return size.(uint64)
} }
c := writeCounter(0) c := writeCounter(0)
if tx.Type() == BlobTxType { rlp.Encode(&c, &tx.inner)
rlp.Encode(&c, &tx.inner) // TODO(karalabe): Replace with SSZ encoding
} else {
rlp.Encode(&c, &tx.inner)
}
size := uint64(c) size := uint64(c)
if tx.Type() != LegacyTxType { if tx.Type() != LegacyTxType {

View File

@ -23,28 +23,28 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
"github.com/holiman/uint256"
) )
// txJSON is the JSON representation of transactions. // txJSON is the JSON representation of transactions.
type txJSON struct { type txJSON struct {
Type hexutil.Uint64 `json:"type"` Type hexutil.Uint64 `json:"type"`
// Common transaction fields: ChainID *hexutil.Big `json:"chainId,omitempty"`
Nonce *hexutil.Uint64 `json:"nonce"` Nonce *hexutil.Uint64 `json:"nonce"`
To *common.Address `json:"to"`
Gas *hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"` GasPrice *hexutil.Big `json:"gasPrice"`
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"` MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"` MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
Gas *hexutil.Uint64 `json:"gas"` MaxFeePerDataGas *hexutil.Big `json:"maxFeePerDataGas,omitempty"`
Value *hexutil.Big `json:"value"` Value *hexutil.Big `json:"value"`
Data *hexutil.Bytes `json:"input"` Input *hexutil.Bytes `json:"input"`
AccessList *AccessList `json:"accessList,omitempty"`
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
V *hexutil.Big `json:"v"` V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"` R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"` S *hexutil.Big `json:"s"`
To *common.Address `json:"to"`
// Access list transaction fields:
ChainID *hexutil.Big `json:"chainId,omitempty"`
AccessList *AccessList `json:"accessList,omitempty"`
// Only used for encoding: // Only used for encoding:
Hash common.Hash `json:"hash"` Hash common.Hash `json:"hash"`
@ -61,39 +61,57 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
switch itx := tx.inner.(type) { switch itx := tx.inner.(type) {
case *LegacyTx: case *LegacyTx:
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.To = tx.To()
enc.Gas = (*hexutil.Uint64)(&itx.Gas) enc.Gas = (*hexutil.Uint64)(&itx.Gas)
enc.GasPrice = (*hexutil.Big)(itx.GasPrice) enc.GasPrice = (*hexutil.Big)(itx.GasPrice)
enc.Value = (*hexutil.Big)(itx.Value) enc.Value = (*hexutil.Big)(itx.Value)
enc.Data = (*hexutil.Bytes)(&itx.Data) enc.Input = (*hexutil.Bytes)(&itx.Data)
enc.To = tx.To()
enc.V = (*hexutil.Big)(itx.V) enc.V = (*hexutil.Big)(itx.V)
enc.R = (*hexutil.Big)(itx.R) enc.R = (*hexutil.Big)(itx.R)
enc.S = (*hexutil.Big)(itx.S) enc.S = (*hexutil.Big)(itx.S)
case *AccessListTx: case *AccessListTx:
enc.ChainID = (*hexutil.Big)(itx.ChainID) enc.ChainID = (*hexutil.Big)(itx.ChainID)
enc.AccessList = &itx.AccessList
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.To = tx.To()
enc.Gas = (*hexutil.Uint64)(&itx.Gas) enc.Gas = (*hexutil.Uint64)(&itx.Gas)
enc.GasPrice = (*hexutil.Big)(itx.GasPrice) enc.GasPrice = (*hexutil.Big)(itx.GasPrice)
enc.Value = (*hexutil.Big)(itx.Value) enc.Value = (*hexutil.Big)(itx.Value)
enc.Data = (*hexutil.Bytes)(&itx.Data) enc.Input = (*hexutil.Bytes)(&itx.Data)
enc.To = tx.To() enc.AccessList = &itx.AccessList
enc.V = (*hexutil.Big)(itx.V) enc.V = (*hexutil.Big)(itx.V)
enc.R = (*hexutil.Big)(itx.R) enc.R = (*hexutil.Big)(itx.R)
enc.S = (*hexutil.Big)(itx.S) enc.S = (*hexutil.Big)(itx.S)
case *DynamicFeeTx: case *DynamicFeeTx:
enc.ChainID = (*hexutil.Big)(itx.ChainID) enc.ChainID = (*hexutil.Big)(itx.ChainID)
enc.AccessList = &itx.AccessList
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.To = tx.To()
enc.Gas = (*hexutil.Uint64)(&itx.Gas) enc.Gas = (*hexutil.Uint64)(&itx.Gas)
enc.MaxFeePerGas = (*hexutil.Big)(itx.GasFeeCap) enc.MaxFeePerGas = (*hexutil.Big)(itx.GasFeeCap)
enc.MaxPriorityFeePerGas = (*hexutil.Big)(itx.GasTipCap) enc.MaxPriorityFeePerGas = (*hexutil.Big)(itx.GasTipCap)
enc.Value = (*hexutil.Big)(itx.Value) enc.Value = (*hexutil.Big)(itx.Value)
enc.Data = (*hexutil.Bytes)(&itx.Data) enc.Input = (*hexutil.Bytes)(&itx.Data)
enc.To = tx.To() enc.AccessList = &itx.AccessList
enc.V = (*hexutil.Big)(itx.V) enc.V = (*hexutil.Big)(itx.V)
enc.R = (*hexutil.Big)(itx.R) enc.R = (*hexutil.Big)(itx.R)
enc.S = (*hexutil.Big)(itx.S) enc.S = (*hexutil.Big)(itx.S)
case *BlobTx:
enc.ChainID = (*hexutil.Big)(itx.ChainID.ToBig())
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
enc.MaxFeePerGas = (*hexutil.Big)(itx.GasFeeCap.ToBig())
enc.MaxPriorityFeePerGas = (*hexutil.Big)(itx.GasTipCap.ToBig())
enc.MaxFeePerDataGas = (*hexutil.Big)(itx.BlobFeeCap.ToBig())
enc.Value = (*hexutil.Big)(itx.Value.ToBig())
enc.Input = (*hexutil.Bytes)(&itx.Data)
enc.AccessList = &itx.AccessList
enc.BlobVersionedHashes = itx.BlobHashes
enc.To = tx.To()
enc.V = (*hexutil.Big)(itx.V.ToBig())
enc.R = (*hexutil.Big)(itx.R.ToBig())
enc.S = (*hexutil.Big)(itx.S.ToBig())
} }
return json.Marshal(&enc) return json.Marshal(&enc)
} }
@ -111,29 +129,29 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
case LegacyTxType: case LegacyTxType:
var itx LegacyTx var itx LegacyTx
inner = &itx inner = &itx
if dec.To != nil {
itx.To = dec.To
}
if dec.Nonce == nil { if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in transaction") return errors.New("missing required field 'nonce' in transaction")
} }
itx.Nonce = uint64(*dec.Nonce) itx.Nonce = uint64(*dec.Nonce)
if dec.GasPrice == nil { if dec.To != nil {
return errors.New("missing required field 'gasPrice' in transaction") itx.To = dec.To
} }
itx.GasPrice = (*big.Int)(dec.GasPrice)
if dec.Gas == nil { if dec.Gas == nil {
return errors.New("missing required field 'gas' in transaction") return errors.New("missing required field 'gas' in transaction")
} }
itx.Gas = uint64(*dec.Gas) itx.Gas = uint64(*dec.Gas)
if dec.GasPrice == nil {
return errors.New("missing required field 'gasPrice' in transaction")
}
itx.GasPrice = (*big.Int)(dec.GasPrice)
if dec.Value == nil { if dec.Value == nil {
return errors.New("missing required field 'value' in transaction") return errors.New("missing required field 'value' in transaction")
} }
itx.Value = (*big.Int)(dec.Value) itx.Value = (*big.Int)(dec.Value)
if dec.Data == nil { if dec.Input == nil {
return errors.New("missing required field 'input' in transaction") return errors.New("missing required field 'input' in transaction")
} }
itx.Data = *dec.Data itx.Data = *dec.Input
if dec.V == nil { if dec.V == nil {
return errors.New("missing required field 'v' in transaction") return errors.New("missing required field 'v' in transaction")
} }
@ -156,40 +174,39 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
case AccessListTxType: case AccessListTxType:
var itx AccessListTx var itx AccessListTx
inner = &itx inner = &itx
// Access list is optional for now.
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
if dec.ChainID == nil { if dec.ChainID == nil {
return errors.New("missing required field 'chainId' in transaction") return errors.New("missing required field 'chainId' in transaction")
} }
itx.ChainID = (*big.Int)(dec.ChainID) itx.ChainID = (*big.Int)(dec.ChainID)
if dec.To != nil {
itx.To = dec.To
}
if dec.Nonce == nil { if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in transaction") return errors.New("missing required field 'nonce' in transaction")
} }
itx.Nonce = uint64(*dec.Nonce) itx.Nonce = uint64(*dec.Nonce)
if dec.GasPrice == nil { if dec.To != nil {
return errors.New("missing required field 'gasPrice' in transaction") itx.To = dec.To
} }
itx.GasPrice = (*big.Int)(dec.GasPrice)
if dec.Gas == nil { if dec.Gas == nil {
return errors.New("missing required field 'gas' in transaction") return errors.New("missing required field 'gas' in transaction")
} }
itx.Gas = uint64(*dec.Gas) itx.Gas = uint64(*dec.Gas)
if dec.GasPrice == nil {
return errors.New("missing required field 'gasPrice' in transaction")
}
itx.GasPrice = (*big.Int)(dec.GasPrice)
if dec.Value == nil { if dec.Value == nil {
return errors.New("missing required field 'value' in transaction") return errors.New("missing required field 'value' in transaction")
} }
itx.Value = (*big.Int)(dec.Value) itx.Value = (*big.Int)(dec.Value)
if dec.Data == nil { if dec.Input == nil {
return errors.New("missing required field 'input' in transaction") return errors.New("missing required field 'input' in transaction")
} }
itx.Data = *dec.Data itx.Data = *dec.Input
if dec.V == nil { if dec.V == nil {
return errors.New("missing required field 'v' in transaction") return errors.New("missing required field 'v' in transaction")
} }
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
itx.V = (*big.Int)(dec.V) itx.V = (*big.Int)(dec.V)
if dec.R == nil { if dec.R == nil {
return errors.New("missing required field 'r' in transaction") return errors.New("missing required field 'r' in transaction")
@ -209,21 +226,21 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
case DynamicFeeTxType: case DynamicFeeTxType:
var itx DynamicFeeTx var itx DynamicFeeTx
inner = &itx inner = &itx
// Access list is optional for now.
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
if dec.ChainID == nil { if dec.ChainID == nil {
return errors.New("missing required field 'chainId' in transaction") return errors.New("missing required field 'chainId' in transaction")
} }
itx.ChainID = (*big.Int)(dec.ChainID) itx.ChainID = (*big.Int)(dec.ChainID)
if dec.To != nil {
itx.To = dec.To
}
if dec.Nonce == nil { if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in transaction") return errors.New("missing required field 'nonce' in transaction")
} }
itx.Nonce = uint64(*dec.Nonce) itx.Nonce = uint64(*dec.Nonce)
if dec.To != nil {
itx.To = dec.To
}
if dec.Gas == nil {
return errors.New("missing required field 'gas' for txdata")
}
itx.Gas = uint64(*dec.Gas)
if dec.MaxPriorityFeePerGas == nil { if dec.MaxPriorityFeePerGas == nil {
return errors.New("missing required field 'maxPriorityFeePerGas' for txdata") return errors.New("missing required field 'maxPriorityFeePerGas' for txdata")
} }
@ -232,21 +249,20 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
return errors.New("missing required field 'maxFeePerGas' for txdata") return errors.New("missing required field 'maxFeePerGas' for txdata")
} }
itx.GasFeeCap = (*big.Int)(dec.MaxFeePerGas) itx.GasFeeCap = (*big.Int)(dec.MaxFeePerGas)
if dec.Gas == nil {
return errors.New("missing required field 'gas' for txdata")
}
itx.Gas = uint64(*dec.Gas)
if dec.Value == nil { if dec.Value == nil {
return errors.New("missing required field 'value' in transaction") return errors.New("missing required field 'value' in transaction")
} }
itx.Value = (*big.Int)(dec.Value) itx.Value = (*big.Int)(dec.Value)
if dec.Data == nil { if dec.Input == nil {
return errors.New("missing required field 'input' in transaction") return errors.New("missing required field 'input' in transaction")
} }
itx.Data = *dec.Data itx.Data = *dec.Input
if dec.V == nil { if dec.V == nil {
return errors.New("missing required field 'v' in transaction") return errors.New("missing required field 'v' in transaction")
} }
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
itx.V = (*big.Int)(dec.V) itx.V = (*big.Int)(dec.V)
if dec.R == nil { if dec.R == nil {
return errors.New("missing required field 'r' in transaction") return errors.New("missing required field 'r' in transaction")
@ -263,6 +279,70 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
} }
} }
case BlobTxType:
var itx BlobTx
inner = &itx
if dec.ChainID == nil {
return errors.New("missing required field 'chainId' in transaction")
}
itx.ChainID = uint256.MustFromBig((*big.Int)(dec.ChainID))
if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in transaction")
}
itx.Nonce = uint64(*dec.Nonce)
if dec.To != nil {
itx.To = dec.To
}
if dec.Gas == nil {
return errors.New("missing required field 'gas' for txdata")
}
itx.Gas = uint64(*dec.Gas)
if dec.MaxPriorityFeePerGas == nil {
return errors.New("missing required field 'maxPriorityFeePerGas' for txdata")
}
itx.GasTipCap = uint256.MustFromBig((*big.Int)(dec.MaxPriorityFeePerGas))
if dec.MaxFeePerGas == nil {
return errors.New("missing required field 'maxFeePerGas' for txdata")
}
itx.GasFeeCap = uint256.MustFromBig((*big.Int)(dec.MaxFeePerGas))
if dec.MaxFeePerDataGas == nil {
return errors.New("missing required field 'maxFeePerDataGas' for txdata")
}
itx.BlobFeeCap = uint256.MustFromBig((*big.Int)(dec.MaxFeePerDataGas))
if dec.Value == nil {
return errors.New("missing required field 'value' in transaction")
}
itx.Value = uint256.MustFromBig((*big.Int)(dec.Value))
if dec.Input == nil {
return errors.New("missing required field 'input' in transaction")
}
itx.Data = *dec.Input
if dec.V == nil {
return errors.New("missing required field 'v' in transaction")
}
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
if dec.BlobVersionedHashes == nil {
return errors.New("missing required field 'blobVersionedHashes' in transaction")
}
itx.BlobHashes = dec.BlobVersionedHashes
itx.V = uint256.MustFromBig((*big.Int)(dec.V))
if dec.R == nil {
return errors.New("missing required field 'r' in transaction")
}
itx.R = uint256.MustFromBig((*big.Int)(dec.R))
if dec.S == nil {
return errors.New("missing required field 's' in transaction")
}
itx.S = uint256.MustFromBig((*big.Int)(dec.S))
withSignature := itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0
if withSignature {
if err := sanityCheckSignature(itx.V.ToBig(), itx.R.ToBig(), itx.S.ToBig(), false); err != nil {
return err
}
}
default: default:
return ErrTxTypeNotSupported return ErrTxTypeNotSupported
} }

View File

@ -29,8 +29,8 @@ type AccessList []AccessTuple
// AccessTuple is the element type of an access list. // AccessTuple is the element type of an access list.
type AccessTuple struct { type AccessTuple struct {
Address common.Address `json:"address" gencodec:"required"` Address common.Address `json:"address" gencodec:"required"`
StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"` StorageKeys []common.Hash `json:"storageKeys" gencodec:"required"`
} }
// StorageKeys returns the total number of storage keys in the access list. // StorageKeys returns the total number of storage keys in the access list.

View File

@ -31,7 +31,7 @@ type BlobTx struct {
GasTipCap *uint256.Int // a.k.a. maxPriorityFeePerGas GasTipCap *uint256.Int // a.k.a. maxPriorityFeePerGas
GasFeeCap *uint256.Int // a.k.a. maxFeePerGas GasFeeCap *uint256.Int // a.k.a. maxFeePerGas
Gas uint64 Gas uint64
To *common.Address // `rlp:"nil"` // nil means contract creation To *common.Address `rlp:"nil"` // nil means contract creation
Value *uint256.Int Value *uint256.Int
Data []byte Data []byte
AccessList AccessList AccessList AccessList