Update database and baseapp initialization

This commit is contained in:
Aleksandr Bezobchuk 2018-09-07 13:34:29 -04:00
parent dbf3fcfc94
commit 2d9592ffaa
5 changed files with 83 additions and 47 deletions

View File

@ -2,6 +2,7 @@ package app
import (
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
@ -13,6 +14,7 @@ import (
"github.com/pkg/errors"
"github.com/cosmos/ethermint/handlers"
"github.com/cosmos/ethermint/state"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
@ -35,8 +37,10 @@ type (
*bam.BaseApp
codec *wire.Codec
ethDB *state.Database
accountKey *sdk.KVStoreKey
storageKey *sdk.KVStoreKey
mainKey *sdk.KVStoreKey
stakeKey *sdk.KVStoreKey
slashingKey *sdk.KVStoreKey
@ -57,23 +61,36 @@ type (
// NewEthermintApp returns a reference to a new initialized Ethermint
// application.
func NewEthermintApp(
logger tmlog.Logger, db dbm.DB, sdkAddr ethcmn.Address, baseAppOpts ...func(*bam.BaseApp),
) *EthermintApp {
func NewEthermintApp(logger tmlog.Logger, db dbm.DB, sdkAddr ethcmn.Address) *EthermintApp {
codec := CreateCodec()
app := &EthermintApp{
BaseApp: bam.NewBaseApp(appName, logger, db, types.TxDecoder(codec, sdkAddr), baseAppOpts...),
codec: codec,
accountKey: sdk.NewKVStoreKey("acc"),
mainKey: sdk.NewKVStoreKey("main"),
stakeKey: sdk.NewKVStoreKey("stake"),
slashingKey: sdk.NewKVStoreKey("slashing"),
govKey: sdk.NewKVStoreKey("gov"),
feeCollKey: sdk.NewKVStoreKey("fee"),
paramsKey: sdk.NewKVStoreKey("params"),
tParamsKey: sdk.NewTransientStoreKey("transient_params"),
cms := store.NewCommitMultiStore(db)
baseAppOpts := []func(*bam.BaseApp){
func(bApp *bam.BaseApp) { bApp.SetCMS(cms) },
}
baseApp := bam.NewBaseApp(appName, logger, db, types.TxDecoder(codec, sdkAddr), baseAppOpts...)
app := &EthermintApp{
BaseApp: baseApp,
codec: codec,
accountKey: types.StoreKeyAccount,
storageKey: types.StoreKeyStorage,
mainKey: types.StoreKeyMain,
stakeKey: types.StoreKeyStake,
slashingKey: types.StoreKeySlashing,
govKey: types.StoreKeyGov,
feeCollKey: types.StoreKeyFeeColl,
paramsKey: types.StoreKeyParams,
tParamsKey: types.StoreKeyTransParams,
}
// create Ethereum state database
ethDB, err := state.NewDatabase(cms, db, state.DefaultStoreCacheSize)
if err != nil {
tmcmn.Exit(err.Error())
}
app.ethDB = ethDB
// set application keepers and mappers
app.accountMapper = auth.NewAccountMapper(codec, app.accountKey, auth.ProtoBaseAccount)
@ -108,7 +125,7 @@ func NewEthermintApp(
app.MountStoresIAVL(
app.mainKey, app.accountKey, app.stakeKey, app.slashingKey,
app.govKey, app.feeCollKey, app.paramsKey,
app.govKey, app.feeCollKey, app.paramsKey, app.storageKey,
)
app.MountStore(app.tParamsKey, sdk.StoreTypeTransient)

View File

@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/core"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
ethstate "github.com/ethereum/go-ethereum/core/state"
@ -16,20 +17,16 @@ import (
)
var (
// AccountsKey is the key used for storing Ethereum accounts in the Cosmos
// SDK multi-store.
AccountsKey = sdk.NewKVStoreKey("account")
// StorageKey is the key used for storing Ethereum contract storage in the
// Cosmos SDK multi-store.
StorageKey = sdk.NewKVStoreKey("storage")
// CodeKey is the key used for storing Ethereum contract code in the Cosmos
// SDK multi-store.
CodeKey = sdk.NewKVStoreKey("code")
)
const (
// DefaultStoreCacheSize defines the default number of key/value pairs for
// the state stored in memory.
DefaultStoreCacheSize = 1024 * 1024
// codeSizeCacheSize is the number of codehash to size associations to
// keep in cached memory. This is to address any DoS attempts on
// EXTCODESIZE calls.
@ -68,25 +65,8 @@ type Database struct {
// 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.
func NewDatabase(stateDB, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
// Initialize an implementation of Ethereum state.Database and create a
// Cosmos SDK multi-store.
db := &Database{
stateStore: store.NewCommitMultiStore(stateDB),
}
// currently do not prune any historical state
db.stateStore.SetPruning(sdk.PruneNothing)
// Create the underlying multi-store stores that will persist account and
// account storage data.
db.stateStore.MountStoreWithDB(AccountsKey, sdk.StoreTypeIAVL, nil)
db.stateStore.MountStoreWithDB(StorageKey, sdk.StoreTypeIAVL, nil)
// Load the latest account state from the Cosmos SDK multi-store.
if err := db.stateStore.LoadLatestVersion(); err != nil {
return nil, err
}
func NewDatabase(stateStore store.CommitMultiStore, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
db := &Database{stateStore: stateStore}
// Set the persistent Cosmos SDK Database and initialize an Ethereum
// trie.Database using an EthereumDB as the underlying implementation of
@ -135,8 +115,8 @@ func (db *Database) OpenTrie(root ethcmn.Hash) (ethstate.Trie, error) {
}
if db.accountsCache == nil {
db.accountsCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(AccountsKey))
db.storageCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(StorageKey))
db.accountsCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyAccount))
db.storageCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyStorage))
}
return &Trie{

View File

@ -5,6 +5,9 @@ import (
"math/rand"
"time"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
dbm "github.com/tendermint/tendermint/libs/db"
@ -28,10 +31,17 @@ func init() {
func newTestDatabase() *Database {
memDB := dbm.NewMemDB()
testDB, err := NewDatabase(memDB, memDB, 100)
cms := store.NewCommitMultiStore(memDB)
cms.SetPruning(sdk.PruneNothing)
cms.MountStoreWithDB(types.StoreKeyAccount, sdk.StoreTypeIAVL, nil)
cms.MountStoreWithDB(types.StoreKeyStorage, sdk.StoreTypeIAVL, nil)
testDB, err := NewDatabase(cms, memDB, 100)
if err != nil {
panic(fmt.Sprintf("failed to create database: %v", err))
}
testDB.stateStore.LoadLatestVersion()
return testDB
}

View File

@ -9,8 +9,12 @@ import (
"runtime/pprof"
"syscall"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/state"
"github.com/cosmos/ethermint/test/importer"
"github.com/cosmos/ethermint/types"
dbm "github.com/tendermint/tendermint/libs/db"
)
@ -52,7 +56,16 @@ func main() {
stateDB := dbm.NewDB("state", dbm.LevelDBBackend, *datadir)
codeDB := dbm.NewDB("code", dbm.LevelDBBackend, *datadir)
ethermintDB, err := state.NewDatabase(stateDB, codeDB, *cachesize)
cms := store.NewCommitMultiStore(stateDB)
cms.SetPruning(sdk.PruneNothing)
cms.MountStoreWithDB(types.StoreKeyAccount, sdk.StoreTypeIAVL, nil)
cms.MountStoreWithDB(types.StoreKeyStorage, sdk.StoreTypeIAVL, nil)
if err := cms.LoadLatestVersion(); err != nil {
panic(fmt.Sprintf("failed to load state store: %v", err))
}
ethermintDB, err := state.NewDatabase(cms, codeDB, *cachesize)
if err != nil {
panic(fmt.Sprintf("failed to initialize geth Database: %v", err))
}

16
types/store_keys.go Normal file
View File

@ -0,0 +1,16 @@
package types
import sdk "github.com/cosmos/cosmos-sdk/types"
// Application multi-store keys
var (
StoreKeyAccount = sdk.NewKVStoreKey("acc")
StoreKeyStorage = sdk.NewKVStoreKey("contract_storage")
StoreKeyMain = sdk.NewKVStoreKey("main")
StoreKeyStake = sdk.NewKVStoreKey("stake")
StoreKeySlashing = sdk.NewKVStoreKey("slashing")
StoreKeyGov = sdk.NewKVStoreKey("gov")
StoreKeyFeeColl = sdk.NewKVStoreKey("fee")
StoreKeyParams = sdk.NewKVStoreKey("params")
StoreKeyTransParams = sdk.NewTransientStoreKey("transient_params")
)