Skip to content

Commit

Permalink
core: don't prune blob sidecar in archive node (#659)
Browse files Browse the repository at this point in the history
This commit adds a flag --no-pruning-sidecar (default: true) to disable blob
sidecar pruning in archive node. Blob sidecar is meant to be pruned, but in the
early state where the offchain storage is not available, it might be better to
keep it in archive node for debugging.
  • Loading branch information
minh-bq authored Jan 15, 2025
1 parent 1b30e64 commit 830b35c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 5 deletions.
1 change: 1 addition & 0 deletions cmd/ronin/chaincmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ The dumpgenesis command dumps the genesis block configuration in JSON format to
utils.CacheFlag,
utils.SyncModeFlag,
utils.GCModeFlag,
utils.NoPruningSideCarFlag,
utils.SnapshotFlag,
utils.CacheDatabaseFlag,
utils.CacheGCFlag,
Expand Down
1 change: 1 addition & 0 deletions cmd/ronin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ var (
utils.SyncModeFlag,
utils.ExitWhenSyncedFlag,
utils.GCModeFlag,
utils.NoPruningSideCarFlag,
utils.SnapshotFlag,
utils.TxLookupLimitFlag,
utils.TriesInMemoryFlag,
Expand Down
21 changes: 20 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ var (
Value: "full",
Category: flags.StateCategory,
}
NoPruningSideCarFlag = &cli.BoolFlag{
Name: "no-pruning-sidecar",
Usage: `Disable blob sidecar pruning in archive node`,
Value: true,
Category: flags.StateCategory,
}
SnapshotFlag = &cli.BoolFlag{
Name: "snapshot",
Usage: `Enables snapshot-database mode (default = enable)`,
Expand Down Expand Up @@ -1907,6 +1913,13 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if ctx.IsSet(GCModeFlag.Name) {
cfg.NoPruning = ctx.String(GCModeFlag.Name) == "archive"
}
if !cfg.NoPruning && ctx.IsSet(NoPruningSideCarFlag.Name) {
log.Warn(fmt.Sprintf("Flag --%s has no effect when gcmode is not archive",
NoPruningSideCarFlag.Name))
}
if cfg.NoPruning && ctx.Bool(NoPruningSideCarFlag.Name) {
cfg.NoPruningSideCar = true
}
if ctx.IsSet(CacheNoPrefetchFlag.Name) {
cfg.NoPrefetch = ctx.Bool(CacheNoPrefetchFlag.Name)
}
Expand Down Expand Up @@ -2305,14 +2318,20 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
if gcmode := ctx.String(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
}
isArchive := ctx.String(GCModeFlag.Name) == "archive"
if !isArchive && ctx.IsSet(NoPruningSideCarFlag.Name) {
log.Warn(fmt.Sprintf("Flag --%s has no effect when gcmode is not archive",
NoPruningSideCarFlag.Name))
}
cache := &core.CacheConfig{
TrieCleanLimit: ethconfig.Defaults.TrieCleanCache,
TrieCleanNoPrefetch: ctx.Bool(CacheNoPrefetchFlag.Name),
TrieDirtyLimit: ethconfig.Defaults.TrieDirtyCache,
TrieDirtyDisabled: ctx.String(GCModeFlag.Name) == "archive",
TrieDirtyDisabled: isArchive,
TrieTimeLimit: ethconfig.Defaults.TrieTimeout,
SnapshotLimit: ethconfig.Defaults.SnapshotCache,
Preimages: ctx.Bool(CachePreimagesFlag.Name),
NoPruningSideCar: isArchive && ctx.Bool(NoPruningSideCarFlag.Name),
}
if cache.TrieDirtyDisabled && !cache.Preimages {
cache.Preimages = true
Expand Down
3 changes: 2 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type CacheConfig struct {
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
Preimages bool // Whether to store preimage of trie key to the disk
TriesInMemory int // The number of tries is kept in memory before pruning
NoPruningSideCar bool // Whether to disable blob sidecar pruning

SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
}
Expand Down Expand Up @@ -1486,7 +1487,7 @@ func (bc *BlockChain) reorgNeeded(localBlock *types.Block, localTd *big.Int, ext

// pruneBlockSidecars prunes the sidecars of blocks that are older than the keep period
func (bc *BlockChain) pruneBlockSidecars(db ethdb.KeyValueWriter, curBlock *types.Block) {
if curBlock.NumberU64() < uint64(bc.blobPrunePeriod) {
if bc.cacheConfig.NoPruningSideCar || curBlock.NumberU64() < uint64(bc.blobPrunePeriod) {
return
}
pruneBlockNumber := curBlock.NumberU64() - uint64(bc.blobPrunePeriod)
Expand Down
23 changes: 20 additions & 3 deletions core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4223,7 +4223,15 @@ func TestInsertChainWithSidecars(t *testing.T) {
wg.Wait()
}

func TestSidecarsPruning(t *testing.T) {
func TestSidecarPruning(t *testing.T) {
testSidecarsPruning(t, true)
}

func TestNoSidecarPruning(t *testing.T) {
testSidecarsPruning(t, false)
}

func testSidecarsPruning(t *testing.T, enabled bool) {
var prunePeriod uint64 = 1000
privateKey, _ := crypto.GenerateKey()
address := crypto.PubkeyToAddress(privateKey.PublicKey)
Expand All @@ -4244,6 +4252,9 @@ func TestSidecarsPruning(t *testing.T) {
if err != nil {
t.Fatalf("Failed to create blockchain, err %s", err)
}
if !enabled {
chain.cacheConfig.NoPruningSideCar = true
}
chain.setBlobPrunePeriod(prunePeriod)
signer := types.NewCancunSigner(chainConfig.ChainID)

Expand Down Expand Up @@ -4309,8 +4320,14 @@ func TestSidecarsPruning(t *testing.T) {
pruneBlockNumber := curBlockNumber - prunePeriod
pruneBlockHash := chain.GetBlockByNumber(uint64(pruneBlockNumber)).Hash()
sidecars := rawdb.ReadBlobSidecars(chain.db, pruneBlockHash, uint64(pruneBlockNumber))
if sidecars != nil {
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-prunePeriod)
if enabled {
if sidecars != nil {
t.Fatalf("Sidecars should be pruned at block %d", curBlockNumber-prunePeriod)
}
} else {
if sidecars == nil {
t.Fatalf("Sidecars must not be pruned at block %d", curBlockNumber-prunePeriod)
}
}
}
}
1 change: 1 addition & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
SnapshotLimit: config.SnapshotCache,
Preimages: config.Preimages,
TriesInMemory: config.TriesInMemory,
NoPruningSideCar: config.NoPruningSideCar,
}
)
eth.blockchain, err = core.NewBlockChain(chainDb, cacheConfig, chainConfig, eth.engine, vmConfig, eth.shouldPreserve, &config.TxLookupLimit)
Expand Down
2 changes: 2 additions & 0 deletions eth/ethconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type Config struct {
NoPruning bool // Whether to disable pruning and flush everything to disk
NoPrefetch bool // Whether to disable prefetching and only load state on demand

NoPruningSideCar bool // Whether to disable blob sidecar pruning

TxLookupLimit uint64 `toml:",omitempty"` // The maximum number of blocks from head whose tx indices are reserved.

// Whitelist of required block number -> hash values to accept
Expand Down

0 comments on commit 830b35c

Please sign in to comment.