Skip to content
Open
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
125 changes: 110 additions & 15 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/XinFinOrg/XDPoSChain/common"
Expand All @@ -31,6 +32,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 @@ -85,6 +89,18 @@ 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),
}
}

func (j *journal) createContract(addr common.Address) {
j.append(createContractChange{account: addr})
}
Expand All @@ -107,11 +123,6 @@ type (
createContractChange struct {
account common.Address
}
resetObjectChange struct {
account common.Address
prev *stateObject
prevdestruct bool
}
selfDestructChange struct {
account common.Address
prev bool // whether account had already self-destructed
Expand Down Expand Up @@ -149,6 +160,7 @@ type (
touchChange struct {
account common.Address
}

// Changes to the access list
accessListAddAccountChange struct {
address common.Address
Expand All @@ -158,6 +170,7 @@ type (
slot common.Hash
}

// Changes to transient storage
transientStorageChange struct {
account common.Address
key, prevalue common.Hash
Expand All @@ -166,32 +179,32 @@ 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 createObjectChange) copy() journalEntry {
return createObjectChange{
account: ch.account,
}
}

func (ch createContractChange) revert(s *StateDB) {
s.getStateObject(ch.account).created = false
s.getStateObject(ch.account).newContract = false
}

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

func (ch resetObjectChange) revert(s *StateDB) {
s.setStateObject(ch.prev)
if !ch.prevdestruct {
delete(s.stateObjectsDestruct, ch.prev.address)
func (ch createContractChange) copy() journalEntry {
return createContractChange{
account: ch.account,
}
}

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

func (ch selfDestructChange) revert(s *StateDB) {
obj := s.getStateObject(ch.account)
if obj != nil {
Expand All @@ -204,6 +217,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 @@ -213,6 +234,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 @@ -221,6 +248,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 @@ -229,6 +263,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(crypto.Keccak256Hash(ch.prevCode), ch.prevCode)
}
Expand All @@ -237,6 +278,13 @@ func (ch codeChange) dirtied() *common.Address {
return &ch.account
}

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

func (ch storageChange) revert(s *StateDB) {
s.getStateObject(ch.account).setState(ch.key, ch.prevalue)
}
Expand All @@ -245,6 +293,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 @@ -253,6 +309,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 @@ -261,6 +325,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 @@ -275,6 +345,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 @@ -283,6 +359,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 @@ -300,10 +382,23 @@ func (ch accessListAddAccountChange) dirtied() *common.Address {
return nil
}

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 {
return nil
}

func (ch accessListAddSlotChange) copy() journalEntry {
return accessListAddSlotChange{
address: ch.address,
slot: ch.slot,
}
}
Loading