Skip to content

Commit 5f4cc3f

Browse files
authored
core/state: fixed hooked StateDB handling of OnCodeChangeV2 (#33148)
While updating to latest Geth, I noticed `OnCodeChangeV2` was not properly handled in `SelfDestruct/6780`, this PR fixes this and bring a unit test. Let me know if it's deemed more approriate to merge the tests with the other one.
1 parent ca91254 commit 5f4cc3f

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

core/state/statedb_hooked.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int {
220220
var prevCode []byte
221221
var prevCodeHash common.Hash
222222

223-
if s.hooks.OnCodeChange != nil {
223+
if s.hooks.OnCodeChange != nil || s.hooks.OnCodeChangeV2 != nil {
224224
prevCode = s.inner.GetCode(address)
225225
prevCodeHash = s.inner.GetCodeHash(address)
226226
}
@@ -246,7 +246,7 @@ func (s *hookedStateDB) SelfDestruct6780(address common.Address) (uint256.Int, b
246246
var prevCode []byte
247247
var prevCodeHash common.Hash
248248

249-
if s.hooks.OnCodeChange != nil {
249+
if s.hooks.OnCodeChange != nil || s.hooks.OnCodeChangeV2 != nil {
250250
prevCodeHash = s.inner.GetCodeHash(address)
251251
prevCode = s.inner.GetCode(address)
252252
}

core/state/statedb_hooked_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,47 @@ func TestHooks(t *testing.T) {
122122
sdb.AddLog(&types.Log{
123123
Address: common.Address{0xbb},
124124
})
125+
126+
if len(result) != len(wants) {
127+
t.Fatalf("number of tracing events wrong, have %d want %d", len(result), len(wants))
128+
}
129+
130+
for i, want := range wants {
131+
if have := result[i]; have != want {
132+
t.Fatalf("error event %d, have\n%v\nwant%v\n", i, have, want)
133+
}
134+
}
135+
}
136+
137+
func TestHooks_OnCodeChangeV2(t *testing.T) {
138+
inner, _ := New(types.EmptyRootHash, NewDatabaseForTesting())
139+
140+
var result []string
141+
var wants = []string{
142+
"0xaa00000000000000000000000000000000000000.code: (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) ->0x1325 (0xa12ae05590de0c93a00bc7ac773c2fdb621e44f814985e72194f921c0050f728) ContractCreation",
143+
"0xaa00000000000000000000000000000000000000.code: 0x1325 (0xa12ae05590de0c93a00bc7ac773c2fdb621e44f814985e72194f921c0050f728) -> (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) SelfDestruct",
144+
"0xbb00000000000000000000000000000000000000.code: (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) ->0x1326 (0x3c54516221d604e623f358bc95996ca3242aaa109bddabcebda13db9b3f90dcb) ContractCreation",
145+
"0xbb00000000000000000000000000000000000000.code: 0x1326 (0x3c54516221d604e623f358bc95996ca3242aaa109bddabcebda13db9b3f90dcb) -> (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470) SelfDestruct",
146+
}
147+
emitF := func(format string, a ...any) {
148+
result = append(result, fmt.Sprintf(format, a...))
149+
}
150+
sdb := NewHookedState(inner, &tracing.Hooks{
151+
OnCodeChangeV2: func(addr common.Address, prevCodeHash common.Hash, prevCode []byte, codeHash common.Hash, code []byte, reason tracing.CodeChangeReason) {
152+
emitF("%v.code: %#x (%v) ->%#x (%v) %s", addr, prevCode, prevCodeHash, code, codeHash, reason)
153+
},
154+
})
155+
sdb.SetCode(common.Address{0xaa}, []byte{0x13, 37}, tracing.CodeChangeContractCreation)
156+
sdb.SelfDestruct(common.Address{0xaa})
157+
158+
sdb.SetCode(common.Address{0xbb}, []byte{0x13, 38}, tracing.CodeChangeContractCreation)
159+
sdb.CreateContract(common.Address{0xbb})
160+
sdb.SelfDestruct6780(common.Address{0xbb})
161+
162+
if len(result) != len(wants) {
163+
t.Fatalf("number of tracing events wrong, have %d want %d", len(result), len(wants))
164+
}
165+
125166
for i, want := range wants {
126167
if have := result[i]; have != want {
127168
t.Fatalf("error event %d, have\n%v\nwant%v\n", i, have, want)

eth/tracers/native/mux.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ func (t *muxTracer) OnCodeChange(a common.Address, prevCodeHash common.Hash, pre
156156
}
157157
}
158158

159+
func (t *muxTracer) OnCodeChangeV2(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte, reason tracing.CodeChangeReason) {
160+
for _, t := range t.tracers {
161+
if t.OnCodeChangeV2 != nil {
162+
t.OnCodeChangeV2(a, prevCodeHash, prev, codeHash, code, reason)
163+
}
164+
}
165+
}
166+
159167
func (t *muxTracer) OnStorageChange(a common.Address, k, prev, new common.Hash) {
160168
for _, t := range t.tracers {
161169
if t.OnStorageChange != nil {

0 commit comments

Comments
 (0)