Skip to content

Commit d20350c

Browse files
committed
doc: all the things
1 parent 261c961 commit d20350c

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

core/state/statedb.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ import (
2828
"github.com/ava-labs/libevm/core/state/snapshot"
2929
"github.com/ava-labs/libevm/core/types"
3030
"github.com/ava-labs/libevm/crypto"
31-
"github.com/ava-labs/libevm/libevm/stateconf"
3231
"github.com/ava-labs/libevm/log"
3332
"github.com/ava-labs/libevm/metrics"
3433
"github.com/ava-labs/libevm/params"
3534
"github.com/ava-labs/libevm/trie"
3635
"github.com/ava-labs/libevm/trie/trienode"
3736
"github.com/ava-labs/libevm/trie/triestate"
3837
"github.com/holiman/uint256"
38+
39+
// libevm extra imports
40+
"github.com/ava-labs/libevm/libevm/stateconf"
3941
)
4042

4143
const (

core/state/statedb.libevm.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,35 @@ func clearTypedNilPointer(snaps SnapshotTree) SnapshotTree {
5454
return snaps
5555
}
5656

57+
// StateDBHooks modify the behaviour of [StateDB] instances.
5758
type StateDBHooks interface {
58-
TransformStateKey(common.Address, common.Hash) common.Hash
59+
// TransformStateKey receives the arguments passed to [StateDB.GetState],
60+
// [StateDB.GetCommittedState] or [StateDB.SetState], and returns the key
61+
// that each of those methods will use for accessing state. This method will
62+
// not, however, be called if any of the aforementioned [StateDB] methods
63+
// receives a [stateconf.SkipStateKeyTransformation] option.
64+
//
65+
// This method SHOULD NOT be used for anything other than achieving
66+
// backwards compatibility with an existing chain. In the event that other
67+
// methods are added to the [StateDBHooks] interface and no key
68+
// transformation is required, it is acceptable for this method to echo the
69+
// [common.Hash], unchanged.
70+
TransformStateKey(_ common.Address, key common.Hash) (newKey common.Hash)
5971
}
6072

73+
// RegisterExtras registers the [StateDBHooks] such that they modify the
74+
// behaviour of all [StateDB] instances. It is expected to be called in an
75+
// `init()` function and MUST NOT be called more than once.
6176
func RegisterExtras(s StateDBHooks) {
6277
registeredExtras.MustRegister(s)
6378
}
6479

80+
// TestOnlyClearRegisteredExtras clears the arguments previously passed to
81+
// [RegisterExtras]. It panics if called from a non-testing call stack.
82+
//
83+
// In tests it SHOULD be called before every call to [RegisterExtras] and then
84+
// defer-called afterwards, either directly or via testing.TB.Cleanup(). This is
85+
// a workaround for the single-call limitation on [RegisterExtras].
6586
func TestOnlyClearRegisteredExtras() {
6687
registeredExtras.TestOnlyClear()
6788
}

eth/tracers/logger/logger_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525
"github.com/ava-labs/libevm/common"
2626
"github.com/ava-labs/libevm/core/state"
2727
"github.com/ava-labs/libevm/core/vm"
28-
"github.com/ava-labs/libevm/libevm/stateconf"
2928
"github.com/ava-labs/libevm/params"
3029
"github.com/holiman/uint256"
30+
31+
// libevm extra imports
32+
"github.com/ava-labs/libevm/libevm/stateconf"
3133
)
3234

3335
type dummyContractRef struct {

libevm/stateconf/conf.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,26 @@ func ExtractTrieDBUpdatePayload(opts ...TrieDBUpdateOption) (common.Hash, common
114114
return *conf.parentBlockHash, *conf.currentBlockHash, true
115115
}
116116

117+
// A StateDBStateOption configures the behaviour of state.StateDB methods for
118+
// getting and setting state: GetState(), GetCommittedState(), and SetState().
117119
type StateDBStateOption = options.Option[stateDBStateConfig]
118120

119121
type stateDBStateConfig struct {
120122
skipKeyTransformation bool
121123
}
122124

125+
// SkipStateKeyTransformation causes any registered state-key transformation
126+
// hook to be ignored. See state.RegisterExtras() for details.
123127
func SkipStateKeyTransformation() StateDBStateOption {
124128
return options.Func[stateDBStateConfig](func(c *stateDBStateConfig) {
125129
c.skipKeyTransformation = true
126130
})
127131
}
128132

133+
// ShouldTransformStateKey parses the options, returning whether or not any
134+
// registered state-key transformation hook should be used; i.e. it returns
135+
// `true` i.f.f. there are no [SkipStateKeyTransformation] options in the
136+
// arguments.
129137
func ShouldTransformStateKey(opts ...StateDBStateOption) bool {
130138
return !options.As(opts...).skipKeyTransformation
131139
}

0 commit comments

Comments
 (0)