Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
152 changes: 120 additions & 32 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package state

import (
"maps"
"math/big"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -30,6 +31,9 @@ type journalEntry interface {

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

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

// journal contains the list of state modifications applied since the last state
Expand Down Expand Up @@ -84,25 +88,30 @@ 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())
}
return &journal{
entries: entries,
dirties: maps.Clone(j.dirties),
}
}

type (
// Changes to the account trie.
createObjectChange struct {
account *common.Address
}
resetObjectChange struct {
account *common.Address
prev *stateObject
prevdestruct bool

// tracking previous states of accounts and storages in snapshot, before each transaction
prevAccount []byte
prevStorage map[common.Hash][]byte

// tracking previous states of accounts and storages in trie, before each commit
prevAccountOriginExist bool
prevAccountOrigin []byte
prevStorageOrigin map[common.Hash][]byte
// 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
prev bool // whether account had already self-destructed
Expand Down Expand Up @@ -157,34 +166,30 @@ type (

func (ch createObjectChange) revert(s *StateDB) {
delete(s.stateObjects, *ch.account)
delete(s.stateObjectsDirty, *ch.account)
}

func (ch createObjectChange) dirtied() *common.Address {
return ch.account
}

func (ch resetObjectChange) revert(s *StateDB) {
s.setStateObject(ch.prev)
if !ch.prevdestruct && s.snap != nil {
delete(s.stateObjectsDestruct, ch.prev.address)
}
if ch.prevAccountOriginExist {
s.accountsOrigin[ch.prev.address] = ch.prevAccountOrigin
}
if ch.prevStorageOrigin != nil {
s.storagesOrigin[ch.prev.address] = ch.prevStorageOrigin
}
if ch.prevAccount != nil {
s.accounts[ch.prev.addrHash] = ch.prevAccount
}
if ch.prevStorage != nil {
s.storages[ch.prev.addrHash] = ch.prevStorage
func (ch createObjectChange) copy() journalEntry {
return createObjectChange{
account: ch.account,
}
}

func (ch resetObjectChange) dirtied() *common.Address {
return ch.account
func (ch createContractChange) revert(s *StateDB) {
s.getStateObject(ch.account).newContract = false
}

func (ch createContractChange) dirtied() *common.Address {
return nil
}

func (ch createContractChange) copy() journalEntry {
return createContractChange{
account: ch.account,
}
}

func (ch selfDestructChange) revert(s *StateDB) {
Expand All @@ -199,6 +204,14 @@ func (ch selfDestructChange) dirtied() *common.Address {
return ch.account
}

func (ch selfDestructChange) copy() journalEntry {
return selfDestructChange{
account: ch.account,
prev: ch.prev,
prevbalance: new(big.Int).Set(ch.prevbalance),
}
}

var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")

func (ch touchChange) revert(s *StateDB) {
Expand All @@ -208,6 +221,12 @@ func (ch touchChange) dirtied() *common.Address {
return ch.account
}

func (ch touchChange) copy() journalEntry {
return touchChange{
account: ch.account,
}
}

func (ch balanceChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setBalance(ch.prev)
}
Expand All @@ -216,6 +235,13 @@ func (ch balanceChange) dirtied() *common.Address {
return ch.account
}

func (ch balanceChange) copy() journalEntry {
return balanceChange{
account: ch.account,
prev: new(big.Int).Set(ch.prev),
}
}

func (ch nonceChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setNonce(ch.prev)
}
Expand All @@ -224,6 +250,13 @@ func (ch nonceChange) dirtied() *common.Address {
return ch.account
}

func (ch nonceChange) copy() journalEntry {
return nonceChange{
account: ch.account,
prev: ch.prev,
}
}

func (ch codeChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
}
Expand All @@ -232,6 +265,14 @@ func (ch codeChange) dirtied() *common.Address {
return ch.account
}

func (ch codeChange) copy() journalEntry {
return codeChange{
account: ch.account,
prevhash: common.CopyBytes(ch.prevhash),
prevcode: common.CopyBytes(ch.prevcode),
}
}

func (ch storageChange) revert(s *StateDB) {
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
}
Expand All @@ -240,6 +281,14 @@ func (ch storageChange) dirtied() *common.Address {
return ch.account
}

func (ch storageChange) copy() journalEntry {
return storageChange{
account: ch.account,
key: ch.key,
prevalue: ch.prevalue,
}
}

func (ch transientStorageChange) revert(s *StateDB) {
s.setTransientState(*ch.account, ch.key, ch.prevalue)
}
Expand All @@ -248,6 +297,14 @@ func (ch transientStorageChange) dirtied() *common.Address {
return nil
}

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
}
Expand All @@ -256,6 +313,12 @@ func (ch refundChange) dirtied() *common.Address {
return nil
}

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 {
Expand All @@ -270,6 +333,12 @@ func (ch addLogChange) dirtied() *common.Address {
return nil
}

func (ch addLogChange) copy() journalEntry {
return addLogChange{
txhash: ch.txhash,
}
}

func (ch addPreimageChange) revert(s *StateDB) {
delete(s.preimages, ch.hash)
}
Expand All @@ -278,6 +347,12 @@ func (ch addPreimageChange) dirtied() *common.Address {
return nil
}

func (ch addPreimageChange) copy() journalEntry {
return addPreimageChange{
hash: ch.hash,
}
}

func (ch accessListAddAccountChange) revert(s *StateDB) {
/*
One important invariant here, is that whenever a (addr, slot) is added, if the
Expand All @@ -299,6 +374,19 @@ func (ch accessListAddSlotChange) revert(s *StateDB) {
s.accessList.DeleteSlot(*ch.address, *ch.slot)
}

func (ch accessListAddAccountChange) copy() journalEntry {
return accessListAddAccountChange{
address: ch.address,
}
}

func (ch accessListAddSlotChange) dirtied() *common.Address {
return nil
}

func (ch accessListAddSlotChange) copy() journalEntry {
return accessListAddSlotChange{
address: ch.address,
slot: ch.slot,
}
}
55 changes: 24 additions & 31 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,8 @@ import (

var emptyCodeHash = crypto.Keccak256(nil)

type Code []byte

func (c Code) String() string {
return string(c) //strings.Join(Disassemble(c), " ")
}

type Storage map[common.Hash]common.Hash

func (s Storage) String() (str string) {
for key, value := range s {
str += fmt.Sprintf("%X : %X\n", key, value)
}

return
}

func (s Storage) Copy() Storage {
cpy := make(Storage)
for key, value := range s {
Expand Down Expand Up @@ -80,8 +66,8 @@ type stateObject struct {
dbErr error

// Write caches.
trie Trie // storage trie, which becomes non-nil on first access
code Code // contract bytecode, which gets set when code is loaded
trie Trie // storage trie, which becomes non-nil on first access
code []byte // contract bytecode, which gets set when code is loaded

originStorage Storage // Storage cache of original entries to dedup rewrites
pendingStorage Storage // Storage entries that need to be flushed to disk, at the end of an entire block
Expand All @@ -91,17 +77,16 @@ type stateObject struct {
// Cache flags.
dirtyCode bool // true if the code was updated

// Flag whether the account was marked as selfDestructed. The selfDestructed account
// is still accessible in the scope of same transaction.
// Flag whether the account was marked as self-destructed. The self-destructed
// account is still accessible in the scope of same transaction.
selfDestructed bool

// Flag whether the account was marked as deleted. The selfDestructed account
// or the account is considered as empty will be marked as deleted at
// the end of transaction and no longer accessible anymore.
deleted bool

// Flag whether the object was created in the current transaction
created bool
// This is an EIP-6780 flag indicating whether the object is eligible for
// self-destruct according to EIP-6780. The flag could be set either when
// the contract is just created within the current transaction, or when the
// object was previously existent and is being deployed as a contract within
// the current transaction.
newContract bool
}

// empty returns whether the account is considered empty.
Expand Down Expand Up @@ -332,6 +317,10 @@ func (s *stateObject) finalise(prefetch bool) {
if len(s.dirtyStorage) > 0 {
s.dirtyStorage = make(Storage)
}
// Revoke the flag at the end of the transaction. It finalizes the status
// of the newly-created object as it's no longer eligible for self-destruct
// by EIP-6780. For non-newly-created objects, it's a no-op.
s.newContract = false
}

// updateTrie writes cached storage modifications into the object's storage trie.
Expand Down Expand Up @@ -518,12 +507,12 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
stateObject.trie = db.db.CopyTrie(s.trie)
}
stateObject.code = s.code
stateObject.dirtyStorage = s.dirtyStorage.Copy()
stateObject.originStorage = s.originStorage.Copy()
stateObject.pendingStorage = s.pendingStorage.Copy()
stateObject.selfDestructed = s.selfDestructed
stateObject.dirtyStorage = s.dirtyStorage.Copy()
stateObject.dirtyCode = s.dirtyCode
stateObject.deleted = s.deleted
stateObject.selfDestructed = s.selfDestructed
stateObject.newContract = s.newContract
return stateObject
}

Expand All @@ -538,7 +527,7 @@ func (s *stateObject) Address() common.Address {

// Code returns the contract code associated with this object, if any.
func (s *stateObject) Code() []byte {
if s.code != nil {
if len(s.code) != 0 {
return s.code
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
Expand All @@ -556,7 +545,7 @@ func (s *stateObject) Code() []byte {
// or zero if none. This method is an almost mirror of Code, but uses a cache
// inside the database to avoid loading codes seen recently.
func (s *stateObject) CodeSize(db Database) int {
if s.code != nil {
if len(s.code) != 0 {
return len(s.code)
}
if bytes.Equal(s.CodeHash(), emptyCodeHash) {
Expand Down Expand Up @@ -610,7 +599,11 @@ func (s *stateObject) Nonce() uint64 {
return s.data.Nonce
}

// Never called, but must be present to allow stateObject to be used
func (s *stateObject) Root() common.Hash {
return s.data.Root
}

// Value Never called, but must be present to allow stateObject to be used
// as a vm.Account interface that also satisfies the vm.ContractRef
// interface. Interfaces are awesome.
func (s *stateObject) Value() *big.Int {
Expand Down
Loading
Loading