Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/filters: implement log filter using new log index #31080

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/geth/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ if one is set. Otherwise it prints the genesis from the datadir.`,
utils.VMTraceFlag,
utils.VMTraceJsonConfigFlag,
utils.TransactionHistoryFlag,
utils.LogHistoryFlag,
utils.LogNoHistoryFlag,
utils.LogExportCheckpointsFlag,
utils.StateHistoryFlag,
}, utils.DatabaseFlags),
Description: `
Expand Down
3 changes: 3 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ var (
utils.SnapshotFlag,
utils.TxLookupLimitFlag, // deprecated
utils.TransactionHistoryFlag,
utils.LogHistoryFlag,
utils.LogNoHistoryFlag,
utils.LogExportCheckpointsFlag,
utils.StateHistoryFlag,
utils.LightServeFlag, // deprecated
utils.LightIngressFlag, // deprecated
Expand Down
26 changes: 26 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,23 @@ var (
Value: ethconfig.Defaults.TransactionHistory,
Category: flags.StateCategory,
}
LogHistoryFlag = &cli.Uint64Flag{
Name: "history.logs",
Usage: "Number of recent blocks to maintain log search index for (default = about one year, 0 = entire chain)",
Value: ethconfig.Defaults.LogHistory,
Category: flags.StateCategory,
}
LogNoHistoryFlag = &cli.BoolFlag{
Name: "history.logs.disable",
Usage: "Do not maintain log search index",
Category: flags.StateCategory,
}
LogExportCheckpointsFlag = &cli.StringFlag{
Name: "history.logs.export",
Usage: "Export checkpoints to file in go source file format",
Category: flags.StateCategory,
Value: "",
}
// Beacon client light sync settings
BeaconApiFlag = &cli.StringSliceFlag{
Name: "beacon.api",
Expand Down Expand Up @@ -1662,6 +1679,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
cfg.StateScheme = rawdb.HashScheme
log.Warn("Forcing hash state-scheme for archive mode")
}
if ctx.IsSet(LogHistoryFlag.Name) {
cfg.LogHistory = ctx.Uint64(LogHistoryFlag.Name)
}
if ctx.IsSet(LogNoHistoryFlag.Name) {
cfg.LogNoHistory = true
}
if ctx.IsSet(LogExportCheckpointsFlag.Name) {
cfg.LogExportCheckpoints = ctx.String(LogExportCheckpointsFlag.Name)
}
if ctx.IsSet(CacheFlag.Name) || ctx.IsSet(CacheTrieFlag.Name) {
cfg.TrieCleanCache = ctx.Int(CacheFlag.Name) * ctx.Int(CacheTrieFlag.Name) / 100
}
Expand Down
29 changes: 19 additions & 10 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,15 @@ type BlockChain struct {
statedb *state.CachingDB // State database to reuse between imports (contains state cache)
txIndexer *txIndexer // Transaction indexer, might be nil if not enabled

hc *HeaderChain
rmLogsFeed event.Feed
chainFeed event.Feed
chainHeadFeed event.Feed
logsFeed event.Feed
blockProcFeed event.Feed
scope event.SubscriptionScope
genesisBlock *types.Block
hc *HeaderChain
rmLogsFeed event.Feed
chainFeed event.Feed
chainHeadFeed event.Feed
logsFeed event.Feed
blockProcFeed event.Feed
blockProcCounter int32
scope event.SubscriptionScope
genesisBlock *types.Block

// This mutex synchronizes chain write operations.
// Readers don't need to take it, they can just read the database.
Expand Down Expand Up @@ -1564,8 +1565,6 @@ func (bc *BlockChain) InsertChain(chain types.Blocks) (int, error) {
if len(chain) == 0 {
return 0, nil
}
bc.blockProcFeed.Send(true)
defer bc.blockProcFeed.Send(false)

// Do a sanity check that the provided chain is actually ordered and linked.
for i := 1; i < len(chain); i++ {
Expand Down Expand Up @@ -1605,6 +1604,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness
if bc.insertStopped() {
return nil, 0, nil
}

if atomic.AddInt32(&bc.blockProcCounter, 1) == 1 {
bc.blockProcFeed.Send(true)
}
defer func() {
if atomic.AddInt32(&bc.blockProcCounter, -1) == 0 {
bc.blockProcFeed.Send(false)
}
}()

// Start a parallel signature recovery (signer will fluke on fork transition, minimal perf loss)
SenderCacher().RecoverFromBlocks(types.MakeSigner(bc.chainConfig, chain[0].Number(), chain[0].Time()), chain)

Expand Down
212 changes: 212 additions & 0 deletions core/filtermaps/chain_view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Copyright 2025 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package filtermaps

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)

// chainView represents an immutable view of a chain with a block hash, a
// block id and a set of receipts associated to each block number. Block id
// can be any unique identifier of the blocks.
// Note that id and receipts are expected to be available up to headNumber
// while the canonical block hash is only expected up to headNumber-1 so that
// it can be implemented by the block builder while the processed head hash
// is not known yet.
type chainView interface {
headNumber() uint64
getBlockHash(number uint64) common.Hash
getBlockId(number uint64) common.Hash
getReceipts(number uint64) types.Receipts
}

// equalViews returns true if the two chain views are equivalent.
func equalViews(cv1, cv2 chainView) bool {
if cv1 == nil || cv2 == nil {
return false
}
head1, head2 := cv1.headNumber(), cv2.headNumber()
return head1 == head2 && cv1.getBlockId(head1) == cv2.getBlockId(head2)
}

// matchViews returns true if the two chain views are equivalent up until the
// specified block number. If the specified number is higher than one of the
// heads then false is returned.
func matchViews(cv1, cv2 chainView, number uint64) bool {
if cv1 == nil || cv2 == nil {
return false
}
head1 := cv1.headNumber()
if head1 < number {
return false
}
head2 := cv2.headNumber()
if head2 < number {
return false
}
if number == head1 || number == head2 {
return cv1.getBlockId(number) == cv2.getBlockId(number)
}
return cv1.getBlockHash(number) == cv2.getBlockHash(number)
}

// blockchain defines functions required by the FilterMaps log indexer.
type blockchain interface {
GetHeader(hash common.Hash, number uint64) *types.Header
GetCanonicalHash(number uint64) common.Hash
GetReceiptsByHash(hash common.Hash) types.Receipts
}

// StoredChainView implements chainView based on a given blockchain.
// Note that the view's head does not have to be the current canonical head
// of the underlying blockchain, it should only possess the block headers
// and receipts up until the expected chain view head.
// Also note that this implementation uses the canonical block hash as block
// id which works as long as the log index structure is not hashed into the
// block headers. Starting from the fork that hashes the log index to the
// block the id needs to be based on a set of fields that exactly defines the
// block but does not include the log index root itself.
type StoredChainView struct {
chain blockchain
head uint64
hashes []common.Hash // block hashes starting backwards from headNumber until first canonical hash
}

// NewStoredChainView creates a new StoredChainView.
func NewStoredChainView(chain blockchain, number uint64, hash common.Hash) *StoredChainView {
cv := &StoredChainView{
chain: chain,
head: number,
hashes: []common.Hash{hash},
}
cv.extendNonCanonical()
return cv
}

// headNumber implements chainView.
func (cv *StoredChainView) headNumber() uint64 {
return cv.head
}

// getBlockHash implements chainView.
func (cv *StoredChainView) getBlockHash(number uint64) common.Hash {
if number >= cv.head {
panic("invalid block number")
}
return cv.blockHash(number)
}

// getBlockId implements chainView.
func (cv *StoredChainView) getBlockId(number uint64) common.Hash {
if number > cv.head {
panic("invalid block number")
}
return cv.blockHash(number)
}

// getReceipts implements chainView.
func (cv *StoredChainView) getReceipts(number uint64) types.Receipts {
if number > cv.head {
panic("invalid block number")
}
return cv.chain.GetReceiptsByHash(cv.blockHash(number))
}

// extendNonCanonical checks whether the previously known reverse list of head
// hashes still ends with one that is canonical on the underlying blockchain.
// If necessary then it traverses further back on the header chain and adds
// more hashes to the list.
func (cv *StoredChainView) extendNonCanonical() bool {
for {
hash, number := cv.hashes[len(cv.hashes)-1], cv.head-uint64(len(cv.hashes)-1)
if cv.chain.GetCanonicalHash(number) == hash {
return true
}
if number == 0 {
log.Error("Unknown genesis block hash found")
return false
}
header := cv.chain.GetHeader(hash, number)
if header == nil {
log.Error("Header not found", "number", number, "hash", hash)
return false
}
cv.hashes = append(cv.hashes, header.ParentHash)
}
}

// blockHash returns the given block hash without doing the head number check.
func (cv *StoredChainView) blockHash(number uint64) common.Hash {
if number+uint64(len(cv.hashes)) <= cv.head {
hash := cv.chain.GetCanonicalHash(number)
if !cv.extendNonCanonical() {
return common.Hash{}
}
if number+uint64(len(cv.hashes)) <= cv.head {
return hash
}
}
return cv.hashes[cv.head-number]
}

// limitedChainView wraps a chainView and truncates it at a given head number.
type limitedChainView struct {
parent chainView
head uint64
}

// newLimitedChainView returns a truncated view of the given parent.
func newLimitedChainView(parent chainView, headNumber uint64) chainView {
if headNumber >= parent.headNumber() {
return parent
}
return &limitedChainView{
parent: parent,
head: headNumber,
}
}

// headNumber implements chainView.
func (cv *limitedChainView) headNumber() uint64 {
return cv.head
}

// getBlockHash implements chainView.
func (cv *limitedChainView) getBlockHash(number uint64) common.Hash {
if number >= cv.head {
panic("invalid block number")
}
return cv.parent.getBlockHash(number)
}

// getBlockId implements chainView.
func (cv *limitedChainView) getBlockId(number uint64) common.Hash {
if number > cv.head {
panic("invalid block number")
}
return cv.parent.getBlockId(number)
}

// getReceipts implements chainView.
func (cv *limitedChainView) getReceipts(number uint64) types.Receipts {
if number > cv.head {
panic("invalid block number")
}
return cv.parent.getReceipts(number)
}
Loading