remove InitGenesis

This commit is contained in:
Ethan Buchman 2018-04-02 18:13:37 +03:00
parent 1d2c2d88b1
commit be05bf4343
6 changed files with 35 additions and 54 deletions

View File

@ -4,8 +4,6 @@
BREAKING CHANGES BREAKING CHANGES
* [baseapp] `AddRoute` takes an `InitGenesis` function for per-module
initialization
* [basecoin] Remove cool/sketchy modules -> moved to new `democoin` * [basecoin] Remove cool/sketchy modules -> moved to new `democoin`
* [basecoin] NewBasecoinApp takes a `map[string]dbm.DB` as temporary measure * [basecoin] NewBasecoinApp takes a `map[string]dbm.DB` as temporary measure
to allow mounting multiple stores with their own DB until they can share one to allow mounting multiple stores with their own DB until they can share one
@ -15,7 +13,6 @@ BREAKING CHANGES
* [basecoind] `init` command outputs JSON of everything necessary for testnet * [basecoind] `init` command outputs JSON of everything necessary for testnet
* [basecoind] `basecoin.db -> data/basecoin.db` * [basecoind] `basecoin.db -> data/basecoin.db`
* [basecli] `data/keys.db -> keys/keys.db` * [basecli] `data/keys.db -> keys/keys.db`
* [cool] Mapper -> Keeper
FEATURES FEATURES

View File

@ -248,11 +248,6 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
// TODO Return something intelligent // TODO Return something intelligent
panic(err) panic(err)
} }
err = app.Router().InitGenesis(app.deliverState.ctx, *genesisState)
if err != nil {
// TODO Return something intelligent
panic(err)
}
// NOTE: we don't commit, but BeginBlock for block 1 // NOTE: we don't commit, but BeginBlock for block 1
// starts from this deliverState // starts from this deliverState

View File

@ -1,8 +1,6 @@
package baseapp package baseapp
import ( import (
"encoding/json"
"fmt"
"regexp" "regexp"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -10,16 +8,14 @@ import (
// Router provides handlers for each transaction type. // Router provides handlers for each transaction type.
type Router interface { type Router interface {
AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) (rtr Router) AddRoute(r string, h sdk.Handler) (rtr Router)
Route(path string) (h sdk.Handler) Route(path string) (h sdk.Handler)
InitGenesis(ctx sdk.Context, data map[string]json.RawMessage) error
} }
// map a transaction type to a handler and an initgenesis function // map a transaction type to a handler and an initgenesis function
type route struct { type route struct {
r string r string
h sdk.Handler h sdk.Handler
i sdk.InitGenesis
} }
type router struct { type router struct {
@ -38,11 +34,11 @@ func NewRouter() *router {
var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString var isAlpha = regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
// AddRoute - TODO add description // AddRoute - TODO add description
func (rtr *router) AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) Router { func (rtr *router) AddRoute(r string, h sdk.Handler) Router {
if !isAlpha(r) { if !isAlpha(r) {
panic("route expressions can only contain alphanumeric characters") panic("route expressions can only contain alphanumeric characters")
} }
rtr.routes = append(rtr.routes, route{r, h, i}) rtr.routes = append(rtr.routes, route{r, h})
return rtr return rtr
} }
@ -57,20 +53,3 @@ func (rtr *router) Route(path string) (h sdk.Handler) {
} }
return nil return nil
} }
// InitGenesis - call `InitGenesis`, where specified, for all routes
// Return the first error if any, otherwise nil
func (rtr *router) InitGenesis(ctx sdk.Context, data map[string]json.RawMessage) error {
for _, route := range rtr.routes {
if route.i != nil {
encoded, found := data[route.r]
if !found {
return sdk.ErrGenesisParse(fmt.Sprintf("Expected module genesis information for module %s but it was not present", route.r))
}
if err := route.i(ctx, encoded); err != nil {
return err
}
}
}
return nil
}

View File

@ -61,9 +61,9 @@ func NewBasecoinApp(logger log.Logger, dbs map[string]dbm.DB) *BasecoinApp {
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore) ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper) stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper)
app.Router(). app.Router().
AddRoute("bank", bank.NewHandler(coinKeeper), nil). AddRoute("bank", bank.NewHandler(coinKeeper)).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper), nil). AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)).
AddRoute("simplestake", simplestake.NewHandler(stakeKeeper), nil) AddRoute("simplestake", simplestake.NewHandler(stakeKeeper))
// initialize BaseApp // initialize BaseApp
app.SetTxDecoder(app.txDecoder) app.SetTxDecoder(app.txDecoder)

View File

@ -64,15 +64,15 @@ func NewDemocoinApp(logger log.Logger, dbs map[string]dbm.DB) *DemocoinApp {
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore) ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper) stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper)
app.Router(). app.Router().
AddRoute("bank", bank.NewHandler(coinKeeper), nil). AddRoute("bank", bank.NewHandler(coinKeeper)).
AddRoute("cool", cool.NewHandler(coolKeeper), coolKeeper.InitGenesis). AddRoute("cool", cool.NewHandler(coolKeeper)).
AddRoute("sketchy", sketchy.NewHandler(), nil). AddRoute("sketchy", sketchy.NewHandler()).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper), nil). AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)).
AddRoute("simplestake", simplestake.NewHandler(stakeKeeper), nil) AddRoute("simplestake", simplestake.NewHandler(stakeKeeper))
// initialize BaseApp // initialize BaseApp
app.SetTxDecoder(app.txDecoder) app.SetTxDecoder(app.txDecoder)
app.SetInitChainer(app.initChainer) app.SetInitChainer(app.initChainerFn(coolKeeper))
app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"]) app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"])
app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"]) app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"])
app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"]) app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"])
@ -143,23 +143,33 @@ func (app *DemocoinApp) txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
} }
// custom logic for democoin initialization // custom logic for democoin initialization
func (app *DemocoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain { func (app *DemocoinApp) initChainerFn(coolKeeper cool.Keeper) sdk.InitChainer {
stateJSON := req.AppStateBytes return func(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes
genesisState := new(types.GenesisState) genesisState := new(types.GenesisState)
err := json.Unmarshal(stateJSON, genesisState) err := json.Unmarshal(stateJSON, genesisState)
if err != nil { if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "") // return sdk.ErrGenesisParse("").TraceCause(err, "")
} }
for _, gacc := range genesisState.Accounts { for _, gacc := range genesisState.Accounts {
acc, err := gacc.ToAppAccount() acc, err := gacc.ToAppAccount()
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
app.accountMapper.SetAccount(ctx, acc)
}
// Application specific genesis handling
err = coolKeeper.InitGenesis(ctx, stateJSON)
if err != nil { if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468 panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "") // return sdk.ErrGenesisParse("").TraceCause(err, "")
} }
app.accountMapper.SetAccount(ctx, acc)
return abci.ResponseInitChain{}
} }
return abci.ResponseInitChain{}
} }

View File

@ -39,7 +39,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
baseApp.SetInitChainer(InitChainer(capKeyMainStore)) baseApp.SetInitChainer(InitChainer(capKeyMainStore))
// Set a handler Route. // Set a handler Route.
baseApp.Router().AddRoute("kvstore", KVStoreHandler(capKeyMainStore), nil) baseApp.Router().AddRoute("kvstore", KVStoreHandler(capKeyMainStore))
// Load latest version. // Load latest version.
if err := baseApp.LoadLatestVersion(capKeyMainStore); err != nil { if err := baseApp.LoadLatestVersion(capKeyMainStore); err != nil {