2018-07-04 23:38:20 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/store"
|
2018-07-18 02:07:02 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2018-07-18 18:14:14 +00:00
|
|
|
|
2018-09-07 17:34:29 +00:00
|
|
|
"github.com/cosmos/ethermint/types"
|
2018-07-18 18:14:14 +00:00
|
|
|
|
2018-07-26 13:36:01 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2018-07-04 23:38:20 +00:00
|
|
|
ethstate "github.com/ethereum/go-ethereum/core/state"
|
|
|
|
ethtrie "github.com/ethereum/go-ethereum/trie"
|
2018-07-18 18:14:14 +00:00
|
|
|
|
2018-07-17 14:34:32 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru"
|
2018-07-18 18:14:14 +00:00
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
)
|
|
|
|
|
2018-10-04 20:11:39 +00:00
|
|
|
// TODO: This functionality and implementation may be deprecated
|
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
var (
|
|
|
|
// CodeKey is the key used for storing Ethereum contract code in the Cosmos
|
|
|
|
// SDK multi-store.
|
2018-07-18 18:14:14 +00:00
|
|
|
CodeKey = sdk.NewKVStoreKey("code")
|
2018-07-04 23:38:20 +00:00
|
|
|
)
|
|
|
|
|
2018-07-17 14:34:32 +00:00
|
|
|
const (
|
2018-09-07 17:34:29 +00:00
|
|
|
// DefaultStoreCacheSize defines the default number of key/value pairs for
|
|
|
|
// the state stored in memory.
|
|
|
|
DefaultStoreCacheSize = 1024 * 1024
|
|
|
|
|
2018-07-17 14:34:32 +00:00
|
|
|
// codeSizeCacheSize is the number of codehash to size associations to
|
|
|
|
// keep in cached memory. This is to address any DoS attempts on
|
|
|
|
// EXTCODESIZE calls.
|
|
|
|
codeSizeCacheSize = 100000
|
|
|
|
)
|
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
// Database implements the Ethereum state.Database interface.
|
|
|
|
type Database struct {
|
|
|
|
// stateStore will be used for the history of accounts (balance, nonce,
|
|
|
|
// storage root hash, code hash) and for the history of contract data
|
|
|
|
// (effects of SSTORE instruction).
|
|
|
|
stateStore store.CommitMultiStore
|
|
|
|
accountsCache store.CacheKVStore
|
|
|
|
storageCache store.CacheKVStore
|
|
|
|
|
2018-07-05 01:00:29 +00:00
|
|
|
// codeDB contains mappings of codeHash => code
|
2018-07-04 23:38:20 +00:00
|
|
|
//
|
|
|
|
// NOTE: This database will store the information in memory until is it
|
|
|
|
// committed, using the function Commit. This function is called outside of
|
|
|
|
// the ApplyTransaction function, therefore in Ethermint we need to make
|
|
|
|
// sure this commit is invoked somewhere after each block or whatever the
|
|
|
|
// appropriate time for it.
|
|
|
|
codeDB dbm.DB
|
|
|
|
ethTrieDB *ethtrie.Database
|
|
|
|
|
2018-07-17 14:34:32 +00:00
|
|
|
// codeSizeCache contains an LRU cache of a specified capacity to cache
|
|
|
|
// EXTCODESIZE calls.
|
|
|
|
codeSizeCache *lru.Cache
|
|
|
|
|
2018-08-15 22:27:30 +00:00
|
|
|
storeCache *lru.Cache
|
2018-08-09 19:49:40 +00:00
|
|
|
|
2018-07-05 15:05:43 +00:00
|
|
|
Tracing bool
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDatabase returns a reference to an initialized Database type which
|
|
|
|
// implements Ethereum's state.Database interface. An error is returned if the
|
|
|
|
// latest state failed to load. The underlying storage structure is defined by
|
|
|
|
// the Cosmos SDK IAVL tree.
|
2018-09-07 17:34:29 +00:00
|
|
|
func NewDatabase(stateStore store.CommitMultiStore, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
|
|
|
|
db := &Database{stateStore: stateStore}
|
2018-07-04 23:38:20 +00:00
|
|
|
|
|
|
|
// Set the persistent Cosmos SDK Database and initialize an Ethereum
|
|
|
|
// trie.Database using an EthereumDB as the underlying implementation of
|
|
|
|
// the ethdb.Database interface. It will be used to facilitate persistence
|
|
|
|
// of contract byte code when committing state.
|
|
|
|
db.codeDB = codeDB
|
2018-10-03 00:22:15 +00:00
|
|
|
db.ethTrieDB = ethtrie.NewDatabase(&EthereumDB{CodeDB: codeDB})
|
2018-07-04 23:38:20 +00:00
|
|
|
|
2018-08-10 13:22:59 +00:00
|
|
|
var err error
|
2018-08-15 22:27:30 +00:00
|
|
|
|
2018-08-10 13:22:59 +00:00
|
|
|
if db.codeSizeCache, err = lru.New(codeSizeCacheSize); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-08-15 22:27:30 +00:00
|
|
|
|
2018-08-10 13:22:59 +00:00
|
|
|
if db.storeCache, err = lru.New(storeCacheSize); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-17 14:34:32 +00:00
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
return db, nil
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:14:14 +00:00
|
|
|
// LatestVersion returns the latest version of the underlying mult-store.
|
2018-07-18 12:16:41 +00:00
|
|
|
func (db *Database) LatestVersion() int64 {
|
|
|
|
return db.stateStore.LastCommitID().Version
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
// OpenTrie implements Ethereum's state.Database interface. It returns a Trie
|
|
|
|
// type which implements the Ethereum state.Trie interface. It us used for
|
|
|
|
// storage of accounts. An error is returned if state cannot load for a
|
|
|
|
// given version. The account cache is reset if the state is successfully
|
|
|
|
// loaded and the version is not the latest.
|
|
|
|
//
|
|
|
|
// CONTRACT: The root parameter is not interpreted as a state root hash, but as
|
|
|
|
// an encoding of an Cosmos SDK IAVL tree version.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (db *Database) OpenTrie(root ethcmn.Hash) (ethstate.Trie, error) {
|
2018-07-04 23:38:20 +00:00
|
|
|
if !isRootEmpty(root) {
|
2018-07-26 13:36:01 +00:00
|
|
|
version := versionFromRootHash(root)
|
2018-07-04 23:38:20 +00:00
|
|
|
|
|
|
|
if db.stateStore.LastCommitID().Version != version {
|
|
|
|
if err := db.stateStore.LoadVersion(version); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-05 15:36:53 +00:00
|
|
|
db.accountsCache = nil
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 15:36:53 +00:00
|
|
|
if db.accountsCache == nil {
|
2018-09-07 17:34:29 +00:00
|
|
|
db.accountsCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyAccount))
|
|
|
|
db.storageCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyStorage))
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Trie{
|
2018-07-05 01:00:29 +00:00
|
|
|
store: db.accountsCache,
|
|
|
|
accountsCache: db.accountsCache,
|
|
|
|
storageCache: db.storageCache,
|
2018-08-10 13:22:59 +00:00
|
|
|
storeCache: db.storeCache,
|
2018-07-05 01:00:29 +00:00
|
|
|
ethTrieDB: db.ethTrieDB,
|
|
|
|
empty: isRootEmpty(root),
|
|
|
|
root: rootHashFromVersion(db.stateStore.LastCommitID().Version),
|
2018-07-04 23:38:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenStorageTrie implements Ethereum's state.Database interface. It returns
|
|
|
|
// a Trie type which implements the Ethereum state.Trie interface. It is used
|
2018-07-05 01:00:29 +00:00
|
|
|
// for storage of contract storage (state). Also, this trie is never committed
|
|
|
|
// separately as all the data is in a single multi-store and is committed when
|
|
|
|
// the account IAVL tree is committed.
|
2018-07-04 23:38:20 +00:00
|
|
|
//
|
|
|
|
// NOTE: It is assumed that the account state has already been loaded via
|
|
|
|
// OpenTrie.
|
|
|
|
//
|
|
|
|
// CONTRACT: The root parameter is not interpreted as a state root hash, but as
|
|
|
|
// an encoding of an IAVL tree version.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (db *Database) OpenStorageTrie(addrHash, root ethcmn.Hash) (ethstate.Trie, error) {
|
2018-07-05 01:00:29 +00:00
|
|
|
// a contract storage trie does not need an accountCache, storageCache or
|
|
|
|
// an Ethereum trie because it will not be used upon commitment.
|
2018-07-04 23:38:20 +00:00
|
|
|
return &Trie{
|
2018-08-10 13:22:59 +00:00
|
|
|
store: db.storageCache,
|
|
|
|
storeCache: db.storeCache,
|
|
|
|
prefix: addrHash.Bytes(),
|
|
|
|
empty: isRootEmpty(root),
|
|
|
|
root: rootHashFromVersion(db.stateStore.LastCommitID().Version),
|
2018-07-04 23:38:20 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyTrie implements Ethereum's state.Database interface. For now, it
|
|
|
|
// performs a no-op as the underlying Cosmos SDK IAVL tree does not support
|
|
|
|
// such an operation.
|
|
|
|
//
|
|
|
|
// TODO: Does the IAVL tree need to support this operation? If so, why and
|
|
|
|
// how?
|
|
|
|
func (db *Database) CopyTrie(ethstate.Trie) ethstate.Trie {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ContractCode implements Ethereum's state.Database interface. It will return
|
|
|
|
// the contract byte code for a given code hash. It will not return an error.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (db *Database) ContractCode(addrHash, codeHash ethcmn.Hash) ([]byte, error) {
|
2018-07-17 14:34:32 +00:00
|
|
|
code := db.codeDB.Get(codeHash[:])
|
|
|
|
|
2018-07-26 13:36:01 +00:00
|
|
|
if codeLen := len(code); codeLen != 0 {
|
|
|
|
db.codeSizeCache.Add(codeHash, codeLen)
|
|
|
|
}
|
|
|
|
|
2018-07-17 14:34:32 +00:00
|
|
|
return code, nil
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ContractCodeSize implements Ethereum's state.Database interface. It will
|
|
|
|
// return the contract byte code size for a given code hash. It will not return
|
|
|
|
// an error.
|
2018-07-26 13:36:01 +00:00
|
|
|
func (db *Database) ContractCodeSize(addrHash, codeHash ethcmn.Hash) (int, error) {
|
2018-07-17 14:34:32 +00:00
|
|
|
if cached, ok := db.codeSizeCache.Get(codeHash); ok {
|
|
|
|
return cached.(int), nil
|
|
|
|
}
|
|
|
|
|
2018-07-17 15:18:14 +00:00
|
|
|
code, err := db.ContractCode(addrHash, codeHash)
|
|
|
|
return len(code), err
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 15:05:43 +00:00
|
|
|
// Commit commits the underlying Cosmos SDK multi-store returning the commit
|
|
|
|
// ID.
|
2018-07-18 18:14:14 +00:00
|
|
|
func (db *Database) Commit() sdk.CommitID {
|
2018-07-05 15:05:43 +00:00
|
|
|
return db.stateStore.Commit()
|
|
|
|
}
|
|
|
|
|
2018-07-04 23:38:20 +00:00
|
|
|
// TrieDB implements Ethereum's state.Database interface. It returns Ethereum's
|
2018-07-05 01:00:29 +00:00
|
|
|
// trie.Database low level trie database used for contract state storage. In
|
|
|
|
// the context of Ethermint, it'll be used to solely store mappings of
|
|
|
|
// codeHash => code.
|
2018-07-04 23:38:20 +00:00
|
|
|
func (db *Database) TrieDB() *ethtrie.Database {
|
2018-07-05 01:00:29 +00:00
|
|
|
return db.ethTrieDB
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// isRootEmpty returns true if a given root hash is empty or false otherwise.
|
2018-07-26 13:36:01 +00:00
|
|
|
func isRootEmpty(root ethcmn.Hash) bool {
|
|
|
|
return root == ethcmn.Hash{}
|
2018-07-04 23:38:20 +00:00
|
|
|
}
|