-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/cosmos-store
- Loading branch information
Showing
10 changed files
with
484 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.