Skip to content

Commit 5ddedd2

Browse files
authored
core, light, trie: remove DiskDB function from trie database (#25690)
1 parent d408cb6 commit 5ddedd2

File tree

7 files changed

+32
-24
lines changed

7 files changed

+32
-24
lines changed

core/state/database.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ type Database interface {
5454
// ContractCodeSize retrieves a particular contracts code's size.
5555
ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
5656

57+
// DiskDB returns the underlying key-value disk database.
58+
DiskDB() ethdb.KeyValueStore
59+
5760
// TrieDB retrieves the low level trie database used for data storage.
5861
TrieDB() *trie.Database
5962
}
@@ -130,13 +133,15 @@ func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
130133
csc, _ := lru.New(codeSizeCacheSize)
131134
return &cachingDB{
132135
db: trie.NewDatabaseWithConfig(db, config),
136+
disk: db,
133137
codeSizeCache: csc,
134138
codeCache: fastcache.New(codeCacheSize),
135139
}
136140
}
137141

138142
type cachingDB struct {
139143
db *trie.Database
144+
disk ethdb.KeyValueStore
140145
codeSizeCache *lru.Cache
141146
codeCache *fastcache.Cache
142147
}
@@ -174,7 +179,7 @@ func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error
174179
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
175180
return code, nil
176181
}
177-
code := rawdb.ReadCode(db.db.DiskDB(), codeHash)
182+
code := rawdb.ReadCode(db.disk, codeHash)
178183
if len(code) > 0 {
179184
db.codeCache.Set(codeHash.Bytes(), code)
180185
db.codeSizeCache.Add(codeHash, len(code))
@@ -190,7 +195,7 @@ func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]b
190195
if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
191196
return code, nil
192197
}
193-
code := rawdb.ReadCodeWithPrefix(db.db.DiskDB(), codeHash)
198+
code := rawdb.ReadCodeWithPrefix(db.disk, codeHash)
194199
if len(code) > 0 {
195200
db.codeCache.Set(codeHash.Bytes(), code)
196201
db.codeSizeCache.Add(codeHash, len(code))
@@ -208,6 +213,11 @@ func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, erro
208213
return len(code), err
209214
}
210215

216+
// DiskDB returns the underlying key-value disk database.
217+
func (db *cachingDB) DiskDB() ethdb.KeyValueStore {
218+
return db.disk
219+
}
220+
211221
// TrieDB retrieves any intermediate trie-node caching layer.
212222
func (db *cachingDB) TrieDB() *trie.Database {
213223
return db.db

core/state/iterator_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"testing"
2222

2323
"github.com/ethereum/go-ethereum/common"
24-
"github.com/ethereum/go-ethereum/ethdb"
2524
)
2625

2726
// Tests that the node iterator indeed walks over the entire database contents.
@@ -55,7 +54,7 @@ func TestNodeIteratorCoverage(t *testing.T) {
5554
t.Errorf("state entry not reported %x", hash)
5655
}
5756
}
58-
it := db.TrieDB().DiskDB().(ethdb.Database).NewIterator(nil, nil)
57+
it := db.DiskDB().NewIterator(nil, nil)
5958
for it.Next() {
6059
key := it.Key()
6160
if bytes.HasPrefix(key, []byte("secure-key-")) {

core/state/snapshot/generate_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,12 @@ func TestGenerateWithExtraAccounts(t *testing.T) {
491491

492492
// Identical in the snap
493493
key := hashData([]byte("acc-1"))
494-
rawdb.WriteAccountSnapshot(helper.triedb.DiskDB(), key, val)
495-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("key-1")), []byte("val-1"))
496-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("key-2")), []byte("val-2"))
497-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("key-3")), []byte("val-3"))
498-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("key-4")), []byte("val-4"))
499-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("key-5")), []byte("val-5"))
494+
rawdb.WriteAccountSnapshot(helper.diskdb, key, val)
495+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("key-1")), []byte("val-1"))
496+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("key-2")), []byte("val-2"))
497+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("key-3")), []byte("val-3"))
498+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("key-4")), []byte("val-4"))
499+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("key-5")), []byte("val-5"))
500500
}
501501
{
502502
// Account two exists only in the snapshot
@@ -508,15 +508,15 @@ func TestGenerateWithExtraAccounts(t *testing.T) {
508508
acc := &Account{Balance: big.NewInt(1), Root: stRoot, CodeHash: emptyCode.Bytes()}
509509
val, _ := rlp.EncodeToBytes(acc)
510510
key := hashData([]byte("acc-2"))
511-
rawdb.WriteAccountSnapshot(helper.triedb.DiskDB(), key, val)
512-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("b-key-1")), []byte("b-val-1"))
513-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("b-key-2")), []byte("b-val-2"))
514-
rawdb.WriteStorageSnapshot(helper.triedb.DiskDB(), key, hashData([]byte("b-key-3")), []byte("b-val-3"))
511+
rawdb.WriteAccountSnapshot(helper.diskdb, key, val)
512+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("b-key-1")), []byte("b-val-1"))
513+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("b-key-2")), []byte("b-val-2"))
514+
rawdb.WriteStorageSnapshot(helper.diskdb, key, hashData([]byte("b-key-3")), []byte("b-val-3"))
515515
}
516516
root := helper.Commit()
517517

