Skip to content

Commit 6eef141

Browse files
authored
les: historical data garbage collection (#19570)
This change introduces garbage collection for the light client. Historical chain data is deleted periodically. If you want to disable the GC, use the --light.nopruning flag.
1 parent b8dd089 commit 6eef141

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+843
-215
lines changed

cmd/geth/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var (
9898
utils.LightEgressFlag,
9999
utils.LightMaxPeersFlag,
100100
utils.LegacyLightPeersFlag,
101+
utils.LightNoPruneFlag,
101102
utils.LightKDFFlag,
102103
utils.UltraLightServersFlag,
103104
utils.UltraLightFractionFlag,

cmd/geth/usage.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ var AppHelpFlagGroups = []flagGroup{
9696
utils.UltraLightServersFlag,
9797
utils.UltraLightFractionFlag,
9898
utils.UltraLightOnlyAnnounceFlag,
99+
utils.LightNoPruneFlag,
99100
},
100101
},
101102
{

cmd/utils/flags.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,10 @@ var (
282282
Name: "ulc.onlyannounce",
283283
Usage: "Ultra light server sends announcements only",
284284
}
285+
LightNoPruneFlag = cli.BoolFlag{
286+
Name: "light.nopruning",
287+
Usage: "Disable ancient light chain data pruning",
288+
}
285289
// Ethash settings
286290
EthashCacheDirFlag = DirectoryFlag{
287291
Name: "ethash.cachedir",
@@ -1070,6 +1074,9 @@ func setLes(ctx *cli.Context, cfg *eth.Config) {
10701074
if ctx.GlobalIsSet(UltraLightOnlyAnnounceFlag.Name) {
10711075
cfg.UltraLightOnlyAnnounce = ctx.GlobalBool(UltraLightOnlyAnnounceFlag.Name)
10721076
}
1077+
if ctx.GlobalIsSet(LightNoPruneFlag.Name) {
1078+
cfg.LightNoPrune = ctx.GlobalBool(LightNoPruneFlag.Name)
1079+
}
10731080
}
10741081

10751082
// makeDatabaseHandles raises out the number of allowed file handles per process
@@ -1800,12 +1807,17 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
18001807
var (
18011808
cache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
18021809
handles = makeDatabaseHandles()
1810+
1811+
err error
1812+
chainDb ethdb.Database
18031813
)
1804-
name := "chaindata"
18051814
if ctx.GlobalString(SyncModeFlag.Name) == "light" {
1806-
name = "lightchaindata"
1815+
name := "lightchaindata"
1816+
chainDb, err = stack.OpenDatabase(name, cache, handles, "")
1817+
} else {
1818+
name := "chaindata"
1819+
chainDb, err = stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "")
18071820
}
1808-
chainDb, err := stack.OpenDatabaseWithFreezer(name, cache, handles, ctx.GlobalString(AncientFlag.Name), "")
18091821
if err != nil {
18101822
Fatalf("Could not open database: %v", err)
18111823
}

consensus/clique/clique.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ func (c *Clique) snapshot(chain consensus.ChainReader, number uint64, hash commo
369369
// at a checkpoint block without a parent (light client CHT), or we have piled
370370
// up more headers than allowed to be reorged (chain reinit from a freezer),
371371
// consider the checkpoint trusted and snapshot it.
372-
if number == 0 || (number%c.config.Epoch == 0 && (len(headers) > params.ImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) {
372+
if number == 0 || (number%c.config.Epoch == 0 && (len(headers) > params.FullImmutabilityThreshold || chain.GetHeaderByNumber(number-1) == nil)) {
373373
checkpoint := chain.GetHeaderByNumber(number)
374374
if checkpoint != nil {
375375
hash := checkpoint.Hash()

core/blockchain.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -901,14 +901,14 @@ func (bc *BlockChain) Stop() {
901901
recent := bc.GetBlockByNumber(number - offset)
902902

903903
log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root())
904-
if err := triedb.Commit(recent.Root(), true); err != nil {
904+
if err := triedb.Commit(recent.Root(), true, nil); err != nil {
905905
log.Error("Failed to commit recent state trie", "err", err)
906906
}
907907
}
908908
}
909909
if snapBase != (common.Hash{}) {
910910
log.Info("Writing snapshot state to disk", "root", snapBase)
911-
if err := triedb.Commit(snapBase, true); err != nil {
911+
if err := triedb.Commit(snapBase, true, nil); err != nil {
912912
log.Error("Failed to commit recent state trie", "err", err)
913913
}
914914
}
@@ -1442,7 +1442,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14421442

14431443
// If we're running an archive node, always flush
14441444
if bc.cacheConfig.TrieDirtyDisabled {
1445-
if err := triedb.Commit(root, false); err != nil {
1445+
if err := triedb.Commit(root, false, nil); err != nil {
14461446
return NonStatTy, err
14471447
}
14481448
} else {
@@ -1476,7 +1476,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
14761476
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory)
14771477
}
14781478
// Flush an entire trie and restart the counters
1479-
triedb.Commit(header.Root, true)
1479+
triedb.Commit(header.Root, true, nil)
14801480
lastWrite = chosen
14811481
bc.gcproc = 0
14821482
}

core/chain_indexer.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ type ChainIndexerBackend interface {
4646

4747
// Commit finalizes the section metadata and stores it into the database.
4848
Commit() error
49+
50+
// Prune deletes the chain index older than the given threshold.
51+
Prune(threshold uint64) error
4952
}
5053

5154
// ChainIndexerChain interface is used for connecting the indexer to a blockchain
@@ -386,7 +389,6 @@ func (c *ChainIndexer) processSection(section uint64, lastHead common.Hash) (com
386389
c.log.Trace("Processing new chain section", "section", section)
387390

388391
// Reset and partial processing
389-
390392
if err := c.backend.Reset(c.ctx, section, lastHead); err != nil {
391393
c.setValidSections(0)
392394
return common.Hash{}, err
@@ -459,6 +461,11 @@ func (c *ChainIndexer) AddChildIndexer(indexer *ChainIndexer) {
459461
}
460462
}
461463

464+
// Prune deletes all chain data older than given threshold.
465+
func (c *ChainIndexer) Prune(threshold uint64) error {
466+
return c.backend.Prune(threshold)
467+
}
468+
462469
// loadValidSections reads the number of valid sections from the index database
463470
// and caches is into the local state.
464471
func (c *ChainIndexer) loadValidSections() {

core/chain_indexer_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,7 @@ func (b *testChainIndexBackend) Commit() error {
236236
}
237237
return nil
238238
}
239+
240+
func (b *testChainIndexBackend) Prune(threshold uint64) error {
241+
return nil
242+
}

core/chain_makers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse
220220
if err != nil {
221221
panic(fmt.Sprintf("state write error: %v", err))
222222
}
223-
if err := statedb.Database().TrieDB().Commit(root, false); err != nil {
223+
if err := statedb.Database().TrieDB().Commit(root, false, nil); err != nil {
224224
panic(fmt.Sprintf("trie write error: %v", err))
225225
}
226226
return block, b.receipts

core/dao_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestDAOForkRangeExtradata(t *testing.T) {
7979
if _, err := bc.InsertChain(blocks); err != nil {
8080
t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
8181
}
82-
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
82+
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true, nil); err != nil {
8383
t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
8484
}
8585
blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
@@ -104,7 +104,7 @@ func TestDAOForkRangeExtradata(t *testing.T) {
104104
if _, err := bc.InsertChain(blocks); err != nil {
105105
t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
106106
}
107-
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
107+
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true, nil); err != nil {
108108
t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
109109
}
110110
blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
@@ -130,7 +130,7 @@ func TestDAOForkRangeExtradata(t *testing.T) {
130130
if _, err := bc.InsertChain(blocks); err != nil {
131131
t.Fatalf("failed to import contra-fork chain for expansion: %v", err)
132132
}
133-
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
133+
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true, nil); err != nil {
134134
t.Fatalf("failed to commit contra-fork head for expansion: %v", err)
135135
}
136136
blocks, _ = GenerateChain(&proConf, conBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})
@@ -150,7 +150,7 @@ func TestDAOForkRangeExtradata(t *testing.T) {
150150
if _, err := bc.InsertChain(blocks); err != nil {
151151
t.Fatalf("failed to import pro-fork chain for expansion: %v", err)
152152
}
153-
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true); err != nil {
153+
if err := bc.stateCache.TrieDB().Commit(bc.CurrentHeader().Root, true, nil); err != nil {
154154
t.Fatalf("failed to commit pro-fork head for expansion: %v", err)
155155
}
156156
blocks, _ = GenerateChain(&conConf, proBc.CurrentBlock(), ethash.NewFaker(), db, 1, func(i int, gen *BlockGen) {})

core/genesis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (g *Genesis) ToBlock(db ethdb.Database) *types.Block {
285285
head.Difficulty = params.GenesisDifficulty
286286
}
287287
statedb.Commit(false)
288-
statedb.Database().TrieDB().Commit(root, true)
288+
statedb.Database().TrieDB().Commit(root, true, nil)
289289

290290
return types.NewBlock(head, nil, nil, nil)
291291
}

0 commit comments

Comments
 (0)