Skip to content

Commit 2abdcff

Browse files
committed
feat: state.SnapshotTree interface for drop-in replacement
1 parent 44068c8 commit 2abdcff

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

core/state/statedb.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type StateDB struct {
6363
prefetcher *triePrefetcher
6464
trie Trie
6565
hasher crypto.KeccakState
66-
snaps *snapshot.Tree // Nil if snapshot is not available
66+
snaps SnapshotTree // Nil if snapshot is not available
6767
snap snapshot.Snapshot // Nil if snapshot is not available
6868

6969
// originalRoot is the pre-state root, before any changes were made.
@@ -141,7 +141,8 @@ type StateDB struct {
141141
}
142142

143143
// New creates a new state from a given trie.
144-
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
144+
func New(root common.Hash, db Database, snaps SnapshotTree) (*StateDB, error) {
145+
snaps = clearTypedNilPointer(snaps)
145146
tr, err := db.OpenTrie(root)
146147
if err != nil {
147148
return nil, err

core/state/statedb.libevm.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2024 the libevm authors.
2+
//
3+
// The libevm additions to go-ethereum are free software: you can redistribute
4+
// them and/or modify them under the terms of the GNU Lesser General Public License
5+
// as published by the Free Software Foundation, either version 3 of the License,
6+
// or (at your option) any later version.
7+
//
8+
// The libevm additions are distributed in the hope that they will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
11+
// General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Lesser General Public License
14+
// along with the go-ethereum library. If not, see
15+
// <http://www.gnu.org/licenses/>.
16+
17+
package state
18+
19+
import (
20+
"reflect"
21+
22+
"github.com/ava-labs/libevm/common"
23+
"github.com/ava-labs/libevm/core/state/snapshot"
24+
)
25+
26+
// SnapshotTree mirrors the functionality of a [snapshot.Tree], allowing for
27+
// drop-in replacements.
28+
type SnapshotTree interface {
29+
Cap(common.Hash, int) error
30+
Snapshot(common.Hash) snapshot.Snapshot
31+
StorageIterator(root, account, seek common.Hash) (snapshot.StorageIterator, error)
32+
Update(
33+
blockRoot common.Hash,
34+
parentRoot common.Hash,
35+
destructs map[common.Hash]struct{},
36+
accounts map[common.Hash][]byte,
37+
storage map[common.Hash]map[common.Hash][]byte,
38+
) error
39+
}
40+
41+
var _ SnapshotTree = (*snapshot.Tree)(nil)
42+
43+
// clearTypedNilPointer returns nil if `snaps == nil` or if it holds a nil
44+
// pointer. The default geth behaviour expected a [snapshot.Tree] pointer
45+
// instead of a SnapshotTree interface, which could result in typed-nil bugs.
46+
func clearTypedNilPointer(snaps SnapshotTree) SnapshotTree {
47+
if v := reflect.ValueOf(snaps); v.Kind() == reflect.Pointer && v.IsNil() {
48+
return nil
49+
}
50+
return snaps
51+
}

0 commit comments

Comments
 (0)