core/state: fix journal to account for setcode over non-empty code

This commit is contained in:
Martin Holst Swende 2024-12-02 14:16:07 +01:00 committed by lightclient
parent 3764d420c2
commit 5fccdfbfd3
No known key found for this signature in database
GPG key ID: 75C916AFEE20183E
2 changed files with 14 additions and 7 deletions

View file

@ -23,7 +23,7 @@ import (
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
)
@ -192,8 +192,11 @@ func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) {
})
}
func (j *journal) setCode(address common.Address) {
j.append(codeChange{account: address})
func (j *journal) setCode(address common.Address, prevCode []byte) {
j.append(codeChange{
account: address,
prevCode: prevCode,
})
}
func (j *journal) nonceChange(address common.Address, prev uint64) {
@ -257,6 +260,7 @@ type (
}
codeChange struct {
account common.Address
prevCode []byte
}
// Changes to other state values.
@ -377,7 +381,7 @@ func (ch nonceChange) copy() journalEntry {
}
func (ch codeChange) revert(s *StateDB) {
s.getStateObject(ch.account).setCode(types.EmptyCodeHash, nil)
s.getStateObject(ch.account).setCode(crypto.Keccak256Hash(ch.prevCode), ch.prevCode)
}
func (ch codeChange) dirtied() *common.Address {
@ -385,7 +389,10 @@ func (ch codeChange) dirtied() *common.Address {
}
func (ch codeChange) copy() journalEntry {
return codeChange{account: ch.account}
return codeChange{
account: ch.account,
prevCode: ch.prevCode,
}
}
func (ch storageChange) revert(s *StateDB) {

View file

@ -543,8 +543,8 @@ func (s *stateObject) CodeSize() int {
}
func (s *stateObject) SetCode(codeHash common.Hash, code []byte) (prev []byte) {
s.db.journal.setCode(s.address)
prev = slices.Clone(s.code)
s.db.journal.setCode(s.address, prev)
s.setCode(codeHash, code)
return prev
}