forked from cerc-io/laconicd-deprecated
Merge pull request #481 from cosmos/bez/refactor-db-app
R4R: Update database and baseapp Initialization
This commit is contained in:
commit
6e1a73bcad
@ -2,6 +2,7 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
||||||
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
"github.com/cosmos/cosmos-sdk/wire"
|
"github.com/cosmos/cosmos-sdk/wire"
|
||||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
@ -13,6 +14,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/cosmos/ethermint/handlers"
|
"github.com/cosmos/ethermint/handlers"
|
||||||
|
"github.com/cosmos/ethermint/state"
|
||||||
"github.com/cosmos/ethermint/types"
|
"github.com/cosmos/ethermint/types"
|
||||||
|
|
||||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||||
@ -35,8 +37,10 @@ type (
|
|||||||
*bam.BaseApp
|
*bam.BaseApp
|
||||||
|
|
||||||
codec *wire.Codec
|
codec *wire.Codec
|
||||||
|
ethDB *state.Database
|
||||||
|
|
||||||
accountKey *sdk.KVStoreKey
|
accountKey *sdk.KVStoreKey
|
||||||
|
storageKey *sdk.KVStoreKey
|
||||||
mainKey *sdk.KVStoreKey
|
mainKey *sdk.KVStoreKey
|
||||||
stakeKey *sdk.KVStoreKey
|
stakeKey *sdk.KVStoreKey
|
||||||
slashingKey *sdk.KVStoreKey
|
slashingKey *sdk.KVStoreKey
|
||||||
@ -57,23 +61,36 @@ type (
|
|||||||
|
|
||||||
// NewEthermintApp returns a reference to a new initialized Ethermint
|
// NewEthermintApp returns a reference to a new initialized Ethermint
|
||||||
// application.
|
// application.
|
||||||
func NewEthermintApp(
|
func NewEthermintApp(logger tmlog.Logger, db dbm.DB, sdkAddr ethcmn.Address) *EthermintApp {
|
||||||
logger tmlog.Logger, db dbm.DB, sdkAddr ethcmn.Address, baseAppOpts ...func(*bam.BaseApp),
|
|
||||||
) *EthermintApp {
|
|
||||||
|
|
||||||
codec := CreateCodec()
|
codec := CreateCodec()
|
||||||
app := &EthermintApp{
|
cms := store.NewCommitMultiStore(db)
|
||||||
BaseApp: bam.NewBaseApp(appName, logger, db, types.TxDecoder(codec, sdkAddr), baseAppOpts...),
|
|
||||||
codec: codec,
|
baseAppOpts := []func(*bam.BaseApp){
|
||||||
accountKey: sdk.NewKVStoreKey("acc"),
|
func(bApp *bam.BaseApp) { bApp.SetCMS(cms) },
|
||||||
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"),
|
|
||||||
}
|
}
|
||||||
|
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
|
// set application keepers and mappers
|
||||||
app.accountMapper = auth.NewAccountMapper(codec, app.accountKey, auth.ProtoBaseAccount)
|
app.accountMapper = auth.NewAccountMapper(codec, app.accountKey, auth.ProtoBaseAccount)
|
||||||
@ -108,7 +125,7 @@ func NewEthermintApp(
|
|||||||
|
|
||||||
app.MountStoresIAVL(
|
app.MountStoresIAVL(
|
||||||
app.mainKey, app.accountKey, app.stakeKey, app.slashingKey,
|
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)
|
app.MountStore(app.tParamsKey, sdk.StoreTypeTransient)
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
"github.com/cosmos/ethermint/core"
|
"github.com/cosmos/ethermint/core"
|
||||||
|
"github.com/cosmos/ethermint/types"
|
||||||
|
|
||||||
ethcmn "github.com/ethereum/go-ethereum/common"
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||||
ethstate "github.com/ethereum/go-ethereum/core/state"
|
ethstate "github.com/ethereum/go-ethereum/core/state"
|
||||||
@ -16,20 +17,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
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
|
// CodeKey is the key used for storing Ethereum contract code in the Cosmos
|
||||||
// SDK multi-store.
|
// SDK multi-store.
|
||||||
CodeKey = sdk.NewKVStoreKey("code")
|
CodeKey = sdk.NewKVStoreKey("code")
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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
|
// codeSizeCacheSize is the number of codehash to size associations to
|
||||||
// keep in cached memory. This is to address any DoS attempts on
|
// keep in cached memory. This is to address any DoS attempts on
|
||||||
// EXTCODESIZE calls.
|
// EXTCODESIZE calls.
|
||||||
@ -68,25 +65,8 @@ type Database struct {
|
|||||||
// implements Ethereum's state.Database interface. An error is returned if the
|
// implements Ethereum's state.Database interface. An error is returned if the
|
||||||
// latest state failed to load. The underlying storage structure is defined by
|
// latest state failed to load. The underlying storage structure is defined by
|
||||||
// the Cosmos SDK IAVL tree.
|
// the Cosmos SDK IAVL tree.
|
||||||
func NewDatabase(stateDB, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
|
func NewDatabase(stateStore store.CommitMultiStore, codeDB dbm.DB, storeCacheSize int) (*Database, error) {
|
||||||
// Initialize an implementation of Ethereum state.Database and create a
|
db := &Database{stateStore: stateStore}
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the persistent Cosmos SDK Database and initialize an Ethereum
|
// Set the persistent Cosmos SDK Database and initialize an Ethereum
|
||||||
// trie.Database using an EthereumDB as the underlying implementation of
|
// 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 {
|
if db.accountsCache == nil {
|
||||||
db.accountsCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(AccountsKey))
|
db.accountsCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyAccount))
|
||||||
db.storageCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(StorageKey))
|
db.storageCache = store.NewCacheKVStore(db.stateStore.GetCommitKVStore(types.StoreKeyStorage))
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Trie{
|
return &Trie{
|
||||||
|
@ -5,6 +5,9 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"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"
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
||||||
|
|
||||||
dbm "github.com/tendermint/tendermint/libs/db"
|
dbm "github.com/tendermint/tendermint/libs/db"
|
||||||
@ -28,10 +31,17 @@ func init() {
|
|||||||
func newTestDatabase() *Database {
|
func newTestDatabase() *Database {
|
||||||
memDB := dbm.NewMemDB()
|
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 {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("failed to create database: %v", err))
|
panic(fmt.Sprintf("failed to create database: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testDB.stateStore.LoadLatestVersion()
|
||||||
|
|
||||||
return testDB
|
return testDB
|
||||||
}
|
}
|
||||||
|
15
test/run.go
15
test/run.go
@ -9,8 +9,12 @@ import (
|
|||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/cosmos/cosmos-sdk/store"
|
||||||
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
|
|
||||||
"github.com/cosmos/ethermint/state"
|
"github.com/cosmos/ethermint/state"
|
||||||
"github.com/cosmos/ethermint/test/importer"
|
"github.com/cosmos/ethermint/test/importer"
|
||||||
|
"github.com/cosmos/ethermint/types"
|
||||||
|
|
||||||
dbm "github.com/tendermint/tendermint/libs/db"
|
dbm "github.com/tendermint/tendermint/libs/db"
|
||||||
)
|
)
|
||||||
@ -52,7 +56,16 @@ func main() {
|
|||||||
stateDB := dbm.NewDB("state", dbm.LevelDBBackend, *datadir)
|
stateDB := dbm.NewDB("state", dbm.LevelDBBackend, *datadir)
|
||||||
codeDB := dbm.NewDB("code", 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 {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("failed to initialize geth Database: %v", err))
|
panic(fmt.Sprintf("failed to initialize geth Database: %v", err))
|
||||||
}
|
}
|
||||||
|
16
types/store_keys.go
Normal file
16
types/store_keys.go
Normal 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")
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user