Skip to content

Commit

Permalink
chore: polish keeper code
Browse files Browse the repository at this point in the history
  • Loading branch information
will@2012 committed Apr 22, 2024
1 parent 87a8e97 commit 95388d1
Show file tree
Hide file tree
Showing 21 changed files with 546 additions and 101 deletions.
2 changes: 2 additions & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ var (
utils.StateHistoryFlag,
utils.ProposeBlockIntervalFlag,
utils.PathDBNodeBufferTypeFlag,
utils.EnableProofKeeperFlag,
utils.KeepProofBlockSpanFlag,
utils.LightServeFlag,
utils.LightIngressFlag,
utils.LightEgressFlag,
Expand Down
21 changes: 20 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/core/opcodeCompiler/compiler"
"math"
"math/big"
"net"
Expand All @@ -35,6 +34,8 @@ import (
"strings"
"time"

"github.com/ethereum/go-ethereum/core/opcodeCompiler/compiler"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -313,6 +314,18 @@ var (
Value: pathdb.DefaultProposeBlockInterval,
Category: flags.StateCategory,
}
EnableProofKeeperFlag = &cli.BoolFlag{
Name: "pathdb.enableproofkeeper",
Usage: "Enable path db proof keeper for store proposed proof",
Value: false,
Category: flags.StateCategory,
}
KeepProofBlockSpanFlag = &cli.Uint64Flag{
Name: "pathdb.keepproofblockspan",
Usage: "Block span of keep proof (default = 90,000 blocks)",
Value: params.FullImmutabilityThreshold,
Category: flags.StateCategory,
}
StateHistoryFlag = &cli.Uint64Flag{
Name: "history.state",
Usage: "Number of recent blocks to retain state history for (default = 90,000 blocks, 0 = entire chain)",
Expand Down Expand Up @@ -1881,6 +1894,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.IsSet(ProposeBlockIntervalFlag.Name) {
cfg.ProposeBlockInterval = ctx.Uint64(ProposeBlockIntervalFlag.Name)
}
if ctx.IsSet(EnableProofKeeperFlag.Name) {
cfg.EnableProofKeeper = ctx.Bool(EnableProofKeeperFlag.Name)
}
if ctx.IsSet(KeepProofBlockSpanFlag.Name) {
cfg.KeepProofBlockSpan = ctx.Uint64(KeepProofBlockSpanFlag.Name)
}
if ctx.String(GCModeFlag.Name) == "archive" && cfg.TransactionHistory != 0 {
cfg.TransactionHistory = 0
log.Warn("Disabled transaction unindexing for archive node")
Expand Down
4 changes: 3 additions & 1 deletion common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ func (d *Decimal) UnmarshalJSON(input []byte) error {
// hex-strings for delivery to rpc-caller.
type ProofList []string

// Put implements ethdb.KeyValueWriter put interface.
func (n *ProofList) Put(key []byte, value []byte) error {
*n = append(*n, hexutil.Encode(value))
return nil
}

// Delete implements ethdb.KeyValueWriter delete interface.
func (n *ProofList) Delete(key []byte) error {
panic("not supported")
}
Expand All @@ -497,7 +499,7 @@ type AccountResult struct {
CodeHash Hash `json:"codeHash"`
Nonce uint64 `json:"nonce"`
StorageHash Hash `json:"storageHash"`
StorageProof []StorageResult `json:"storageProof"` // optional
StorageProof []StorageResult `json:"storageProof"`
}

// StorageResult provides a proof for a key-value pair.
Expand Down
15 changes: 9 additions & 6 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/trie"
"github.com/ethereum/go-ethereum/trie/triedb/hashdb"
"github.com/ethereum/go-ethereum/trie/triedb/pathdb"
Expand Down Expand Up @@ -157,11 +156,12 @@ type CacheConfig struct {
StateScheme string // Scheme used to store ethereum states and merkle tree nodes on top
PathNodeBuffer pathdb.NodeBufferType // Type of trienodebuffer to cache trie nodes in disklayer
ProposeBlockInterval uint64 // Propose block to L1 block interval.
EnableProofKeeper bool // Whether to enable proof keeper
KeepProofBlockSpan uint64 // Block span of keep proof
SnapshotNoBuild bool // Whether the background generation is allowed
SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it

TrieCommitInterval uint64 // Define a block height interval, commit trie every TrieCommitInterval block height.
RpcClient *rpc.Client
}

// triedbConfig derives the configures for trie database.
Expand Down Expand Up @@ -230,6 +230,7 @@ type BlockChain struct {
flushInterval atomic.Int64 // Time interval (processing time) after which to flush a state
triedb *trie.Database // The database handler for maintaining trie nodes.
stateCache state.Database // State database to reuse between imports (contains state cache)
proofKeeper *ProofKeeper // Store/Query op-proposal proof to ensure consistent.

// txLookupLimit is the maximum number of blocks from head whose tx indices
// are reserved:
Expand Down Expand Up @@ -281,8 +282,6 @@ type BlockChain struct {
processor Processor // Block transaction processor interface
forker *ForkChoice
vmConfig vm.Config

ProofKeeper *ProofKeeper
}

// NewBlockChain returns a fully initialised block chain using information
Expand All @@ -293,7 +292,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
cacheConfig = defaultCacheConfig
}
opts := &proofKeeperOptions{
enable: true, // todo
enable: cacheConfig.EnableProofKeeper,
keepProofBlockSpan: cacheConfig.KeepProofBlockSpan,
watchStartKeepCh: make(chan *pathdb.KeepRecord),
notifyFinishKeepCh: make(chan struct{}),
}
Expand Down Expand Up @@ -352,7 +352,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
if err != nil {
return nil, err
}
bc.ProofKeeper = proofKeeper
bc.proofKeeper = proofKeeper

bc.hc, err = NewHeaderChain(db, chainConfig, engine, bc.insertStopped)
if err != nil {
Expand Down Expand Up @@ -1048,6 +1048,9 @@ func (bc *BlockChain) Stop() {
if err := bc.triedb.Journal(bc.CurrentBlock().Root); err != nil {
log.Info("Failed to journal in-memory trie nodes", "err", err)
}
if err := bc.proofKeeper.Stop(); err != nil {
log.Info("Failed to stop proof keeper", "err", err)
}
} else {
// Ensure the state of a recent block is also stored to disk before exiting.
// We're writing three different states to catch different restart scenarios:
Expand Down
3 changes: 3 additions & 0 deletions core/blockchain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,6 @@ func (bc *BlockChain) SubscribeLogsEvent(ch chan<- []*types.Log) event.Subscript
func (bc *BlockChain) SubscribeBlockProcessingEvent(ch chan<- bool) event.Subscription {
return bc.scope.Track(bc.blockProcFeed.Subscribe(ch))
}

// ProofKeeper returns block chain proof keeper.
func (bc *BlockChain) ProofKeeper() *ProofKeeper { return bc.proofKeeper }
Loading

0 comments on commit 95388d1

Please sign in to comment.