Skip to content

Commit 6dd38d2

Browse files
authored
cmd/geth, triedb: add pathdb state verification (#32086)
This pull request ports the snapshot iteration logic from the legacy implementation.
1 parent 9c5c0e3 commit 6dd38d2

File tree

3 files changed

+396
-21
lines changed

3 files changed

+396
-21
lines changed

cmd/geth/snapshot.go

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -220,35 +220,45 @@ func verifyState(ctx *cli.Context) error {
220220
triedb := utils.MakeTrieDatabase(ctx, chaindb, false, true, false)
221221
defer triedb.Close()
222222

223-
snapConfig := snapshot.Config{
224-
CacheSize: 256,
225-
Recovery: false,
226-
NoBuild: true,
227-
AsyncBuild: false,
228-
}
229-
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, headBlock.Root())
230-
if err != nil {
231-
log.Error("Failed to open snapshot tree", "err", err)
232-
return err
233-
}
234-
if ctx.NArg() > 1 {
235-
log.Error("Too many arguments given")
236-
return errors.New("too many arguments")
237-
}
238-
var root = headBlock.Root()
223+
var (
224+
err error
225+
root = headBlock.Root()
226+
)
239227
if ctx.NArg() == 1 {
240228
root, err = parseRoot(ctx.Args().First())
241229
if err != nil {
242230
log.Error("Failed to resolve state root", "err", err)
243231
return err
244232
}
245233
}
246-
if err := snaptree.Verify(root); err != nil {
247-
log.Error("Failed to verify state", "root", root, "err", err)
248-
return err
234+
if triedb.Scheme() == rawdb.PathScheme {
235+
if err := triedb.VerifyState(root); err != nil {
236+
log.Error("Failed to verify state", "root", root, "err", err)
237+
return err
238+
}
239+
log.Info("Verified the state", "root", root)
240+
241+
// TODO(rjl493456442) implement dangling checks in pathdb.
242+
return nil
243+
} else {
244+
snapConfig := snapshot.Config{
245+
CacheSize: 256,
246+
Recovery: false,
247+
NoBuild: true,
248+
AsyncBuild: false,
249+
}
250+
snaptree, err := snapshot.New(snapConfig, chaindb, triedb, headBlock.Root())
251+
if err != nil {
252+
log.Error("Failed to open snapshot tree", "err", err)
253+
return err
254+
}
255+
if err := snaptree.Verify(root); err != nil {
256+
log.Error("Failed to verify state", "root", root, "err", err)
257+
return err
258+
}
259+
log.Info("Verified the state", "root", root)
260+
return snapshot.CheckDanglingStorage(chaindb)
249261
}
250-
log.Info("Verified the state", "root", root)
251-
return snapshot.CheckDanglingStorage(chaindb)
252262
}
253263

254264
// checkDanglingStorage iterates the snap storage data, and verifies that all

triedb/database.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ func (db *Database) Journal(root common.Hash) error {
317317
return pdb.Journal(root)
318318
}
319319

320+
// VerifyState traverses the flat states specified by the given state root and
321+
// ensures they are matched with each other.
322+
func (db *Database) VerifyState(root common.Hash) error {
323+
pdb, ok := db.backend.(*pathdb.Database)
324+
if !ok {
325+
return errors.New("not supported")
326+
}
327+
return pdb.VerifyState(root)
328+
}
329+
320330
// AccountIterator creates a new account iterator for the specified root hash and
321331
// seeks to a starting account hash.
322332
func (db *Database) AccountIterator(root common.Hash, seek common.Hash) (pathdb.AccountIterator, error) {

0 commit comments

Comments
 (0)