diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 73b2de49d8..7640c1d94c 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -241,18 +241,7 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC // TODO Return something intelligent panic(err) } - err = app.Router().ForEach(func(r string, _ sdk.Handler, i sdk.InitGenesis) error { - if i != nil { - encoded, exists := (*genesisState)[r] - if !exists { - // TODO should this be a Cosmos SDK standard error? - return errors.New(fmt.Sprintf("Expected module genesis information for module %s but it was not present", r)) - } else { - return i(app.deliverState.ctx, encoded) - } - } - return nil - }) + err = app.Router().InitGenesis(app.deliverState.ctx, *genesisState) if err != nil { // TODO Return something intelligent panic(err) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index a90c39d541..23abf7fd7f 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -180,7 +180,7 @@ func TestInitChainer(t *testing.T) { // set initChainer and try again - should see the value app.SetInitChainer(initChainer) - app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) + app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}")}) // must have valid JSON genesis file, even if empty app.Commit() res = app.Query(query) assert.Equal(t, value, res.Value) diff --git a/baseapp/router.go b/baseapp/router.go index 5012d48cf4..17be883092 100644 --- a/baseapp/router.go +++ b/baseapp/router.go @@ -1,6 +1,8 @@ package baseapp import ( + "encoding/json" + "fmt" "regexp" sdk "github.com/cosmos/cosmos-sdk/types" @@ -10,8 +12,7 @@ import ( type Router interface { AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) (rtr Router) Route(path string) (h sdk.Handler) - RouteGenesis(path string) (i sdk.InitGenesis) - ForEach(func(r string, h sdk.Handler, i sdk.InitGenesis) error) error + InitGenesis(ctx sdk.Context, data map[string]json.RawMessage) error } // map a transaction type to a handler and an initgenesis function @@ -46,34 +47,29 @@ func (rtr *router) AddRoute(r string, h sdk.Handler, i sdk.InitGenesis) Router { return rtr } -// TODO handle expressive matches. -func matchRoute(path string, route string) bool { - return path == route -} - // Route - TODO add description +// TODO handle expressive matches. func (rtr *router) Route(path string) (h sdk.Handler) { for _, route := range rtr.routes { - if matchRoute(path, route.r) { + if route.r == path { return route.h } } return nil } -func (rtr *router) RouteGenesis(path string) (i sdk.InitGenesis) { +// 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 matchRoute(path, route.r) { - return route.i - } - } - return nil -} - -func (rtr *router) ForEach(f func(string, sdk.Handler, sdk.InitGenesis) error) error { - for _, route := range rtr.routes { - if err := f(route.r, route.h, route.i); err != nil { - return err + 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 diff --git a/types/errors.go b/types/errors.go index ca16584ecc..1115d39376 100644 --- a/types/errors.go +++ b/types/errors.go @@ -32,7 +32,7 @@ const ( CodeInsufficientCoins CodeType = 10 CodeInvalidCoins CodeType = 11 - CodeGenesisParse CodeType = 0xdead // TODO: remove ? + CodeGenesisParse CodeType = 0xdead // TODO: remove ? // why remove? ) // NOTE: Don't stringer this, we'll put better messages in later. diff --git a/types/initgenesis.go b/types/initgenesis.go index 8b63568301..ff3c03cf4d 100644 --- a/types/initgenesis.go +++ b/types/initgenesis.go @@ -4,7 +4,7 @@ import ( "encoding/json" ) -/* Run only once on chain initialization, should write genesis state to store - or throw an error if some required information was not provided, in which case - the application will panic. */ +// Run only once on chain initialization, should write genesis state to store +// or throw an error if some required information was not provided, in which case +// the application will panic. type InitGenesis func(ctx Context, data json.RawMessage) error diff --git a/x/auth/rest/query.go b/x/auth/rest/query.go index 1ef9abe449..22c364ccda 100644 --- a/x/auth/rest/query.go +++ b/x/auth/rest/query.go @@ -16,7 +16,7 @@ import ( type commander struct { storeName string cdc *wire.Codec - decoder sdk.AccountDecoder + decoder sdk.AccountDecoder } func QueryAccountRequestHandler(storeName string, cdc *wire.Codec, decoder sdk.AccountDecoder) func(http.ResponseWriter, *http.Request) {