2019-05-27 13:51:49 +00:00
|
|
|
// Copyright 2019 The go-ethereum Authors
|
2017-06-27 13:57:06 +00:00
|
|
|
// 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 (
|
2019-05-27 13:51:49 +00:00
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
2017-06-27 13:57:06 +00:00
|
|
|
"fmt"
|
2019-05-27 13:51:49 +00:00
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
2019-12-10 13:12:21 +00:00
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
2017-06-27 13:57:06 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common/dbutils"
|
2020-01-31 04:11:20 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/common/debug"
|
2019-05-27 13:51:49 +00:00
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types/accounts"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/trie"
|
2017-06-27 13:57:06 +00:00
|
|
|
)
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Trie cache generation limit after which to evict trie nodes from memory.
|
|
|
|
var MaxTrieCacheGen = uint32(1024 * 1024)
|
|
|
|
|
2020-01-08 09:55:56 +00:00
|
|
|
const (
|
|
|
|
//FirstContractIncarnation - first incarnation for contract accounts. After 1 it increases by 1.
|
|
|
|
FirstContractIncarnation = 1
|
|
|
|
//NonContractIncarnation incarnation for non contracts
|
|
|
|
NonContractIncarnation = 0
|
|
|
|
)
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
type StateReader interface {
|
|
|
|
ReadAccountData(address common.Address) (*accounts.Account, error)
|
|
|
|
ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error)
|
2019-10-21 12:54:47 +00:00
|
|
|
ReadAccountCode(address common.Address, codeHash common.Hash) ([]byte, error)
|
|
|
|
ReadAccountCodeSize(address common.Address, codeHash common.Hash) (int, error)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type StateWriter interface {
|
|
|
|
UpdateAccountData(ctx context.Context, address common.Address, original, account *accounts.Account) error
|
2019-12-20 12:25:40 +00:00
|
|
|
UpdateAccountCode(addrHash common.Hash, incarnation uint64, codeHash common.Hash, code []byte) error
|
2019-05-27 13:51:49 +00:00
|
|
|
DeleteAccount(ctx context.Context, address common.Address, original *accounts.Account) error
|
|
|
|
WriteAccountStorage(ctx context.Context, address common.Address, incarnation uint64, key, original, value *common.Hash) error
|
|
|
|
CreateContract(address common.Address) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type NoopWriter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNoopWriter() *NoopWriter {
|
|
|
|
return &NoopWriter{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *NoopWriter) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *NoopWriter) DeleteAccount(_ context.Context, address common.Address, original *accounts.Account) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
func (nw *NoopWriter) UpdateAccountCode(addrHash common.Hash, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *NoopWriter) WriteAccountStorage(_ context.Context, address common.Address, incarnation uint64, key, original, value *common.Hash) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nw *NoopWriter) CreateContract(address common.Address) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Structure holding updates, deletes, and reads registered within one change period
|
|
|
|
// A change period can be transaction within a block, or a block within group of blocks
|
|
|
|
type Buffer struct {
|
2020-03-23 22:10:36 +00:00
|
|
|
codeReads map[common.Hash]common.Hash
|
|
|
|
codeUpdates map[common.Hash][]byte
|
2019-11-15 22:48:49 +00:00
|
|
|
storageUpdates map[common.Hash]map[common.Hash][]byte
|
|
|
|
storageReads map[common.Hash]map[common.Hash]struct{}
|
2019-05-27 13:51:49 +00:00
|
|
|
accountUpdates map[common.Hash]*accounts.Account
|
|
|
|
accountReads map[common.Hash]struct{}
|
2019-11-15 22:48:49 +00:00
|
|
|
deleted map[common.Hash]struct{}
|
|
|
|
created map[common.Hash]struct{}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prepares buffer for work or clears previous data
|
|
|
|
func (b *Buffer) initialise() {
|
2020-03-23 22:10:36 +00:00
|
|
|
b.codeReads = make(map[common.Hash]common.Hash)
|
|
|
|
b.codeUpdates = make(map[common.Hash][]byte)
|
2019-11-15 22:48:49 +00:00
|
|
|
b.storageUpdates = make(map[common.Hash]map[common.Hash][]byte)
|
|
|
|
b.storageReads = make(map[common.Hash]map[common.Hash]struct{})
|
2019-05-27 13:51:49 +00:00
|
|
|
b.accountUpdates = make(map[common.Hash]*accounts.Account)
|
|
|
|
b.accountReads = make(map[common.Hash]struct{})
|
2019-11-15 22:48:49 +00:00
|
|
|
b.deleted = make(map[common.Hash]struct{})
|
|
|
|
b.created = make(map[common.Hash]struct{})
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2017-06-27 13:57:06 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Replaces account pointer with pointers to the copies
|
|
|
|
func (b *Buffer) detachAccounts() {
|
|
|
|
for addrHash, account := range b.accountUpdates {
|
|
|
|
if account != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
b.accountUpdates[addrHash] = account.SelfCopy()
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merges the content of another buffer into this one
|
|
|
|
func (b *Buffer) merge(other *Buffer) {
|
2020-03-23 22:10:36 +00:00
|
|
|
for addrHash, codeHash := range other.codeReads {
|
|
|
|
b.codeReads[addrHash] = codeHash
|
|
|
|
}
|
|
|
|
|
|
|
|
for addrHash, code := range other.codeUpdates {
|
|
|
|
b.codeUpdates[addrHash] = code
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash, om := range other.storageUpdates {
|
|
|
|
m, ok := b.storageUpdates[addrHash]
|
2019-05-27 13:51:49 +00:00
|
|
|
if !ok {
|
|
|
|
m = make(map[common.Hash][]byte)
|
2019-11-15 22:48:49 +00:00
|
|
|
b.storageUpdates[addrHash] = m
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
for keyHash, v := range om {
|
|
|
|
m[keyHash] = v
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash, om := range other.storageReads {
|
|
|
|
m, ok := b.storageReads[addrHash]
|
2019-05-27 13:51:49 +00:00
|
|
|
if !ok {
|
|
|
|
m = make(map[common.Hash]struct{})
|
2019-11-15 22:48:49 +00:00
|
|
|
b.storageReads[addrHash] = m
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
for keyHash := range om {
|
|
|
|
m[keyHash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for addrHash, account := range other.accountUpdates {
|
|
|
|
b.accountUpdates[addrHash] = account
|
|
|
|
}
|
|
|
|
for addrHash := range other.accountReads {
|
|
|
|
b.accountReads[addrHash] = struct{}{}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash := range other.deleted {
|
|
|
|
b.deleted[addrHash] = struct{}{}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash := range other.created {
|
|
|
|
b.created[addrHash] = struct{}{}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrieDbState implements StateReader by wrapping a trie and a database, where trie acts as a cache for the database
|
|
|
|
type TrieDbState struct {
|
2020-03-30 16:01:24 +00:00
|
|
|
t *trie.Trie
|
|
|
|
tMu *sync.Mutex
|
|
|
|
db ethdb.Database
|
|
|
|
blockNr uint64
|
|
|
|
buffers []*Buffer
|
|
|
|
aggregateBuffer *Buffer // Merge of all buffers
|
|
|
|
currentBuffer *Buffer
|
|
|
|
historical bool
|
|
|
|
noHistory bool
|
|
|
|
resolveReads bool
|
|
|
|
savePreimages bool
|
|
|
|
resolveSetBuilder *trie.ResolveSetBuilder
|
|
|
|
tp *trie.TriePruning
|
|
|
|
newStream trie.Stream
|
|
|
|
hashBuilder *trie.HashBuilder
|
|
|
|
resolver *trie.Resolver
|
|
|
|
incarnationMap map[common.Hash]uint64 // Temporary map of incarnation in case we cannot figure out from the database
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:21:15 +00:00
|
|
|
func NewTrieDbState(root common.Hash, db ethdb.Database, blockNr uint64) *TrieDbState {
|
2019-05-27 13:51:49 +00:00
|
|
|
t := trie.New(root)
|
|
|
|
tp := trie.NewTriePruning(blockNr)
|
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
tds := &TrieDbState{
|
2020-01-15 13:56:50 +00:00
|
|
|
t: t,
|
|
|
|
tMu: new(sync.Mutex),
|
|
|
|
db: db,
|
|
|
|
blockNr: blockNr,
|
|
|
|
resolveSetBuilder: trie.NewResolveSetBuilder(),
|
|
|
|
tp: tp,
|
|
|
|
savePreimages: true,
|
2020-01-30 13:16:12 +00:00
|
|
|
hashBuilder: trie.NewHashBuilder(false),
|
2020-02-10 17:05:32 +00:00
|
|
|
incarnationMap: make(map[common.Hash]uint64),
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
2020-02-12 13:52:59 +00:00
|
|
|
t.SetTouchFunc(tp.Touch)
|
2019-12-10 13:12:21 +00:00
|
|
|
|
2020-03-30 16:01:24 +00:00
|
|
|
tp.SetUnloadNodeFunc(tds.putIntermediateHash)
|
|
|
|
tp.SetCreateNodeFunc(tds.delIntermediateHash)
|
2020-01-31 04:11:20 +00:00
|
|
|
|
2020-03-26 13:21:15 +00:00
|
|
|
return tds
|
2019-12-10 13:12:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 11:19:00 +00:00
|
|
|
func (tds *TrieDbState) EnablePreimages(ep bool) {
|
|
|
|
tds.savePreimages = ep
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) SetHistorical(h bool) {
|
|
|
|
tds.historical = h
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) SetResolveReads(rr bool) {
|
|
|
|
tds.resolveReads = rr
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) SetNoHistory(nh bool) {
|
|
|
|
tds.noHistory = nh
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
2020-02-12 13:52:59 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) Copy() *TrieDbState {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
2019-05-27 13:51:49 +00:00
|
|
|
tcopy := *tds.t
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Unlock()
|
2019-05-27 13:51:49 +00:00
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
n := tds.getBlockNr()
|
|
|
|
tp := trie.NewTriePruning(n)
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
cpy := TrieDbState{
|
2020-02-10 17:05:32 +00:00
|
|
|
t: &tcopy,
|
|
|
|
tMu: new(sync.Mutex),
|
|
|
|
db: tds.db,
|
|
|
|
blockNr: n,
|
|
|
|
tp: tp,
|
|
|
|
hashBuilder: trie.NewHashBuilder(false),
|
|
|
|
incarnationMap: make(map[common.Hash]uint64),
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
2020-01-31 04:11:20 +00:00
|
|
|
|
2020-03-30 16:01:24 +00:00
|
|
|
cpy.tp.SetUnloadNodeFunc(cpy.putIntermediateHash)
|
|
|
|
cpy.tp.SetCreateNodeFunc(cpy.delIntermediateHash)
|
2020-01-31 04:11:20 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
return &cpy
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:52:59 +00:00
|
|
|
func (tds *TrieDbState) putIntermediateHash(key []byte, nodeHash []byte) {
|
2020-03-01 09:00:14 +00:00
|
|
|
if err := tds.db.Put(dbutils.IntermediateTrieHashBucket, common.CopyBytes(key), common.CopyBytes(nodeHash)); err != nil {
|
2020-02-12 13:52:59 +00:00
|
|
|
log.Warn("could not put intermediate trie hash", "err", err)
|
|
|
|
}
|
2020-01-31 04:11:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:52:59 +00:00
|
|
|
func (tds *TrieDbState) delIntermediateHash(prefixAsNibbles []byte) {
|
|
|
|
if len(prefixAsNibbles) == 0 {
|
2020-01-31 04:11:20 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-12 13:52:59 +00:00
|
|
|
|
|
|
|
if len(prefixAsNibbles)%2 == 1 { // only put to bucket prefixes with even number of nibbles
|
2020-01-31 04:11:20 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-12 13:52:59 +00:00
|
|
|
|
2020-03-11 10:31:49 +00:00
|
|
|
key := make([]byte, len(prefixAsNibbles)/2)
|
2020-03-01 09:00:14 +00:00
|
|
|
trie.CompressNibbles(prefixAsNibbles, &key)
|
2020-02-12 13:52:59 +00:00
|
|
|
|
2020-03-01 09:00:14 +00:00
|
|
|
if err := tds.db.Delete(dbutils.IntermediateTrieHashBucket, key); err != nil {
|
2020-02-12 13:52:59 +00:00
|
|
|
log.Warn("could not delete intermediate trie hash", "err", err)
|
|
|
|
return
|
2020-01-31 04:11:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) Database() ethdb.Database {
|
|
|
|
return tds.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) Trie() *trie.Trie {
|
|
|
|
return tds.t
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) StartNewBuffer() {
|
|
|
|
if tds.currentBuffer != nil {
|
|
|
|
if tds.aggregateBuffer == nil {
|
|
|
|
tds.aggregateBuffer = &Buffer{}
|
|
|
|
tds.aggregateBuffer.initialise()
|
|
|
|
}
|
|
|
|
tds.aggregateBuffer.merge(tds.currentBuffer)
|
|
|
|
tds.currentBuffer.detachAccounts()
|
|
|
|
}
|
|
|
|
tds.currentBuffer = &Buffer{}
|
|
|
|
tds.currentBuffer.initialise()
|
|
|
|
tds.buffers = append(tds.buffers, tds.currentBuffer)
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
func (tds *TrieDbState) WithNewBuffer() *TrieDbState {
|
|
|
|
aggregateBuffer := &Buffer{}
|
|
|
|
aggregateBuffer.initialise()
|
|
|
|
|
|
|
|
currentBuffer := &Buffer{}
|
|
|
|
currentBuffer.initialise()
|
|
|
|
|
|
|
|
buffers := []*Buffer{currentBuffer}
|
|
|
|
|
|
|
|
tds.tMu.Lock()
|
|
|
|
t := &TrieDbState{
|
2020-01-15 13:56:50 +00:00
|
|
|
t: tds.t,
|
|
|
|
tMu: tds.tMu,
|
|
|
|
db: tds.db,
|
|
|
|
blockNr: tds.getBlockNr(),
|
|
|
|
buffers: buffers,
|
|
|
|
aggregateBuffer: aggregateBuffer,
|
|
|
|
currentBuffer: currentBuffer,
|
|
|
|
historical: tds.historical,
|
|
|
|
noHistory: tds.noHistory,
|
|
|
|
resolveReads: tds.resolveReads,
|
|
|
|
resolveSetBuilder: tds.resolveSetBuilder,
|
|
|
|
tp: tds.tp,
|
2020-01-30 13:16:12 +00:00
|
|
|
hashBuilder: trie.NewHashBuilder(false),
|
2019-12-10 13:12:21 +00:00
|
|
|
}
|
|
|
|
tds.tMu.Unlock()
|
|
|
|
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) LastRoot() common.Hash {
|
2020-02-03 12:02:26 +00:00
|
|
|
if tds == nil || tds.tMu == nil {
|
|
|
|
return common.Hash{}
|
|
|
|
}
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
2019-05-27 13:51:49 +00:00
|
|
|
return tds.t.Hash()
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
// ComputeTrieRoots is a combination of `ResolveStateTrie` and `UpdateStateTrie`
|
2019-05-27 13:51:49 +00:00
|
|
|
// DESCRIBED: docs/programmers_guide/guide.md#organising-ethereum-state-into-a-merkle-tree
|
|
|
|
func (tds *TrieDbState) ComputeTrieRoots() ([]common.Hash, error) {
|
2020-04-01 16:04:41 +00:00
|
|
|
if _, err := tds.ResolveStateTrie(false, false); err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return tds.UpdateStateTrie()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStateTrie assumes that the state trie is already fully resolved, i.e. any operations
|
|
|
|
// will find necessary data inside the trie.
|
|
|
|
func (tds *TrieDbState) UpdateStateTrie() ([]common.Hash, error) {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
roots, err := tds.updateTrieRoots(true)
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.clearUpdates()
|
|
|
|
return roots, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) PrintTrie(w io.Writer) {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
2019-12-19 10:44:43 +00:00
|
|
|
defer tds.tMu.Unlock()
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.t.Print(w)
|
|
|
|
fmt.Fprintln(w, "") //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds a map where for each address (of a smart contract) there is
|
|
|
|
// a sorted list of all key hashes that were touched within the
|
|
|
|
// period for which we are aggregating updates
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) buildStorageTouches(withReads bool, withValues bool) (common.StorageKeys, [][]byte) {
|
|
|
|
storageTouches := common.StorageKeys{}
|
|
|
|
var values [][]byte
|
|
|
|
for addrHash, m := range tds.aggregateBuffer.storageUpdates {
|
2019-11-21 15:56:39 +00:00
|
|
|
if withValues {
|
|
|
|
if _, ok := tds.aggregateBuffer.deleted[addrHash]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
for keyHash := range m {
|
2019-11-15 22:48:49 +00:00
|
|
|
var storageKey common.StorageKey
|
|
|
|
copy(storageKey[:], addrHash[:])
|
|
|
|
copy(storageKey[common.HashLength:], keyHash[:])
|
|
|
|
storageTouches = append(storageTouches, storageKey)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
}
|
|
|
|
if withReads {
|
|
|
|
for addrHash, m := range tds.aggregateBuffer.storageReads {
|
|
|
|
mWrite := tds.aggregateBuffer.storageUpdates[addrHash]
|
|
|
|
for keyHash := range m {
|
|
|
|
if mWrite != nil {
|
|
|
|
if _, ok := mWrite[keyHash]; ok {
|
|
|
|
// Avoid repeating the same storage keys if they are both read and updated
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var storageKey common.StorageKey
|
|
|
|
copy(storageKey[:], addrHash[:])
|
|
|
|
copy(storageKey[common.HashLength:], keyHash[:])
|
|
|
|
storageTouches = append(storageTouches, storageKey)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
sort.Sort(storageTouches)
|
|
|
|
if withValues {
|
|
|
|
// We assume that if withValues == true, then withReads == false
|
|
|
|
var addrHash common.Hash
|
|
|
|
var keyHash common.Hash
|
|
|
|
for _, storageKey := range storageTouches {
|
|
|
|
copy(addrHash[:], storageKey[:])
|
|
|
|
copy(keyHash[:], storageKey[common.HashLength:])
|
|
|
|
values = append(values, tds.aggregateBuffer.storageUpdates[addrHash][keyHash])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
return storageTouches, values
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expands the storage tries (by loading data from the database) if it is required
|
|
|
|
// for accessing storage slots containing in the storageTouches map
|
2020-01-24 10:58:01 +00:00
|
|
|
func (tds *TrieDbState) resolveStorageTouches(storageTouches common.StorageKeys, resolveFunc func(*trie.Resolver) error) error {
|
2020-01-30 13:16:12 +00:00
|
|
|
var firstRequest = true
|
2019-11-15 22:48:49 +00:00
|
|
|
for _, storageKey := range storageTouches {
|
|
|
|
if need, req := tds.t.NeedResolution(storageKey[:common.HashLength], storageKey[:]); need {
|
2020-01-30 13:16:12 +00:00
|
|
|
if tds.resolver == nil {
|
|
|
|
tds.resolver = trie.NewResolver(0, false, tds.blockNr)
|
|
|
|
tds.resolver.SetHistorical(tds.historical)
|
|
|
|
} else if firstRequest {
|
|
|
|
tds.resolver.Reset(0, false, tds.blockNr)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-30 13:16:12 +00:00
|
|
|
firstRequest = false
|
|
|
|
tds.resolver.AddRequest(req)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 13:16:12 +00:00
|
|
|
if !firstRequest {
|
2020-03-11 10:31:49 +00:00
|
|
|
res := resolveFunc(tds.resolver)
|
|
|
|
return res
|
2020-01-30 13:16:12 +00:00
|
|
|
}
|
|
|
|
return nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate pending block proof so that it will be sufficient for accessing all storage slots in storageTouches
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) populateStorageBlockProof(storageTouches common.StorageKeys) error { //nolint
|
|
|
|
for _, storageKey := range storageTouches {
|
2020-01-15 13:56:50 +00:00
|
|
|
tds.resolveSetBuilder.AddStorageTouch(storageKey[:])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) buildCodeTouches(withReads bool) map[common.Hash]common.Hash {
|
|
|
|
return tds.aggregateBuffer.codeReads
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Builds a sorted list of all address hashes that were touched within the
|
|
|
|
// period for which we are aggregating updates
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) buildAccountTouches(withReads bool, withValues bool) (common.Hashes, []*accounts.Account) {
|
|
|
|
accountTouches := common.Hashes{}
|
|
|
|
var aValues []*accounts.Account
|
2019-11-21 15:56:39 +00:00
|
|
|
for addrHash, aValue := range tds.aggregateBuffer.accountUpdates {
|
|
|
|
if aValue != nil {
|
|
|
|
if _, ok := tds.aggregateBuffer.deleted[addrHash]; ok {
|
|
|
|
accountTouches = append(accountTouches, addrHash)
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
accountTouches = append(accountTouches, addrHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
if withReads {
|
|
|
|
for addrHash := range tds.aggregateBuffer.accountReads {
|
|
|
|
if _, ok := tds.aggregateBuffer.accountUpdates[addrHash]; !ok {
|
|
|
|
accountTouches = append(accountTouches, addrHash)
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
sort.Sort(accountTouches)
|
|
|
|
if withValues {
|
|
|
|
// We assume that if withValues == true, then withReads == false
|
|
|
|
aValues = make([]*accounts.Account, len(accountTouches))
|
|
|
|
for i, addrHash := range accountTouches {
|
2019-11-21 15:56:39 +00:00
|
|
|
if i < len(accountTouches)-1 && addrHash == accountTouches[i+1] {
|
|
|
|
aValues[i] = nil // Entry that would wipe out existing storage
|
|
|
|
} else {
|
|
|
|
a := tds.aggregateBuffer.accountUpdates[addrHash]
|
|
|
|
if a != nil {
|
|
|
|
if _, ok := tds.aggregateBuffer.storageUpdates[addrHash]; ok {
|
|
|
|
var ac accounts.Account
|
|
|
|
ac.Copy(a)
|
|
|
|
ac.Root = trie.EmptyRoot
|
|
|
|
a = &ac
|
|
|
|
}
|
|
|
|
}
|
|
|
|
aValues[i] = a
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
return accountTouches, aValues
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) resolveCodeTouches(codeTouches map[common.Hash]common.Hash, resolveFunc trie.ResolveFunc) error {
|
|
|
|
firstRequest := true
|
|
|
|
for address, codeHash := range codeTouches {
|
|
|
|
if need, req := tds.t.NeedResolutonForCode(address, codeHash); need {
|
|
|
|
if tds.resolver == nil {
|
|
|
|
tds.resolver = trie.NewResolver(0, true, tds.blockNr)
|
|
|
|
tds.resolver.SetHistorical(tds.historical)
|
|
|
|
} else if firstRequest {
|
|
|
|
tds.resolver.Reset(0, true, tds.blockNr)
|
|
|
|
}
|
|
|
|
firstRequest = false
|
|
|
|
tds.resolver.AddCodeRequest(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !firstRequest {
|
|
|
|
return resolveFunc(tds.resolver)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Expands the accounts trie (by loading data from the database) if it is required
|
|
|
|
// for accessing accounts whose addresses are contained in the accountTouches
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) resolveAccountTouches(accountTouches common.Hashes, resolveFunc trie.ResolveFunc) error {
|
2020-01-30 13:16:12 +00:00
|
|
|
var firstRequest = true
|
2019-05-27 13:51:49 +00:00
|
|
|
for _, addrHash := range accountTouches {
|
|
|
|
if need, req := tds.t.NeedResolution(nil, addrHash[:]); need {
|
2020-01-30 13:16:12 +00:00
|
|
|
if tds.resolver == nil {
|
|
|
|
tds.resolver = trie.NewResolver(0, true, tds.blockNr)
|
|
|
|
tds.resolver.SetHistorical(tds.historical)
|
|
|
|
} else if firstRequest {
|
|
|
|
tds.resolver.Reset(0, true, tds.blockNr)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-30 13:16:12 +00:00
|
|
|
firstRequest = false
|
|
|
|
tds.resolver.AddRequest(req)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-30 13:16:12 +00:00
|
|
|
if !firstRequest {
|
|
|
|
return resolveFunc(tds.resolver)
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) populateAccountBlockProof(accountTouches common.Hashes) {
|
2019-05-27 13:51:49 +00:00
|
|
|
for _, addrHash := range accountTouches {
|
|
|
|
a := addrHash
|
2020-01-15 13:56:50 +00:00
|
|
|
tds.resolveSetBuilder.AddTouch(a[:])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
// ExtractTouches returns two lists of keys - for accounts and storage items correspondingly
|
|
|
|
// Each list is the collection of keys that have been "touched" (inserted, updated, or simply accessed)
|
|
|
|
// since the last invocation of `ExtractTouches`.
|
|
|
|
func (tds *TrieDbState) ExtractTouches() (accountTouches [][]byte, storageTouches [][]byte) {
|
2020-01-15 13:56:50 +00:00
|
|
|
return tds.resolveSetBuilder.ExtractTouches()
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) resolveStateTrieWithFunc(resolveFunc trie.ResolveFunc) error {
|
2019-05-27 13:51:49 +00:00
|
|
|
// Aggregating the current buffer, if any
|
|
|
|
if tds.currentBuffer != nil {
|
|
|
|
if tds.aggregateBuffer == nil {
|
|
|
|
tds.aggregateBuffer = &Buffer{}
|
|
|
|
tds.aggregateBuffer.initialise()
|
|
|
|
}
|
|
|
|
tds.aggregateBuffer.merge(tds.currentBuffer)
|
|
|
|
}
|
|
|
|
if tds.aggregateBuffer == nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// Prepare (resolve) storage tries so that actual modifications can proceed without database access
|
2019-11-15 22:48:49 +00:00
|
|
|
storageTouches, _ := tds.buildStorageTouches(tds.resolveReads, false)
|
2019-05-27 13:51:49 +00:00
|
|
|
|
|
|
|
// Prepare (resolve) accounts trie so that actual modifications can proceed without database access
|
2019-11-15 22:48:49 +00:00
|
|
|
accountTouches, _ := tds.buildAccountTouches(tds.resolveReads, false)
|
2020-03-23 22:10:36 +00:00
|
|
|
|
|
|
|
// Prepare (resolve) contract code reads so that actual modifications can proceed without database access
|
|
|
|
codeTouches := tds.buildCodeTouches(tds.resolveReads)
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = tds.resolveAccountTouches(accountTouches, resolveFunc); err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
if err = tds.resolveCodeTouches(codeTouches, resolveFunc); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if tds.resolveReads {
|
|
|
|
tds.populateAccountBlockProof(accountTouches)
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
if err = tds.resolveStorageTouches(storageTouches, resolveFunc); err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-24 10:58:01 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if tds.resolveReads {
|
|
|
|
if err := tds.populateStorageBlockProof(storageTouches); err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-21 12:54:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-24 10:58:01 +00:00
|
|
|
// ResolveStateTrie resolves parts of the state trie that would be necessary for any updates
|
|
|
|
// (and reads, if `resolveReads` is set).
|
2020-04-01 16:04:41 +00:00
|
|
|
func (tds *TrieDbState) ResolveStateTrie(extractWitnesses bool, trace bool) ([]*trie.Witness, error) {
|
2020-01-24 10:58:01 +00:00
|
|
|
var witnesses []*trie.Witness
|
|
|
|
|
|
|
|
resolveFunc := func(resolver *trie.Resolver) error {
|
|
|
|
if resolver == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
resolver.CollectWitnesses(extractWitnesses)
|
2020-04-01 16:04:41 +00:00
|
|
|
if err := resolver.ResolveWithDb(tds.db, tds.blockNr, trace); err != nil {
|
2020-01-24 10:58:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !extractWitnesses {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resolverWitnesses := resolver.PopCollectedWitnesses()
|
|
|
|
if len(resolverWitnesses) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if witnesses == nil {
|
|
|
|
witnesses = resolverWitnesses
|
|
|
|
} else {
|
|
|
|
witnesses = append(witnesses, resolverWitnesses...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err := tds.resolveStateTrieWithFunc(resolveFunc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return witnesses, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResolveStateTrieStateless uses a witness DB to resolve subtries
|
|
|
|
func (tds *TrieDbState) ResolveStateTrieStateless(database trie.WitnessStorage) error {
|
|
|
|
var startPos int64
|
|
|
|
resolveFunc := func(resolver *trie.Resolver) error {
|
|
|
|
if resolver == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pos, err := resolver.ResolveStateless(database, tds.blockNr, MaxTrieCacheGen, startPos)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
startPos = pos
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tds.resolveStateTrieWithFunc(resolveFunc)
|
|
|
|
}
|
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
// CalcTrieRoots calculates trie roots without modifying the state trie
|
|
|
|
func (tds *TrieDbState) CalcTrieRoots(trace bool) (common.Hash, error) {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
2019-11-15 22:48:49 +00:00
|
|
|
// Retrive the list of inserted/updated/deleted storage items (keys and values)
|
|
|
|
storageKeys, sValues := tds.buildStorageTouches(false, true)
|
|
|
|
if trace {
|
|
|
|
fmt.Printf("len(storageKeys)=%d, len(sValues)=%d\n", len(storageKeys), len(sValues))
|
|
|
|
}
|
|
|
|
// Retrive the list of inserted/updated/deleted accounts (keys and values)
|
|
|
|
accountKeys, aValues := tds.buildAccountTouches(false, true)
|
|
|
|
if trace {
|
|
|
|
fmt.Printf("len(accountKeys)=%d, len(aValues)=%d\n", len(accountKeys), len(aValues))
|
|
|
|
}
|
2020-01-30 13:16:12 +00:00
|
|
|
return trie.HashWithModifications(tds.t, accountKeys, aValues, storageKeys, sValues, common.HashLength, &tds.newStream, tds.hashBuilder, trace)
|
2019-11-15 22:48:49 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
// forward is `true` if the function is used to progress the state forward (by adding blocks)
|
|
|
|
// forward is `false` if the function is used to rewind the state (for reorgs, for example)
|
|
|
|
func (tds *TrieDbState) updateTrieRoots(forward bool) ([]common.Hash, error) {
|
2019-05-27 13:51:49 +00:00
|
|
|
accountUpdates := tds.aggregateBuffer.accountUpdates
|
|
|
|
// Perform actual updates on the tries, and compute one trie root per buffer
|
|
|
|
// These roots can be used to populate receipt.PostState on pre-Byzantium
|
|
|
|
roots := make([]common.Hash, len(tds.buffers))
|
2019-12-20 12:25:40 +00:00
|
|
|
// The following map is to prevent repeated clearouts of the storage
|
|
|
|
alreadyCreated := make(map[common.Hash]struct{})
|
2019-05-27 13:51:49 +00:00
|
|
|
for i, b := range tds.buffers {
|
|
|
|
// New contracts are being created at these addresses. Therefore, we need to clear the storage items
|
|
|
|
// that might be remaining in the trie and figure out the next incarnations
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash := range b.created {
|
2019-10-18 12:11:50 +00:00
|
|
|
// Prevent repeated storage clearouts
|
2019-11-15 22:48:49 +00:00
|
|
|
if _, ok := alreadyCreated[addrHash]; ok {
|
2019-10-18 12:11:50 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
alreadyCreated[addrHash] = struct{}{}
|
2019-05-27 13:51:49 +00:00
|
|
|
if account, ok := b.accountUpdates[addrHash]; ok && account != nil {
|
2019-12-20 12:25:40 +00:00
|
|
|
b.accountUpdates[addrHash].Root = trie.EmptyRoot
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
if account, ok := tds.aggregateBuffer.accountUpdates[addrHash]; ok && account != nil {
|
|
|
|
tds.aggregateBuffer.accountUpdates[addrHash].Root = trie.EmptyRoot
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-02-12 13:52:59 +00:00
|
|
|
//fmt.Println("updateTrieRoots del subtree", addrHash.String())
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// The only difference between Delete and DeleteSubtree is that Delete would delete accountNode too,
|
|
|
|
// wherewas DeleteSubtree will keep the accountNode, but will make the storage sub-trie empty
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.DeleteSubtree(addrHash[:])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
for addrHash, account := range b.accountUpdates {
|
|
|
|
if account != nil {
|
2020-02-12 13:52:59 +00:00
|
|
|
//fmt.Println("updateTrieRoots b.accountUpdates", addrHash.String(), account.Incarnation)
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.t.UpdateAccount(addrHash[:], account)
|
|
|
|
} else {
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.Delete(addrHash[:])
|
2020-03-23 22:10:36 +00:00
|
|
|
delete(b.codeUpdates, addrHash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for addrHash, newCode := range b.codeUpdates {
|
|
|
|
if err := tds.t.UpdateAccountCode(addrHash[:], newCode); err != nil {
|
|
|
|
return nil, err
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash, m := range b.storageUpdates {
|
2019-05-27 13:51:49 +00:00
|
|
|
for keyHash, v := range m {
|
2019-11-15 22:48:49 +00:00
|
|
|
cKey := dbutils.GenerateCompositeTrieKey(addrHash, keyHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
if len(v) > 0 {
|
|
|
|
//fmt.Printf("Update storage trie addrHash %x, keyHash %x: %x\n", addrHash, keyHash, v)
|
|
|
|
if forward {
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.Update(cKey, v)
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
// If rewinding, it might not be possible to execute storage item update.
|
|
|
|
// If we rewind from the state where a contract does not exist anymore (it was self-destructed)
|
|
|
|
// to the point where it existed (with storage), then rewinding to the point of existence
|
|
|
|
// will not bring back the full storage trie. Instead there will be one hashNode.
|
|
|
|
// So we probe for this situation first
|
|
|
|
if _, ok := tds.t.Get(cKey); ok {
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.Update(cKey, v)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if forward {
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.Delete(cKey)
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
// If rewinding, it might not be possible to execute storage item update.
|
|
|
|
// If we rewind from the state where a contract does not exist anymore (it was self-destructed)
|
|
|
|
// to the point where it existed (with storage), then rewinding to the point of existence
|
|
|
|
// will not bring back the full storage trie. Instead there will be one hashNode.
|
|
|
|
// So we probe for this situation first
|
|
|
|
if _, ok := tds.t.Get(cKey); ok {
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.Delete(cKey)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
|
|
|
if forward || debug.IsThinHistory() {
|
2019-05-27 13:51:49 +00:00
|
|
|
if account, ok := b.accountUpdates[addrHash]; ok && account != nil {
|
|
|
|
ok, root := tds.t.DeepHash(addrHash[:])
|
|
|
|
if ok {
|
|
|
|
account.Root = root
|
2019-11-05 16:28:25 +00:00
|
|
|
//fmt.Printf("(b)Set %x root for addrHash %x\n", root, addrHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
//fmt.Printf("(b)Set empty root for addrHash %x\n", addrHash)
|
|
|
|
account.Root = trie.EmptyRoot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if account, ok := accountUpdates[addrHash]; ok && account != nil {
|
|
|
|
ok, root := tds.t.DeepHash(addrHash[:])
|
|
|
|
if ok {
|
|
|
|
account.Root = root
|
2019-11-05 16:28:25 +00:00
|
|
|
//fmt.Printf("Set %x root for addrHash %x\n", root, addrHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
//fmt.Printf("Set empty root for addrHash %x\n", addrHash)
|
|
|
|
account.Root = trie.EmptyRoot
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Simply comparing the correctness of the storageRoot computations
|
|
|
|
if account, ok := b.accountUpdates[addrHash]; ok && account != nil {
|
|
|
|
ok, h := tds.t.DeepHash(addrHash[:])
|
|
|
|
if !ok {
|
|
|
|
h = trie.EmptyRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.Root != h {
|
2019-11-15 22:48:49 +00:00
|
|
|
return nil, fmt.Errorf("mismatched storage root for %x: expected %x, got %x", addrHash, account.Root, h)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if account, ok := accountUpdates[addrHash]; ok && account != nil {
|
|
|
|
ok, h := tds.t.DeepHash(addrHash[:])
|
|
|
|
if !ok {
|
|
|
|
h = trie.EmptyRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
if account.Root != h {
|
2019-11-15 22:48:49 +00:00
|
|
|
return nil, fmt.Errorf("mismatched storage root for %x: expected %x, got %x", addrHash, account.Root, h)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
// For the contracts that got deleted
|
2019-11-15 22:48:49 +00:00
|
|
|
for addrHash := range b.deleted {
|
|
|
|
if _, ok := b.created[addrHash]; ok {
|
2019-11-05 16:28:25 +00:00
|
|
|
// In some rather artificial circumstances, an account can be recreated after having been self-destructed
|
|
|
|
// in the same block. It can only happen when contract is introduced in the genesis state with nonce 0
|
|
|
|
// rather than created by a transaction (in that case, its starting nonce is 1). The self-destructed
|
|
|
|
// contract actually gets removed from the state only at the end of the block, so if its nonce is not 0,
|
|
|
|
// it will prevent any re-creation within the same block. However, if the contract is introduced in
|
|
|
|
// the genesis state, its nonce is 0, and that means it can be self-destructed, and then re-created,
|
|
|
|
// all in the same block. In such cases, we must preserve storage modifications happening after the
|
|
|
|
// self-destruction
|
|
|
|
continue
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
if account, ok := b.accountUpdates[addrHash]; ok && account != nil {
|
2019-11-05 16:28:25 +00:00
|
|
|
//fmt.Printf("(b)Set empty root for addrHash %x due to deleted\n", addrHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
account.Root = trie.EmptyRoot
|
|
|
|
}
|
|
|
|
if account, ok := accountUpdates[addrHash]; ok && account != nil {
|
2019-11-05 16:28:25 +00:00
|
|
|
//fmt.Printf("Set empty root for addrHash %x due to deleted\n", addrHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
account.Root = trie.EmptyRoot
|
|
|
|
}
|
2020-03-11 10:31:49 +00:00
|
|
|
|
2020-02-06 10:53:09 +00:00
|
|
|
tds.t.DeleteSubtree(addrHash[:])
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
roots[i] = tds.t.Hash()
|
|
|
|
}
|
2020-02-22 07:56:42 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
return roots, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) clearUpdates() {
|
|
|
|
tds.buffers = nil
|
|
|
|
tds.currentBuffer = nil
|
|
|
|
tds.aggregateBuffer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) SetBlockNr(blockNr uint64) {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.setBlockNr(blockNr)
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.tp.SetBlockNr(blockNr)
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
func (tds *TrieDbState) GetBlockNr() uint64 {
|
|
|
|
return tds.getBlockNr()
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) UnwindTo(blockNr uint64) error {
|
|
|
|
tds.StartNewBuffer()
|
|
|
|
b := tds.currentBuffer
|
2019-10-31 10:59:00 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
if err := tds.db.RewindData(tds.blockNr, blockNr, func(bucket, key, value []byte) error {
|
|
|
|
//fmt.Printf("bucket: %x, key: %x, value: %x\n", bucket, key, value)
|
|
|
|
if bytes.Equal(bucket, dbutils.AccountsHistoryBucket) {
|
|
|
|
var addrHash common.Hash
|
|
|
|
copy(addrHash[:], key)
|
|
|
|
if len(value) > 0 {
|
|
|
|
var acc accounts.Account
|
|
|
|
if err := acc.DecodeForStorage(value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-08 09:55:56 +00:00
|
|
|
// Fetch the code hash
|
|
|
|
if acc.Incarnation > 0 && debug.IsThinHistory() && acc.IsEmptyCodeHash() {
|
2020-01-15 14:04:16 +00:00
|
|
|
if codeHash, err := tds.db.Get(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash, acc.Incarnation)); err == nil {
|
2020-01-08 09:55:56 +00:00
|
|
|
copy(acc.CodeHash[:], codeHash)
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
b.accountUpdates[addrHash] = &acc
|
2020-01-15 14:04:16 +00:00
|
|
|
value = make([]byte, acc.EncodingLengthForStorage())
|
|
|
|
acc.EncodeForStorage(value)
|
2019-10-21 12:00:42 +00:00
|
|
|
if err := tds.db.Put(dbutils.AccountsBucket, addrHash[:], value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
b.accountUpdates[addrHash] = nil
|
2019-10-21 12:00:42 +00:00
|
|
|
if err := tds.db.Delete(dbutils.AccountsBucket, addrHash[:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
} else if bytes.Equal(bucket, dbutils.StorageHistoryBucket) {
|
2019-10-21 12:00:42 +00:00
|
|
|
var addrHash common.Hash
|
|
|
|
copy(addrHash[:], key[:common.HashLength])
|
2019-05-27 13:51:49 +00:00
|
|
|
var keyHash common.Hash
|
2020-01-15 14:55:43 +00:00
|
|
|
copy(keyHash[:], key[common.HashLength+common.IncarnationLength:])
|
2019-11-15 22:48:49 +00:00
|
|
|
m, ok := b.storageUpdates[addrHash]
|
2019-05-27 13:51:49 +00:00
|
|
|
if !ok {
|
|
|
|
m = make(map[common.Hash][]byte)
|
2019-11-15 22:48:49 +00:00
|
|
|
b.storageUpdates[addrHash] = m
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
if len(value) > 0 {
|
2019-11-13 09:52:43 +00:00
|
|
|
m[keyHash] = value
|
2020-01-15 14:55:43 +00:00
|
|
|
if err := tds.db.Put(dbutils.StorageBucket, key[:common.HashLength+common.IncarnationLength+common.HashLength], value); err != nil {
|
2019-10-21 12:00:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
m[keyHash] = nil
|
2020-01-15 14:55:43 +00:00
|
|
|
if err := tds.db.Delete(dbutils.StorageBucket, key[:common.HashLength+common.IncarnationLength+common.HashLength]); err != nil {
|
2019-10-21 12:00:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-01 16:04:41 +00:00
|
|
|
if _, err := tds.ResolveStateTrie(false, false); err != nil {
|
2019-10-21 12:54:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-10 13:12:21 +00:00
|
|
|
|
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
2019-10-21 12:54:47 +00:00
|
|
|
if _, err := tds.updateTrieRoots(false); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := tds.blockNr; i > blockNr; i-- {
|
|
|
|
if err := tds.db.DeleteTimestamp(i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-12-10 13:12:21 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
tds.clearUpdates()
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.setBlockNr(blockNr)
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) readAccountDataByHash(addrHash common.Hash) (*accounts.Account, error) {
|
2019-12-19 10:44:43 +00:00
|
|
|
if acc, ok := tds.GetAccount(addrHash); ok {
|
2019-05-27 13:51:49 +00:00
|
|
|
return acc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not present in the trie, try the database
|
|
|
|
var err error
|
|
|
|
var enc []byte
|
|
|
|
if tds.historical {
|
|
|
|
enc, err = tds.db.GetAsOf(dbutils.AccountsBucket, dbutils.AccountsHistoryBucket, addrHash[:], tds.blockNr+1)
|
|
|
|
if err != nil {
|
|
|
|
enc = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
enc, err = tds.db.Get(dbutils.AccountsBucket, addrHash[:])
|
|
|
|
if err != nil {
|
|
|
|
enc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(enc) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var a accounts.Account
|
|
|
|
if err := a.DecodeForStorage(enc); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2020-01-15 14:04:16 +00:00
|
|
|
if tds.historical && debug.IsThinHistory() && a.Incarnation > 0 {
|
2019-12-20 12:25:40 +00:00
|
|
|
codeHash, err := tds.db.Get(dbutils.ContractCodeBucket, dbutils.GenerateStoragePrefix(addrHash, a.Incarnation))
|
|
|
|
if err == nil {
|
|
|
|
a.CodeHash = common.BytesToHash(codeHash)
|
|
|
|
} else {
|
|
|
|
log.Error("Get code hash is incorrect", "err", err)
|
|
|
|
}
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
return &a, nil
|
|
|
|
}
|
|
|
|
|
2019-12-19 10:44:43 +00:00
|
|
|
func (tds *TrieDbState) GetAccount(addrHash common.Hash) (*accounts.Account, bool) {
|
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
acc, ok := tds.t.GetAccount(addrHash[:])
|
|
|
|
return acc, ok
|
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tds *TrieDbState) ReadAccountData(address common.Address) (*accounts.Account, error) {
|
|
|
|
addrHash, err := common.HashData(address[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tds.resolveReads {
|
|
|
|
if _, ok := tds.currentBuffer.accountUpdates[addrHash]; !ok {
|
|
|
|
tds.currentBuffer.accountReads[addrHash] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
return tds.readAccountDataByHash(addrHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) savePreimage(save bool, hash, preimage []byte) error {
|
2019-12-06 11:19:00 +00:00
|
|
|
if !save || !tds.savePreimages {
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Following check is to minimise the overwriting the same value of preimage
|
|
|
|
// in the database, which would cause extra write churn
|
|
|
|
if p, _ := tds.db.Get(dbutils.PreimagePrefix, hash); p != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return tds.db.Put(dbutils.PreimagePrefix, hash, preimage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) HashAddress(address common.Address, save bool) (common.Hash, error) {
|
|
|
|
addrHash, err := common.HashData(address[:])
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return addrHash, tds.savePreimage(save, addrHash[:], address[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) HashKey(key *common.Hash, save bool) (common.Hash, error) {
|
|
|
|
keyHash, err := common.HashData(key[:])
|
|
|
|
if err != nil {
|
|
|
|
return common.Hash{}, err
|
|
|
|
}
|
|
|
|
return keyHash, tds.savePreimage(save, keyHash[:], key[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) GetKey(shaKey []byte) []byte {
|
|
|
|
key, _ := tds.db.Get(dbutils.PreimagePrefix, shaKey)
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
|
2019-11-15 22:48:49 +00:00
|
|
|
addrHash, err := tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
if tds.currentBuffer != nil {
|
2019-11-15 22:48:49 +00:00
|
|
|
if _, ok := tds.currentBuffer.deleted[addrHash]; ok {
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tds.aggregateBuffer != nil {
|
2019-11-15 22:48:49 +00:00
|
|
|
if _, ok := tds.aggregateBuffer.deleted[addrHash]; ok {
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
seckey, err := tds.HashKey(key, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tds.resolveReads {
|
|
|
|
var addReadRecord = false
|
2019-11-15 22:48:49 +00:00
|
|
|
if mWrite, ok := tds.currentBuffer.storageUpdates[addrHash]; ok {
|
2019-05-27 13:51:49 +00:00
|
|
|
if _, ok1 := mWrite[seckey]; !ok1 {
|
|
|
|
addReadRecord = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
addReadRecord = true
|
|
|
|
}
|
|
|
|
if addReadRecord {
|
2019-11-15 22:48:49 +00:00
|
|
|
m, ok := tds.currentBuffer.storageReads[addrHash]
|
2019-05-27 13:51:49 +00:00
|
|
|
if !ok {
|
|
|
|
m = make(map[common.Hash]struct{})
|
2019-11-15 22:48:49 +00:00
|
|
|
tds.currentBuffer.storageReads[addrHash] = m
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
m[seckey] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
2020-02-06 10:53:09 +00:00
|
|
|
enc, ok := tds.t.Get(dbutils.GenerateCompositeTrieKey(addrHash, seckey))
|
2019-11-13 09:52:43 +00:00
|
|
|
if !ok {
|
2019-05-27 13:51:49 +00:00
|
|
|
// Not present in the trie, try database
|
|
|
|
if tds.historical {
|
|
|
|
enc, err = tds.db.GetAsOf(dbutils.StorageBucket, dbutils.StorageHistoryBucket, dbutils.GenerateCompositeStorageKey(addrHash, incarnation, seckey), tds.blockNr)
|
|
|
|
if err != nil {
|
|
|
|
enc = nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
enc, err = tds.db.Get(dbutils.StorageBucket, dbutils.GenerateCompositeStorageKey(addrHash, incarnation, seckey))
|
|
|
|
if err != nil {
|
|
|
|
enc = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return enc, nil
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) ReadCodeByHash(codeHash common.Hash) (code []byte, err error) {
|
|
|
|
if bytes.Equal(codeHash[:], emptyCodeHash) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
code, err = tds.db.Get(dbutils.CodeBucket, codeHash[:])
|
|
|
|
if tds.resolveReads {
|
|
|
|
// we have to be careful, because the code might change
|
|
|
|
// during the block executuion, so we are always
|
|
|
|
// storing the latest code hash
|
|
|
|
tds.resolveSetBuilder.ReadCode(codeHash)
|
|
|
|
}
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:56:24 +00:00
|
|
|
func (tds *TrieDbState) readAccountCodeFromTrie(addrHash []byte) ([]byte, bool) {
|
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
return tds.t.GetAccountCode(addrHash)
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
func (tds *TrieDbState) ReadAccountCode(address common.Address, codeHash common.Hash) (code []byte, err error) {
|
2019-05-27 13:51:49 +00:00
|
|
|
if bytes.Equal(codeHash[:], emptyCodeHash) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2020-03-23 22:10:36 +00:00
|
|
|
|
|
|
|
addrHash, err := tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:56:24 +00:00
|
|
|
if cached, ok := tds.readAccountCodeFromTrie(addrHash[:]); ok {
|
2020-03-23 22:10:36 +00:00
|
|
|
code, err = cached, nil
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
code, err = tds.db.Get(dbutils.CodeBucket, codeHash[:])
|
|
|
|
}
|
|
|
|
if tds.resolveReads {
|
2019-10-21 12:54:47 +00:00
|
|
|
addrHash, err1 := common.HashData(address[:])
|
|
|
|
if err1 != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, ok := tds.currentBuffer.accountUpdates[addrHash]; !ok {
|
|
|
|
tds.currentBuffer.accountReads[addrHash] = struct{}{}
|
|
|
|
}
|
2020-03-23 22:10:36 +00:00
|
|
|
// we have to be careful, because the code might change
|
|
|
|
// during the block executuion, so we are always
|
|
|
|
// storing the latest code hash
|
|
|
|
tds.currentBuffer.codeReads[addrHash] = codeHash
|
|
|
|
tds.resolveSetBuilder.ReadCode(codeHash)
|
2017-06-27 13:57:06 +00:00
|
|
|
}
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
func (tds *TrieDbState) ReadAccountCodeSize(address common.Address, codeHash common.Hash) (codeSize int, err error) {
|
2020-03-23 22:10:36 +00:00
|
|
|
addrHash, err := tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2020-03-28 13:56:24 +00:00
|
|
|
if code, ok := tds.readAccountCodeFromTrie(addrHash[:]); ok {
|
2020-03-23 22:10:36 +00:00
|
|
|
codeSize, err = len(code), nil
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
2019-10-21 12:54:47 +00:00
|
|
|
code, err = tds.ReadAccountCode(address, codeHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
codeSize = len(code)
|
|
|
|
}
|
|
|
|
if tds.resolveReads {
|
2019-10-21 12:54:47 +00:00
|
|
|
addrHash, err1 := common.HashData(address[:])
|
|
|
|
if err1 != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if _, ok := tds.currentBuffer.accountUpdates[addrHash]; !ok {
|
|
|
|
tds.currentBuffer.accountReads[addrHash] = struct{}{}
|
|
|
|
}
|
2020-03-23 22:10:36 +00:00
|
|
|
// we have to be careful, because the code might change
|
|
|
|
// during the block executuion, so we are always
|
|
|
|
// storing the latest code hash
|
|
|
|
tds.currentBuffer.codeReads[addrHash] = codeHash
|
|
|
|
tds.resolveSetBuilder.ReadCode(codeHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
return codeSize, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// nextIncarnation determines what should be the next incarnation of an account (i.e. how many time it has existed before at this address)
|
2019-11-15 22:48:49 +00:00
|
|
|
func (tds *TrieDbState) nextIncarnation(addrHash common.Hash) (uint64, error) {
|
2019-05-27 13:51:49 +00:00
|
|
|
var found bool
|
2020-01-15 14:55:43 +00:00
|
|
|
var incarnationBytes [common.IncarnationLength]byte
|
2019-05-27 13:51:49 +00:00
|
|
|
if tds.historical {
|
|
|
|
// We reserve ethdb.MaxTimestampLength (8) at the end of the key to accomodate any possible timestamp
|
|
|
|
// (timestamp's encoding may have variable length)
|
2020-01-15 14:55:43 +00:00
|
|
|
startkey := make([]byte, common.HashLength+common.IncarnationLength+common.HashLength+ethdb.MaxTimestampLength)
|
2019-05-27 13:51:49 +00:00
|
|
|
var fixedbits uint = 8 * common.HashLength
|
|
|
|
copy(startkey, addrHash[:])
|
2019-11-15 22:48:49 +00:00
|
|
|
if err := tds.db.WalkAsOf(dbutils.StorageBucket, dbutils.StorageHistoryBucket, startkey, fixedbits, tds.blockNr, func(k, _ []byte) (bool, error) {
|
2019-05-27 13:51:49 +00:00
|
|
|
copy(incarnationBytes[:], k[common.HashLength:])
|
|
|
|
found = true
|
|
|
|
return false, nil
|
2019-11-15 22:48:49 +00:00
|
|
|
}); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-10 17:05:32 +00:00
|
|
|
if inc, ok := tds.incarnationMap[addrHash]; ok {
|
|
|
|
return inc + 1, nil
|
|
|
|
}
|
2020-01-15 14:55:43 +00:00
|
|
|
startkey := make([]byte, common.HashLength+common.IncarnationLength+common.HashLength)
|
2019-05-27 13:51:49 +00:00
|
|
|
var fixedbits uint = 8 * common.HashLength
|
|
|
|
copy(startkey, addrHash[:])
|
2019-11-15 22:48:49 +00:00
|
|
|
if err := tds.db.Walk(dbutils.StorageBucket, startkey, fixedbits, func(k, v []byte) (bool, error) {
|
2019-05-27 13:51:49 +00:00
|
|
|
copy(incarnationBytes[:], k[common.HashLength:])
|
|
|
|
found = true
|
|
|
|
return false, nil
|
2019-11-15 22:48:49 +00:00
|
|
|
}); err != nil {
|
2019-05-27 13:51:49 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found {
|
2020-02-10 17:05:32 +00:00
|
|
|
return (^binary.BigEndian.Uint64(incarnationBytes[:])) + 1, nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-01-08 09:55:56 +00:00
|
|
|
return FirstContractIncarnation, nil
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prevMemStats runtime.MemStats
|
|
|
|
|
|
|
|
type TrieStateWriter struct {
|
|
|
|
tds *TrieDbState
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) PruneTries(print bool) {
|
2019-12-10 13:12:21 +00:00
|
|
|
tds.tMu.Lock()
|
2019-12-19 10:44:43 +00:00
|
|
|
defer tds.tMu.Unlock()
|
2020-02-10 17:05:32 +00:00
|
|
|
tds.incarnationMap = make(map[common.Hash]uint64)
|
2019-05-27 13:51:49 +00:00
|
|
|
if print {
|
|
|
|
prunableNodes := tds.t.CountPrunableNodes()
|
|
|
|
fmt.Printf("[Before] Actual prunable nodes: %d, accounted: %d\n", prunableNodes, tds.tp.NodeCount())
|
|
|
|
}
|
|
|
|
|
|
|
|
tds.tp.PruneTo(tds.t, int(MaxTrieCacheGen))
|
|
|
|
|
|
|
|
if print {
|
|
|
|
prunableNodes := tds.t.CountPrunableNodes()
|
|
|
|
fmt.Printf("[After] Actual prunable nodes: %d, accounted: %d\n", prunableNodes, tds.tp.NodeCount())
|
|
|
|
}
|
2019-12-10 13:12:21 +00:00
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
2020-02-08 22:18:20 +00:00
|
|
|
log.Info("Memory", "nodes", tds.tp.NodeCount(), "hashes", tds.t.HashMapSize(),
|
|
|
|
"alloc", int(m.Alloc/1024), "sys", int(m.Sys/1024), "numGC", int(m.NumGC))
|
2019-05-27 13:51:49 +00:00
|
|
|
if print {
|
|
|
|
fmt.Printf("Pruning done. Nodes: %d, alloc: %d, sys: %d, numGC: %d\n", tds.tp.NodeCount(), int(m.Alloc/1024), int(m.Sys/1024), int(m.NumGC))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) TrieStateWriter() *TrieStateWriter {
|
|
|
|
return &TrieStateWriter{tds: tds}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) DbStateWriter() *DbStateWriter {
|
|
|
|
return &DbStateWriter{tds: tds}
|
|
|
|
}
|
|
|
|
|
|
|
|
func accountsEqual(a1, a2 *accounts.Account) bool {
|
|
|
|
if a1.Nonce != a2.Nonce {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !a1.Initialised {
|
|
|
|
if a2.Initialised {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if !a2.Initialised {
|
|
|
|
return false
|
|
|
|
} else if a1.Balance.Cmp(&a2.Balance) != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a1.Root != a2.Root {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if a1.CodeHash == (common.Hash{}) {
|
|
|
|
if a2.CodeHash != (common.Hash{}) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
} else if a2.CodeHash == (common.Hash{}) {
|
|
|
|
return false
|
|
|
|
} else if a1.CodeHash != a2.CodeHash {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsw *TrieStateWriter) UpdateAccountData(_ context.Context, address common.Address, original, account *accounts.Account) error {
|
|
|
|
addrHash, err := tsw.tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tsw.tds.currentBuffer.accountUpdates[addrHash] = account
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsw *TrieStateWriter) DeleteAccount(_ context.Context, address common.Address, original *accounts.Account) error {
|
|
|
|
addrHash, err := tsw.tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tsw.tds.currentBuffer.accountUpdates[addrHash] = nil
|
2019-11-15 22:48:49 +00:00
|
|
|
delete(tsw.tds.currentBuffer.storageUpdates, addrHash)
|
|
|
|
tsw.tds.currentBuffer.deleted[addrHash] = struct{}{}
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-20 12:25:40 +00:00
|
|
|
func (tsw *TrieStateWriter) UpdateAccountCode(addrHash common.Hash, incarnation uint64, codeHash common.Hash, code []byte) error {
|
2019-05-27 13:51:49 +00:00
|
|
|
if tsw.tds.resolveReads {
|
2020-03-23 22:10:36 +00:00
|
|
|
tsw.tds.resolveSetBuilder.CreateCode(codeHash)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
2020-03-23 22:10:36 +00:00
|
|
|
tsw.tds.currentBuffer.codeUpdates[addrHash] = code
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tsw *TrieStateWriter) WriteAccountStorage(_ context.Context, address common.Address, incarnation uint64, key, original, value *common.Hash) error {
|
|
|
|
addrHash, err := tsw.tds.HashAddress(address, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v := bytes.TrimLeft(value[:], "\x00")
|
2019-11-15 22:48:49 +00:00
|
|
|
m, ok := tsw.tds.currentBuffer.storageUpdates[addrHash]
|
2019-05-27 13:51:49 +00:00
|
|
|
if !ok {
|
|
|
|
m = make(map[common.Hash][]byte)
|
2019-11-15 22:48:49 +00:00
|
|
|
tsw.tds.currentBuffer.storageUpdates[addrHash] = m
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
seckey, err := tsw.tds.HashKey(key, false /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(v) > 0 {
|
2019-11-13 09:52:43 +00:00
|
|
|
m[seckey] = v
|
2019-05-27 13:51:49 +00:00
|
|
|
} else {
|
|
|
|
m[seckey] = nil
|
|
|
|
}
|
|
|
|
//fmt.Printf("WriteAccountStorage %x %x: %x, buffer %d\n", addrHash, seckey, value, len(tsw.tds.buffers))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-21 12:54:47 +00:00
|
|
|
// ExtractWitness produces block witness for the block just been processed, in a serialised form
|
2020-01-15 13:56:50 +00:00
|
|
|
func (tds *TrieDbState) ExtractWitness(trace bool, isBinary bool) (*trie.Witness, error) {
|
2020-03-23 22:10:36 +00:00
|
|
|
rs := tds.resolveSetBuilder.Build(isBinary)
|
2019-12-10 13:12:21 +00:00
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
return tds.makeBlockWitness(trace, rs, isBinary)
|
2019-05-27 13:51:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-25 15:40:30 +00:00
|
|
|
// ExtractWitness produces block witness for the block just been processed, in a serialised form
|
|
|
|
func (tds *TrieDbState) ExtractWitnessForPrefix(prefix []byte, trace bool, isBinary bool) (*trie.Witness, error) {
|
|
|
|
rs := tds.resolveSetBuilder.Build(isBinary)
|
|
|
|
|
|
|
|
return tds.makeBlockWitnessForPrefix(prefix, trace, rs, isBinary)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) makeBlockWitnessForPrefix(prefix []byte, trace bool, rs *trie.ResolveSet, isBinary bool) (*trie.Witness, error) {
|
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
|
|
|
t := tds.t
|
|
|
|
if isBinary {
|
|
|
|
t = trie.HexToBin(tds.t).Trie()
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.ExtractWitnessForPrefix(prefix, tds.blockNr, trace, rs)
|
|
|
|
}
|
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
func (tds *TrieDbState) makeBlockWitness(trace bool, rs *trie.ResolveSet, isBinary bool) (*trie.Witness, error) {
|
2019-12-19 10:44:43 +00:00
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
2020-01-15 13:56:50 +00:00
|
|
|
t := tds.t
|
|
|
|
if isBinary {
|
|
|
|
t = trie.HexToBin(tds.t).Trie()
|
2019-12-19 10:44:43 +00:00
|
|
|
}
|
2020-01-15 13:56:50 +00:00
|
|
|
|
2020-03-23 22:10:36 +00:00
|
|
|
return t.ExtractWitness(tds.blockNr, trace, rs)
|
2019-12-19 10:44:43 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 13:51:49 +00:00
|
|
|
func (tsw *TrieStateWriter) CreateContract(address common.Address) error {
|
2019-11-15 22:48:49 +00:00
|
|
|
addrHash, err := tsw.tds.HashAddress(address, true /*save*/)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tsw.tds.currentBuffer.created[addrHash] = struct{}{}
|
2019-12-20 12:25:40 +00:00
|
|
|
if account, ok := tsw.tds.currentBuffer.accountUpdates[addrHash]; ok && account != nil {
|
2020-02-10 17:05:32 +00:00
|
|
|
incarnation, err := tsw.tds.nextIncarnation(addrHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-20 12:25:40 +00:00
|
|
|
account.SetIncarnation(incarnation)
|
2020-02-10 17:05:32 +00:00
|
|
|
tsw.tds.incarnationMap[addrHash] = incarnation
|
2019-12-20 12:25:40 +00:00
|
|
|
}
|
2019-05-27 13:51:49 +00:00
|
|
|
return nil
|
2018-02-05 16:40:32 +00:00
|
|
|
}
|
2019-10-18 12:11:50 +00:00
|
|
|
|
|
|
|
func (tds *TrieDbState) TriePruningDebugDump() string {
|
|
|
|
return tds.tp.DebugDump()
|
|
|
|
}
|
2019-12-10 13:12:21 +00:00
|
|
|
|
|
|
|
func (tds *TrieDbState) getBlockNr() uint64 {
|
|
|
|
return atomic.LoadUint64(&tds.blockNr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tds *TrieDbState) setBlockNr(n uint64) {
|
|
|
|
atomic.StoreUint64(&tds.blockNr, n)
|
|
|
|
}
|
2020-02-06 10:53:09 +00:00
|
|
|
|
|
|
|
// GetNodeByHash gets node's RLP by hash.
|
|
|
|
func (tds *TrieDbState) GetNodeByHash(hash common.Hash) []byte {
|
|
|
|
tds.tMu.Lock()
|
|
|
|
defer tds.tMu.Unlock()
|
|
|
|
|
|
|
|
return tds.t.GetNodeByHash(hash)
|
|
|
|
}
|