Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/state, core/vm: set-based per-scope journalling #30500

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
}
continue
}
statedb.DiscardSnapshot(snapshot)
includedTxs = append(includedTxs, tx)
if hashError != nil {
return nil, nil, nil, NewError(ErrorMissingBlockhash, hashError)
Expand Down
3 changes: 2 additions & 1 deletion cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ func runCmd(ctx *cli.Context) error {
sdb := state.NewDatabase(triedb, nil)
statedb, _ = state.New(genesis.Root(), sdb)
chainConfig = genesisConfig.Config

id := statedb.Snapshot()
defer statedb.DiscardSnapshot(id)
if ctx.String(SenderFlag.Name) != "" {
sender = common.HexToAddress(ctx.String(SenderFlag.Name))
}
Expand Down
1 change: 1 addition & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database) (common.Hash, e
if err != nil {
return common.Hash{}, err
}

for addr, account := range *ga {
if account.Balance != nil {
// This is not actually logged via tracer because OnGenesisBlock
Expand Down
100 changes: 59 additions & 41 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,73 +32,91 @@ type revision struct {
journalIndex int
}

// journalEntry is a modification entry in the state change journal that can be
// journalEntry is a modification entry in the state change linear journal that can be
// reverted on demand.
type journalEntry interface {
// revert undoes the changes introduced by this journal entry.
// revert undoes the changes introduced by this entry.
revert(*StateDB)

// dirtied returns the Ethereum address modified by this journal entry.
// dirtied returns the Ethereum address modified by this entry.
dirtied() *common.Address

// copy returns a deep-copied journal entry.
// copy returns a deep-copied entry.
copy() journalEntry
}

// journal contains the list of state modifications applied since the last state
// linearJournal 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
type linearJournal struct {
entries []journalEntry // Current changes tracked by the linearJournal
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{
// compile-time interface check
var _ journal = (*linearJournal)(nil)

// newLinearJournal creates a new initialized linearJournal.
func newLinearJournal() *linearJournal {
return &linearJournal{
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() {
func (j *linearJournal) reset() {
j.entries = j.entries[:0]
j.validRevisions = j.validRevisions[:0]
clear(j.dirties)
j.nextRevisionId = 0
}

func (j linearJournal) dirtyAccounts() []common.Address {
dirty := make([]common.Address, 0, len(j.dirties))
// flatten into list
for addr := range j.dirties {
dirty = append(dirty, addr)
}
return dirty
}

// snapshot returns an identifier for the current revision of the state.
func (j *journal) snapshot() int {
func (j *linearJournal) snapshot() int {
id := j.nextRevisionId
j.nextRevisionId++
j.validRevisions = append(j.validRevisions, revision{id, j.length()})
return id
}

// revertToSnapshot reverts all state changes made since the given revision.
func (j *journal) revertToSnapshot(revid int, s *StateDB) {
func (j *linearJournal) revertToSnapshot(revid int, s *StateDB) {
// 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))
panic(fmt.Errorf("revision id %v cannot be reverted (valid revisions: %d)", revid, len(j.validRevisions)))
}
snapshot := j.validRevisions[idx].journalIndex

// Replay the journal to undo changes and remove invalidated snapshots
// Replay the linearJournal to undo changes and remove invalidated snapshots
j.revert(s, snapshot)
j.validRevisions = j.validRevisions[:idx]
}

// append inserts a new modification entry to the end of the change journal.
func (j *journal) append(entry journalEntry) {
// DiscardSnapshot removes the snapshot with the given id; after calling this
// method, it is no longer possible to revert to that particular snapshot, the
// changes are considered part of the parent scope.
func (j *linearJournal) DiscardSnapshot(id int) {
//
}

// append inserts a new modification entry to the end of the change linearJournal.
func (j *linearJournal) append(entry journalEntry) {
j.entries = append(j.entries, entry)
if addr := entry.dirtied(); addr != nil {
j.dirties[*addr]++
Expand All @@ -107,7 +125,7 @@ func (j *journal) append(entry journalEntry) {

// revert undoes a batch of journalled modifications along with any reverted
// dirty handling too.
func (j *journal) revert(statedb *StateDB, snapshot int) {
func (j *linearJournal) 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)
Expand All @@ -125,46 +143,46 @@ func (j *journal) revert(statedb *StateDB, snapshot int) {
// 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) {
func (j *linearJournal) dirty(addr common.Address) {
j.dirties[addr]++
}

// length returns the current number of entries in the journal.
func (j *journal) length() int {
// length returns the current number of entries in the linearJournal.
func (j *linearJournal) length() int {
return len(j.entries)
}

// copy returns a deep-copied journal.
func (j *journal) copy() *journal {
func (j *linearJournal) copy() journal {
entries := make([]journalEntry, 0, j.length())
for i := 0; i < j.length(); i++ {
entries = append(entries, j.entries[i].copy())
}
return &journal{
return &linearJournal{
entries: entries,
dirties: maps.Clone(j.dirties),
validRevisions: slices.Clone(j.validRevisions),
nextRevisionId: j.nextRevisionId,
}
}

func (j *journal) logChange(txHash common.Hash) {
func (j *linearJournal) logChange(txHash common.Hash) {
j.append(addLogChange{txhash: txHash})
}

func (j *journal) createObject(addr common.Address) {
func (j *linearJournal) createObject(addr common.Address) {
j.append(createObjectChange{account: addr})
}

func (j *journal) createContract(addr common.Address) {
func (j *linearJournal) createContract(addr common.Address, account *types.StateAccount) {
j.append(createContractChange{account: addr})
}

func (j *journal) destruct(addr common.Address) {
func (j *linearJournal) destruct(addr common.Address, account *types.StateAccount) {
j.append(selfDestructChange{account: addr})
}

func (j *journal) storageChange(addr common.Address, key, prev, origin common.Hash) {
func (j *linearJournal) storageChange(addr common.Address, key, prev, origin common.Hash) {
j.append(storageChange{
account: addr,
key: key,
Expand All @@ -173,37 +191,37 @@ func (j *journal) storageChange(addr common.Address, key, prev, origin common.Ha
})
}

func (j *journal) transientStateChange(addr common.Address, key, prev common.Hash) {
func (j *linearJournal) transientStateChange(addr common.Address, key, prev common.Hash) {
j.append(transientStorageChange{
account: addr,
key: key,
prevalue: prev,
})
}

func (j *journal) refundChange(previous uint64) {
func (j *linearJournal) refundChange(previous uint64) {
j.append(refundChange{prev: previous})
}

func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) {
func (j *linearJournal) balanceChange(addr common.Address, account *types.StateAccount, destructed, newContract bool) {
j.append(balanceChange{
account: addr,
prev: previous.Clone(),
prev: account.Balance.Clone(),
})
}

func (j *journal) setCode(address common.Address) {
func (j *linearJournal) setCode(address common.Address, account *types.StateAccount) {
j.append(codeChange{account: address})
}

func (j *journal) nonceChange(address common.Address, prev uint64) {
func (j *linearJournal) nonceChange(address common.Address, account *types.StateAccount, destructed, newContract bool) {
j.append(nonceChange{
account: address,
prev: prev,
prev: account.Nonce,
})
}

func (j *journal) touchChange(address common.Address) {
func (j *linearJournal) touchChange(address common.Address, account *types.StateAccount, destructed, newContract bool) {
j.append(touchChange{
account: address,
})
Expand All @@ -214,11 +232,11 @@ func (j *journal) touchChange(address common.Address) {
}
}

func (j *journal) accessListAddAccount(addr common.Address) {
func (j *linearJournal) accessListAddAccount(addr common.Address) {
j.append(accessListAddAccountChange{addr})
}

func (j *journal) accessListAddSlot(addr common.Address, slot common.Hash) {
func (j *linearJournal) accessListAddSlot(addr common.Address, slot common.Hash) {
j.append(accessListAddSlotChange{
address: addr,
slot: slot,
Expand All @@ -231,7 +249,7 @@ type (
account common.Address
}
// createContractChange represents an account becoming a contract-account.
// This event happens prior to executing initcode. The journal-event simply
// This event happens prior to executing initcode. The linearJournal-event simply
// manages the created-flag, in order to allow same-tx destruction.
createContractChange struct {
account common.Address
Expand Down Expand Up @@ -457,7 +475,7 @@ func (ch addLogChange) copy() journalEntry {
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:
addr is not already present, the add causes two linearJournal entries:
- one for the address,
- one for the (address,slot)
Therefore, when unrolling the change, we can always blindly delete the
Expand Down
86 changes: 86 additions & 0 deletions core/state/journal_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package state

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

type journal interface {
// snapshot returns an identifier for the current revision of the state.
// The lifeycle of journalling is as follows:
// - snapshot() starts a 'scope'.
// - Tee method snapshot() may be called any number of times.
// - For each call to snapshot, there should be a corresponding call to end
// the scope via either of:
// - revertToSnapshot, which undoes the changes in the scope, or
// - discardSnapshot, which discards the ability to revert the changes in the scope.
// - This operation might merge the changes into the parent scope.
// If it does not merge the changes into the parent scope, it must create
// a new snapshot internally, in order to ensure that order of changes
// remains intact.
snapshot() int

// revertToSnapshot reverts all state changes made since the given revision.
revertToSnapshot(revid int, s *StateDB)

// reset clears the journal so it can be reused.
reset()

// DiscardSnapshot removes the snapshot with the given id; after calling this
// method, it is no longer possible to revert to that particular snapshot, the
// changes are considered part of the parent scope.
DiscardSnapshot(revid int)

// dirtyAccounts returns a list of all accounts modified in this journal
dirtyAccounts() []common.Address

// accessListAddAccount journals the adding of addr to the access list
accessListAddAccount(addr common.Address)

// accessListAddSlot journals the adding of addr/slot to the access list
accessListAddSlot(addr common.Address, slot common.Hash)

// logChange journals the adding of a log related to the txHash
logChange(txHash common.Hash)

// createObject journals the event of a new account created in the trie.
createObject(addr common.Address)

// createContract journals the creation of a new contract at addr.
// OBS: This method must not be applied twice, it assumes that the pre-state
// (i.e the rollback-state) is non-created.
createContract(addr common.Address, account *types.StateAccount)

// destruct journals the destruction of an account in the trie.
// pre-state (i.e the rollback-state) is non-destructed (and, for the purpose
// of EIP-XXX (TODO lookup), created in this tx).
destruct(addr common.Address, account *types.StateAccount)

// storageChange journals a change in the storage data related to addr.
// It records the key and previous value of the slot.
storageChange(addr common.Address, key, prev, origin common.Hash)

// transientStateChange journals a change in the t-storage data related to addr.
// It records the key and previous value of the slot.
transientStateChange(addr common.Address, key, prev common.Hash)

// refundChange journals that the refund has been changed, recording the previous value.
refundChange(previous uint64)

// balanceChange journals that the balance of addr has been changed, recording the previous value
balanceChange(addr common.Address, account *types.StateAccount, destructed, newContract bool)

// setCode journals that the code of addr has been set.
// OBS: This method must not be applied twice -- it always assumes that the
// pre-state (i.e the rollback-state) is "no code".
setCode(addr common.Address, account *types.StateAccount)

// nonceChange journals that the nonce of addr was changed, recording the previous value.
nonceChange(addr common.Address, account *types.StateAccount, destructed, newContract bool)

// touchChange journals that the account at addr was touched during execution.
touchChange(addr common.Address, account *types.StateAccount, destructed, newContract bool)

// copy returns a deep-copied journal.
copy() journal
}
Loading