go-ethereum/core/state/journal.go
2026-04-29 18:28:13 +08:00

716 lines
21 KiB
Go

// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package state
import (
"fmt"
"maps"
"slices"
"sort"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
)
// stateBytesPerSlot is the number of "state-creation bytes" billed for a slot
// transitioning from zero to non-zero within a call frame, and refunded when
// such a slot is cleared back to zero whose tx-original value was also zero.
const stateBytesPerSlot = 64
// stateBytesPerAccount is the per-account overhead billed when a brand-new
// account is created in a call frame.
const stateBytesPerAccount = 120
// frameRange is a half-open interval [start, end) of journal entry indices,
// used to record the slice of entries occupied by a closed child call frame.
type frameRange struct {
start, end int
}
type revision struct {
id int
journalIndex int
// closedChildren holds the [start, end) ranges of child call frames that
// have been closed under this revision via closeSnapshot. Together with
// journalIndex (this frame's own start) and the current journal length
// (this frame's tentative end) they describe the slice of entries that
// belong directly to this frame, with descendant frames' entries excluded.
//
// Invariant: ranges are appended in increasing order, are non-overlapping,
// and lie entirely within [journalIndex, len(entries)).
closedChildren []frameRange
// childStateBytes is the sum of state-creation bytes that this frame's
// successful child frames (and their successful descendants, transitively)
// produced via closeSnapshot. It is propagated upwards each time a child
// closes, so that if THIS frame is later reverted, the caller can recover
// the total amount that was emitted for state changes which the revert is
// now throwing away.
childStateBytes int
}
// journalEntry is a modification entry in the state change journal that can be
// reverted on demand.
type journalEntry interface {
// revert undoes the changes introduced by this journal entry.
revert(*StateDB)
// dirtied returns the Ethereum address modified by this journal entry.
// indicates false if no address was changed.
dirtied() (common.Address, bool)
// copy returns a deep-copied journal entry.
copy() journalEntry
}
// journal contains the list of state modifications applied since the last state
// commit. These are tracked to be able to be reverted in the case of an execution
// exception or request for reversal.
type journal struct {
entries []journalEntry // Current changes tracked by the journal
dirties map[common.Address]int // Dirty accounts and the number of changes
validRevisions []revision
nextRevisionId int
}
// newJournal creates a new initialized journal.
func newJournal() *journal {
return &journal{
dirties: make(map[common.Address]int),
}
}
// reset clears the journal, after this operation the journal can be used anew.
// It is semantically similar to calling 'newJournal', but the underlying slices
// can be reused.
func (j *journal) reset() {
j.entries = j.entries[:0]
j.validRevisions = j.validRevisions[:0]
clear(j.dirties)
j.nextRevisionId = 0
}
// snapshot returns an identifier for the current revision of the state.
func (j *journal) snapshot() int {
id := j.nextRevisionId
j.nextRevisionId++
j.validRevisions = append(j.validRevisions, revision{id: id, journalIndex: j.length()})
return id
}
// revertToSnapshot reverts all state changes made since the given revision.
//
// It returns the sum of state-creation bytes that successful child frames
// nested within the reverted scope(s) had previously emitted via
// closeSnapshot. The caller can use this figure to undo whatever bookkeeping
// (e.g. gas charging) it did at the time those bytes were reported, since the
// state changes those bytes were paying for are now being thrown away.
func (j *journal) revertToSnapshot(revid int, s *StateDB) int {
// Find the snapshot in the stack of valid snapshots.
idx := sort.Search(len(j.validRevisions), func(i int) bool {
return j.validRevisions[i].id >= revid
})
if idx == len(j.validRevisions) || j.validRevisions[idx].id != revid {
panic(fmt.Errorf("revision id %v cannot be reverted", revid))
}
snapshot := j.validRevisions[idx].journalIndex
// Sum the child-state-bytes carried by every revision being unwound. When
// revertToSnapshot tears down multiple stacked frames at once, each of
// them may itself have closed children whose bytes were inherited but
// never bubbled further up; collecting all of them here lets the caller
// undo the full subtree's emissions in one go.
var refund int
for i := idx; i < len(j.validRevisions); i++ {
refund += j.validRevisions[i].childStateBytes
}
// Replay the journal to undo changes and remove invalidated snapshots
j.revert(s, snapshot)
j.validRevisions = j.validRevisions[:idx]
return refund
}
// closeSnapshot marks the end of the call frame identified by revid without
// reverting any state. The frame's entry range [snapshot_index, current_length)
// is recorded on its parent revision so callers can later iterate the parent's
// own entries while skipping over closed children (and, transitively, their
// descendants — descendant ranges are absorbed into the closing child's range
// when the descendant itself was closed earlier under that child).
//
// closeSnapshot must be invoked in LIFO order: revid must identify the topmost
// snapshot. It panics otherwise. The corresponding revision is popped, so a
// subsequent revertToSnapshot on the same id is no longer valid.
//
// It returns the net state-creation bytes attributable to THIS frame's own
// storage changes (descendant frames' contributions are excluded — they were
// already reported when the descendants closed). For each storage slot that
// this frame touched directly:
// - if the slot is non-zero now and was zero when the frame first touched
// it, +stateBytesPerSlot is accumulated;
// - if the slot is zero now, was non-zero when the frame first touched it,
// and was zero at the start of the transaction, -stateBytesPerSlot is
// accumulated.
//
// The returned value is also folded into the parent's childStateBytes (along
// with this frame's own childStateBytes) so a future revertToSnapshot on the
// parent can recover the entire subtree's accumulated bytes.
func (j *journal) closeSnapshot(revid int) int {
if len(j.validRevisions) == 0 {
panic(fmt.Errorf("revision id %v cannot be closed: no open snapshot", revid))
}
top := len(j.validRevisions) - 1
if j.validRevisions[top].id != revid {
panic(fmt.Errorf("revision id %v cannot be closed: top is %v",
revid, j.validRevisions[top].id))
}
rev := &j.validRevisions[top]
// Compute net state-creation bytes for THIS frame's own slot changes,
// skipping any entries that lie inside a closed child's range.
thisBytes := j.computeFrameStateBytes(rev)
// Record this frame's range and propagate accumulated bytes to the
// parent. The propagated total is "this frame's own bytes" + "this
// frame's already-accumulated child bytes": from the parent's vantage
// point the whole subtree is now a single closed child.
closed := frameRange{
start: rev.journalIndex,
end: len(j.entries),
}
if top > 0 {
parent := &j.validRevisions[top-1]
if closed.start < closed.end {
parent.closedChildren = append(parent.closedChildren, closed)
}
parent.childStateBytes += thisBytes + rev.childStateBytes
}
// Drop this revision's bookkeeping. The slice is reused by the parent so
// avoid pinning it via the popped tail.
rev.closedChildren = nil
rev.childStateBytes = 0
j.validRevisions = j.validRevisions[:top]
return thisBytes
}
// computeFrameStateBytes walks the entries that belong directly to rev (skipping
// any closed-child ranges) and sums the per-step state-creation contribution of
// each individual SSTORE.
//
// State-creation accounting is the per-step sum of three independent
// contributions, each applied locally to its own journal entry:
//
// 1. storageChange (slot SSTORE):
// - origin != 0 → 0 (rearranging pre-existing storage)
// - prev == 0 && new != 0 → +stateBytesPerSlot (new slot created)
// - prev != 0 && new == 0 → -stateBytesPerSlot (in-tx creation undone)
//
// 2. codeChange (SetCode on an account): a brand-new contract publishes its
// bytecode for the first time. Origin code is implicitly empty in this
// accounting — we treat the prev-empty/new-non-empty transition as the
// creation event and bill its byte size, with the inverse transition
// refunding it for symmetry.
// - len(prev) == 0 && len(new) > 0 → +len(new) (code committed)
// - len(prev) > 0 && len(new) == 0 → -len(prev) (in-tx code committed then cleared)
//
// 3. createObjectChange (account materialised in state): each event adds
// +stateBytesPerAccount of per-account overhead.
//
// The per-step formulation composes naturally: a frame's bytes is the sum of
// deltas of its own entries, and the sum of every frame's bytes across the
// subtree equals the sum of deltas across all entries — i.e. the same number
// you would get from a single whole-frame walk. Slots/code/accounts whose
// intermediate values bounce across frame boundaries reconcile automatically
// without any need to dedup by "first touch".
func (j *journal) computeFrameStateBytes(rev *revision) int {
var total int
zero := common.Hash{}
visit := func(e journalEntry) {
switch ch := e.(type) {
case storageChange:
switch {
case ch.origvalue != zero:
// Slot was already populated at tx-start; any in-tx
// transition is rearranging existing storage.
case ch.prevvalue == zero && ch.newvalue != zero:
total += stateBytesPerSlot
case ch.prevvalue != zero && ch.newvalue == zero:
total -= stateBytesPerSlot
}
case codeChange:
switch {
case len(ch.prevCode) == 0 && len(ch.newCode) > 0:
total += len(ch.newCode)
case len(ch.prevCode) > 0 && len(ch.newCode) == 0:
total -= len(ch.prevCode)
}
case createObjectChange:
total += stateBytesPerAccount
}
}
idx := rev.journalIndex
for _, child := range rev.closedChildren {
for ; idx < child.start; idx++ {
visit(j.entries[idx])
}
idx = child.end
}
for ; idx < len(j.entries); idx++ {
visit(j.entries[idx])
}
return total
}
// frameEntries invokes visit for each entry that belongs directly to the
// current (topmost) call frame, skipping entries that lie within any closed
// child frame's range. Entries are visited in append order. If no frame is
// open, frameEntries is a no-op.
func (j *journal) frameEntries(visit func(entry journalEntry)) {
if len(j.validRevisions) == 0 {
return
}
rev := j.validRevisions[len(j.validRevisions)-1]
idx := rev.journalIndex
for _, child := range rev.closedChildren {
for ; idx < child.start; idx++ {
visit(j.entries[idx])
}
idx = child.end
}
for ; idx < len(j.entries); idx++ {
visit(j.entries[idx])
}
}
// append inserts a new modification entry to the end of the change journal.
func (j *journal) append(entry journalEntry) {
j.entries = append(j.entries, entry)
if addr, dirty := entry.dirtied(); dirty {
j.dirties[addr]++
}
}
// revert undoes a batch of journalled modifications along with any reverted
// dirty handling too.
func (j *journal) revert(statedb *StateDB, snapshot int) {
for i := len(j.entries) - 1; i >= snapshot; i-- {
// Undo the changes made by the operation
j.entries[i].revert(statedb)
// Drop any dirty tracking induced by the change
if addr, dirty := j.entries[i].dirtied(); dirty {
if j.dirties[addr]--; j.dirties[addr] == 0 {
delete(j.dirties, addr)
}
}
}
j.entries = j.entries[:snapshot]
}
// dirty explicitly sets an address to dirty, even if the change entries would
// otherwise suggest it as clean. This method is an ugly hack to handle the RIPEMD
// precompile consensus exception.
func (j *journal) dirty(addr common.Address) {
j.dirties[addr]++
}
// length returns the current number of entries in the journal.
func (j *journal) length() int {
return len(j.entries)
}
// copy returns a deep-copied journal.
func (j *journal) copy() *journal {
entries := make([]journalEntry, 0, j.length())
for i := 0; i < j.length(); i++ {
entries = append(entries, j.entries[i].copy())
}
revisions := make([]revision, len(j.validRevisions))
for i, r := range j.validRevisions {
revisions[i] = revision{
id: r.id,
journalIndex: r.journalIndex,
closedChildren: slices.Clone(r.closedChildren),
childStateBytes: r.childStateBytes,
}
}
return &journal{
entries: entries,
dirties: maps.Clone(j.dirties),
validRevisions: revisions,
nextRevisionId: j.nextRevisionId,
}
}
func (j *journal) logChange(txHash common.Hash) {
j.append(addLogChange{txhash: txHash})
}
func (j *journal) createObject(addr common.Address) {
j.append(createObjectChange{account: addr})
}
func (j *journal) createContract(addr common.Address) {
j.append(createContractChange{account: addr})
}
func (j *journal) destruct(addr common.Address) {
j.append(selfDestructChange{account: addr})
}
func (j *journal) storageChange(addr common.Address, key, prev, newval, origin common.Hash) {
j.append(storageChange{
account: addr,
key: key,
prevvalue: prev,
newvalue: newval,
origvalue: origin,
})
}
func (j *journal) transientStateChange(addr common.Address, key, prev common.Hash) {
j.append(transientStorageChange{
account: addr,
key: key,
prevalue: prev,
})
}
func (j *journal) refundChange(previous uint64) {
j.append(refundChange{prev: previous})
}
func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) {
j.append(balanceChange{
account: addr,
prev: previous.Clone(),
})
}
func (j *journal) setCode(address common.Address, prevCode, newCode []byte) {
j.append(codeChange{
account: address,
prevCode: prevCode,
newCode: newCode,
})
}
func (j *journal) nonceChange(address common.Address, prev uint64) {
j.append(nonceChange{
account: address,
prev: prev,
})
}
func (j *journal) touchChange(address common.Address) {
j.append(touchChange{
account: address,
})
if address == ripemd {
// Explicitly put it in the dirty-cache, which is otherwise generated from
// flattened journals.
j.dirty(address)
}
}
func (j *journal) accessListAddAccount(addr common.Address) {
j.append(accessListAddAccountChange{addr})
}
func (j *journal) accessListAddSlot(addr common.Address, slot common.Hash) {
j.append(accessListAddSlotChange{
address: addr,
slot: slot,
})
}
type (
// Changes to the account trie.
createObjectChange struct {
account common.Address
}
// createContractChange represents an account becoming a contract-account.
// This event happens prior to executing initcode. The journal-event simply
// manages the created-flag, in order to allow same-tx destruction.
createContractChange struct {
account common.Address
}
selfDestructChange struct {
account common.Address
}
// Changes to individual accounts.
balanceChange struct {
account common.Address
prev *uint256.Int
}
nonceChange struct {
account common.Address
prev uint64
}
storageChange struct {
account common.Address
key common.Hash
prevvalue common.Hash
newvalue common.Hash
origvalue common.Hash
}
codeChange struct {
account common.Address
prevCode []byte
newCode []byte
}
// Changes to other state values.
refundChange struct {
prev uint64
}
addLogChange struct {
txhash common.Hash
}
touchChange struct {
account common.Address
}
// Changes to the access list
accessListAddAccountChange struct {
address common.Address
}
accessListAddSlotChange struct {
address common.Address
slot common.Hash
}
// Changes to transient storage
transientStorageChange struct {
account common.Address
key, prevalue common.Hash
}
)
func (ch createObjectChange) revert(s *StateDB) {
delete(s.stateObjects, ch.account)
}
func (ch createObjectChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch createObjectChange) copy() journalEntry {
return createObjectChange{
account: ch.account,
}
}
func (ch createContractChange) revert(s *StateDB) {
s.getStateObject(ch.account).newContract = false
}
func (ch createContractChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch createContractChange) copy() journalEntry {
return createContractChange{
account: ch.account,
}
}
func (ch selfDestructChange) revert(s *StateDB) {
obj := s.getStateObject(ch.account)
if obj != nil {
obj.selfDestructed = false
}
}
func (ch selfDestructChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch selfDestructChange) copy() journalEntry {
return selfDestructChange{
account: ch.account,
}
}
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
func (ch touchChange) revert(s *StateDB) {
}
func (ch touchChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch touchChange) copy() journalEntry {
return touchChange{
account: ch.account,
}
}
func (ch balanceChange) revert(s *StateDB) {
s.getStateObject(ch.account).setBalance(ch.prev)
}
func (ch balanceChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch balanceChange) copy() journalEntry {
return balanceChange{
account: ch.account,
prev: new(uint256.Int).Set(ch.prev),
}
}
func (ch nonceChange) revert(s *StateDB) {
s.getStateObject(ch.account).setNonce(ch.prev)
}
func (ch nonceChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch nonceChange) copy() journalEntry {
return nonceChange{
account: ch.account,
prev: ch.prev,
}
}
func (ch codeChange) revert(s *StateDB) {
s.getStateObject(ch.account).setCode(crypto.Keccak256Hash(ch.prevCode), ch.prevCode)
}
func (ch codeChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch codeChange) copy() journalEntry {
return codeChange{
account: ch.account,
prevCode: ch.prevCode,
newCode: ch.newCode,
}
}
func (ch storageChange) revert(s *StateDB) {
s.getStateObject(ch.account).setState(ch.key, ch.prevvalue, ch.origvalue)
}
func (ch storageChange) dirtied() (common.Address, bool) {
return ch.account, true
}
func (ch storageChange) copy() journalEntry {
return storageChange{
account: ch.account,
key: ch.key,
prevvalue: ch.prevvalue,
newvalue: ch.newvalue,
origvalue: ch.origvalue,
}
}
func (ch transientStorageChange) revert(s *StateDB) {
s.setTransientState(ch.account, ch.key, ch.prevalue)
}
func (ch transientStorageChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch transientStorageChange) copy() journalEntry {
return transientStorageChange{
account: ch.account,
key: ch.key,
prevalue: ch.prevalue,
}
}
func (ch refundChange) revert(s *StateDB) {
s.refund = ch.prev
}
func (ch refundChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch refundChange) copy() journalEntry {
return refundChange{
prev: ch.prev,
}
}
func (ch addLogChange) revert(s *StateDB) {
logs := s.logs[ch.txhash]
if len(logs) == 1 {
delete(s.logs, ch.txhash)
} else {
s.logs[ch.txhash] = logs[:len(logs)-1]
}
s.logSize--
}
func (ch addLogChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch addLogChange) copy() journalEntry {
return addLogChange{
txhash: ch.txhash,
}
}
func (ch accessListAddAccountChange) revert(s *StateDB) {
/*
One important invariant here, is that whenever a (addr, slot) is added, if the
addr is not already present, the add causes two journal entries:
- one for the address,
- one for the (address,slot)
Therefore, when unrolling the change, we can always blindly delete the
(addr) at this point, since no storage adds can remain when come upon
a single (addr) change.
*/
s.accessList.DeleteAddress(ch.address)
}
func (ch accessListAddAccountChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch accessListAddAccountChange) copy() journalEntry {
return accessListAddAccountChange{
address: ch.address,
}
}
func (ch accessListAddSlotChange) revert(s *StateDB) {
s.accessList.DeleteSlot(ch.address, ch.slot)
}
func (ch accessListAddSlotChange) dirtied() (common.Address, bool) {
return common.Address{}, false
}
func (ch accessListAddSlotChange) copy() journalEntry {
return accessListAddSlotChange{
address: ch.address,
slot: ch.slot,
}
}