Skip to content

Commit aee3a29

Browse files
committed
cmd/geth, triedb: add pathdb state verification (ethereum#32086)
This pull request ports the snapshot iteration logic from the legacy implementation.
1 parent e28b6d8 commit aee3a29

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
@@ -312,6 +312,16 @@ func (db *Database) Journal(root common.Hash) error {
312312
return pdb.Journal(root)
313313
}
314314

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

0 commit comments

Comments
 (0)