Skip to content

Commit

Permalink
core/state: simplify codechange journalling
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Jan 26, 2024
1 parent 163f345 commit fc340e5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
16 changes: 7 additions & 9 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package state

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
)

Expand Down Expand Up @@ -145,12 +146,10 @@ func (j *journal) JournalBalanceChange(addr common.Address, previous *uint256.In
})
}

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

func (j *journal) JournalNonceChange(address common.Address, prev uint64) {
Expand Down Expand Up @@ -228,8 +227,7 @@ type (
key, prevalue common.Hash
}
codeChange struct {
account *common.Address
prevcode, prevhash []byte
account *common.Address
}

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

func (ch codeChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
s.getStateObject(*ch.account).setCode(types.EmptyCodeHash, nil)
}

func (ch codeChange) dirtied() *common.Address {
Expand Down
2 changes: 1 addition & 1 deletion core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func (s *stateObject) CodeSize() int {
}

func (s *stateObject) SetCode(codeHash common.Hash, code []byte) {
s.db.journal.JournalSetCode(s.address, s.Code(), s.CodeHash())
s.db.journal.JournalSetCode(s.address)
s.setCode(codeHash, code)
}

Expand Down
6 changes: 6 additions & 0 deletions core/state/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
{
name: "SetCode",
fn: func(a testAction, s *StateDB) {
// SetCode can only be performed in case the addr does
// not already hold code
if c := s.GetCode(addr); len(c) > 0 {
// no-op
return
}
code := make([]byte, 16)
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
Expand Down

0 comments on commit fc340e5

Please sign in to comment.