Skip to content

Commit fc340e5

Browse files
committed
core/state: simplify codechange journalling
1 parent 163f345 commit fc340e5

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

core/state/journal.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package state
1818

1919
import (
2020
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/core/types"
2122
"github.com/holiman/uint256"
2223
)
2324

@@ -145,12 +146,10 @@ func (j *journal) JournalBalanceChange(addr common.Address, previous *uint256.In
145146
})
146147
}
147148

148-
func (j *journal) JournalSetCode(address common.Address, prevcode, prevHash []byte) {
149-
j.append(codeChange{
150-
account: &address,
151-
prevhash: prevHash,
152-
prevcode: prevcode,
153-
})
149+
// JournalSetCode journals the setting of code: it is implicit that the previous
150+
// values were "no code" and emptyCodeHash.
151+
func (j *journal) JournalSetCode(address common.Address) {
152+
j.append(codeChange{account: &address})
154153
}
155154

156155
func (j *journal) JournalNonceChange(address common.Address, prev uint64) {
@@ -228,8 +227,7 @@ type (
228227
key, prevalue common.Hash
229228
}
230229
codeChange struct {
231-
account *common.Address
232-
prevcode, prevhash []byte
230+
account *common.Address
233231
}
234232

235233
// Changes to other state values.
@@ -330,7 +328,7 @@ func (ch nonceChange) dirtied() *common.Address {
330328
}
331329

332330
func (ch codeChange) revert(s *StateDB) {
333-
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
331+
s.getStateObject(*ch.account).setCode(types.EmptyCodeHash, nil)
334332
}
335333

336334
func (ch codeChange) dirtied() *common.Address {

core/state/state_object.go

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

490490
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
491-
s.db.journal.JournalSetCode(s.address, s.Code(), s.CodeHash())
491+
s.db.journal.JournalSetCode(s.address)
492492
s.setCode(codeHash, code)
493493
}
494494

core/state/statedb_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
296296
{
297297
name: "SetCode",
298298
fn: func(a testAction, s *StateDB) {
299+
// SetCode can only be performed in case the addr does
300+
// not already hold code
301+
if c := s.GetCode(addr); len(c) > 0 {
302+
// no-op
303+
return
304+
}
299305
code := make([]byte, 16)
300306
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
301307
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))

0 commit comments

Comments
 (0)