Skip to content

Commit 17b3f2e

Browse files
committed
core/state: implement state iterator
1 parent de6672d commit 17b3f2e

File tree

7 files changed

+372
-121
lines changed

7 files changed

+372
-121
lines changed

core/state/database.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,24 @@ const (
4040
pointCacheSize = 4096
4141
)
4242

43+
// PreimageReader wraps the function Preimage for accessing the preimage of
44+
// a given hash.
45+
type PreimageReader interface {
46+
// Preimage returns the preimage of associated hash.
47+
Preimage(hash common.Hash) []byte
48+
}
49+
4350
// Database wraps access to tries and contract code.
4451
type Database interface {
52+
PreimageReader
53+
4554
// Reader returns a state reader associated with the specified state root.
4655
Reader(root common.Hash) (Reader, error)
4756

57+
// Iteratee returns a state iteratee associated with the specified state root,
58+
// through which the account iterator and storage iterator can be created.
59+
Iteratee(root common.Hash) (Iteratee, error)
60+
4861
// OpenTrie opens the main account trie.
4962
OpenTrie(root common.Hash) (Trie, error)
5063

@@ -57,9 +70,6 @@ type Database interface {
5770
// TrieDB returns the underlying trie database for managing trie nodes.
5871
TrieDB() *triedb.Database
5972

60-
// Snapshot returns the underlying state snapshot.
61-
Snapshot() *snapshot.Tree
62-
6373
// Commit flushes all pending writes and finalizes the state transition,
6474
// committing the changes to the underlying storage. It returns an error
6575
// if the commit fails.
@@ -187,6 +197,11 @@ func (db *CachingDB) WithSnapshot(snapshot *snapshot.Tree) *CachingDB {
187197
return db
188198
}
189199

200+
// Preimage returns the preimage of associated hash.
201+
func (db *CachingDB) Preimage(hash common.Hash) []byte {
202+
return db.triedb.Preimage(hash)
203+
}
204+
190205
// Reader returns a state reader associated with the specified state root.
191206
func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) {
192207
var readers []StateReader
@@ -277,11 +292,6 @@ func (db *CachingDB) PointCache() *utils.PointCache {
277292
return db.pointCache
278293
}
279294

280-
// Snapshot returns the underlying state snapshot.
281-
func (db *CachingDB) Snapshot() *snapshot.Tree {
282-
return db.snap
283-
}
284-
285295
// Commit flushes all pending writes and finalizes the state transition,
286296
// committing the changes to the underlying storage. It returns an error
287297
// if the commit fails.
@@ -316,6 +326,12 @@ func (db *CachingDB) Commit(update *stateUpdate) error {
316326
return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet())
317327
}
318328

329+
// Iteratee returns a state iteratee associated with the specified state root,
330+
// through which the account iterator and storage iterator can be created.
331+
func (db *CachingDB) Iteratee(root common.Hash) (Iteratee, error) {
332+
return newStateIteratee(!db.triedb.IsVerkle(), root, db.triedb, db.snap)
333+
}
334+
319335
// mustCopyTrie returns a deep-copied trie.
320336
func mustCopyTrie(t Trie) Trie {
321337
switch t := t.(type) {

core/state/database_history.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/core/state/codedb"
24-
"github.com/ethereum/go-ethereum/core/state/snapshot"
2524
"github.com/ethereum/go-ethereum/core/types"
2625
"github.com/ethereum/go-ethereum/rlp"
2726
"github.com/ethereum/go-ethereum/trie/utils"
@@ -114,6 +113,11 @@ func NewHistoricDatabase(triedb *triedb.Database, codedb *codedb.Database) *Hist
114113
}
115114
}
116115

116+
// Preimage returns the preimage of associated hash.
117+
func (db *HistoricDB) Preimage(hash common.Hash) []byte {
118+
return db.triedb.Preimage(hash)
119+
}
120+
117121
// Reader implements Database interface, returning a reader of the specific state.
118122
func (db *HistoricDB) Reader(stateRoot common.Hash) (Reader, error) {
119123
hr, err := db.triedb.HistoricReader(stateRoot)
@@ -144,14 +148,15 @@ func (db *HistoricDB) TrieDB() *triedb.Database {
144148
return db.triedb
145149
}
146150

147-
// Snapshot returns the underlying state snapshot.
148-
func (db *HistoricDB) Snapshot() *snapshot.Tree {
149-
return nil
150-
}
151-
152151
// Commit flushes all pending writes and finalizes the state transition,
153152
// committing the changes to the underlying storage. It returns an error
154153
// if the commit fails.
155154
func (db *HistoricDB) Commit(update *stateUpdate) error {
156155
return errors.New("not implemented")
157156
}
157+
158+
// Iteratee returns a state iteratee associated with the specified state root,
159+
// through which the account iterator and storage iterator can be created.
160+
func (db *HistoricDB) Iteratee(root common.Hash) (Iteratee, error) {
161+
return nil, errors.New("not implemented")
162+
}

0 commit comments

Comments
 (0)