Skip to content

Commit

Permalink
core/state: modify how self-destruct journalling works
Browse files Browse the repository at this point in the history
The self-destruct journalling is a bit strange: we allow the
'selfdestruct' operation to be journalled several times. This makes it
so that we also are forced to store whether the account was
already destructed.
What we can do instead, is to only journal the first destruction, and after
that only journal balance-changes, but not journal the selfdestruct itself.

This simplifies the journalling, so that internals about state management
does not leak into the journal-API.
  • Loading branch information
holiman committed Jan 29, 2024
1 parent 1925ff7 commit 0379c92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 4 additions & 11 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,8 @@ func (j *journal) JournalCreate(addr common.Address) {
j.append(createObjectChange{account: &addr})
}

func (j *journal) JournalDestruct(addr common.Address, previouslyDestructed bool, prevBalance *uint256.Int) {
j.append(selfDestructChange{
account: &addr,
prev: previouslyDestructed,
prevbalance: prevBalance.Clone(),
})
func (j *journal) JournalDestruct(addr common.Address) {
j.append(selfDestructChange{account: &addr})
}

func (j *journal) JournalSetState(addr common.Address, key, prev common.Hash) {
Expand Down Expand Up @@ -241,9 +237,7 @@ type (
prevStorageOrigin map[common.Hash][]byte
}
selfDestructChange struct {
account *common.Address
prev bool // whether account had already self-destructed
prevbalance *uint256.Int
account *common.Address
}

// Changes to individual accounts.
Expand Down Expand Up @@ -326,8 +320,7 @@ func (ch resetObjectChange) dirtied() *common.Address {
func (ch selfDestructChange) revert(s *StateDB) {
obj := s.getStateObject(*ch.account)
if obj != nil {
obj.selfDestructed = ch.prev
obj.setBalance(ch.prevbalance)
obj.selfDestructed = false
}
}

Expand Down
14 changes: 11 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,17 @@ func (s *StateDB) SelfDestruct(addr common.Address) {
if stateObject == nil {
return
}
s.journal.JournalDestruct(addr, stateObject.selfDestructed, stateObject.Balance())
stateObject.markSelfdestructed()
stateObject.data.Balance = new(uint256.Int)
// Regardless of whether it is already destructed or not, we do have to
// journal the balance-change, if we set it to zero here.
if !stateObject.Balance().IsZero() {
stateObject.SetBalance(new(uint256.Int))
}
// If it is already marked as self-destructed, we do not need to add it
// for journalling a second time.
if !stateObject.selfDestructed {
s.journal.JournalDestruct(addr)
stateObject.markSelfdestructed()
}
}

func (s *StateDB) Selfdestruct6780(addr common.Address) {
Expand Down

0 comments on commit 0379c92

Please sign in to comment.