handle error

This commit is contained in:
roodeag 2024-03-21 12:48:18 +08:00
parent bca6c40709
commit 5a79cf998a
2 changed files with 30 additions and 11 deletions

View file

@ -288,7 +288,10 @@ func testIterativeStateSync(t *testing.T, count int, commit bool, bypath bool, s
} }
} }
// Copy the preimages from source db in order to traverse the state. // Copy the preimages from source db in order to traverse the state.
srcDb.TrieDB().WritePreimages() err = srcDb.TrieDB().WritePreimages()
if err != nil {
t.Fatalf("write preimages error: %v", err)
}
copyPreimages(srcDisk, dstDb) copyPreimages(srcDisk, dstDb)
// Cross check that the two states are in sync // Cross check that the two states are in sync
@ -389,7 +392,10 @@ func testIterativeDelayedStateSync(t *testing.T, scheme string) {
} }
} }
// Copy the preimages from source db in order to traverse the state. // Copy the preimages from source db in order to traverse the state.
srcDb.TrieDB().WritePreimages() err = srcDb.TrieDB().WritePreimages()
if err != nil {
t.Fatalf("write preimages error: %v", err)
}
copyPreimages(srcDisk, dstDb) copyPreimages(srcDisk, dstDb)
// Cross check that the two states are in sync // Cross check that the two states are in sync
@ -487,7 +493,10 @@ func testIterativeRandomStateSync(t *testing.T, count int, scheme string) {
} }
} }
// Copy the preimages from source db in order to traverse the state. // Copy the preimages from source db in order to traverse the state.
srcDb.TrieDB().WritePreimages() err = srcDb.TrieDB().WritePreimages()
if err != nil {
t.Fatalf("write preimages error: %v", err)
}
copyPreimages(srcDisk, dstDb) copyPreimages(srcDisk, dstDb)
// Cross check that the two states are in sync // Cross check that the two states are in sync
@ -591,7 +600,10 @@ func testIterativeRandomDelayedStateSync(t *testing.T, scheme string) {
} }
} }
// Copy the preimages from source db in order to traverse the state. // Copy the preimages from source db in order to traverse the state.
srcDb.TrieDB().WritePreimages() err = srcDb.TrieDB().WritePreimages()
if err != nil {
t.Fatalf("write preimages error: %v", err)
}
copyPreimages(srcDisk, dstDb) copyPreimages(srcDisk, dstDb)
// Cross check that the two states are in sync // Cross check that the two states are in sync
@ -707,7 +719,10 @@ func testIncompleteStateSync(t *testing.T, scheme string) {
} }
} }
// Copy the preimages from source db in order to traverse the state. // Copy the preimages from source db in order to traverse the state.
srcDb.TrieDB().WritePreimages() err = srcDb.TrieDB().WritePreimages()
if err != nil {
t.Fatalf("write preimages error: %v", err)
}
copyPreimages(db, dstDb) copyPreimages(db, dstDb)
// Sanity check that removing any node from the database is detected // Sanity check that removing any node from the database is detected

View file

@ -143,7 +143,7 @@ func (db *Database) Reader(blockRoot common.Hash) (database.Reader, error) {
// Therefore, these maps must not be changed afterwards. // Therefore, these maps must not be changed afterwards.
func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error { func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, nodes *trienode.MergedNodeSet, states *triestate.Set) error {
if db.preimages != nil { if db.preimages != nil {
db.preimages.commit(false) return db.preimages.commit(false)
} }
return db.backend.Update(root, parent, block, nodes, states) return db.backend.Update(root, parent, block, nodes, states)
} }
@ -153,7 +153,7 @@ func (db *Database) Update(root common.Hash, parent common.Hash, block uint64, n
// also written. // also written.
func (db *Database) Commit(root common.Hash, report bool) error { func (db *Database) Commit(root common.Hash, report bool) error {
if db.preimages != nil { if db.preimages != nil {
db.preimages.commit(true) return db.preimages.commit(true)
} }
return db.backend.Commit(root, report) return db.backend.Commit(root, report)
} }
@ -188,15 +188,19 @@ func (db *Database) Scheme() string {
// It is meant to be called when closing the blockchain object, so that all // It is meant to be called when closing the blockchain object, so that all
// resources held can be released correctly. // resources held can be released correctly.
func (db *Database) Close() error { func (db *Database) Close() error {
db.WritePreimages() err := db.WritePreimages()
if err != nil {
return err
}
return db.backend.Close() return db.backend.Close()
} }
// WritePreimages flushes all accumulated preimages to disk forcibly. // WritePreimages flushes all accumulated preimages to disk forcibly.
func (db *Database) WritePreimages() { func (db *Database) WritePreimages() error {
if db.preimages != nil { if db.preimages != nil {
db.preimages.commit(true) return db.preimages.commit(true)
} }
return nil
} }
// Preimage retrieves a cached trie node pre-image from preimage store. // Preimage retrieves a cached trie node pre-image from preimage store.
@ -226,7 +230,7 @@ func (db *Database) Cap(limit common.StorageSize) error {
return errors.New("not supported") return errors.New("not supported")
} }
if db.preimages != nil { if db.preimages != nil {
db.preimages.commit(false) return db.preimages.commit(false)
} }
return hdb.Cap(limit) return hdb.Cap(limit)
} }