2016-10-04 10:36:02 +00:00
|
|
|
// 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 (
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// journalEntry is a modification entry in the state change journal that can be
|
|
|
|
// reverted on demand.
|
2016-10-04 10:36:02 +00:00
|
|
|
type journalEntry interface {
|
2018-03-27 12:13:30 +00:00
|
|
|
// revert undoes the changes introduced by this journal entry.
|
|
|
|
revert(*StateDB)
|
|
|
|
|
|
|
|
// dirtied returns the Ethereum address modified by this journal entry.
|
|
|
|
dirtied() *common.Address
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// journal contains the list of state modifications applied since the last state
|
2021-10-10 13:00:00 +00:00
|
|
|
// commit. These are tracked to be able to be reverted in the case of an execution
|
|
|
|
// exception or request for reversal.
|
2017-10-01 19:07:30 +00:00
|
|
|
type journal struct {
|
2018-03-27 12:13:30 +00:00
|
|
|
entries []journalEntry // Current changes tracked by the journal
|
|
|
|
dirties map[common.Address]int // Dirty accounts and the number of changes
|
|
|
|
}
|
|
|
|
|
2021-10-10 13:00:00 +00:00
|
|
|
// newJournal creates a new initialized journal.
|
2018-03-27 12:13:30 +00:00
|
|
|
func newJournal() *journal {
|
|
|
|
return &journal{
|
|
|
|
dirties: make(map[common.Address]int),
|
|
|
|
}
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// append inserts a new modification entry to the end of the change journal.
|
2017-10-01 19:07:30 +00:00
|
|
|
func (j *journal) append(entry journalEntry) {
|
|
|
|
j.entries = append(j.entries, entry)
|
2018-03-27 12:13:30 +00:00
|
|
|
if addr := entry.dirtied(); addr != nil {
|
|
|
|
j.dirties[*addr]++
|
|
|
|
}
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// 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)
|
2017-10-01 19:07:30 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// Drop any dirty tracking induced by the change
|
|
|
|
if addr := j.entries[i].dirtied(); addr != nil {
|
|
|
|
if j.dirties[*addr]--; j.dirties[*addr] == 0 {
|
|
|
|
delete(j.dirties, *addr)
|
|
|
|
}
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
j.entries = j.entries[:snapshot]
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// 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]++
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
// length returns the current number of entries in the journal.
|
|
|
|
func (j *journal) length() int {
|
|
|
|
return len(j.entries)
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
|
|
|
type (
|
|
|
|
// Changes to the account trie.
|
|
|
|
createObjectChange struct {
|
|
|
|
account *common.Address
|
|
|
|
}
|
|
|
|
resetObjectChange struct {
|
2023-06-01 09:09:32 +00:00
|
|
|
account *common.Address
|
2020-03-06 12:05:44 +00:00
|
|
|
prev *stateObject
|
|
|
|
prevdestruct bool
|
2023-06-05 13:25:57 +00:00
|
|
|
prevAccount []byte
|
|
|
|
prevStorage map[common.Hash][]byte
|
2023-07-11 13:43:23 +00:00
|
|
|
|
|
|
|
prevAccountOriginExist bool
|
|
|
|
prevAccountOrigin []byte
|
|
|
|
prevStorageOrigin map[common.Hash][]byte
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
2016-10-05 20:22:31 +00:00
|
|
|
suicideChange struct {
|
2016-10-04 10:36:02 +00:00
|
|
|
account *common.Address
|
|
|
|
prev bool // whether account had already suicided
|
|
|
|
prevbalance *big.Int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes to individual accounts.
|
|
|
|
balanceChange struct {
|
|
|
|
account *common.Address
|
|
|
|
prev *big.Int
|
|
|
|
}
|
|
|
|
nonceChange struct {
|
|
|
|
account *common.Address
|
|
|
|
prev uint64
|
|
|
|
}
|
|
|
|
storageChange struct {
|
|
|
|
account *common.Address
|
|
|
|
key, prevalue common.Hash
|
|
|
|
}
|
|
|
|
codeChange struct {
|
|
|
|
account *common.Address
|
|
|
|
prevcode, prevhash []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// Changes to other state values.
|
|
|
|
refundChange struct {
|
2017-11-13 11:47:27 +00:00
|
|
|
prev uint64
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
|
|
|
addLogChange struct {
|
|
|
|
txhash common.Hash
|
|
|
|
}
|
2017-01-17 11:19:50 +00:00
|
|
|
addPreimageChange struct {
|
|
|
|
hash common.Hash
|
|
|
|
}
|
2016-11-24 15:24:04 +00:00
|
|
|
touchChange struct {
|
2019-11-22 14:56:05 +00:00
|
|
|
account *common.Address
|
2016-11-24 15:24:04 +00:00
|
|
|
}
|
2020-10-23 06:26:57 +00:00
|
|
|
// Changes to the access list
|
|
|
|
accessListAddAccountChange struct {
|
|
|
|
address *common.Address
|
|
|
|
}
|
|
|
|
accessListAddSlotChange struct {
|
|
|
|
address *common.Address
|
|
|
|
slot *common.Hash
|
|
|
|
}
|
2022-11-16 09:18:52 +00:00
|
|
|
|
|
|
|
transientStorageChange struct {
|
|
|
|
account *common.Address
|
|
|
|
key, prevalue common.Hash
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
)
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch createObjectChange) revert(s *StateDB) {
|
2016-10-04 10:36:02 +00:00
|
|
|
delete(s.stateObjects, *ch.account)
|
|
|
|
delete(s.stateObjectsDirty, *ch.account)
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch createObjectChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch resetObjectChange) revert(s *StateDB) {
|
2016-10-04 10:36:02 +00:00
|
|
|
s.setStateObject(ch.prev)
|
2022-12-28 13:53:43 +00:00
|
|
|
if !ch.prevdestruct {
|
|
|
|
delete(s.stateObjectsDestruct, ch.prev.address)
|
2020-03-06 12:05:44 +00:00
|
|
|
}
|
2023-06-05 13:25:57 +00:00
|
|
|
if ch.prevAccount != nil {
|
2023-07-11 13:43:23 +00:00
|
|
|
s.accounts[ch.prev.addrHash] = ch.prevAccount
|
2023-06-05 13:25:57 +00:00
|
|
|
}
|
|
|
|
if ch.prevStorage != nil {
|
2023-07-11 13:43:23 +00:00
|
|
|
s.storages[ch.prev.addrHash] = ch.prevStorage
|
|
|
|
}
|
|
|
|
if ch.prevAccountOriginExist {
|
|
|
|
s.accountsOrigin[ch.prev.addrHash] = ch.prevAccountOrigin
|
|
|
|
}
|
|
|
|
if ch.prevStorageOrigin != nil {
|
|
|
|
s.storagesOrigin[ch.prev.addrHash] = ch.prevStorageOrigin
|
2023-06-05 13:25:57 +00:00
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch resetObjectChange) dirtied() *common.Address {
|
2023-06-01 09:09:32 +00:00
|
|
|
return ch.account
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch suicideChange) revert(s *StateDB) {
|
2017-02-22 22:29:59 +00:00
|
|
|
obj := s.getStateObject(*ch.account)
|
2016-10-04 10:36:02 +00:00
|
|
|
if obj != nil {
|
2016-10-05 20:22:31 +00:00
|
|
|
obj.suicided = ch.prev
|
2016-10-04 10:36:02 +00:00
|
|
|
obj.setBalance(ch.prevbalance)
|
|
|
|
}
|
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch suicideChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
2016-11-24 15:24:04 +00:00
|
|
|
var ripemd = common.HexToAddress("0000000000000000000000000000000000000003")
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch touchChange) revert(s *StateDB) {
|
2017-10-01 19:07:30 +00:00
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch touchChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
2016-11-24 15:24:04 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch balanceChange) revert(s *StateDB) {
|
2017-02-22 22:29:59 +00:00
|
|
|
s.getStateObject(*ch.account).setBalance(ch.prev)
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch balanceChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch nonceChange) revert(s *StateDB) {
|
2017-02-22 22:29:59 +00:00
|
|
|
s.getStateObject(*ch.account).setNonce(ch.prev)
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch nonceChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch codeChange) revert(s *StateDB) {
|
2017-02-22 22:29:59 +00:00
|
|
|
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch codeChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch storageChange) revert(s *StateDB) {
|
2017-02-22 22:29:59 +00:00
|
|
|
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch storageChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
2022-11-16 09:18:52 +00:00
|
|
|
func (ch transientStorageChange) revert(s *StateDB) {
|
|
|
|
s.setTransientState(*ch.account, ch.key, ch.prevalue)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch transientStorageChange) dirtied() *common.Address {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch refundChange) revert(s *StateDB) {
|
2016-10-04 10:36:02 +00:00
|
|
|
s.refund = ch.prev
|
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch refundChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-04 10:36:02 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch addLogChange) revert(s *StateDB) {
|
2016-10-04 10:36:02 +00:00
|
|
|
logs := s.logs[ch.txhash]
|
|
|
|
if len(logs) == 1 {
|
|
|
|
delete(s.logs, ch.txhash)
|
|
|
|
} else {
|
|
|
|
s.logs[ch.txhash] = logs[:len(logs)-1]
|
|
|
|
}
|
2017-09-21 18:44:46 +00:00
|
|
|
s.logSize--
|
2016-10-04 10:36:02 +00:00
|
|
|
}
|
2018-03-27 12:13:30 +00:00
|
|
|
|
|
|
|
func (ch addLogChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-01-17 11:19:50 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch addPreimageChange) revert(s *StateDB) {
|
2017-01-17 11:19:50 +00:00
|
|
|
delete(s.preimages, ch.hash)
|
|
|
|
}
|
2017-10-01 19:07:30 +00:00
|
|
|
|
2018-03-27 12:13:30 +00:00
|
|
|
func (ch addPreimageChange) dirtied() *common.Address {
|
2017-10-01 19:07:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-23 06:26:57 +00:00
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch accessListAddSlotChange) revert(s *StateDB) {
|
|
|
|
s.accessList.DeleteSlot(*ch.address, *ch.slot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ch accessListAddSlotChange) dirtied() *common.Address {
|
|
|
|
return nil
|
|
|
|
}
|