From 19000cea887347a9ddf2945ac7794f807f681e99 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Fri, 29 Aug 2025 09:51:34 +0800 Subject: [PATCH] triedb/pathdb: fix test --- triedb/pathdb/history_reader_test.go | 44 +++++++++++++++++----------- triedb/pathdb/reader.go | 4 +-- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/triedb/pathdb/history_reader_test.go b/triedb/pathdb/history_reader_test.go index b90611bdf0..9028a886ce 100644 --- a/triedb/pathdb/history_reader_test.go +++ b/triedb/pathdb/history_reader_test.go @@ -37,19 +37,29 @@ func waitIndexing(db *Database) { } } -func checkHistoricalState(env *tester, root common.Hash, hr *historyReader) error { - // Short circuit if the historical state is no longer available - id := rawdb.ReadStateID(env.db.diskdb, root) - if id == nil { +func stateAvail(id uint64, env *tester) bool { + if env.db.config.StateHistory == 0 { + return true + } + dl := env.db.tree.bottom() + if dl.stateID() <= env.db.config.StateHistory { + return true + } + firstID := dl.stateID() - env.db.config.StateHistory + 1 + + return id+1 >= firstID +} + +func checkHistoricalState(env *tester, root common.Hash, id uint64, hr *historyReader) error { + if !stateAvail(id, env) { return nil } - meta, err := readStateHistoryMeta(env.db.stateFreezer, *id) - if err != nil { - return nil // e.g., the referred state history has been pruned - } - if meta.root != root { - return fmt.Errorf("state %#x is not canonincal", root) + + // Short circuit if the historical state is no longer available + if rawdb.ReadStateID(env.db.diskdb, root) == nil { + return fmt.Errorf("state not found %d %x", id, root) } + var ( dl = env.db.tree.bottom() stateID = rawdb.ReadStateID(env.db.diskdb, root) @@ -141,14 +151,14 @@ func testHistoryReader(t *testing.T, historyLimit uint64) { var ( roots = env.roots - dRoot = env.db.tree.bottom().rootHash() + dl = env.db.tree.bottom() hr = newHistoryReader(env.db.diskdb, env.db.stateFreezer) ) - for _, root := range roots { - if root == dRoot { + for i, root := range roots { + if root == dl.rootHash() { break } - if err := checkHistoricalState(env, root, hr); err != nil { + if err := checkHistoricalState(env, root, uint64(i+1), hr); err != nil { t.Fatal(err) } } @@ -157,11 +167,11 @@ func testHistoryReader(t *testing.T, historyLimit uint64) { env.extend(4) waitIndexing(env.db) - for _, root := range roots { - if root == dRoot { + for i, root := range roots { + if root == dl.rootHash() { break } - if err := checkHistoricalState(env, root, hr); err != nil { + if err := checkHistoricalState(env, root, uint64(i+1), hr); err != nil { t.Fatal(err) } } diff --git a/triedb/pathdb/reader.go b/triedb/pathdb/reader.go index a03924f8ff..842ac0972e 100644 --- a/triedb/pathdb/reader.go +++ b/triedb/pathdb/reader.go @@ -224,11 +224,11 @@ func (db *Database) HistoricReader(root common.Hash) (*HistoricalStateReader, er } // Ensure the requested state is canonical, historical states on side chain // are not accessible. - meta, err := readStateHistoryMeta(db.stateFreezer, *id) + meta, err := readStateHistoryMeta(db.stateFreezer, *id+1) if err != nil { return nil, err // e.g., the referred state history has been pruned } - if meta.root != root { + if meta.parent != root { return nil, fmt.Errorf("state %#x is not canonincal", root) } return &HistoricalStateReader{