Skip to content

Commit

Permalink
Merge branch 'dev' into feature/cosmos-store
Browse files Browse the repository at this point in the history
  • Loading branch information
antho1404 authored Aug 24, 2019
2 parents 3dfd9bd + 94cb2bc commit c563310
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 359 deletions.
19 changes: 17 additions & 2 deletions core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/mesg-foundation/engine/config"
"github.com/mesg-foundation/engine/container"
"github.com/mesg-foundation/engine/cosmos"
"github.com/mesg-foundation/engine/database"
"github.com/mesg-foundation/engine/database/store"
"github.com/mesg-foundation/engine/hash"
Expand All @@ -17,13 +18,13 @@ import (
instancesdk "github.com/mesg-foundation/engine/sdk/instance"
servicesdk "github.com/mesg-foundation/engine/sdk/service"
"github.com/mesg-foundation/engine/server/grpc"
"github.com/mesg-foundation/engine/tendermint"
"github.com/mesg-foundation/engine/version"
"github.com/mesg-foundation/engine/x/xerrors"
"github.com/mesg-foundation/engine/x/xnet"
"github.com/mesg-foundation/engine/x/xos"
"github.com/mesg-foundation/engine/x/xsignal"
"github.com/sirupsen/logrus"
db "github.com/tendermint/tm-db"
)