518518
// To verify the test: If we now inspect the snap db, there should exist extraneous storage items
519-
if data := rawdb.ReadStorageSnapshot(helper.triedb.DiskDB(), hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
519+
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data == nil {
520520
t.Fatalf("expected snap storage to exist")
521521
}
522522
snap := generateSnapshot(helper.diskdb, helper.triedb, 16, root)
@@ -534,7 +534,7 @@ func TestGenerateWithExtraAccounts(t *testing.T) {
534534
snap.genAbort <- stop
535535
<-stop
536536
// If we now inspect the snap db, there should exist no extraneous storage items
537-
if data := rawdb.ReadStorageSnapshot(helper.triedb.DiskDB(), hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
537+
if data := rawdb.ReadStorageSnapshot(helper.diskdb, hashData([]byte("acc-2")), hashData([]byte("b-key-1"))); data != nil {
538538
t.Fatalf("expected slot to be removed, got %v", string(data))
539539
}
540540
}

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
908908
storageTrieNodes int
909909
nodes = trie.NewMergedNodeSet()
910910
)
911-
codeWriter := s.db.TrieDB().DiskDB().NewBatch()
911+
codeWriter := s.db.DiskDB().NewBatch()
912912
for addr := range s.stateObjectsDirty {
913913
if obj := s.stateObjects[addr]; !obj.deleted {
914914
// Write any contract code associated with the state object

core/state/sync_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
100100
}
101101

102102
// checkTrieConsistency checks that all nodes in a (sub-)trie are indeed present.
103-
func checkTrieConsistency(db ethdb.Database, root common.Hash) error {
103+
func checkTrieConsistency(db ethdb.KeyValueStore, root common.Hash) error {
104104
if v, _ := db.Get(root[:]); v == nil {
105105
return nil // Consider a non existent state consistent.
106106
}
@@ -553,7 +553,7 @@ func TestIncompleteStateSync(t *testing.T) {
553553
}
554554
}
555555
isCode[common.BytesToHash(emptyCodeHash)] = struct{}{}
556-
checkTrieConsistency(srcDb.TrieDB().DiskDB().(ethdb.Database), srcRoot)
556+
checkTrieConsistency(srcDb.DiskDB(), srcRoot)
557557

558558
// Create a destination state and sync with the scheduler
559559
dstDb := rawdb.NewMemoryDatabase()

light/trie.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func (db *odrDatabase) TrieDB() *trie.Database {
9696
return nil
9797
}
9898

99+
func (db *odrDatabase) DiskDB() ethdb.KeyValueStore {
100+
panic("not implemented")
101+
}
102+
99103
type odrTrie struct {
100104
db *odrDatabase
101105
id *TrieID

trie/database.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,6 @@ func NewDatabaseWithConfig(diskdb ethdb.KeyValueStore, config *Config) *Database
304304
return db
305305
}
306306

307-
// DiskDB retrieves the persistent storage backing the trie database.
308-
func (db *Database) DiskDB() ethdb.KeyValueStore {
309-
return db.diskdb
310-
}
311-
312307
// insert inserts a simplified trie node into the memory database.
313308
// All nodes inserted by this function will be reference tracked
314309
// and in theory should only used for **trie nodes** insertion.

0 commit comments

Comments
 (0)