2022-01-05 07:28:27 +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 statedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"math/big"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// 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)
|
2022-01-05 07:28:27 +00:00
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// Dirtied returns the Ethereum address modified by this journal entry.
|
|
|
|
Dirtied() *common.Address
|
2022-01-05 07:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2022-10-10 10:38:33 +00:00
|
|
|
entries []JournalEntry // Current changes tracked by the journal
|
2022-01-05 07:28:27 +00:00
|
|
|
dirties map[common.Address]int // Dirty accounts and the number of changes
|
|
|
|
}
|
|
|
|
|
|
|
|
// newJournal creates a new initialized journal.
|
|
|
|
func newJournal() *journal {
|
|
|
|
return &journal{
|
|
|
|
dirties: make(map[common.Address]int),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sortedDirties sort the dirty addresses for deterministic iteration
|
|
|
|
func (j *journal) sortedDirties() []common.Address {
|
|
|
|
keys := make([]common.Address, len(j.dirties))
|
|
|
|
i := 0
|
2022-10-18 06:44:29 +00:00
|
|
|
// #nosec G705
|
2022-01-05 07:28:27 +00:00
|
|
|
for k := range j.dirties {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
sort.Slice(keys, func(i, j int) bool {
|
|
|
|
return bytes.Compare(keys[i].Bytes(), keys[j].Bytes()) < 0
|
|
|
|
})
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
// append inserts a new modification entry to the end of the change journal.
|
2022-10-10 10:38:33 +00:00
|
|
|
func (j *journal) append(entry JournalEntry) {
|
2022-01-05 07:28:27 +00:00
|
|
|
j.entries = append(j.entries, entry)
|
2022-10-10 10:38:33 +00:00
|
|
|
if addr := entry.Dirtied(); addr != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
j.dirties[*addr]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
// Revert undoes a batch of journalled modifications along with any Reverted
|
2022-01-05 07:28:27 +00:00
|
|
|
// dirty handling too.
|
2022-10-10 10:38:33 +00:00
|
|
|
func (j *journal) Revert(statedb *StateDB, snapshot int) {
|
2022-01-05 07:28:27 +00:00
|
|
|
for i := len(j.entries) - 1; i >= snapshot; i-- {
|
|
|
|
// Undo the changes made by the operation
|
2022-10-10 10:38:33 +00:00
|
|
|
j.entries[i].Revert(statedb)
|
2022-01-05 07:28:27 +00:00
|
|
|
|
|
|
|
// Drop any dirty tracking induced by the change
|
2022-10-10 10:38:33 +00:00
|
|
|
if addr := j.entries[i].Dirtied(); addr != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
if j.dirties[*addr]--; j.dirties[*addr] == 0 {
|
|
|
|
delete(j.dirties, *addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j.entries = j.entries[:snapshot]
|
|
|
|
}
|
|
|
|
|
|
|
|
// length returns the current number of entries in the journal.
|
|
|
|
func (j *journal) length() int {
|
|
|
|
return len(j.entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
// Changes to the account trie.
|
|
|
|
createObjectChange struct {
|
|
|
|
account *common.Address
|
|
|
|
}
|
|
|
|
resetObjectChange struct {
|
|
|
|
prev *stateObject
|
|
|
|
}
|
|
|
|
suicideChange struct {
|
|
|
|
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 {
|
|
|
|
prev uint64
|
|
|
|
}
|
|
|
|
addLogChange struct{}
|
|
|
|
|
|
|
|
// Changes to the access list
|
|
|
|
accessListAddAccountChange struct {
|
|
|
|
address *common.Address
|
|
|
|
}
|
|
|
|
accessListAddSlotChange struct {
|
|
|
|
address *common.Address
|
|
|
|
slot *common.Hash
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch createObjectChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
delete(s.stateObjects, *ch.account)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch createObjectChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch resetObjectChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.setStateObject(ch.prev)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch resetObjectChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch suicideChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
obj := s.getStateObject(*ch.account)
|
|
|
|
if obj != nil {
|
|
|
|
obj.suicided = ch.prev
|
|
|
|
obj.setBalance(ch.prevbalance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch suicideChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch balanceChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.getStateObject(*ch.account).setBalance(ch.prev)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch balanceChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch nonceChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.getStateObject(*ch.account).setNonce(ch.prev)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch nonceChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch codeChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch codeChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch storageChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch storageChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return ch.account
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch refundChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.refund = ch.prev
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch refundChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch addLogChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.logs = s.logs[:len(s.logs)-1]
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch addLogChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch accessListAddAccountChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
/*
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch accessListAddAccountChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch accessListAddSlotChange) Revert(s *StateDB) {
|
2022-01-05 07:28:27 +00:00
|
|
|
s.accessList.DeleteSlot(*ch.address, *ch.slot)
|
|
|
|
}
|
|
|
|
|
2022-10-10 10:38:33 +00:00
|
|
|
func (ch accessListAddSlotChange) Dirtied() *common.Address {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil
|
|
|
|
}
|