triedb/pathdb: fix test

This commit is contained in:
Gary Rong 2025-08-29 09:35:39 +08:00
parent 8984043821
commit 0574091a8d
3 changed files with 21 additions and 11 deletions

View file

@ -320,11 +320,12 @@ func (r *historyReader) read(state stateIdentQuery, stateID uint64, lastID uint6
tail, err := r.freezer.Tail() tail, err := r.freezer.Tail()
if err != nil { if err != nil {
return nil, err return nil, err
} } // firstID = tail+1
// stateID == tail is allowed, as the first history object preserved
// is tail+1 // stateID+1 == firstID is allowed, as all the subsequent state histories
// are present with no gap inside.
if stateID < tail { if stateID < tail {
return nil, errors.New("historical state has been pruned") return nil, fmt.Errorf("historical state has been pruned, first: %d, state: %d", tail+1, stateID)
} }
// To serve the request, all state histories from stateID+1 to lastID // To serve the request, all state histories from stateID+1 to lastID

View file

@ -39,9 +39,17 @@ func waitIndexing(db *Database) {
func checkHistoricalState(env *tester, root common.Hash, hr *historyReader) error { func checkHistoricalState(env *tester, root common.Hash, hr *historyReader) error {
// Short circuit if the historical state is no longer available // Short circuit if the historical state is no longer available
if rawdb.ReadStateID(env.db.diskdb, root) == nil { id := rawdb.ReadStateID(env.db.diskdb, root)
if id == nil {
return nil 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)
}
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)
@ -125,8 +133,8 @@ func testHistoryReader(t *testing.T, historyLimit uint64) {
defer func() { defer func() {
maxDiffLayers = 128 maxDiffLayers = 128
}() }()
//log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
// log.SetDefault(log.NewLogger(log.NewTerminalHandlerWithLevel(os.Stderr, log.LevelDebug, true)))
env := newTester(t, historyLimit, false, 64, true, "") env := newTester(t, historyLimit, false, 64, true, "")
defer env.release() defer env.release()
waitIndexing(env.db) waitIndexing(env.db)

View file

@ -18,6 +18,7 @@ package pathdb
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"reflect" "reflect"
"testing" "testing"
@ -244,11 +245,11 @@ func TestTruncateOutOfRange(t *testing.T) {
expErr error expErr error
}{ }{
{0, head, nil}, // nothing to delete {0, head, nil}, // nothing to delete
{0, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)}, {0, head + 1, errHeadTruncationOutOfRange},
{0, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)}, {0, tail - 1, errHeadTruncationOutOfRange},
{1, tail, nil}, // nothing to delete {1, tail, nil}, // nothing to delete
{1, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)}, {1, head + 1, errTailTruncationOutOfRange},
{1, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)}, {1, tail - 1, errTailTruncationOutOfRange},
} }
for _, c := range cases { for _, c := range cases {
var gotErr error var gotErr error
@ -257,7 +258,7 @@ func TestTruncateOutOfRange(t *testing.T) {
} else { } else {
_, gotErr = truncateFromTail(freezer, c.target) _, gotErr = truncateFromTail(freezer, c.target)
} }
if !reflect.DeepEqual(gotErr, c.expErr) { if !errors.Is(gotErr, c.expErr) {
t.Errorf("Unexpected error, want: %v, got: %v", c.expErr, gotErr) t.Errorf("Unexpected error, want: %v, got: %v", c.expErr, gotErr)
} }
} }