Skip to content

Commit

Permalink
feat: state.SnapshotTree interface for drop-in replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR4N committed Nov 26, 2024
1 parent 44068c8 commit 2abdcff
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
5 changes: 3 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type StateDB struct {
prefetcher *triePrefetcher
trie Trie
hasher crypto.KeccakState
snaps *snapshot.Tree // Nil if snapshot is not available
snaps SnapshotTree // Nil if snapshot is not available
snap snapshot.Snapshot // Nil if snapshot is not available

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

// New creates a new state from a given trie.
func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
func New(root common.Hash, db Database, snaps SnapshotTree) (*StateDB, error) {
snaps = clearTypedNilPointer(snaps)
tr, err := db.OpenTrie(root)
if err != nil {
return nil, err
Expand Down
51 changes: 51 additions & 0 deletions core/state/statedb.libevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 the libevm authors.
//
// The libevm additions to go-ethereum are free software: you can redistribute
// them and/or modify them under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// The libevm additions are distributed in the hope that they will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see
// <http://www.gnu.org/licenses/>.

package state

import (
"reflect"

"github.com/ava-labs/libevm/common"
"github.com/ava-labs/libevm/core/state/snapshot"
)

// SnapshotTree mirrors the functionality of a [snapshot.Tree], allowing for
// drop-in replacements.
type SnapshotTree interface {
Cap(common.Hash, int) error
Snapshot(common.Hash) snapshot.Snapshot
StorageIterator(root, account, seek common.Hash) (snapshot.StorageIterator, error)
Update(
blockRoot common.Hash,
parentRoot common.Hash,
destructs map[common.Hash]struct{},
accounts map[common.Hash][]byte,
storage map[common.Hash]map[common.Hash][]byte,
) error
}

var _ SnapshotTree = (*snapshot.Tree)(nil)

// clearTypedNilPointer returns nil if `snaps == nil` or if it holds a nil
// pointer. The default geth behaviour expected a [snapshot.Tree] pointer
// instead of a SnapshotTree interface, which could result in typed-nil bugs.
func clearTypedNilPointer(snaps SnapshotTree) SnapshotTree {
if v := reflect.ValueOf(snaps); v.Kind() == reflect.Pointer && v.IsNil() {
return nil
}
return snaps
}

0 comments on commit 2abdcff

Please sign in to comment.