var network = flag.Bool("experimental-network", false, "start the engine with the network")
Expand Down Expand Up @@ -145,11 +146,25 @@ func main() {
logger.Init(cfg.Log.Format, cfg.Log.Level, cfg.Log.ForceColors)

if *network {
// init cosmos app
db, err := db.NewGoLevelDB("app", cfg.Cosmos.Path)
if err != nil {
logrus.WithField("module", "main").Fatalln(err)
}
app := cosmos.NewApp(logger.TendermintLogger(), db)
cosmos.InitDefaultAppModules(app)
err = app.Load()
if err != nil {
logrus.WithField("module", "main").Fatalln(err)
}

// create tendermint node
node, err := tendermint.NewNode(cfg.Tendermint.Config, &cfg.Cosmos)
node, err := cosmos.NewNode(app, cfg.Tendermint.Config, &cfg.Cosmos)
if err != nil {
logrus.WithField("module", "main").Fatalln(err)
}

// start tendermint node
logrus.WithField("module", "main").WithField("seeds", cfg.Tendermint.P2P.Seeds).Info("starting tendermint node")
if err := node.Start(); err != nil {
logrus.WithField("module", "main").Fatalln(err)
Expand Down
130 changes: 130 additions & 0 deletions cosmos/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package cosmos

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/baseapp"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/auth"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
)

// App is a Cosmos application that inherit from BaseApp.
type App struct {
*baseapp.BaseApp
cdc *codec.Codec
modulesBasic []module.AppModuleBasic
modules []module.AppModule
storeKeys map[string]*sdk.KVStoreKey
transientStoreKeys map[string]*sdk.TransientStoreKey
orderBeginBlockers []string
orderEndBlockers []string
orderInitGenesis []string
anteHandler sdk.AnteHandler
}

// NewApp initializes a new App.
func NewApp(logger log.Logger, db dbm.DB) *App {
cdc := codec.New()
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)

return &App{
BaseApp: bam.NewBaseApp("engine", logger, db, auth.DefaultTxDecoder(cdc)),
modules: []module.AppModule{},
cdc: cdc,
storeKeys: map[string]*sdk.KVStoreKey{
bam.MainStoreKey: sdk.NewKVStoreKey(bam.MainStoreKey),
},
transientStoreKeys: map[string]*sdk.TransientStoreKey{},
}
}

// DefaultGenesis returns the default genesis from the basic manager.
func (a *App) DefaultGenesis() map[string]json.RawMessage {
basicManager := module.NewBasicManager(a.modulesBasic...)
basicManager.RegisterCodec(a.cdc)
return basicManager.DefaultGenesis()
}

// RegisterModule registers a module to the app.
func (a *App) RegisterModule(module module.AppModule) {
a.modulesBasic = append(a.modulesBasic, module)
a.modules = append(a.modules, module)
}

// RegisterOrderInitGenesis sets the order of the modules when they are called to initialize the genesis.
func (a *App) RegisterOrderInitGenesis(moduleNames ...string) {
a.orderInitGenesis = moduleNames
}

// RegisterOrderBeginBlocks sets the order of the modules when they are called on the begin block event.
func (a *App) RegisterOrderBeginBlocks(beginBlockers ...string) {
a.orderBeginBlockers = beginBlockers
}

// RegisterOrderEndBlocks sets the order of the modules when they are called on the end block event.
func (a *App) RegisterOrderEndBlocks(endBlockers ...string) {
a.orderEndBlockers = endBlockers
}

// RegisterStoreKey registers a store key to the app.
func (a *App) RegisterStoreKey(storeKey *sdk.KVStoreKey) {
a.storeKeys[storeKey.Name()] = storeKey
}

// RegisterTransientStoreKey registers a transient store key to the app.
func (a *App) RegisterTransientStoreKey(transientStoreKey *sdk.TransientStoreKey) {
a.transientStoreKeys[transientStoreKey.Name()] = transientStoreKey
}

// SetAnteHandler registers the authentication handler to the app.
func (a *App) SetAnteHandler(anteHandler sdk.AnteHandler) {
a.anteHandler = anteHandler
}

// Cdc returns the codec of the app.
func (a *App) Cdc() *codec.Codec {
return a.cdc
}

// Load creates the module manager, registers the modules to it, mounts the stores and finally load the app itself.
func (a *App) Load() error {
// where all the magic happen
// basically register everything on baseapp and load it

mm := module.NewManager(a.modules...)
mm.SetOrderBeginBlockers(a.orderBeginBlockers...)
mm.SetOrderEndBlockers(a.orderEndBlockers...)

// Sets the order of Genesis - Order matters, genutil is to always come last
mm.SetOrderInitGenesis(a.orderInitGenesis...)

// register all module routes and module queriers
mm.RegisterRoutes(a.Router(), a.QueryRouter())

// The initChainer handles translating the genesis.json file into initial state for the network
a.SetInitChainer(func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisData map[string]json.RawMessage
if err := a.cdc.UnmarshalJSON(req.AppStateBytes, &genesisData); err != nil {
panic(err)
}
return mm.InitGenesis(ctx, genesisData)
})
a.SetBeginBlocker(mm.BeginBlock)
a.SetEndBlocker(mm.EndBlock)

// The AnteHandler handles signature verification and transaction pre-processing
a.SetAnteHandler(a.anteHandler)

// initialize stores
a.MountKVStores(a.storeKeys)
a.MountTransientStores(a.transientStoreKeys)

return a.LoadLatestVersion(a.storeKeys[bam.MainStoreKey])
}
118 changes: 118 additions & 0 deletions cosmos/initModules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cosmos

import (
cosmostypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/distribution"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"github.com/cosmos/cosmos-sdk/x/genutil"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
"github.com/cosmos/cosmos-sdk/x/supply"
)

