2017-06-27 16:57:06 +03:00
|
|
|
// Copyright 2017 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 (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
)
|
|
|
|
|
2018-04-03 18:13:19 +03:00
|
|
|
// Trie cache generation limit after which to evict trie nodes from memory.
|
2017-06-27 16:57:06 +03:00
|
|
|
var MaxTrieCacheGen = uint16(120)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Number of past tries to keep. This value is chosen such that
|
|
|
|
// reasonable chain reorg depths will hit an existing trie.
|
|
|
|
maxPastTries = 12
|
|
|
|
|
|
|
|
// Number of codehash->size associations to keep.
|
|
|
|
codeSizeCacheSize = 100000
|
|
|
|
)
|
|
|
|
|
|
|
|
// Database wraps access to tries and contract code.
|
|
|
|
type Database interface {
|
|
|
|
// OpenTrie opens the main account trie.
|
|
|
|
OpenTrie(root common.Hash) (Trie, error)
|
2018-02-05 19:40:32 +03:00
|
|
|
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2017-06-27 16:57:06 +03:00
|
|
|
OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
|
2018-02-05 19:40:32 +03:00
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
|
|
|
CopyTrie(Trie) Trie
|
2018-02-05 19:40:32 +03:00
|
|
|
|
|
|
|
// ContractCode retrieves a particular contract's code.
|
|
|
|
ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
|
|
|
|
|
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
|
|
|
ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
|
|
|
|
|
|
|
|
// TrieDB retrieves the low level trie database used for data storage.
|
|
|
|
TrieDB() *trie.Database
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Trie is a Ethereum Merkle Trie.
|
|
|
|
type Trie interface {
|
|
|
|
TryGet(key []byte) ([]byte, error)
|
|
|
|
TryUpdate(key, value []byte) error
|
|
|
|
TryDelete(key []byte) error
|
2018-02-05 19:40:32 +03:00
|
|
|
Commit(onleaf trie.LeafCallback) (common.Hash, error)
|
2017-06-27 16:57:06 +03:00
|
|
|
Hash() common.Hash
|
|
|
|
NodeIterator(startKey []byte) trie.NodeIterator
|
|
|
|
GetKey([]byte) []byte // TODO(fjl): remove this when SecureTrie is removed
|
2018-02-05 19:40:32 +03:00
|
|
|
Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase creates a backing store for state. The returned database is safe for
|
2018-02-05 19:40:32 +03:00
|
|
|
// concurrent use and retains cached trie nodes in memory. The pool is an optional
|
|
|
|
// intermediate trie-node memory pool between the low level storage layer and the
|
|
|
|
// high level trie abstraction.
|
2017-06-27 16:57:06 +03:00
|
|
|
func NewDatabase(db ethdb.Database) Database {
|
|
|
|
csc, _ := lru.New(codeSizeCacheSize)
|
2018-02-05 19:40:32 +03:00
|
|
|
return &cachingDB{
|
|
|
|
db: trie.NewDatabase(db),
|
|
|
|
codeSizeCache: csc,
|
|
|
|
}
|
2017-06-27 16:57:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachingDB struct {
|
2018-02-05 19:40:32 +03:00
|
|
|
db *trie.Database
|
2017-06-27 16:57:06 +03:00
|
|
|
mu sync.Mutex
|
|
|
|
pastTries []*trie.SecureTrie
|
|
|
|
codeSizeCache *lru.Cache
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// OpenTrie opens the main account trie.
|
2017-06-27 16:57:06 +03:00
|
|
|
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
|
|
|
|
for i := len(db.pastTries) - 1; i >= 0; i-- {
|
|
|
|
if db.pastTries[i].Hash() == root {
|
|
|
|
return cachedTrie{db.pastTries[i].Copy(), db}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr, err := trie.NewSecure(root, db.db, MaxTrieCacheGen)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cachedTrie{tr, db}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *cachingDB) pushTrie(t *trie.SecureTrie) {
|
|
|
|
db.mu.Lock()
|
|
|
|
defer db.mu.Unlock()
|
|
|
|
|
|
|
|
if len(db.pastTries) >= maxPastTries {
|
|
|
|
copy(db.pastTries, db.pastTries[1:])
|
|
|
|
db.pastTries[len(db.pastTries)-1] = t
|
|
|
|
} else {
|
|
|
|
db.pastTries = append(db.pastTries, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2017-06-27 16:57:06 +03:00
|
|
|
func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
|
|
|
|
return trie.NewSecure(root, db.db, 0)
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
2017-06-27 16:57:06 +03:00
|
|
|
func (db *cachingDB) CopyTrie(t Trie) Trie {
|
|
|
|
switch t := t.(type) {
|
|
|
|
case cachedTrie:
|
|
|
|
return cachedTrie{t.SecureTrie.Copy(), db}
|
|
|
|
case *trie.SecureTrie:
|
|
|
|
return t.Copy()
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown trie type %T", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// ContractCode retrieves a particular contract's code.
|
2017-06-27 16:57:06 +03:00
|
|
|
func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
|
2018-02-05 19:40:32 +03:00
|
|
|
code, err := db.db.Node(codeHash)
|
2017-06-27 16:57:06 +03:00
|
|
|
if err == nil {
|
|
|
|
db.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
}
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
2017-06-27 16:57:06 +03:00
|
|
|
func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
|
|
|
|
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
|
|
|
|
return cached.(int), nil
|
|
|
|
}
|
|
|
|
code, err := db.ContractCode(addrHash, codeHash)
|
|
|
|
return len(code), err
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
// TrieDB retrieves any intermediate trie-node caching layer.
|
|
|
|
func (db *cachingDB) TrieDB() *trie.Database {
|
|
|
|
return db.db
|
|
|
|
}
|
|
|
|
|
2017-06-27 16:57:06 +03:00
|
|
|
// cachedTrie inserts its trie into a cachingDB on commit.
|
|
|
|
type cachedTrie struct {
|
|
|
|
*trie.SecureTrie
|
|
|
|
db *cachingDB
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:40:32 +03:00
|
|
|
func (m cachedTrie) Commit(onleaf trie.LeafCallback) (common.Hash, error) {
|
|
|
|
root, err := m.SecureTrie.Commit(onleaf)
|
2017-06-27 16:57:06 +03:00
|
|
|
if err == nil {
|
|
|
|
m.db.pushTrie(m.SecureTrie)
|
|
|
|
}
|
|
|
|
return root, err
|
|
|
|
}
|
2018-02-05 19:40:32 +03:00
|
|
|
|
|
|
|
func (m cachedTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.Putter) error {
|
|
|
|
return m.SecureTrie.Prove(key, fromLevel, proofDb)
|
|
|
|
}
|