Commit 4c8c5fe6 authored by protolambda's avatar protolambda Committed by GitHub

op-program,op-service: dedup BlockInfo header wrapper, always cache hash,...

op-program,op-service: dedup BlockInfo header wrapper, always cache hash, avoid recomputing hash (#13681)
parent eb7a567b
......@@ -59,7 +59,7 @@ func (p *PreimageOracle) headerByBlockHash(blockHash common.Hash) *types.Header
}
func (p *PreimageOracle) HeaderByBlockHash(blockHash common.Hash) eth.BlockInfo {
return eth.HeaderBlockInfo(p.headerByBlockHash(blockHash))
return eth.HeaderBlockInfoTrusted(blockHash, p.headerByBlockHash(blockHash))
}
func (p *PreimageOracle) TransactionsByBlockHash(blockHash common.Hash) (eth.BlockInfo, types.Transactions) {
......@@ -75,7 +75,7 @@ func (p *PreimageOracle) TransactionsByBlockHash(blockHash common.Hash) (eth.Blo
panic(fmt.Errorf("failed to decode list of txs: %w", err))
}
return eth.HeaderBlockInfo(header), txs
return eth.HeaderBlockInfoTrusted(blockHash, header), txs
}
func (p *PreimageOracle) ReceiptsByBlockHash(blockHash common.Hash) (eth.BlockInfo, types.Receipts) {
......
......@@ -79,65 +79,87 @@ func BlockToInfo(b *types.Block) BlockInfo {
var _ BlockInfo = (*blockInfo)(nil)
// headerBlockInfo is a conversion type of types.Header turning it into a
// BlockInfo.
type headerBlockInfo struct{ *types.Header }
// BlockInfo, but using a cached hash value.
type headerBlockInfo struct {
hash common.Hash
header *types.Header
}
var _ BlockInfo = (*headerBlockInfo)(nil)
func (h headerBlockInfo) ParentHash() common.Hash {
return h.Header.ParentHash
func (h *headerBlockInfo) Hash() common.Hash {
return h.hash
}
func (h headerBlockInfo) Coinbase() common.Address {
return h.Header.Coinbase
func (h *headerBlockInfo) ParentHash() common.Hash {
return h.header.ParentHash
}
func (h headerBlockInfo) Root() common.Hash {
return h.Header.Root
func (h *headerBlockInfo) Coinbase() common.Address {
return h.header.Coinbase
}
func (h headerBlockInfo) NumberU64() uint64 {
return h.Header.Number.Uint64()
func (h *headerBlockInfo) Root() common.Hash {
return h.header.Root
}
func (h headerBlockInfo) Time() uint64 {
return h.Header.Time
func (h *headerBlockInfo) NumberU64() uint64 {
return h.header.Number.Uint64()
}
func (h headerBlockInfo) MixDigest() common.Hash {
return h.Header.MixDigest
func (h *headerBlockInfo) Time() uint64 {
return h.header.Time
}
func (h headerBlockInfo) BaseFee() *big.Int {
return h.Header.BaseFee
func (h *headerBlockInfo) MixDigest() common.Hash {
return h.header.MixDigest
}
func (h headerBlockInfo) BlobBaseFee() *big.Int {
if h.ExcessBlobGas == nil {
func (h *headerBlockInfo) BaseFee() *big.Int {
return h.header.BaseFee
}
func (h *headerBlockInfo) BlobBaseFee() *big.Int {
if h.header.ExcessBlobGas == nil {
return nil
}
return eip4844.CalcBlobFee(*h.ExcessBlobGas)
return eip4844.CalcBlobFee(*h.header.ExcessBlobGas)
}
func (h *headerBlockInfo) ReceiptHash() common.Hash {
return h.header.ReceiptHash
}
func (h *headerBlockInfo) GasUsed() uint64 {
return h.header.GasUsed
}
func (h headerBlockInfo) ReceiptHash() common.Hash {
return h.Header.ReceiptHash
func (h *headerBlockInfo) GasLimit() uint64 {
return h.header.GasLimit
}
func (h headerBlockInfo) GasUsed() uint64 {
return h.Header.GasUsed
func (h *headerBlockInfo) ParentBeaconRoot() *common.Hash {
return h.header.ParentBeaconRoot
}
func (h headerBlockInfo) GasLimit() uint64 {
return h.Header.GasLimit
func (h *headerBlockInfo) HeaderRLP() ([]byte, error) {
return rlp.EncodeToBytes(h.header) // usage is rare and mostly 1-time-use, no need to cache
}
func (h headerBlockInfo) ParentBeaconRoot() *common.Hash {
return h.Header.ParentBeaconRoot
func (h *headerBlockInfo) MarshalJSON() ([]byte, error) {
return h.header.MarshalJSON()
}
func (h headerBlockInfo) HeaderRLP() ([]byte, error) {
return rlp.EncodeToBytes(h.Header)
func (h *headerBlockInfo) UnmarshalJSON(input []byte) error {
return h.header.UnmarshalJSON(input)
}
// HeaderBlockInfo returns h as a BlockInfo implementation.
// HeaderBlockInfo returns h as a BlockInfo implementation, with pre-cached blockhash.
func HeaderBlockInfo(h *types.Header) BlockInfo {
return headerBlockInfo{h}
return &headerBlockInfo{hash: h.Hash(), header: h}
}
// HeaderBlockInfoTrusted returns a BlockInfo, with trusted pre-cached block-hash.
func HeaderBlockInfoTrusted(hash common.Hash, h *types.Header) BlockInfo {
return &headerBlockInfo{hash: hash, header: h}
}
......@@ -6,12 +6,10 @@ import (
"math/big"
"strings"
"github.com/ethereum/go-ethereum/rlp"
"github.com/holiman/uint256"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
......@@ -31,74 +29,6 @@ import (
//
// This way we minimize RPC calls, enable batching, and can choose to verify what the RPC gives us.
// headerInfo is a conversion type of types.Header turning it into a
// BlockInfo, but using a cached hash value.
type headerInfo struct {
hash common.Hash
*types.Header
}
var _ eth.BlockInfo = (*headerInfo)(nil)
func (h headerInfo) Hash() common.Hash {
return h.hash
}
func (h headerInfo) ParentHash() common.Hash {
return h.Header.ParentHash
}
func (h headerInfo) Coinbase() common.Address {
return h.Header.Coinbase
}
func (h headerInfo) Root() common.Hash {
return h.Header.Root
}
func (h headerInfo) NumberU64() uint64 {
return h.Header.Number.Uint64()
}
func (h headerInfo) Time() uint64 {
return h.Header.Time
}
func (h headerInfo) MixDigest() common.Hash {
return h.Header.MixDigest
}
func (h headerInfo) BaseFee() *big.Int {
return h.Header.BaseFee
}
func (h headerInfo) BlobBaseFee() *big.Int {
if h.Header.ExcessBlobGas == nil {
return nil
}
return eip4844.CalcBlobFee(*h.Header.ExcessBlobGas)
}
func (h headerInfo) ReceiptHash() common.Hash {
return h.Header.ReceiptHash
}
func (h headerInfo) GasUsed() uint64 {
return h.Header.GasUsed
}
func (h headerInfo) GasLimit() uint64 {
return h.Header.GasLimit
}
func (h headerInfo) ParentBeaconRoot() *common.Hash {
return h.Header.ParentBeaconRoot
}
func (h headerInfo) HeaderRLP() ([]byte, error) {
return rlp.EncodeToBytes(h.Header)
}
type RPCHeader struct {
ParentHash common.Hash `json:"parentHash"`
UncleHash common.Hash `json:"sha3Uncles"`
......@@ -200,7 +130,7 @@ func (hdr *RPCHeader) Info(trustCache bool, mustBePostMerge bool) (eth.BlockInfo
return nil, fmt.Errorf("failed to verify block hash: computed %s but RPC said %s", computed, hdr.Hash)
}
}
return &headerInfo{hdr.Hash, hdr.createGethHeader()}, nil
return eth.HeaderBlockInfoTrusted(hdr.Hash, hdr.createGethHeader()), nil
}
func (hdr *RPCHeader) BlockID() eth.BlockID {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment