internal/ethapi: eth API changes needed for 4844 (#27928)

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
Marius van der Wijden 2023-08-18 14:22:35 +02:00 committed by GitHub
parent 950ccddfc8
commit 7dea9c10cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1333,15 +1333,18 @@ func RPCMarshalHeader(head *types.Header) map[string]interface{} {
"transactionsRoot": head.TxHash, "transactionsRoot": head.TxHash,
"receiptsRoot": head.ReceiptHash, "receiptsRoot": head.ReceiptHash,
} }
if head.BaseFee != nil { if head.BaseFee != nil {
result["baseFeePerGas"] = (*hexutil.Big)(head.BaseFee) result["baseFeePerGas"] = (*hexutil.Big)(head.BaseFee)
} }
if head.WithdrawalsHash != nil { if head.WithdrawalsHash != nil {
result["withdrawalsRoot"] = head.WithdrawalsHash result["withdrawalsRoot"] = head.WithdrawalsHash
} }
if head.BlobGasUsed != nil {
result["blobGasUsed"] = hexutil.Uint64(*head.BlobGasUsed)
}
if head.ExcessBlobGas != nil {
result["excessBlobGas"] = hexutil.Uint64(*head.ExcessBlobGas)
}
return result return result
} }
@ -1400,26 +1403,28 @@ func (s *BlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Block, inc
// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction // RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct { type RPCTransaction struct {
BlockHash *common.Hash `json:"blockHash"` BlockHash *common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"` BlockNumber *hexutil.Big `json:"blockNumber"`
From common.Address `json:"from"` From common.Address `json:"from"`
Gas hexutil.Uint64 `json:"gas"` Gas hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"` GasPrice *hexutil.Big `json:"gasPrice"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"` GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"` GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
Hash common.Hash `json:"hash"` MaxFeePerBlobGas *hexutil.Big `json:"maxFeePerBlobGas,omitempty"`
Input hexutil.Bytes `json:"input"` Hash common.Hash `json:"hash"`
Nonce hexutil.Uint64 `json:"nonce"` Input hexutil.Bytes `json:"input"`
To *common.Address `json:"to"` Nonce hexutil.Uint64 `json:"nonce"`
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"` To *common.Address `json:"to"`
Value *hexutil.Big `json:"value"` TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
Type hexutil.Uint64 `json:"type"` Value *hexutil.Big `json:"value"`
Accesses *types.AccessList `json:"accessList,omitempty"` Type hexutil.Uint64 `json:"type"`
ChainID *hexutil.Big `json:"chainId,omitempty"` Accesses *types.AccessList `json:"accessList,omitempty"`
V *hexutil.Big `json:"v"` ChainID *hexutil.Big `json:"chainId,omitempty"`
R *hexutil.Big `json:"r"` BlobVersionedHashes []common.Hash `json:"blobVersionedHashes,omitempty"`
S *hexutil.Big `json:"s"` V *hexutil.Big `json:"v"`
YParity *hexutil.Uint64 `json:"yParity,omitempty"` R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
YParity *hexutil.Uint64 `json:"yParity,omitempty"`
} }
// newRPCTransaction returns a transaction that will serialize to the RPC // newRPCTransaction returns a transaction that will serialize to the RPC
@ -1473,15 +1478,43 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
// if the transaction has been mined, compute the effective gas price // if the transaction has been mined, compute the effective gas price
if baseFee != nil && blockHash != (common.Hash{}) { if baseFee != nil && blockHash != (common.Hash{}) {
// price = min(gasTipCap + baseFee, gasFeeCap) // price = min(gasTipCap + baseFee, gasFeeCap)
price := math.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap()) result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
result.GasPrice = (*hexutil.Big)(price)
} else { } else {
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap()) result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
} }
case types.BlobTxType:
al := tx.AccessList()
yparity := hexutil.Uint64(v.Sign())
result.Accesses = &al
result.ChainID = (*hexutil.Big)(tx.ChainId())
result.YParity = &yparity
result.GasFeeCap = (*hexutil.Big)(tx.GasFeeCap())
result.GasTipCap = (*hexutil.Big)(tx.GasTipCap())
// if the transaction has been mined, compute the effective gas price
if baseFee != nil && blockHash != (common.Hash{}) {
result.GasPrice = (*hexutil.Big)(effectiveGasPrice(tx, baseFee))
} else {
result.GasPrice = (*hexutil.Big)(tx.GasFeeCap())
}
result.MaxFeePerBlobGas = (*hexutil.Big)(tx.BlobGasFeeCap())
result.BlobVersionedHashes = tx.BlobHashes()
} }
return result return result
} }
// effectiveGasPrice computes the transaction gas fee, based on the given basefee value.
//
// price = min(gasTipCap + baseFee, gasFeeCap)
func effectiveGasPrice(tx *types.Transaction, baseFee *big.Int) *big.Int {
fee := tx.GasTipCap()
fee = fee.Add(fee, baseFee)
if tx.GasTipCapIntCmp(fee) < 0 {
return tx.GasTipCap()
}
return fee
}
// NewRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation // NewRPCPendingTransaction returns a pending transaction that will serialize to the RPC representation
func NewRPCPendingTransaction(tx *types.Transaction, current *types.Header, config *params.ChainConfig) *RPCTransaction { func NewRPCPendingTransaction(tx *types.Transaction, current *types.Header, config *params.ChainConfig) *RPCTransaction {
var ( var (
@ -1786,6 +1819,11 @@ func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber u
fields["logs"] = []*types.Log{} fields["logs"] = []*types.Log{}
} }
if tx.Type() == types.BlobTxType {
fields["blobGasUsed"] = hexutil.Uint64(receipt.BlobGasUsed)
fields["blobGasPrice"] = (*hexutil.Big)(receipt.BlobGasPrice)
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) { if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress fields["contractAddress"] = receipt.ContractAddress