// InitDefaultAppModules initializes the default cosmos modules.
func InitDefaultAppModules(app *App) {
// init cosmos stores
paramsStoreKey := cosmostypes.NewKVStoreKey(params.StoreKey)
app.RegisterStoreKey(paramsStoreKey)
paramsTStoreKey := cosmostypes.NewTransientStoreKey(params.TStoreKey)
app.RegisterTransientStoreKey(paramsTStoreKey)
supplyStoreKey := cosmostypes.NewKVStoreKey(supply.StoreKey)
app.RegisterStoreKey(supplyStoreKey)
stakingStoreKey := cosmostypes.NewKVStoreKey(staking.StoreKey)
app.RegisterStoreKey(stakingStoreKey)
stakingTStoreKey := cosmostypes.NewTransientStoreKey(staking.TStoreKey)
app.RegisterTransientStoreKey(stakingTStoreKey)
distrStoreKey := cosmostypes.NewKVStoreKey(distribution.StoreKey)
app.RegisterStoreKey(distrStoreKey)
slashingStoreKey := cosmostypes.NewKVStoreKey(slashing.StoreKey)
app.RegisterStoreKey(slashingStoreKey)

// init cosmos keepers
paramsKeeper := params.NewKeeper(
app.Cdc(),
paramsStoreKey,
paramsTStoreKey,
params.DefaultCodespace,
)
accountKeeper := auth.NewAccountKeeper(
app.Cdc(),
paramsStoreKey,
paramsKeeper.Subspace(auth.DefaultParamspace),
auth.ProtoBaseAccount,
)
bankKeeper := bank.NewBaseKeeper(
accountKeeper,
paramsKeeper.Subspace(bank.DefaultParamspace),
bank.DefaultCodespace,
nil,
)
supplyKeeper := supply.NewKeeper(
app.Cdc(),
supplyStoreKey,
accountKeeper,
bankKeeper,
map[string][]string{
auth.FeeCollectorName: nil,
distribution.ModuleName: nil,
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
},
)
stakingKeeper := staking.NewKeeper(
app.Cdc(),
stakingStoreKey,
stakingTStoreKey,
supplyKeeper,
paramsKeeper.Subspace(staking.DefaultParamspace),
staking.DefaultCodespace,
)
distrKeeper := distribution.NewKeeper(
app.Cdc(),
distrStoreKey,
paramsKeeper.Subspace(distribution.DefaultParamspace),
&stakingKeeper,
supplyKeeper,
distribution.DefaultCodespace,
auth.FeeCollectorName,
nil,
)
slashingKeeper := slashing.NewKeeper(
app.Cdc(),
slashingStoreKey,
&stakingKeeper,
paramsKeeper.Subspace(slashing.DefaultParamspace),
slashing.DefaultCodespace,
)
stakingKeeper = *stakingKeeper.SetHooks(
staking.NewMultiStakingHooks(
distrKeeper.Hooks(),
slashingKeeper.Hooks()),
)

// init cosmos module
app.RegisterModule(genaccounts.NewAppModule(accountKeeper))
app.RegisterModule(genutil.NewAppModule(accountKeeper, stakingKeeper, app.DeliverTx))
app.RegisterModule(auth.NewAppModule(accountKeeper))
app.RegisterModule(bank.NewAppModule(bankKeeper, accountKeeper))
app.RegisterModule(supply.NewAppModule(supplyKeeper, accountKeeper))
app.RegisterModule(distribution.NewAppModule(distrKeeper, supplyKeeper))
app.RegisterModule(slashing.NewAppModule(slashingKeeper, stakingKeeper))
app.RegisterModule(staking.NewAppModule(stakingKeeper, distrKeeper, accountKeeper, supplyKeeper))

app.RegisterOrderBeginBlocks(distribution.ModuleName, slashing.ModuleName)
app.RegisterOrderEndBlocks(staking.ModuleName)

app.RegisterOrderInitGenesis(
genaccounts.ModuleName,
distribution.ModuleName,
staking.ModuleName,
auth.ModuleName,
bank.ModuleName,
slashing.ModuleName,
genutil.ModuleName,
)
}
6 changes: 3 additions & 3 deletions tendermint/keybase.go → cosmos/keybase.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tendermint
package cosmos

import (
clientkey "github.com/cosmos/cosmos-sdk/client/keys"
Expand All @@ -13,8 +13,8 @@ type Keybase struct {
keys.Keybase
}

// NewFSKeybase initializes a filesystem keybase at a particular dir.
func NewFSKeybase(dir string) (*Keybase, error) {
// NewKeybase initializes a filesystem keybase at a particular dir.
func NewKeybase(dir string) (*Keybase, error) {
kb, err := clientkey.NewKeyBaseFromDir(dir)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit c563310

Please sign in to comment.