From ff1bac7379bdb7a05abbd89960901f4b13f11d2e Mon Sep 17 00:00:00 2001 From: Egor Lysenko Date: Mon, 11 Oct 2021 13:58:38 +0100 Subject: [PATCH 1/3] allow manual block hash caching --- core/types/block.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/types/block.go b/core/types/block.go index 6ba287cb29b6..8dd3efabceb4 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -85,6 +85,9 @@ type Header struct { // BaseFee was added by EIP-1559 and is ignored in legacy headers. BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` + + // caches + hash atomic.Value `rlp:"-"` } // field type overrides for gencodec @@ -102,9 +105,17 @@ type headerMarshaling struct { // Hash returns the block hash of the header, which is simply the keccak256 hash of its // RLP encoding. func (h *Header) Hash() common.Hash { + cached := h.hash.Load() + if cached != nil { + return cached.(common.Hash) + } return rlpHash(h) } +func (h *Header) SetHashCache(hash common.Hash) { + h.hash.Store(hash) +} + var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size()) // Size returns the approximate memory used by all internal contents. It is used From 89b7927cd4a052591f4382f4382721809bd15d52 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 12 Oct 2021 00:21:37 +1000 Subject: [PATCH 2/3] more clear block hash --- core/types/block.go | 40 ++++++++++------------------------------ ethclient/ethclient.go | 5 +++-- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/core/types/block.go b/core/types/block.go index 8dd3efabceb4..7dd3f6757bbf 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -87,7 +87,7 @@ type Header struct { BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"` // caches - hash atomic.Value `rlp:"-"` + externalHash atomic.Value `rlp:"-"` } // field type overrides for gencodec @@ -104,16 +104,19 @@ type headerMarshaling struct { // Hash returns the block hash of the header, which is simply the keccak256 hash of its // RLP encoding. +// Also hash of the header could be overridden with external value. func (h *Header) Hash() common.Hash { - cached := h.hash.Load() - if cached != nil { - return cached.(common.Hash) + external := h.externalHash.Load() + if external != nil { + return external.(common.Hash) } + return rlpHash(h) } -func (h *Header) SetHashCache(hash common.Hash) { - h.hash.Store(hash) +// SetExternalHash overrides hash with external value. +func (h *Header) SetExternalHash(hash common.Hash) { + h.externalHash.Store(hash) } var headerSize = common.StorageSize(reflect.TypeOf(Header{}).Size()) @@ -173,7 +176,6 @@ type Block struct { transactions Transactions // caches - hash atomic.Value size atomic.Value // Td is used by package core to store the total difficulty @@ -382,31 +384,9 @@ func (b *Block) WithBody(transactions []*Transaction, uncles []*Header) *Block { return block } -// WithHash returns a new block with the custom hash. -func (b *Block) WithHash(h common.Hash) *Block { - block := &Block{ - header: CopyHeader(b.header), - transactions: make([]*Transaction, len(b.transactions)), - uncles: make([]*Header, len(b.uncles)), - } - copy(block.transactions, b.transactions) - for i, uncle := range b.uncles { - block.uncles[i] = CopyHeader(uncle) - } - - block.hash.Store(h) - return block -} - // Hash returns the keccak256 hash of b's header. -// The hash is computed on the first call and cached thereafter. func (b *Block) Hash() common.Hash { - if hash := b.hash.Load(); hash != nil { - return hash.(common.Hash) - } - v := b.header.Hash() - b.hash.Store(v) - return v + return b.header.Hash() } type Blocks []*Block diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 1b9050a44e87..754e00fcc330 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -163,9 +163,10 @@ func (ec *Client) getBlock(ctx context.Context, method string, args ...interface txs[i] = tx.tx } + head.SetExternalHash(body.Hash) block := types.NewBlockWithHeader(head). - WithBody(txs, uncles). - WithHash(body.Hash) + WithBody(txs, uncles) + return block, nil } From a65cf949c192af4a911a0949e0f90225c3109a79 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 12 Oct 2021 00:36:18 +1000 Subject: [PATCH 3/3] CopyHeader() update --- core/types/block.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/types/block.go b/core/types/block.go index 7dd3f6757bbf..aa8f521f62d3 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -258,6 +258,13 @@ func CopyHeader(h *Header) *Header { cpy.Extra = make([]byte, len(h.Extra)) copy(cpy.Extra, h.Extra) } + + external := h.externalHash.Load() + if external != nil { + hash := external.(common.Hash) + cpy.SetExternalHash(hash) + } + return &cpy }