core/state: simplify codechange journalling

This commit is contained in:
Martin Holst Swende 2024-01-26 15:09:40 +01:00
parent 8d5ceb6cad
commit 137fdb7fe2
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
3 changed files with 13 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import (
"maps" "maps"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256" "github.com/holiman/uint256"
) )
@ -159,12 +160,8 @@ func (j *journal) JournalBalanceChange(addr common.Address, previous *uint256.In
}) })
} }
func (j *journal) JournalSetCode(address common.Address, prevcode, prevHash []byte) { func (j *journal) JournalSetCode(address common.Address) {
j.append(codeChange{ j.append(codeChange{account: &address})
account: &address,
prevhash: prevHash,
prevcode: prevcode,
})
} }
func (j *journal) JournalNonceChange(address common.Address, prev uint64) { func (j *journal) JournalNonceChange(address common.Address, prev uint64) {
@ -220,8 +217,7 @@ type (
origvalue common.Hash origvalue common.Hash
} }
codeChange struct { codeChange struct {
account *common.Address account *common.Address
prevcode, prevhash []byte
} }
// Changes to other state values. // Changes to other state values.
@ -348,7 +344,7 @@ func (ch nonceChange) copy() journalEntry {
} }
func (ch codeChange) revert(s *StateDB) { 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 { func (ch codeChange) dirtied() *common.Address {
@ -356,11 +352,7 @@ func (ch codeChange) dirtied() *common.Address {
} }
func (ch codeChange) copy() journalEntry { func (ch codeChange) copy() journalEntry {
return codeChange{ return codeChange{account: ch.account}
account: ch.account,
prevhash: common.CopyBytes(ch.prevhash),
prevcode: common.CopyBytes(ch.prevcode),
}
} }
func (ch storageChange) revert(s *StateDB) { func (ch storageChange) revert(s *StateDB) {

View file

@ -574,7 +574,7 @@ func (s *stateObject) CodeSize() int {
} }
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) { 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)
if s.db.logger != nil && s.db.logger.OnCodeChange != nil { if s.db.logger != nil && s.db.logger.OnCodeChange != nil {
// TODO remove prevcode from this callback // TODO remove prevcode from this callback
s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code) s.db.logger.OnCodeChange(s.address, common.BytesToHash(s.CodeHash()), nil, codeHash, code)

View file

@ -372,6 +372,12 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction {
{ {
name: "SetCode", name: "SetCode",
fn: func(a testAction, s *StateDB) { 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) code := make([]byte, 16)
binary.BigEndian.PutUint64(code, uint64(a.args[0])) binary.BigEndian.PutUint64(code, uint64(a.args[0]))
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1])) binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))