triedb/pathdb: fix test

This commit is contained in:
Gary Rong 2025-08-29 09:51:34 +08:00
parent 0574091a8d
commit 19000cea88
2 changed files with 29 additions and 19 deletions

View file

@ -37,19 +37,29 @@ func waitIndexing(db *Database) {
} }
} }
func checkHistoricalState(env *tester, root common.Hash, hr *historyReader) error { func stateAvail(id uint64, env *tester) bool {
// Short circuit if the historical state is no longer available if env.db.config.StateHistory == 0 {
id := rawdb.ReadStateID(env.db.diskdb, root) return true
if id == nil { }
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 return nil
} }
meta, err := readStateHistoryMeta(env.db.stateFreezer, *id)
if err != nil { // Short circuit if the historical state is no longer available
return nil // e.g., the referred state history has been pruned if rawdb.ReadStateID(env.db.diskdb, root) == nil {
} return fmt.Errorf("state not found %d %x", id, root)
if meta.root != root {
return fmt.Errorf("state %#x is not canonincal", root)
} }
var ( var (
dl = env.db.tree.bottom() dl = env.db.tree.bottom()
stateID = rawdb.ReadStateID(env.db.diskdb, root) stateID = rawdb.ReadStateID(env.db.diskdb, root)
@ -141,14 +151,14 @@ func testHistoryReader(t *testing.T, historyLimit uint64) {
var ( var (
roots = env.roots roots = env.roots
dRoot = env.db.tree.bottom().rootHash() dl = env.db.tree.bottom()
hr = newHistoryReader(env.db.diskdb, env.db.stateFreezer) hr = newHistoryReader(env.db.diskdb, env.db.stateFreezer)
) )
for _, root := range roots { for i, root := range roots {
if root == dRoot { if root == dl.rootHash() {
break break
} }
if err := checkHistoricalState(env, root, hr); err != nil { if err := checkHistoricalState(env, root, uint64(i+1), hr); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }
@ -157,11 +167,11 @@ func testHistoryReader(t *testing.T, historyLimit uint64) {
env.extend(4) env.extend(4)
waitIndexing(env.db) waitIndexing(env.db)
for _, root := range roots { for i, root := range roots {
if root == dRoot { if root == dl.rootHash() {
break break
} }
if err := checkHistoricalState(env, root, hr); err != nil { if err := checkHistoricalState(env, root, uint64(i+1), hr); err != nil {
t.Fatal(err) t.Fatal(err)
} }
} }

View file

@ -224,11 +224,11 @@ func (db *Database) HistoricReader(root common.Hash) (*HistoricalStateReader, er
} }
// Ensure the requested state is canonical, historical states on side chain // Ensure the requested state is canonical, historical states on side chain
// are not accessible. // are not accessible.
meta, err := readStateHistoryMeta(db.stateFreezer, *id) meta, err := readStateHistoryMeta(db.stateFreezer, *id+1)
if err != nil { if err != nil {
return nil, err // e.g., the referred state history has been pruned 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 nil, fmt.Errorf("state %#x is not canonincal", root)
} }
return &HistoricalStateReader{ return &HistoricalStateReader{