Skip to content

Commit 328add2

Browse files
authored
core/tracing: add code change reason (ethereum#32525)
Closes ethereum#32376
1 parent 6f08b3a commit 328add2

24 files changed

+186
-59
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
423423
sdb := state.NewDatabase(tdb, nil)
424424
statedb, _ := state.New(types.EmptyRootHash, sdb)
425425
for addr, a := range accounts {
426-
statedb.SetCode(addr, a.Code)
426+
statedb.SetCode(addr, a.Code, tracing.CodeChangeUnspecified)
427427
statedb.SetNonce(addr, a.Nonce, tracing.NonceChangeGenesis)
428428
statedb.SetBalance(addr, uint256.MustFromBig(a.Balance), tracing.BalanceIncreaseGenesisBalance)
429429
for k, v := range a.Storage {

cmd/evm/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func runCmd(ctx *cli.Context) error {
322322
}
323323
} else {
324324
if len(code) > 0 {
325-
prestate.SetCode(receiver, code)
325+
prestate.SetCode(receiver, code, tracing.CodeChangeUnspecified)
326326
}
327327
execFunc = func() ([]byte, uint64, error) {
328328
// don't mutate the state!

core/genesis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
153153
if account.Balance != nil {
154154
statedb.AddBalance(addr, uint256.MustFromBig(account.Balance), tracing.BalanceIncreaseGenesisBalance)
155155
}
156-
statedb.SetCode(addr, account.Code)
156+
statedb.SetCode(addr, account.Code, tracing.CodeChangeGenesis)
157157
statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis)
158158
for key, value := range account.Storage {
159159
statedb.SetState(addr, key, value)
@@ -179,7 +179,7 @@ func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database) (common.Hash, e
179179
// already captures the allocations.
180180
statedb.AddBalance(addr, uint256.MustFromBig(account.Balance), tracing.BalanceIncreaseGenesisBalance)
181181
}
182-
statedb.SetCode(addr, account.Code)
182+
statedb.SetCode(addr, account.Code, tracing.CodeChangeGenesis)
183183
statedb.SetNonce(addr, account.Nonce, tracing.NonceChangeGenesis)
184184
for key, value := range account.Storage {
185185
statedb.SetState(addr, key, value)

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ func (s *StateDB) SetNonce(addr common.Address, nonce uint64, reason tracing.Non
458458
}
459459
}
460460

461-
func (s *StateDB) SetCode(addr common.Address, code []byte) (prev []byte) {
461+
func (s *StateDB) SetCode(addr common.Address, code []byte, reason tracing.CodeChangeReason) (prev []byte) {
462462
stateObject := s.getOrNewStateObject(addr)
463463
if stateObject != nil {
464464
return stateObject.SetCode(crypto.Keccak256Hash(code), code)

core/state/statedb_fuzz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction
8989
code := make([]byte, 16)
9090
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
9191
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
92-
s.SetCode(addr, code)
92+
s.SetCode(addr, code, tracing.CodeChangeUnspecified)
9393
},
9494
args: make([]int64, 2),
9595
},

core/state/statedb_hooked.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,20 @@ func (s *hookedStateDB) SetNonce(address common.Address, nonce uint64, reason tr
189189
}
190190
}
191191

192-
func (s *hookedStateDB) SetCode(address common.Address, code []byte) []byte {
193-
prev := s.inner.SetCode(address, code)
194-
if s.hooks.OnCodeChange != nil {
192+
func (s *hookedStateDB) SetCode(address common.Address, code []byte, reason tracing.CodeChangeReason) []byte {
193+
prev := s.inner.SetCode(address, code, reason)
194+
if s.hooks.OnCodeChangeV2 != nil || s.hooks.OnCodeChange != nil {
195195
prevHash := types.EmptyCodeHash
196196
if len(prev) != 0 {
197197
prevHash = crypto.Keccak256Hash(prev)
198198
}
199-
s.hooks.OnCodeChange(address, prevHash, prev, crypto.Keccak256Hash(code), code)
199+
codeHash := crypto.Keccak256Hash(code)
200+
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+
}
200206
}
201207
return prev
202208
}
@@ -224,8 +230,12 @@ func (s *hookedStateDB) SelfDestruct(address common.Address) uint256.Int {
224230
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
225231
}
226232

227-
if s.hooks.OnCodeChange != nil && len(prevCode) > 0 {
228-
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
233+
if len(prevCode) > 0 {
234+
if s.hooks.OnCodeChangeV2 != nil {
235+
s.hooks.OnCodeChangeV2(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil, tracing.CodeChangeSelfDestruct)
236+
} else if s.hooks.OnCodeChange != nil {
237+
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
238+
}
229239
}
230240

231241
return prev
@@ -246,8 +256,12 @@ func (s *hookedStateDB) SelfDestruct6780(address common.Address) (uint256.Int, b
246256
s.hooks.OnBalanceChange(address, prev.ToBig(), new(big.Int), tracing.BalanceDecreaseSelfdestruct)
247257
}
248258

249-
if s.hooks.OnCodeChange != nil && changed && len(prevCode) > 0 {
250-
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
259+
if changed && len(prevCode) > 0 {
260+
if s.hooks.OnCodeChangeV2 != nil {
261+
s.hooks.OnCodeChangeV2(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil, tracing.CodeChangeSelfDestruct)
262+
} else if s.hooks.OnCodeChange != nil {
263+
s.hooks.OnCodeChange(address, prevCodeHash, prevCode, types.EmptyCodeHash, nil)
264+
}
251265
}
252266

253267
return prev, changed

core/state/statedb_hooked_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestHooks(t *testing.T) {
114114
sdb.AddBalance(common.Address{0xaa}, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
115115
sdb.SubBalance(common.Address{0xaa}, uint256.NewInt(50), tracing.BalanceChangeTransfer)
116116
sdb.SetNonce(common.Address{0xaa}, 1337, tracing.NonceChangeGenesis)
117-
sdb.SetCode(common.Address{0xaa}, []byte{0x13, 37})
117+
sdb.SetCode(common.Address{0xaa}, []byte{0x13, 37}, tracing.CodeChangeUnspecified)
118118
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x11"))
119119
sdb.SetState(common.Address{0xaa}, common.HexToHash("0x01"), common.HexToHash("0x22"))
120120
sdb.SetTransientState(common.Address{0xaa}, common.HexToHash("0x02"), common.HexToHash("0x01"))

core/state/statedb_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func TestUpdateLeaks(t *testing.T) {
6565
state.SetState(addr, common.BytesToHash([]byte{i, i, i}), common.BytesToHash([]byte{i, i, i, i}))
6666
}
6767
if i%3 == 0 {
68-
state.SetCode(addr, []byte{i, i, i, i, i})
68+
state.SetCode(addr, []byte{i, i, i, i, i}, tracing.CodeChangeUnspecified)
6969
}
7070
}
7171

@@ -101,7 +101,7 @@ func TestIntermediateLeaks(t *testing.T) {
101101
state.SetState(addr, common.Hash{i, i, i, tweak}, common.Hash{i, i, i, i, tweak})
102102
}
103103
if i%3 == 0 {
104-
state.SetCode(addr, []byte{i, i, i, i, i, tweak})
104+
state.SetCode(addr, []byte{i, i, i, i, i, tweak}, tracing.CodeChangeUnspecified)
105105
}
106106
}
107107

@@ -374,7 +374,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
374374
code := make([]byte, 16)
375375
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
376376
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
377-
s.SetCode(addr, code)
377+
s.SetCode(addr, code, tracing.CodeChangeUnspecified)
378378
},
379379
args: make([]int64, 2),
380380
},
@@ -403,7 +403,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
403403
// which would cause a difference in state when unrolling
404404
// the journal. (CreateContact assumes created was false prior to
405405
// invocation, and the journal rollback sets it to false).
406-
s.SetCode(addr, []byte{1})
406+
s.SetCode(addr, []byte{1}, tracing.CodeChangeUnspecified)
407407
}
408408
},
409409
},
@@ -731,7 +731,7 @@ func TestCopyCommitCopy(t *testing.T) {
731731
sval := common.HexToHash("bbb")
732732

733733
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
734-
state.SetCode(addr, []byte("hello")) // Change an external metadata
734+
state.SetCode(addr, []byte("hello"), tracing.CodeChangeUnspecified) // Change an external metadata
735735
state.SetState(addr, skey, sval) // Change the storage trie
736736

737737
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
@@ -804,7 +804,7 @@ func TestCopyCopyCommitCopy(t *testing.T) {
804804
sval := common.HexToHash("bbb")
805805

806806
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
807-
state.SetCode(addr, []byte("hello")) // Change an external metadata
807+
state.SetCode(addr, []byte("hello"), tracing.CodeChangeUnspecified) // Change an external metadata
808808
state.SetState(addr, skey, sval) // Change the storage trie
809809

810810
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
@@ -874,7 +874,7 @@ func TestCommitCopy(t *testing.T) {
874874
sval1, sval2 := common.HexToHash("b1"), common.HexToHash("b2")
875875

876876
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
877-
state.SetCode(addr, []byte("hello")) // Change an external metadata
877+
state.SetCode(addr, []byte("hello"), tracing.CodeChangeUnspecified) // Change an external metadata
878878
state.SetState(addr, skey1, sval1) // Change the storage trie
879879

880880
if balance := state.GetBalance(addr); balance.Cmp(uint256.NewInt(42)) != 0 {
@@ -987,10 +987,10 @@ func testMissingTrieNodes(t *testing.T, scheme string) {
987987
addr := common.BytesToAddress([]byte("so"))
988988
{
989989
state.SetBalance(addr, uint256.NewInt(1), tracing.BalanceChangeUnspecified)
990-
state.SetCode(addr, []byte{1, 2, 3})
990+
state.SetCode(addr, []byte{1, 2, 3}, tracing.CodeChangeUnspecified)
991991
a2 := common.BytesToAddress([]byte("another"))
992992
state.SetBalance(a2, uint256.NewInt(100), tracing.BalanceChangeUnspecified)
993-
state.SetCode(a2, []byte{1, 2, 4})
993+
state.SetCode(a2, []byte{1, 2, 4}, tracing.CodeChangeUnspecified)
994994
root, _ = state.Commit(0, false, false)
995995
t.Logf("root: %x", root)
996996
// force-flush

core/state/trie_prefetcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func filledStateDB() *StateDB {
3939
sval := common.HexToHash("bbb")
4040

4141
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
42-
state.SetCode(addr, []byte("hello")) // Change an external metadata
42+
state.SetCode(addr, []byte("hello"), tracing.CodeChangeUnspecified) // Change an external metadata
4343
state.SetState(addr, skey, sval) // Change the storage trie
4444
for i := 0; i < 100; i++ {
4545
sk := common.BigToHash(big.NewInt(int64(i)))
@@ -81,7 +81,7 @@ func TestVerklePrefetcher(t *testing.T) {
8181
sval := testrand.Hash()
8282

8383
state.SetBalance(addr, uint256.NewInt(42), tracing.BalanceChangeUnspecified) // Change the account trie
84-
state.SetCode(addr, []byte("hello")) // Change an external metadata
84+
state.SetCode(addr, []byte("hello"), tracing.CodeChangeUnspecified) // Change an external metadata
8585
state.SetState(addr, skey, sval) // Change the storage trie
8686
root, _ := state.Commit(0, true, false)
8787

core/state_transition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,12 +617,12 @@ func (st *stateTransition) applyAuthorization(auth *types.SetCodeAuthorization)
617617
st.state.SetNonce(authority, auth.Nonce+1, tracing.NonceChangeAuthorization)
618618
if auth.Address == (common.Address{}) {
619619
// Delegation to zero address means clear.
620-
st.state.SetCode(authority, nil)
620+
st.state.SetCode(authority, nil, tracing.CodeChangeAuthorizationClear)
621621
return nil
622622
}
623623

624624
// Otherwise install delegation to auth.Address.
625-
st.state.SetCode(authority, types.AddressToDelegation(auth.Address))
625+
st.state.SetCode(authority, types.AddressToDelegation(auth.Address), tracing.CodeChangeAuthorization)
626626

627627
return nil
628628
}

0 commit comments

Comments
 (0)