Skip to content

Commit

Permalink
implement OpenStorageTrie for verkle trees (#210)
Browse files Browse the repository at this point in the history
* implement OpenStorageTrie for verkle trees

* add a few comments for future maintenance

* fix linter issue
  • Loading branch information
gballet authored Jun 12, 2023
1 parent e1422c7 commit 14ceeb4
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 24 deletions.
8 changes: 5 additions & 3 deletions core/state/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Database interface {
OpenTrie(root common.Hash) (Trie, error)

// OpenStorageTrie opens the storage trie of an account.
OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error)
OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash, main Trie) (Trie, error)

// CopyTrie returns an independent copy of the given trie.
CopyTrie(Trie) Trie
Expand Down Expand Up @@ -190,9 +190,11 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
}

// OpenStorageTrie opens the storage trie of an account.
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash) (Trie, error) {
func (db *cachingDB) OpenStorageTrie(stateRoot common.Hash, addrHash, root common.Hash, self Trie) (Trie, error) {
if db.ended {
panic("not yet implemented")
// TODO return an adapter object to detect whether this is a storage trie. Or just a regular
// VerkleTrie after adding a "storage" flag to the VerkleTrie.
return self, nil
}
tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, addrHash, root), db.db)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/state/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (it *NodeIterator) step() error {
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob()), &account); err != nil {
return err
}
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, common.BytesToHash(it.stateIt.LeafKey()), account.Root)
dataTrie, err := it.state.db.OpenStorageTrie(it.state.originalRoot, common.BytesToHash(it.stateIt.LeafKey()), account.Root, nil)
if err != nil {
return err
}
Expand Down
21 changes: 6 additions & 15 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func (s *stateObject) getTrie(db Database) Trie {
}
if s.trie == nil {
var err error
s.trie, err = db.OpenStorageTrie(s.db.originalRoot, s.addrHash, s.data.Root)
s.trie, err = db.OpenStorageTrie(s.db.originalRoot, s.addrHash, s.data.Root, s.db.trie)
if err != nil {
s.trie, _ = db.OpenStorageTrie(s.db.originalRoot, s.addrHash, common.Hash{})
s.trie, _ = db.OpenStorageTrie(s.db.originalRoot, s.addrHash, common.Hash{}, s.db.trie)
s.setError(fmt.Errorf("can't create storage trie: %v", err))
}
}
Expand Down Expand Up @@ -222,19 +222,14 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
}
// If the snapshot is unavailable or reading from it fails, load from the database.
if s.db.snap == nil || err != nil {
var tr Trie
if s.db.GetTrie().IsVerkle() {
tr = s.db.GetTrie()
} else {
tr = s.getTrie(db)
}
tr := s.getTrie(db)
start := time.Now()
if s.db.GetTrie().IsVerkle() {
var v []byte
v, err = tr.TryGet(s.address[:], key.Bytes())
copy(value[:], v)
} else {
enc, err = s.getTrie(db).TryGet(s.address[:], key.Bytes())
enc, err = tr.TryGet(s.address[:], key.Bytes())
}
if metrics.EnabledExpensive {
s.db.StorageReads += time.Since(start)
Expand Down Expand Up @@ -332,12 +327,8 @@ func (s *stateObject) updateTrie(db Database) Trie {
// The snapshot storage map for the object
var storage map[common.Hash][]byte
// Insert all the pending updates into the trie
var tr Trie
if s.db.trie.IsVerkle() {
tr = s.db.trie
} else {
tr = s.getTrie(db)
}
tr := s.getTrie(db)

hasher := s.db.hasher

usedStorage := make([][]byte, 0, len(s.pendingStorage))
Expand Down
2 changes: 1 addition & 1 deletion core/state/trie_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (sf *subfetcher) loop() {
}
sf.trie = trie
} else {
trie, err := sf.db.OpenStorageTrie(sf.state, sf.owner, sf.root)
trie, err := sf.db.OpenStorageTrie(sf.state, sf.owner, sf.root, nil /* safe to set to nil for now, as there is no prefetcher for verkle */)
if err != nil {
log.Warn("Trie prefetcher failed opening trie", "root", sf.root, "err", err)
return
Expand Down
2 changes: 1 addition & 1 deletion les/server_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func handleGetProofs(msg Decoder) (serveRequestFn, uint64, uint64, error) {
p.bumpInvalid()
continue
}
trie, err = statedb.OpenStorageTrie(root, common.BytesToHash(request.AccKey), account.Root)
trie, err = statedb.OpenStorageTrie(root, common.BytesToHash(request.AccKey), account.Root, nil)
if trie == nil || err != nil {
p.Log().Warn("Failed to open storage trie for proof", "block", header.Number, "hash", header.Hash(), "account", common.BytesToHash(request.AccKey), "root", account.Root, "err", err)
continue
Expand Down
2 changes: 1 addition & 1 deletion light/odr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
t state.Trie
)
if len(req.Id.AccKey) > 0 {
t, err = odr.serverState.OpenStorageTrie(req.Id.StateRoot, common.BytesToHash(req.Id.AccKey), req.Id.Root)
t, err = odr.serverState.OpenStorageTrie(req.Id.StateRoot, common.BytesToHash(req.Id.AccKey), req.Id.Root, nil)
} else {
t, err = odr.serverState.OpenTrie(req.Id.Root)
}
Expand Down
2 changes: 1 addition & 1 deletion light/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (db *odrDatabase) OpenTrie(root common.Hash) (state.Trie, error) {
return &odrTrie{db: db, id: db.id}, nil
}

func (db *odrDatabase) OpenStorageTrie(state, addrHash, root common.Hash) (state.Trie, error) {
func (db *odrDatabase) OpenStorageTrie(state, addrHash, root common.Hash, _ state.Trie) (state.Trie, error) {
return &odrTrie{db: db, id: StorageTrieID(db.id, addrHash, root)}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion trie/verkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (trie *VerkleTrie) Commit(_ bool) (common.Hash, *NodeSet, error) {
}
}

return nodes[0].CommitmentBytes, NewNodeSet(common.Hash{}), nil
return nodes[0].CommitmentBytes, nil /* XXX this fixes the multiple 0-owner issue, but something more significant should be returned */, nil
}

// NodeIterator returns an iterator that returns nodes of the trie. Iteration
Expand Down

0 comments on commit 14ceeb4

Please sign in to comment.