2017-06-27 08:57:06 -05: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"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
|
|
|
lru "github.com/hashicorp/golang-lru"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// 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 10:40:32 -06:00
|
|
|
|
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2017-06-27 08:57:06 -05:00
|
|
|
OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
|
2018-02-05 10:40:32 -06:00
|
|
|
|
2017-06-27 08:57:06 -05:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
|
|
|
CopyTrie(Trie) Trie
|
2018-02-05 10:40:32 -06: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 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
2019-03-14 08:25:12 -05:00
|
|
|
// Trie is a Ethereum Merkle Patricia trie.
|
2017-06-27 08:57:06 -05:00
|
|
|
type Trie interface {
|
2019-03-14 08:25:12 -05:00
|
|
|
// GetKey returns the sha3 preimage of a hashed key that was previously used
|
|
|
|
// to store a value.
|
|
|
|
//
|
|
|
|
// TODO(fjl): remove this when SecureTrie is removed
|
|
|
|
GetKey([]byte) []byte
|
|
|
|
|
|
|
|
// TryGet returns the value for key stored in the trie. The value bytes must
|
|
|
|
// not be modified by the caller. If a node was not found in the database, a
|
|
|
|
// trie.MissingNodeError is returned.
|
2017-06-27 08:57:06 -05:00
|
|
|
TryGet(key []byte) ([]byte, error)
|
2019-03-14 08:25:12 -05:00
|
|
|
|
|
|
|
// TryUpdate associates key with value in the trie. If value has length zero, any
|
|
|
|
// existing value is deleted from the trie. The value bytes must not be modified
|
|
|
|
// by the caller while they are stored in the trie. If a node was not found in the
|
|
|
|
// database, a trie.MissingNodeError is returned.
|
2017-06-27 08:57:06 -05:00
|
|
|
TryUpdate(key, value []byte) error
|
2019-03-14 08:25:12 -05:00
|
|
|
|
|
|
|
// TryDelete removes any existing value for key from the trie. If a node was not
|
|
|
|
// found in the database, a trie.MissingNodeError is returned.
|
2017-06-27 08:57:06 -05:00
|
|
|
TryDelete(key []byte) error
|
2019-03-14 08:25:12 -05:00
|
|
|
|
|
|
|
// Hash returns the root hash of the trie. It does not write to the database and
|
|
|
|
// can be used even if the trie doesn't have one.
|
2017-06-27 08:57:06 -05:00
|
|
|
Hash() common.Hash
|
2019-03-14 08:25:12 -05:00
|
|
|
|
|
|
|
// Commit writes all nodes to the trie's memory database, tracking the internal
|
|
|
|
// and external (for account tries) references.
|
|
|
|
Commit(onleaf trie.LeafCallback) (common.Hash, error)
|
|
|
|
|
|
|
|
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
|
|
|
|
// starts at the key after the given start key.
|
2017-06-27 08:57:06 -05:00
|
|
|
NodeIterator(startKey []byte) trie.NodeIterator
|
2019-03-14 08:25:12 -05:00
|
|
|
|
|
|
|
// Prove constructs a Merkle proof for key. The result contains all encoded nodes
|
|
|
|
// on the path to the value at key. The value itself is also included in the last
|
|
|
|
// node and can be retrieved by verifying the proof.
|
|
|
|
//
|
|
|
|
// If the trie does not contain a value for key, the returned proof contains all
|
|
|
|
// nodes of the longest existing prefix of the key (at least the root), ending
|
|
|
|
// with the node that proves the absence of the key.
|
2018-09-24 07:57:49 -05:00
|
|
|
Prove(key []byte, fromLevel uint, proofDb ethdb.Writer) error
|
2017-06-27 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase creates a backing store for state. The returned database is safe for
|
2019-03-14 08:25:12 -05:00
|
|
|
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
|
|
|
|
// historical state in memory, use the NewDatabaseWithCache constructor.
|
2017-06-27 08:57:06 -05:00
|
|
|
func NewDatabase(db ethdb.Database) Database {
|
2018-11-12 10:47:34 -06:00
|
|
|
return NewDatabaseWithCache(db, 0)
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:25:12 -05:00
|
|
|
// NewDatabaseWithCache creates a backing store for state. The returned database
|
|
|
|
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
|
|
|
|
// large memory cache.
|
2018-11-12 10:47:34 -06:00
|
|
|
func NewDatabaseWithCache(db ethdb.Database, cache int) Database {
|
2017-06-27 08:57:06 -05:00
|
|
|
csc, _ := lru.New(codeSizeCacheSize)
|
2018-02-05 10:40:32 -06:00
|
|
|
return &cachingDB{
|
2018-11-12 10:47:34 -06:00
|
|
|
db: trie.NewDatabaseWithCache(db, cache),
|
2018-02-05 10:40:32 -06:00
|
|
|
codeSizeCache: csc,
|
|
|
|
}
|
2017-06-27 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachingDB struct {
|
2018-02-05 10:40:32 -06:00
|
|
|
db *trie.Database
|
2017-06-27 08:57:06 -05:00
|
|
|
codeSizeCache *lru.Cache
|
|
|
|
}
|
|
|
|
|
2019-03-14 08:25:12 -05:00
|
|
|
// OpenTrie opens the main account trie at a specific root hash.
|
2017-06-27 08:57:06 -05:00
|
|
|
func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
|
2019-03-14 08:25:12 -05:00
|
|
|
return trie.NewSecure(root, db.db)
|
2017-06-27 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// OpenStorageTrie opens the storage trie of an account.
|
2017-06-27 08:57:06 -05:00
|
|
|
func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
|
2019-03-14 08:25:12 -05:00
|
|
|
return trie.NewSecure(root, db.db)
|
2017-06-27 08:57:06 -05:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// CopyTrie returns an independent copy of the given trie.
|
2017-06-27 08:57:06 -05:00
|
|
|
func (db *cachingDB) CopyTrie(t Trie) Trie {
|
|
|
|
switch t := t.(type) {
|
|
|
|
case *trie.SecureTrie:
|
|
|
|
return t.Copy()
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unknown trie type %T", t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// ContractCode retrieves a particular contract's code.
|
2017-06-27 08:57:06 -05:00
|
|
|
func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
|
2018-02-05 10:40:32 -06:00
|
|
|
code, err := db.db.Node(codeHash)
|
2017-06-27 08:57:06 -05:00
|
|
|
if err == nil {
|
|
|
|
db.codeSizeCache.Add(codeHash, len(code))
|
|
|
|
}
|
|
|
|
return code, err
|
|
|
|
}
|
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// ContractCodeSize retrieves a particular contracts code's size.
|
2017-06-27 08:57:06 -05: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 10:40:32 -06:00
|
|
|
// TrieDB retrieves any intermediate trie-node caching layer.
|
|
|
|
func (db *cachingDB) TrieDB() *trie.Database {
|
|
|
|
return db.db
|
|
|
|
}
|