Skip to content

Commit 4002f12

Browse files
committed
Bubble up errors reading snapshot info from db
1 parent fb79423 commit 4002f12

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

core/rawdb/accessors_snapshot.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,8 @@ func DeleteSnapshotRoot(db ethdb.KeyValueWriter) {
7373
}
7474

7575
// ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
76-
func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte {
77-
data, _ := db.Get(accountSnapshotKey(hash))
78-
return data
76+
func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) ([]byte, error) {
77+
return db.Get(accountSnapshotKey(hash))
7978
}
8079

8180
// WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
@@ -93,9 +92,8 @@ func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) {
9392
}
9493

9594
// ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
96-
func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte {
97-
data, _ := db.Get(storageSnapshotKey(accountHash, storageHash))
98-
return data
95+
func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) ([]byte, error) {
96+
return db.Get(storageSnapshotKey(accountHash, storageHash))
9997
}
10098

10199
// WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.

core/state/snapshot/disklayer.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
117117
return blob, nil
118118
}
119119
// Cache doesn't contain account, pull from disk and cache for later
120-
blob := rawdb.ReadAccountSnapshot(dl.diskdb, hash)
120+
blob, err := rawdb.ReadAccountSnapshot(dl.diskdb, hash)
121+
if err != nil {
122+
return nil, err
123+
}
121124
dl.cache.Set(hash[:], blob)
122125

123126
snapshotCleanAccountMissMeter.Mark(1)
@@ -157,7 +160,10 @@ func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, erro
157160
return blob, nil
158161
}
159162
// Cache doesn't contain storage slot, pull from disk and cache for later
160-
blob := rawdb.ReadStorageSnapshot(dl.diskdb, accountHash, storageHash)
163+
blob, err := rawdb.ReadStorageSnapshot(dl.diskdb, accountHash, storageHash)
164+
if err != nil {
165+
return nil, err
166+
}
161167
dl.cache.Set(key, blob)
162168

163169
snapshotCleanStorageMissMeter.Mark(1)

core/state/snapshot/disklayer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func TestDiskMerge(t *testing.T) {
183183
// assertDatabaseAccount ensures that an account from the database matches the given blob.
184184
assertDatabaseAccount := func(account common.Hash, data []byte) {
185185
t.Helper()
186-
if blob := rawdb.ReadAccountSnapshot(db, account); !bytes.Equal(blob, data) {
186+
if blob, _ := rawdb.ReadAccountSnapshot(db, account); !bytes.Equal(blob, data) {
187187
t.Errorf("account database access (%x) mismatch: have %x, want %x", account, blob, data)
188188
}
189189
}
@@ -197,7 +197,7 @@ func TestDiskMerge(t *testing.T) {
197197
// assertDatabaseStorage ensures that a storage slot from the database matches the given blob.
198198
assertDatabaseStorage := func(account common.Hash, slot common.Hash, data []byte) {
199199
t.Helper()
200-
if blob := rawdb.ReadStorageSnapshot(db, account, slot); !bytes.Equal(blob, data) {
200+
if blob, _ := rawdb.ReadStorageSnapshot(db, account, slot); !bytes.Equal(blob, data) {
201201
t.Errorf("storage database access (%x:%x) mismatch: have %x, want %x", account, slot, blob, data)
202202
}
203203
}
@@ -387,7 +387,7 @@ func TestDiskPartialMerge(t *testing.T) {
387387
// exist otherwise.
388388
assertDatabaseAccount := func(account common.Hash, data []byte) {
389389
t.Helper()
390-
blob := rawdb.ReadAccountSnapshot(db, account)
390+
blob, _ := rawdb.ReadAccountSnapshot(db, account)
391391
if bytes.Compare(account[:], genMarker) > 0 && blob != nil {
392392
t.Fatalf("test %d: post-marker (%x) account database access (%x) succeeded: %x", i, genMarker, account, blob)
393393
}
@@ -407,7 +407,7 @@ func TestDiskPartialMerge(t *testing.T) {
407407
// and does not exist otherwise.
408408
assertDatabaseStorage := func(account common.Hash, slot common.Hash, data []byte) {
409409
t.Helper()
410-
blob := rawdb.ReadStorageSnapshot(db, account, slot)
410+
blob, _ := rawdb.ReadStorageSnapshot(db, account, slot)
411411
if bytes.Compare(append(account[:], slot[:]...), genMarker) > 0 && blob != nil {
412412
t.Fatalf("test %d: post-marker (%x) storage database access (%x:%x) succeeded: %x", i, genMarker, account, slot, blob)
413413
}

core/state/snapshot/generate_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ func testGenerateWithExtraAccounts(t *testing.T, scheme string) {
578578
root := helper.Commit()
579579

580580
// To verify the test: If we now inspect the snap db, there should exist extraneous storage items
581-
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
581+
if data, _ := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
582582
t.Fatalf("expected snap storage to exist")
583583
}
584584
snap := generateSnapshot(helper.diskdb, helper.triedb, 16, root)
@@ -596,7 +596,7 @@ func testGenerateWithExtraAccounts(t *testing.T, scheme string) {
596596
snap.genAbort <- stop
597597
<-stop
598598
// If we now inspect the snap db, there should exist no extraneous storage items
599-
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
599+
if data, _ := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
600600
t.Fatalf("expected slot to be removed, got %v", string(data))
601601
}
602602
}
@@ -746,7 +746,7 @@ func testGenerateWithMalformedSnapdata(t *testing.T, scheme string) {
746746
snap.genAbort <- stop
747747
<-stop
748748
// If we now inspect the snap db, there should exist no extraneous storage items
749-
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
749+
if data, _ := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
750750
t.Fatalf("expected slot to be removed, got %v", string(data))
751751
}
752752
}

core/state/snapshot/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func checkDanglingDiskStorage(chaindb ethdb.KeyValueStore) error {
6161
log.Info("Iterating snap storage", "at", fmt.Sprintf("%#x", accKey), "elapsed", common.PrettyDuration(time.Since(start)))
6262
lastReport = time.Now()
6363
}
64-
if data := rawdb.ReadAccountSnapshot(chaindb, common.BytesToHash(accKey)); len(data) == 0 {
64+
if data, _ := rawdb.ReadAccountSnapshot(chaindb, common.BytesToHash(accKey)); len(data) == 0 {
6565
log.Warn("Dangling storage - missing account", "account", fmt.Sprintf("%#x", accKey), "storagekey", fmt.Sprintf("%#x", k))
6666
return fmt.Errorf("dangling snapshot storage account %#x", accKey)
6767
}
@@ -97,7 +97,7 @@ func CheckJournalAccount(db ethdb.KeyValueStore, hash common.Hash) error {
9797
// Look up the disk layer first
9898
baseRoot := rawdb.ReadSnapshotRoot(db)
9999
fmt.Printf("Disklayer: Root: %x\n", baseRoot)
100-
if data := rawdb.ReadAccountSnapshot(db, hash); data != nil {
100+
if data, _ := rawdb.ReadAccountSnapshot(db, hash); data != nil {
101101
account, err := types.FullAccount(data)
102102
if err != nil {
103103
panic(err)

0 commit comments

Comments
 (0)