Skip to content

Commit 137fdb7

Browse files
committed
core/state: simplify codechange journalling
1 parent 8d5ceb6 commit 137fdb7

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

core/state/journal.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"maps"
2121

2222
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/core/types"
2324
"github.com/holiman/uint256"
2425
)
2526

@@ -159,12 +160,8 @@ func (j *journal) JournalBalanceChange(addr common.Address, previous *uint256.In
159160
})
160161
}
161162

162-
func (j *journal) JournalSetCode(address common.Address, prevcode, prevHash []byte) {
163-
j.append(codeChange{
164-
account: &address,
165-
prevhash: prevHash,
166-
prevcode: prevcode,
167-
})
163+
func (j *journal) JournalSetCode(address common.Address) {
164+
j.append(codeChange{account: &address})
168165
}
169166

170167
func (j *journal) JournalNonceChange(address common.Address, prev uint64) {
@@ -220,8 +217,7 @@ type (
220217
origvalue common.Hash
221218
}
222219
codeChange struct {
223-
account *common.Address
224-
prevcode, prevhash []byte
220+
account *common.Address
225221
}
226222

227223
// Changes to other state values.
@@ -348,19 +344,15 @@ func (ch nonceChange) copy() journalEntry {
348344
}
349345

350346
func (ch codeChange) revert(s *StateDB) {
351-
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
347+
s.getStateObject(*ch.account).setCode(types.EmptyCodeHash, nil)
352348
}
353349

354350
func (ch codeChange) dirtied() *common.Address {
355351
return ch.account
356352
}
357353

358354
func (ch codeChange) copy() journalEntry {
359-
return codeChange{
360-
account: ch.account,
361-
prevhash: common.CopyBytes(ch.prevhash),
362-
prevcode: common.CopyBytes(ch.prevcode),
363-
}
355+
return codeChange{account: ch.account}
364356
}
365357

366358
func (ch storageChange) revert(s *StateDB) {

core/state/state_object.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func (s *stateObject) CodeSize() int {
574574
}
575575

576576
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
577-
s.db.journal.JournalSetCode(s.address, s.Code(), s.CodeHash())
577+
s.db.journal.JournalSetCode(s.address)
578578
if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
579579
// TODO remove prevcode from this callback
580580
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)

core/state/statedb_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
372372
{
373373
name: "SetCode",
374374
fn: func(a testAction, s *StateDB) {
375+
// SetCode can only be performed in case the addr does
376+
// not already hold code
377+
if c := s.GetCode(addr); len(c) > 0 {
378+
// no-op
379+
return
380+
}
375381
code := make([]byte, 16)
376382
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
377383
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))

0 commit comments

Comments
 (0)