@@ -18,6 +18,7 @@ package main
18
18
19
19
import (
20
20
"bytes"
21
+ "encoding/json"
21
22
"errors"
22
23
"fmt"
23
24
"os"
@@ -29,13 +30,16 @@ import (
29
30
30
31
"github.com/ethereum/go-ethereum/cmd/utils"
31
32
"github.com/ethereum/go-ethereum/common"
33
+ "github.com/ethereum/go-ethereum/core"
32
34
"github.com/ethereum/go-ethereum/core/rawdb"
33
35
"github.com/ethereum/go-ethereum/core/state"
34
36
"github.com/ethereum/go-ethereum/core/state/pruner"
35
37
"github.com/ethereum/go-ethereum/core/state/snapshot"
36
38
"github.com/ethereum/go-ethereum/crypto"
39
+ "github.com/ethereum/go-ethereum/eth/ethconfig"
37
40
"github.com/ethereum/go-ethereum/ethdb"
38
41
"github.com/ethereum/go-ethereum/log"
42
+ "github.com/ethereum/go-ethereum/metrics"
39
43
"github.com/ethereum/go-ethereum/node"
40
44
"github.com/ethereum/go-ethereum/rlp"
41
45
"github.com/ethereum/go-ethereum/trie"
@@ -129,6 +133,32 @@ geth snapshot verify-state <state-root>
129
133
will traverse the whole accounts and storages set based on the specified
130
134
snapshot and recalculate the root hash of state for verification.
131
135
In other words, this command does the snapshot to trie conversion.
136
+ ` ,
137
+ },
138
+ {
139
+ Name : "insecure-prune-all" ,
140
+ Usage : "Prune all trie state data except genesis block, it will break storage for fullnode, only suitable for fast node " +
141
+ "who do not need trie storage at all" ,
142
+ ArgsUsage : "<genesisPath>" ,
143
+ Action : utils .MigrateFlags (pruneAllState ),
144
+ Category : "MISCELLANEOUS COMMANDS" ,
145
+ Flags : []cli.Flag {
146
+ utils .DataDirFlag ,
147
+ utils .AncientFlag ,
148
+ utils .RopstenFlag ,
149
+ utils .RinkebyFlag ,
150
+ utils .GoerliFlag ,
151
+ },
152
+ Description : `
153
+ will prune all historical trie state data except genesis block.
154
+ All trie nodes will be deleted from the database.
155
+
156
+ It expects the genesis file as argument.
157
+
158
+ WARNING: It's necessary to delete the trie clean cache after the pruning.
159
+ If you specify another directory for the trie clean cache via "--cache.trie.journal"
160
+ during the use of Geth, please also specify it here for correct deletion. Otherwise
161
+ the trie clean cache with default directory will be deleted.
132
162
` ,
133
163
},
134
164
{
@@ -195,7 +225,7 @@ func accessDb(ctx *cli.Context, stack *node.Node) (ethdb.Database, error) {
195
225
}
196
226
headHeader := headBlock .Header ()
197
227
//Make sure the MPT and snapshot matches before pruning, otherwise the node can not start.
198
- snaptree , err := snapshot .New (chaindb , trie .NewDatabase (chaindb ), 256 , TriesInMemory , headBlock .Root (), false , false , false )
228
+ snaptree , err := snapshot .New (chaindb , trie .NewDatabase (chaindb ), 256 , TriesInMemory , headBlock .Root (), false , false , false , false )
199
229
if err != nil {
200
230
log .Error ("snaptree error" , "err" , err )
201
231
return nil , err // The relevant snapshot(s) might not exist
@@ -362,6 +392,48 @@ func pruneState(ctx *cli.Context) error {
362
392
return nil
363
393
}
364
394
395
+ func pruneAllState (ctx * cli.Context ) error {
396
+ stack , _ := makeConfigNode (ctx )
397
+ defer stack .Close ()
398
+
399
+ genesisPath := ctx .Args ().First ()
400
+ if len (genesisPath ) == 0 {
401
+ utils .Fatalf ("Must supply path to genesis JSON file" )
402
+ }
403
+ file , err := os .Open (genesisPath )
404
+ if err != nil {
405
+ utils .Fatalf ("Failed to read genesis file: %v" , err )
406
+ }
407
+ defer file .Close ()
408
+
409
+ g := new (core.Genesis )
410
+ if err := json .NewDecoder (file ).Decode (g ); err != nil {
411
+ cfg := gethConfig {
412
+ Eth : ethconfig .Defaults ,
413
+ Node : defaultNodeConfig (),
414
+ Metrics : metrics .DefaultConfig ,
415
+ }
416
+
417
+ // Load config file.
418
+ if err := loadConfig (genesisPath , & cfg ); err != nil {
419
+ utils .Fatalf ("%v" , err )
420
+ }
421
+ g = cfg .Eth .Genesis
422
+ }
423
+
424
+ chaindb := utils .MakeChainDatabase (ctx , stack , false , false )
425
+ pruner , err := pruner .NewAllPruner (chaindb )
426
+ if err != nil {
427
+ log .Error ("Failed to open snapshot tree" , "err" , err )
428
+ return err
429
+ }
430
+ if err = pruner .PruneAll (g ); err != nil {
431
+ log .Error ("Failed to prune state" , "err" , err )
432
+ return err
433
+ }
434
+ return nil
435
+ }
436
+
365
437
func verifyState (ctx * cli.Context ) error {
366
438
stack , _ := makeConfigNode (ctx )
367
439
defer stack .Close ()
@@ -372,7 +444,7 @@ func verifyState(ctx *cli.Context) error {
372
444
log .Error ("Failed to load head block" )
373
445
return errors .New ("no head block" )
374
446
}
375
- snaptree , err := snapshot .New (chaindb , trie .NewDatabase (chaindb ), 256 , 128 , headBlock .Root (), false , false , false )
447
+ snaptree , err := snapshot .New (chaindb , trie .NewDatabase (chaindb ), 256 , 128 , headBlock .Root (), false , false , false , false )
376
448
if err != nil {
377
449
log .Error ("Failed to open snapshot tree" , "err" , err )
378
450
return err
0 commit comments