Skip to content

Commit 79b6a56

Browse files
authored
core/state: prevent SetCode hook if contract code is not changed (#32980)
This PR prevents the SetCode hook from being called when the contract code remains unchanged. This situation can occur in the following cases: - The deployed runtime code has zero length - An EIP-7702 authorization attempt tries to unset a non-delegated account - An EIP-7702 authorization attempt tries to delegate to the same account
1 parent d73bfeb commit 79b6a56

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

core/state/statedb_hooked.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,18 @@ func (s *hookedStateDB) SetNonce(address common.Address, nonce uint64, reason tr
191191

192192
func (s *hookedStateDB) SetCode(address common.Address, code []byte, reason tracing.CodeChangeReason) []byte {
193193
prev := s.inner.SetCode(address, code, reason)
194+
194195
if s.hooks.OnCodeChangeV2 != nil || s.hooks.OnCodeChange != nil {
195-
prevHash := types.EmptyCodeHash
196-
if len(prev) != 0 {
197-
prevHash = crypto.Keccak256Hash(prev)
198-
}
196+
prevHash := crypto.Keccak256Hash(prev)
199197
codeHash := crypto.Keccak256Hash(code)
200198

201-
if s.hooks.OnCodeChangeV2 != nil {
202-
s.hooks.OnCodeChangeV2(address, prevHash, prev, codeHash, code, reason)
203-
} else if s.hooks.OnCodeChange != nil {
204-
s.hooks.OnCodeChange(address, prevHash, prev, codeHash, code)
199+
// Invoke the hooks only if the contract code is changed
200+
if prevHash != codeHash {
201+
if s.hooks.OnCodeChangeV2 != nil {
202+
s.hooks.OnCodeChangeV2(address, prevHash, prev, codeHash, code, reason)
203+
} else if s.hooks.OnCodeChange != nil {
204+
s.hooks.OnCodeChange(address, prevHash, prev, codeHash, code)
205+
}
205206
}
206207
}
207208
return prev

0 commit comments

Comments
 (0)