From 5efee7d61c729ccdf6482e72ff2ffc9ad2c76b3a Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Fri, 15 May 2020 19:03:03 +0200 Subject: [PATCH 01/17] add test of alternative impl --- types/module/module.go | 8 ++++---- types/router.go | 7 ++++++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index 4a3447230878..720e3e7444d6 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -139,8 +139,7 @@ type AppModule interface { RegisterInvariants(sdk.InvariantRegistry) // routes - Route() string - NewHandler() sdk.Handler + NewRoute() sdk.Route QuerierRoute() string NewQuerierHandler() sdk.Querier @@ -247,8 +246,9 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { - if module.Route() != "" { - router.AddRoute(module.Route(), module.NewHandler()) + handler := module.NewRoute() + if handler != nil { + router.AddRoute(handler) } if module.QuerierRoute() != "" { queryRouter.AddRoute(module.QuerierRoute(), module.NewQuerierHandler()) diff --git a/types/router.go b/types/router.go index 12d0455f805d..b6d9719c3fdc 100644 --- a/types/router.go +++ b/types/router.go @@ -26,10 +26,15 @@ var ( // Router provides handlers for each transaction type. type Router interface { - AddRoute(r string, h Handler) Router + AddRoute(r Route) Router Route(ctx Context, path string) Handler } +type Route interface { + Name() string + Handler() Handler +} + // QueryRouter provides queryables for each query path. type QueryRouter interface { AddRoute(r string, h Querier) QueryRouter From 1319b0307cbb62954f38638c97e50249e5e4930c Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Fri, 15 May 2020 19:10:10 +0200 Subject: [PATCH 02/17] simplify query route too --- types/module/module.go | 15 ++++++++------- types/router.go | 7 ++++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index 720e3e7444d6..787f23d76f39 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -140,8 +140,7 @@ type AppModule interface { // routes NewRoute() sdk.Route - QuerierRoute() string - NewQuerierHandler() sdk.Querier + NewQueryRoute() sdk.QueryRoute // ABCI BeginBlock(sdk.Context, abci.RequestBeginBlock) @@ -246,12 +245,14 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { - handler := module.NewRoute() - if handler != nil { - router.AddRoute(handler) + route := module.NewRoute() + if route != nil { + router.AddRoute(route) } - if module.QuerierRoute() != "" { - queryRouter.AddRoute(module.QuerierRoute(), module.NewQuerierHandler()) + + queryRoute := module.NewQueryRoute() + if queryRoute != nil { + queryRouter.AddRoute(queryRoute) } } } diff --git a/types/router.go b/types/router.go index b6d9719c3fdc..9bdb52c972b0 100644 --- a/types/router.go +++ b/types/router.go @@ -37,6 +37,11 @@ type Route interface { // QueryRouter provides queryables for each query path. type QueryRouter interface { - AddRoute(r string, h Querier) QueryRouter + AddRoute(route QueryRoute) QueryRouter Route(path string) Querier } + +type QueryRoute interface { + Name() string + Handler() Querier +} From c0c1d351941fc3ad9b2728f67486bacf12c3b219 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Fri, 15 May 2020 19:14:19 +0200 Subject: [PATCH 03/17] change name querier --- types/router.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/router.go b/types/router.go index 9bdb52c972b0..9ffe7fab118b 100644 --- a/types/router.go +++ b/types/router.go @@ -43,5 +43,5 @@ type QueryRouter interface { type QueryRoute interface { Name() string - Handler() Querier + Querier() Querier } From 206e08ef697a55fc17f47100321cf8147b8b6dd9 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Fri, 15 May 2020 19:22:02 +0200 Subject: [PATCH 04/17] simplify register routes --- types/module/module.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index 787f23d76f39..d3ea31c26d30 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -245,13 +245,11 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { - route := module.NewRoute() - if route != nil { + if route := module.NewRoute(); route != nil { router.AddRoute(route) } - queryRoute := module.NewQueryRoute() - if queryRoute != nil { + if queryRoute := module.NewQueryRoute(); queryRoute != nil { queryRouter.AddRoute(queryRoute) } } From a3fcbe4e1edbc0e04bf03f368862ff4d81c3af61 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Mon, 18 May 2020 18:17:32 +0200 Subject: [PATCH 05/17] revert change --- types/module/module.go | 15 ++++++++------- types/router.go | 14 ++------------ 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/types/module/module.go b/types/module/module.go index d3ea31c26d30..4a3447230878 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -139,8 +139,10 @@ type AppModule interface { RegisterInvariants(sdk.InvariantRegistry) // routes - NewRoute() sdk.Route - NewQueryRoute() sdk.QueryRoute + Route() string + NewHandler() sdk.Handler + QuerierRoute() string + NewQuerierHandler() sdk.Querier // ABCI BeginBlock(sdk.Context, abci.RequestBeginBlock) @@ -245,12 +247,11 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { - if route := module.NewRoute(); route != nil { - router.AddRoute(route) + if module.Route() != "" { + router.AddRoute(module.Route(), module.NewHandler()) } - - if queryRoute := module.NewQueryRoute(); queryRoute != nil { - queryRouter.AddRoute(queryRoute) + if module.QuerierRoute() != "" { + queryRouter.AddRoute(module.QuerierRoute(), module.NewQuerierHandler()) } } } diff --git a/types/router.go b/types/router.go index 9ffe7fab118b..12d0455f805d 100644 --- a/types/router.go +++ b/types/router.go @@ -26,22 +26,12 @@ var ( // Router provides handlers for each transaction type. type Router interface { - AddRoute(r Route) Router + AddRoute(r string, h Handler) Router Route(ctx Context, path string) Handler } -type Route interface { - Name() string - Handler() Handler -} - // QueryRouter provides queryables for each query path. type QueryRouter interface { - AddRoute(route QueryRoute) QueryRouter + AddRoute(r string, h Querier) QueryRouter Route(path string) Querier } - -type QueryRoute interface { - Name() string - Querier() Querier -} From 90650f88480c690f4b8d472eb76c37053b2a0b45 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Mon, 18 May 2020 18:27:42 +0200 Subject: [PATCH 06/17] add route --- types/router.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/types/router.go b/types/router.go index 12d0455f805d..33ca635a665b 100644 --- a/types/router.go +++ b/types/router.go @@ -30,6 +30,12 @@ type Router interface { Route(ctx Context, path string) Handler } +// Route represents a Route used by the Router. +type Route interface { + Route() string + Handler() Handler +} + // QueryRouter provides queryables for each query path. type QueryRouter interface { AddRoute(r string, h Querier) QueryRouter From 87f40ce360478a9f4b2b3172706a751a8d10266a Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Mon, 18 May 2020 18:52:58 +0200 Subject: [PATCH 07/17] add router --- types/router.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/types/router.go b/types/router.go index 33ca635a665b..90ddfc8815f6 100644 --- a/types/router.go +++ b/types/router.go @@ -36,6 +36,23 @@ type Route interface { Handler() Handler } +type route struct { + route string + handler Handler +} + +func NewRouter(r string, h Handler) Route { + return &route{route: r, handler: h} +} + +func (r route) Route() string { + return r.route +} + +func (r route) Handler() Handler { + return r.handler +} + // QueryRouter provides queryables for each query path. type QueryRouter interface { AddRoute(r string, h Querier) QueryRouter From 11148761d2db3dcf7c0cbfdd5c4c0c6a7b00ceea Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 9 Jun 2020 13:54:32 +0200 Subject: [PATCH 08/17] first step refactor --- baseapp/baseapp_test.go | 44 +++++++++++++++++---------- baseapp/router.go | 10 +++---- baseapp/router_test.go | 6 ++-- server/mock/app.go | 2 +- tests/mocks/types_router.go | 59 ++++++++++++++++++++++++++++++++++--- types/module/module.go | 3 +- types/module/module_test.go | 4 +-- types/router.go | 4 +-- 8 files changed, 98 insertions(+), 34 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 36afc93cef15..ac17a190ccb3 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -781,9 +781,9 @@ func TestCheckTx(t *testing.T) { anteOpt := func(bapp *BaseApp) { bapp.SetAnteHandler(anteHandlerTxTest(t, capKey1, counterKey)) } routerOpt := func(bapp *BaseApp) { // TODO: can remove this once CheckTx doesnt process msgs. - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + bapp.Router().AddRoute(sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { return &sdk.Result{}, nil - }) + })) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -831,7 +831,8 @@ func TestDeliverTx(t *testing.T) { // test increments in the handler deliverKey := []byte("deliver-key") routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + r := sdk.NewRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -884,8 +885,10 @@ func TestMultiMsgDeliverTx(t *testing.T) { deliverKey := []byte("deliver-key") deliverKey2 := []byte("deliver-key2") routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) - bapp.Router().AddRoute(routeMsgCounter2, handlerMsgCounter(t, capKey1, deliverKey2)) + r1 := sdk.NewRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + r2 := sdk.NewRoute(routeMsgCounter2, handlerMsgCounter(t, capKey1, deliverKey2)) + bapp.Router().AddRoute(r1) + bapp.Router().AddRoute(r2) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -960,10 +963,11 @@ func TestSimulateTx(t *testing.T) { } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { ctx.GasMeter().ConsumeGas(gasConsumed, "test") return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1024,9 +1028,10 @@ func TestRunInvalidTransaction(t *testing.T) { }) } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1149,11 +1154,12 @@ func TestTxGasLimits(t *testing.T) { } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { count := msg.(msgCounter).Counter ctx.GasMeter().ConsumeGas(uint64(count), "counter-handler") return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1232,11 +1238,12 @@ func TestMaxBlockGasLimits(t *testing.T) { } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { count := msg.(msgCounter).Counter ctx.GasMeter().ConsumeGas(uint64(count), "counter-handler") return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1317,9 +1324,10 @@ func TestCustomRunTxPanicHandler(t *testing.T) { }) } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1356,7 +1364,8 @@ func TestBaseAppAnteHandler(t *testing.T) { deliverKey := []byte("deliver-key") routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + r := sdk.NewRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + bapp.Router().AddRoute(r) } cdc := codec.New() @@ -1451,11 +1460,12 @@ func TestGasConsumptionBadTx(t *testing.T) { } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { count := msg.(msgCounter).Counter ctx.GasMeter().ConsumeGas(uint64(count), "counter-handler") return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } cdc := codec.New() @@ -1504,11 +1514,12 @@ func TestQuery(t *testing.T) { } routerOpt := func(bapp *BaseApp) { - bapp.Router().AddRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { + r := sdk.NewRoute(routeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (*sdk.Result, error) { store := ctx.KVStore(capKey1) store.Set(key, value) return &sdk.Result{}, nil }) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) @@ -1639,8 +1650,8 @@ type testCustomRouter struct { routes sync.Map } -func (rtr *testCustomRouter) AddRoute(path string, h sdk.Handler) sdk.Router { - rtr.routes.Store(path, h) +func (rtr *testCustomRouter) AddRoute(route sdk.Route) sdk.Router { + rtr.routes.Store(route.Route(), route.Handler()) return rtr } @@ -1662,7 +1673,8 @@ func TestWithRouter(t *testing.T) { deliverKey := []byte("deliver-key") routerOpt := func(bapp *BaseApp) { bapp.SetRouter(&testCustomRouter{routes: sync.Map{}}) - bapp.Router().AddRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + r := sdk.NewRoute(routeMsgCounter, handlerMsgCounter(t, capKey1, deliverKey)) + bapp.Router().AddRoute(r) } app := setupBaseApp(t, anteOpt, routerOpt) diff --git a/baseapp/router.go b/baseapp/router.go index 02303bbbb4f3..6e24473bc866 100644 --- a/baseapp/router.go +++ b/baseapp/router.go @@ -21,15 +21,15 @@ func NewRouter() *Router { // AddRoute adds a route path to the router with a given handler. The route must // be alphanumeric. -func (rtr *Router) AddRoute(path string, h sdk.Handler) sdk.Router { - if !sdk.IsAlphaNumeric(path) { +func (rtr *Router) AddRoute(route sdk.Route) sdk.Router { + if !sdk.IsAlphaNumeric(route.Route()) { panic("route expressions can only contain alphanumeric characters") } - if rtr.routes[path] != nil { - panic(fmt.Sprintf("route %s has already been initialized", path)) + if rtr.routes[route.Route()] != nil { + panic(fmt.Sprintf("route %s has already been initialized", route.Route())) } - rtr.routes[path] = h + rtr.routes[route.Route()] = route.Handler() return rtr } diff --git a/baseapp/router_test.go b/baseapp/router_test.go index 86b727568d5d..1e11dc0ca089 100644 --- a/baseapp/router_test.go +++ b/baseapp/router_test.go @@ -17,15 +17,15 @@ func TestRouter(t *testing.T) { // require panic on invalid route require.Panics(t, func() { - rtr.AddRoute("*", testHandler) + rtr.AddRoute(sdk.NewRoute("*", testHandler)) }) - rtr.AddRoute("testRoute", testHandler) + rtr.AddRoute(sdk.NewRoute("testRoute", testHandler)) h := rtr.Route(sdk.Context{}, "testRoute") require.NotNil(t, h) // require panic on duplicate route require.Panics(t, func() { - rtr.AddRoute("testRoute", testHandler) + rtr.AddRoute(sdk.NewRoute("testRoute", testHandler)) }) } diff --git a/server/mock/app.go b/server/mock/app.go index 6779ce2c3f17..71341f5621aa 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -37,7 +37,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) { baseApp.SetInitChainer(InitChainer(capKeyMainStore)) // Set a handler Route. - baseApp.Router().AddRoute("kvstore", KVStoreHandler(capKeyMainStore)) + baseApp.Router().AddRoute(sdk.NewRoute("kvstore", KVStoreHandler(capKeyMainStore))) // Load latest version. if err := baseApp.LoadLatestVersion(); err != nil { diff --git a/tests/mocks/types_router.go b/tests/mocks/types_router.go index 924d95146eda..9c436309e4dd 100644 --- a/tests/mocks/types_router.go +++ b/tests/mocks/types_router.go @@ -34,17 +34,17 @@ func (m *MockRouter) EXPECT() *MockRouterMockRecorder { } // AddRoute mocks base method -func (m *MockRouter) AddRoute(r string, h types.Handler) types.Router { +func (m *MockRouter) AddRoute(r types.Route) types.Router { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AddRoute", r, h) + ret := m.ctrl.Call(m, "AddRoute", r) ret0, _ := ret[0].(types.Router) return ret0 } // AddRoute indicates an expected call of AddRoute -func (mr *MockRouterMockRecorder) AddRoute(r, h interface{}) *gomock.Call { +func (mr *MockRouterMockRecorder) AddRoute(r interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddRoute", reflect.TypeOf((*MockRouter)(nil).AddRoute), r, h) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddRoute", reflect.TypeOf((*MockRouter)(nil).AddRoute), r) } // Route mocks base method @@ -61,6 +61,57 @@ func (mr *MockRouterMockRecorder) Route(ctx, path interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Route", reflect.TypeOf((*MockRouter)(nil).Route), ctx, path) } +// MockRoute is a mock of Route interface +type MockRoute struct { + ctrl *gomock.Controller + recorder *MockRouteMockRecorder +} + +// MockRouteMockRecorder is the mock recorder for MockRoute +type MockRouteMockRecorder struct { + mock *MockRoute +} + +// NewMockRoute creates a new mock instance +func NewMockRoute(ctrl *gomock.Controller) *MockRoute { + mock := &MockRoute{ctrl: ctrl} + mock.recorder = &MockRouteMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use +func (m *MockRoute) EXPECT() *MockRouteMockRecorder { + return m.recorder +} + +// Route mocks base method +func (m *MockRoute) Route() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Route") + ret0, _ := ret[0].(string) + return ret0 +} + +// Route indicates an expected call of Route +func (mr *MockRouteMockRecorder) Route() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Route", reflect.TypeOf((*MockRoute)(nil).Route)) +} + +// Handler mocks base method +func (m *MockRoute) Handler() types.Handler { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Handler") + ret0, _ := ret[0].(types.Handler) + return ret0 +} + +// Handler indicates an expected call of Handler +func (mr *MockRouteMockRecorder) Handler() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Handler", reflect.TypeOf((*MockRoute)(nil).Handler)) +} + // MockQueryRouter is a mock of QueryRouter interface type MockQueryRouter struct { ctrl *gomock.Controller diff --git a/types/module/module.go b/types/module/module.go index 92ec90b3a491..816c319ac7b9 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -256,7 +256,8 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { if module.Route() != "" { - router.AddRoute(module.Route(), module.NewHandler()) + r := sdk.NewRoute(module.Route(), module.NewHandler()) + router.AddRoute(r) } if module.QuerierRoute() != "" { queryRouter.AddRoute(module.QuerierRoute(), module.NewQuerierHandler()) diff --git a/types/module/module_test.go b/types/module/module_test.go index e3a63fe2a601..fd96458c8477 100644 --- a/types/module/module_test.go +++ b/types/module/module_test.go @@ -146,8 +146,8 @@ func TestManager_RegisterRoutes(t *testing.T) { mockAppModule2.EXPECT().Route().Times(2).Return("route2") mockAppModule1.EXPECT().NewHandler().Times(1).Return(handler1) mockAppModule2.EXPECT().NewHandler().Times(1).Return(handler2) - router.EXPECT().AddRoute(gomock.Eq("route1"), gomock.Eq(handler1)).Times(1) - router.EXPECT().AddRoute(gomock.Eq("route2"), gomock.Eq(handler2)).Times(1) + router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route1", handler1))).Times(1) + router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route2", handler2))).Times(1) queryRouter := mocks.NewMockQueryRouter(mockCtrl) mockAppModule1.EXPECT().QuerierRoute().Times(2).Return("querierRoute1") diff --git a/types/router.go b/types/router.go index 90ddfc8815f6..75d990b2d89f 100644 --- a/types/router.go +++ b/types/router.go @@ -26,7 +26,7 @@ var ( // Router provides handlers for each transaction type. type Router interface { - AddRoute(r string, h Handler) Router + AddRoute(r Route) Router Route(ctx Context, path string) Handler } @@ -41,7 +41,7 @@ type route struct { handler Handler } -func NewRouter(r string, h Handler) Route { +func NewRoute(r string, h Handler) Route { return &route{route: r, handler: h} } From 726b8cbfe413c559537e17f19355f4baa8f308ee Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 9 Jun 2020 16:52:24 +0200 Subject: [PATCH 09/17] refactor --- tests/mocks/types_module_module.go | 18 ++---------------- types/module/module.go | 13 ++++--------- types/module/module_test.go | 9 +++------ x/auth/module.go | 5 +---- x/bank/module.go | 7 +++---- x/capability/module.go | 5 +---- x/crisis/module.go | 9 ++------- x/distribution/module.go | 9 ++------- x/evidence/module.go | 9 ++------- x/gov/module.go | 9 ++------- x/ibc-transfer/module.go | 9 ++------- x/ibc/module.go | 9 ++------- x/mint/module.go | 5 +---- x/params/module.go | 4 +--- x/slashing/module.go | 9 ++------- x/staking/module.go | 9 ++------- x/upgrade/module.go | 5 +---- 17 files changed, 33 insertions(+), 110 deletions(-) diff --git a/tests/mocks/types_module_module.go b/tests/mocks/types_module_module.go index 2db37ef602aa..a56300ab453e 100644 --- a/tests/mocks/types_module_module.go +++ b/tests/mocks/types_module_module.go @@ -437,10 +437,10 @@ func (mr *MockAppModuleMockRecorder) RegisterInvariants(arg0 interface{}) *gomoc } // Route mocks base method -func (m *MockAppModule) Route() string { +func (m *MockAppModule) Route() types.Route { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Route") - ret0, _ := ret[0].(string) + ret0, _ := ret[0].(types.Route) return ret0 } @@ -450,20 +450,6 @@ func (mr *MockAppModuleMockRecorder) Route() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Route", reflect.TypeOf((*MockAppModule)(nil).Route)) } -// NewHandler mocks base method -func (m *MockAppModule) NewHandler() types.Handler { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewHandler") - ret0, _ := ret[0].(types.Handler) - return ret0 -} - -// NewHandler indicates an expected call of NewHandler -func (mr *MockAppModuleMockRecorder) NewHandler() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewHandler", reflect.TypeOf((*MockAppModule)(nil).NewHandler)) -} - // QuerierRoute mocks base method func (m *MockAppModule) QuerierRoute() string { m.ctrl.T.Helper() diff --git a/types/module/module.go b/types/module/module.go index 816c319ac7b9..bdc0aee65d7a 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -141,8 +141,7 @@ type AppModule interface { RegisterInvariants(sdk.InvariantRegistry) // routes - Route() string - NewHandler() sdk.Handler + Route() sdk.Route // Deprecated: use RegisterQueryService QuerierRoute() string // Deprecated: use RegisterQueryService @@ -173,10 +172,7 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule { func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route empty module message route -func (GenesisOnlyAppModule) Route() string { return "" } - -// NewHandler returns an empty module handler -func (GenesisOnlyAppModule) NewHandler() sdk.Handler { return nil } +func (GenesisOnlyAppModule) Route() sdk.Route { return nil } // QuerierRoute returns an empty module querier route func (GenesisOnlyAppModule) QuerierRoute() string { return "" } @@ -255,9 +251,8 @@ func (m *Manager) RegisterInvariants(ir sdk.InvariantRegistry) { // RegisterRoutes registers all module routes and module querier routes func (m *Manager) RegisterRoutes(router sdk.Router, queryRouter sdk.QueryRouter) { for _, module := range m.Modules { - if module.Route() != "" { - r := sdk.NewRoute(module.Route(), module.NewHandler()) - router.AddRoute(r) + if module.Route() != nil { + router.AddRoute(module.Route()) } if module.QuerierRoute() != "" { queryRouter.AddRoute(module.QuerierRoute(), module.NewQuerierHandler()) diff --git a/types/module/module_test.go b/types/module/module_test.go index fd96458c8477..ead9fa3afe3d 100644 --- a/types/module/module_test.go +++ b/types/module/module_test.go @@ -69,9 +69,8 @@ func TestGenesisOnlyAppModule(t *testing.T) { mockInvariantRegistry := mocks.NewMockInvariantRegistry(mockCtrl) goam := module.NewGenesisOnlyAppModule(mockModule) - require.Empty(t, goam.Route()) + require.Nil(t, goam.Route()) require.Empty(t, goam.QuerierRoute()) - require.Nil(t, goam.NewHandler()) require.Nil(t, goam.NewQuerierHandler()) // no-op @@ -142,10 +141,8 @@ func TestManager_RegisterRoutes(t *testing.T) { router := mocks.NewMockRouter(mockCtrl) handler1, handler2 := sdk.Handler(nil), sdk.Handler(nil) - mockAppModule1.EXPECT().Route().Times(2).Return("route1") - mockAppModule2.EXPECT().Route().Times(2).Return("route2") - mockAppModule1.EXPECT().NewHandler().Times(1).Return(handler1) - mockAppModule2.EXPECT().NewHandler().Times(1).Return(handler2) + mockAppModule1.EXPECT().Route().Times(2).Return(sdk.NewRoute("route1", handler1)) + mockAppModule2.EXPECT().Route().Times(2).Return(sdk.NewRoute("route2", handler2)) router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route1", handler1))).Times(1) router.EXPECT().AddRoute(gomock.Eq(sdk.NewRoute("route2", handler2))).Times(1) diff --git a/x/auth/module.go b/x/auth/module.go index 291654a114a3..1b1dda7fe759 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -105,10 +105,7 @@ func (AppModule) Name() string { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the auth module. -func (AppModule) Route() string { return "" } - -// NewHandler returns an sdk.Handler for the auth module. -func (AppModule) NewHandler() sdk.Handler { return nil } +func (AppModule) Route() sdk.Route { return nil } // QuerierRoute returns the auth module's querier route name. func (AppModule) QuerierRoute() string { diff --git a/x/bank/module.go b/x/bank/module.go index 2461450f72bd..65cd34f6c0fd 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -108,10 +108,9 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the bank module. -func (AppModule) Route() string { return RouterKey } - -// NewHandler returns an sdk.Handler for the bank module. -func (am AppModule) NewHandler() sdk.Handler { return NewHandler(am.keeper) } +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) +} // QuerierRoute returns the bank module's querier route name. func (AppModule) QuerierRoute() string { return RouterKey } diff --git a/x/capability/module.go b/x/capability/module.go index 0ebd9aa2c27c..a8e998146e89 100644 --- a/x/capability/module.go +++ b/x/capability/module.go @@ -95,14 +95,11 @@ func (am AppModule) Name() string { } // Route returns the capability module's message routing key. -func (AppModule) Route() string { return "" } +func (AppModule) Route() sdk.Route { return nil } // QuerierRoute returns the capability module's query routing key. func (AppModule) QuerierRoute() string { return "" } -// NewHandler returns the capability module's message Handler. -func (am AppModule) NewHandler() sdk.Handler { return nil } - // NewQuerierHandler returns the capability module's Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return nil } diff --git a/x/crisis/module.go b/x/crisis/module.go index 6111a289c9c4..8bf205202972 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -94,13 +94,8 @@ func (AppModule) Name() string { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the crisis module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the crisis module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(*am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(*am.keeper)) } // QuerierRoute returns no querier route. diff --git a/x/distribution/module.go b/x/distribution/module.go index 6848c012500a..e350e53b8907 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -119,13 +119,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the distribution module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the distribution module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute returns the distribution module's querier route name. diff --git a/x/evidence/module.go b/x/evidence/module.go index 2990d52555d1..bef19f5d44a3 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -127,8 +127,8 @@ func (am AppModule) Name() string { } // Route returns the evidence module's message routing key. -func (AppModule) Route() string { - return RouterKey +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute returns the evidence module's query routing key. @@ -136,11 +136,6 @@ func (AppModule) QuerierRoute() string { return QuerierRoute } -// NewHandler returns the evidence module's message Handler. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) -} - // NewQuerierHandler returns the evidence module's Querier. func (am AppModule) NewQuerierHandler() sdk.Querier { return NewQuerier(am.keeper) diff --git a/x/gov/module.go b/x/gov/module.go index 48ba7bddc928..2a046a98fd37 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -135,13 +135,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the gov module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the gov module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute returns the gov module's querier route name. diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index 45eea63a3758..4b162310d35b 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -105,13 +105,8 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route implements the AppModule interface -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler implements the AppModule interface -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute implements the AppModule interface diff --git a/x/ibc/module.go b/x/ibc/module.go index 8c1e51bab85f..1e13ad308abd 100644 --- a/x/ibc/module.go +++ b/x/ibc/module.go @@ -108,13 +108,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the ibc module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the ibc module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(*am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(*am.keeper)) } // QuerierRoute returns the ibc module's querier route name. diff --git a/x/mint/module.go b/x/mint/module.go index 5c03561b7646..ed29f9c408e2 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -100,10 +100,7 @@ func (AppModule) Name() string { func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the mint module. -func (AppModule) Route() string { return "" } - -// NewHandler returns an sdk.Handler for the mint module. -func (am AppModule) NewHandler() sdk.Handler { return nil } +func (AppModule) Route() sdk.Route { return nil } // QuerierRoute returns the mint module's querier route name. func (AppModule) QuerierRoute() string { diff --git a/x/params/module.go b/x/params/module.go index 8811752cebbe..1ba3282060c6 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -79,14 +79,12 @@ func NewAppModule(k Keeper) AppModule { func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} -func (am AppModule) NewHandler() sdk.Handler { return nil } - // InitGenesis performs a no-op. func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.RawMessage) []abci.ValidatorUpdate { return []abci.ValidatorUpdate{} } -func (AppModule) Route() string { return "" } +func (AppModule) Route() sdk.Route { return nil } // GenerateGenesisState performs a no-op. func (AppModule) GenerateGenesisState(simState *module.SimulationState) {} diff --git a/x/slashing/module.go b/x/slashing/module.go index d8f1627311af..c588a971ca6a 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -110,13 +110,8 @@ func (AppModule) Name() string { func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the slashing module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the slashing module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute returns the slashing module's querier route name. diff --git a/x/staking/module.go b/x/staking/module.go index 693496db1bb2..34ba61d20feb 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -134,13 +134,8 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the staking module. -func (AppModule) Route() string { - return RouterKey -} - -// NewHandler returns an sdk.Handler for the staking module. -func (am AppModule) NewHandler() sdk.Handler { - return NewHandler(am.keeper) +func (am AppModule) Route() sdk.Route { + return sdk.NewRoute(RouterKey,NewHandler(am.keeper)) } // QuerierRoute returns the staking module's querier route name. diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 2bf29a698687..98238c5a0b2a 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -98,10 +98,7 @@ func NewAppModule(keeper Keeper) AppModule { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route is empty, as we do not handle Messages (just proposals) -func (AppModule) Route() string { return "" } - -// NewHandler is empty, as we do not handle Messages (just proposals) -func (am AppModule) NewHandler() sdk.Handler { return nil } +func (AppModule) Route() sdk.Route { return nil } // QuerierRoute returns the route we respond to for abci queries func (AppModule) QuerierRoute() string { return QuerierKey } From 0d7875ac1c05ac74fa65e848c9ff227ce1773a25 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 9 Jun 2020 17:00:48 +0200 Subject: [PATCH 10/17] update documentation --- docs/building-modules/handler.md | 4 ++-- docs/building-modules/module-manager.md | 5 ++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/docs/building-modules/handler.md b/docs/building-modules/handler.md index 08850b258259..88f114f390bd 100644 --- a/docs/building-modules/handler.md +++ b/docs/building-modules/handler.md @@ -28,8 +28,8 @@ Let us break it down: Module `handler`s are typically implemented in a `./handler.go` file inside the module's folder. The [module manager](./module-manager.md) is used to add the module's `handler`s to the -[application's `router`](../core/baseapp.md#message-routing) via the `NewHandler()` method. Typically, -the manager's `NewHandler()` method simply calls a `NewHandler()` method defined in `handler.go`, +[application's `router`](../core/baseapp.md#message-routing) via the `Route()` method. Typically, +the manager's `Route()` method simply constructs a Route that calls a `NewHandler()` method defined in `handler.go`, which looks like the following: ```go diff --git a/docs/building-modules/module-manager.md b/docs/building-modules/module-manager.md index 5adc44fd22c4..8d8abf668c1f 100644 --- a/docs/building-modules/module-manager.md +++ b/docs/building-modules/module-manager.md @@ -67,8 +67,7 @@ The `AppModule` interface defines the inter-dependent methods modules need to im Let us go through the methods of `AppModule`: - `RegisterInvariants(sdk.InvariantRegistry)`: Registers the [`invariants`](./invariants.md) of the module. If the invariants deviates from its predicted value, the [`InvariantRegistry`](./invariants.md#registry) triggers appropriate logic (most often the chain will be halted). -- `Route()`: Returns the name of the module's route, for [`message`s](./messages-and-queries.md#messages) to be routed to the module by [`baseapp`](../core/baseapp.md#message-routing). -- `NewHandler()`: Returns a [`handler`](./handler.md) given the `Type()` of the `message`, in order to process the `message`. +- `Route()`: Returns the route for [`message`s](./messages-and-queries.md#messages) to be routed to the module by [`baseapp`](../core/baseapp.md#message-routing). - `QuerierRoute()`: Returns the name of the module's query route, for [`queries`](./messages-and-queries.md#queries) to be routes to the module by [`baseapp`](../core/baseapp.md#query-routing). - `NewQuerierHandler()`: Returns a [`querier`](./querier.md) given the query `path`, in order to process the `query`. - `BeginBlock(sdk.Context, abci.RequestBeginBlock)`: This method gives module developers the option to implement logic that is automatically triggered at the beginning of each block. Implement empty if no logic needs to be triggered at the beginning of each block for this module. @@ -78,7 +77,7 @@ Let us go through the methods of `AppModule`: Typically, the various application module interfaces are implemented in a file called `module.go`, located in the module's folder (e.g. `./x/module/module.go`). -Almost every module need to implement the `AppModuleBasic` and `AppModule` interfaces. If the module is only used for genesis, it will implement `AppModuleGenesis` instead of `AppModule`. The concrete type that implements the interface can add parameters that are required for the implementation of the various methods of the interface. For example, the `NewHandler()` function often calls a `NewHandler(k keeper)` function defined in [`handler.go`](./handler.md) and therefore needs to pass the module's [`keeper`](./keeper.md) as parameter. +Almost every module need to implement the `AppModuleBasic` and `AppModule` interfaces. If the module is only used for genesis, it will implement `AppModuleGenesis` instead of `AppModule`. The concrete type that implements the interface can add parameters that are required for the implementation of the various methods of the interface. For example, the `Route()` function often calls a `NewHandler(k keeper)` function defined in [`handler.go`](./handler.md) and therefore needs to pass the module's [`keeper`](./keeper.md) as parameter. ```go // example From 8e191e47e7c469ebaedd1d34bd119e69b332c932 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 9 Jun 2020 18:59:12 +0200 Subject: [PATCH 11/17] update --- go.sum | 3 --- 1 file changed, 3 deletions(-) diff --git a/go.sum b/go.sum index 3bae58262634..f3859f33641e 100644 --- a/go.sum +++ b/go.sum @@ -78,7 +78,6 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= -github.com/confio/ics23 v0.6.0 h1:bQsi55t2+xjW6EWDl83IBF1VWurplbUu+OT6pukeiEo= github.com/confio/ics23-iavl v0.6.0 h1:vVRCuVaP38FCw1kTeEdFuGuiY+2vAGTBQoH7Zxkq/ws= github.com/confio/ics23-iavl v0.6.0/go.mod h1:mmXAxD1vWoO0VP8YHu6mM1QHGv71NQqa1iSVm4HeKcY= github.com/confio/ics23/go v0.0.0-20200323120010-7d9a00f0a2fa/go.mod h1:W1I3XC8d9N8OTu/ct5VJ84ylcOunZwMXsWkd27nvVts= @@ -467,8 +466,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= -github.com/stretchr/testify v1.6.0 h1:jlIyCplCJFULU/01vCkhKuTyc3OorI3bJFuw6obfgho= -github.com/stretchr/testify v1.6.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= From 7411a3ca911aeea964a705fe8904c3d83a1d2236 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 9 Jun 2020 19:00:05 +0200 Subject: [PATCH 12/17] add format --- x/staking/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/staking/module.go b/x/staking/module.go index 34ba61d20feb..1b999444d078 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -135,7 +135,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { // Route returns the message routing key for the staking module. func (am AppModule) Route() sdk.Route { - return sdk.NewRoute(RouterKey,NewHandler(am.keeper)) + return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } // QuerierRoute returns the staking module's querier route name. From acbe3013fe3df2d9c01ab7e478ff62591b8fc636 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Wed, 10 Jun 2020 16:10:28 +0200 Subject: [PATCH 13/17] simplify appmodule --- CHANGELOG.md | 2 ++ baseapp/baseapp_test.go | 2 +- baseapp/router.go | 8 ++++---- server/mock/app.go | 2 +- tests/mocks/types_router.go | 12 ++++++------ types/router.go | 4 ++-- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d43191d081c8..825814d1267e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ older clients. ### API Breaking Changes +* [\#6231](https://github.com/cosmos/cosmos-sdk/pull/6231) Simplify `AppModule` interface, `Route` and `Handler` methods become only `Route` +and returns a new Route interface type. * [\#6212](https://github.com/cosmos/cosmos-sdk/pull/6212) Remove `Get*` prefixes from key construction functions * [\#6079](https://github.com/cosmos/cosmos-sdk/pull/6079) Remove `UpgradeOldPrivValFile` (deprecated in Tendermint Core v0.28). * (modules) [\#5664](https://github.com/cosmos/cosmos-sdk/pull/5664) Remove amino `Codec` from simulation `StoreDecoder`, which now returns a function closure in order to unmarshal the key-value pairs. diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index ac17a190ccb3..7b555641e444 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -1651,7 +1651,7 @@ type testCustomRouter struct { } func (rtr *testCustomRouter) AddRoute(route sdk.Route) sdk.Router { - rtr.routes.Store(route.Route(), route.Handler()) + rtr.routes.Store(route.Path(), route.Handler()) return rtr } diff --git a/baseapp/router.go b/baseapp/router.go index 6e24473bc866..7e2e70a0c6f3 100644 --- a/baseapp/router.go +++ b/baseapp/router.go @@ -22,14 +22,14 @@ func NewRouter() *Router { // AddRoute adds a route path to the router with a given handler. The route must // be alphanumeric. func (rtr *Router) AddRoute(route sdk.Route) sdk.Router { - if !sdk.IsAlphaNumeric(route.Route()) { + if !sdk.IsAlphaNumeric(route.Path()) { panic("route expressions can only contain alphanumeric characters") } - if rtr.routes[route.Route()] != nil { - panic(fmt.Sprintf("route %s has already been initialized", route.Route())) + if rtr.routes[route.Path()] != nil { + panic(fmt.Sprintf("route %s has already been initialized", route.Path())) } - rtr.routes[route.Route()] = route.Handler() + rtr.routes[route.Path()] = route.Handler() return rtr } diff --git a/server/mock/app.go b/server/mock/app.go index 71341f5621aa..51a50efc72b0 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -36,7 +36,7 @@ func NewApp(rootDir string, logger log.Logger) (abci.Application, error) { baseApp.SetInitChainer(InitChainer(capKeyMainStore)) - // Set a handler Route. + // Set a Route. baseApp.Router().AddRoute(sdk.NewRoute("kvstore", KVStoreHandler(capKeyMainStore))) // Load latest version. diff --git a/tests/mocks/types_router.go b/tests/mocks/types_router.go index 9c436309e4dd..47316787382e 100644 --- a/tests/mocks/types_router.go +++ b/tests/mocks/types_router.go @@ -84,18 +84,18 @@ func (m *MockRoute) EXPECT() *MockRouteMockRecorder { return m.recorder } -// Route mocks base method -func (m *MockRoute) Route() string { +// Path mocks base method +func (m *MockRoute) Path() string { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Route") + ret := m.ctrl.Call(m, "Path") ret0, _ := ret[0].(string) return ret0 } -// Route indicates an expected call of Route -func (mr *MockRouteMockRecorder) Route() *gomock.Call { +// Path indicates an expected call of Path +func (mr *MockRouteMockRecorder) Path() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Route", reflect.TypeOf((*MockRoute)(nil).Route)) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Path", reflect.TypeOf((*MockRoute)(nil).Path)) } // Handler mocks base method diff --git a/types/router.go b/types/router.go index 75d990b2d89f..f294f5dca9a8 100644 --- a/types/router.go +++ b/types/router.go @@ -32,7 +32,7 @@ type Router interface { // Route represents a Route used by the Router. type Route interface { - Route() string + Path() string Handler() Handler } @@ -45,7 +45,7 @@ func NewRoute(r string, h Handler) Route { return &route{route: r, handler: h} } -func (r route) Route() string { +func (r route) Path() string { return r.route } From 01ac041edc38c187b579c007ace29043f66700db Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Wed, 10 Jun 2020 16:10:57 +0200 Subject: [PATCH 14/17] update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 825814d1267e..db9fe27bda01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,7 @@ older clients. ### API Breaking Changes -* [\#6231](https://github.com/cosmos/cosmos-sdk/pull/6231) Simplify `AppModule` interface, `Route` and `Handler` methods become only `Route` +* [\#6231](https://github.com/cosmos/cosmos-sdk/pull/6231) Simplify `AppModule` interface, `Route` and `NewHandler` methods become only `Route` and returns a new Route interface type. * [\#6212](https://github.com/cosmos/cosmos-sdk/pull/6212) Remove `Get*` prefixes from key construction functions * [\#6079](https://github.com/cosmos/cosmos-sdk/pull/6079) Remove `UpgradeOldPrivValFile` (deprecated in Tendermint Core v0.28). From 6a04b9cc3dcfaeb8d50c3565b0a6a1e2b3660caf Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Wed, 10 Jun 2020 16:12:47 +0200 Subject: [PATCH 15/17] rename vars --- types/router.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/types/router.go b/types/router.go index f294f5dca9a8..a30d41a5c1e1 100644 --- a/types/router.go +++ b/types/router.go @@ -37,16 +37,16 @@ type Route interface { } type route struct { - route string + path string handler Handler } -func NewRoute(r string, h Handler) Route { - return &route{route: r, handler: h} +func NewRoute(p string, h Handler) Route { + return &route{path: p, handler: h} } func (r route) Path() string { - return r.route + return r.path } func (r route) Handler() Handler { From cee833d9dfb16350c79ba356549e1edb56462108 Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Wed, 10 Jun 2020 19:40:29 +0200 Subject: [PATCH 16/17] remove interface --- baseapp/baseapp_test.go | 2 +- baseapp/router.go | 2 +- tests/mocks/types_module_module.go | 4 +-- tests/mocks/types_router.go | 53 +----------------------------- types/module/module.go | 4 +-- types/router.go | 18 ++++------ x/auth/module.go | 2 +- x/bank/module.go | 2 +- x/capability/module.go | 2 +- x/crisis/module.go | 2 +- x/distribution/module.go | 2 +- x/evidence/module.go | 2 +- x/gov/module.go | 2 +- x/ibc-transfer/module.go | 2 +- x/ibc/module.go | 2 +- x/mint/module.go | 2 +- x/params/module.go | 2 +- x/slashing/module.go | 2 +- x/staking/module.go | 2 +- x/upgrade/module.go | 2 +- 20 files changed, 27 insertions(+), 84 deletions(-) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 7b555641e444..22e0acd3cd7a 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -1650,7 +1650,7 @@ type testCustomRouter struct { routes sync.Map } -func (rtr *testCustomRouter) AddRoute(route sdk.Route) sdk.Router { +func (rtr *testCustomRouter) AddRoute(route *sdk.Route) sdk.Router { rtr.routes.Store(route.Path(), route.Handler()) return rtr } diff --git a/baseapp/router.go b/baseapp/router.go index 7e2e70a0c6f3..4108afe36c37 100644 --- a/baseapp/router.go +++ b/baseapp/router.go @@ -21,7 +21,7 @@ func NewRouter() *Router { // AddRoute adds a route path to the router with a given handler. The route must // be alphanumeric. -func (rtr *Router) AddRoute(route sdk.Route) sdk.Router { +func (rtr *Router) AddRoute(route *sdk.Route) sdk.Router { if !sdk.IsAlphaNumeric(route.Path()) { panic("route expressions can only contain alphanumeric characters") } diff --git a/tests/mocks/types_module_module.go b/tests/mocks/types_module_module.go index a56300ab453e..d2663f5d4c91 100644 --- a/tests/mocks/types_module_module.go +++ b/tests/mocks/types_module_module.go @@ -437,10 +437,10 @@ func (mr *MockAppModuleMockRecorder) RegisterInvariants(arg0 interface{}) *gomoc } // Route mocks base method -func (m *MockAppModule) Route() types.Route { +func (m *MockAppModule) Route() *types.Route { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Route") - ret0, _ := ret[0].(types.Route) + ret0, _ := ret[0].(*types.Route) return ret0 } diff --git a/tests/mocks/types_router.go b/tests/mocks/types_router.go index 47316787382e..33fe6edee67a 100644 --- a/tests/mocks/types_router.go +++ b/tests/mocks/types_router.go @@ -34,7 +34,7 @@ func (m *MockRouter) EXPECT() *MockRouterMockRecorder { } // AddRoute mocks base method -func (m *MockRouter) AddRoute(r types.Route) types.Router { +func (m *MockRouter) AddRoute(r *types.Route) types.Router { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "AddRoute", r) ret0, _ := ret[0].(types.Router) @@ -61,57 +61,6 @@ func (mr *MockRouterMockRecorder) Route(ctx, path interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Route", reflect.TypeOf((*MockRouter)(nil).Route), ctx, path) } -// MockRoute is a mock of Route interface -type MockRoute struct { - ctrl *gomock.Controller - recorder *MockRouteMockRecorder -} - -// MockRouteMockRecorder is the mock recorder for MockRoute -type MockRouteMockRecorder struct { - mock *MockRoute -} - -// NewMockRoute creates a new mock instance -func NewMockRoute(ctrl *gomock.Controller) *MockRoute { - mock := &MockRoute{ctrl: ctrl} - mock.recorder = &MockRouteMockRecorder{mock} - return mock -} - -// EXPECT returns an object that allows the caller to indicate expected use -func (m *MockRoute) EXPECT() *MockRouteMockRecorder { - return m.recorder -} - -// Path mocks base method -func (m *MockRoute) Path() string { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Path") - ret0, _ := ret[0].(string) - return ret0 -} - -// Path indicates an expected call of Path -func (mr *MockRouteMockRecorder) Path() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Path", reflect.TypeOf((*MockRoute)(nil).Path)) -} - -// Handler mocks base method -func (m *MockRoute) Handler() types.Handler { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Handler") - ret0, _ := ret[0].(types.Handler) - return ret0 -} - -// Handler indicates an expected call of Handler -func (mr *MockRouteMockRecorder) Handler() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Handler", reflect.TypeOf((*MockRoute)(nil).Handler)) -} - // MockQueryRouter is a mock of QueryRouter interface type MockQueryRouter struct { ctrl *gomock.Controller diff --git a/types/module/module.go b/types/module/module.go index bdc0aee65d7a..a46bb74b706e 100644 --- a/types/module/module.go +++ b/types/module/module.go @@ -141,7 +141,7 @@ type AppModule interface { RegisterInvariants(sdk.InvariantRegistry) // routes - Route() sdk.Route + Route() *sdk.Route // Deprecated: use RegisterQueryService QuerierRoute() string // Deprecated: use RegisterQueryService @@ -172,7 +172,7 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule { func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route empty module message route -func (GenesisOnlyAppModule) Route() sdk.Route { return nil } +func (GenesisOnlyAppModule) Route() *sdk.Route { return nil } // QuerierRoute returns an empty module querier route func (GenesisOnlyAppModule) QuerierRoute() string { return "" } diff --git a/types/router.go b/types/router.go index a30d41a5c1e1..d009e2afa227 100644 --- a/types/router.go +++ b/types/router.go @@ -26,30 +26,24 @@ var ( // Router provides handlers for each transaction type. type Router interface { - AddRoute(r Route) Router + AddRoute(r *Route) Router Route(ctx Context, path string) Handler } -// Route represents a Route used by the Router. -type Route interface { - Path() string - Handler() Handler -} - -type route struct { +type Route struct { path string handler Handler } -func NewRoute(p string, h Handler) Route { - return &route{path: p, handler: h} +func NewRoute(p string, h Handler) *Route { + return &Route{path: p, handler: h} } -func (r route) Path() string { +func (r Route) Path() string { return r.path } -func (r route) Handler() Handler { +func (r Route) Handler() Handler { return r.handler } diff --git a/x/auth/module.go b/x/auth/module.go index 1b1dda7fe759..d448304e7cb0 100644 --- a/x/auth/module.go +++ b/x/auth/module.go @@ -105,7 +105,7 @@ func (AppModule) Name() string { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the auth module. -func (AppModule) Route() sdk.Route { return nil } +func (AppModule) Route() *sdk.Route { return nil } // QuerierRoute returns the auth module's querier route name. func (AppModule) QuerierRoute() string { diff --git a/x/bank/module.go b/x/bank/module.go index 65cd34f6c0fd..0d7daff9d6c2 100644 --- a/x/bank/module.go +++ b/x/bank/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the bank module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/capability/module.go b/x/capability/module.go index a8e998146e89..e3d4a865d2d9 100644 --- a/x/capability/module.go +++ b/x/capability/module.go @@ -95,7 +95,7 @@ func (am AppModule) Name() string { } // Route returns the capability module's message routing key. -func (AppModule) Route() sdk.Route { return nil } +func (AppModule) Route() *sdk.Route { return nil } // QuerierRoute returns the capability module's query routing key. func (AppModule) QuerierRoute() string { return "" } diff --git a/x/crisis/module.go b/x/crisis/module.go index 8bf205202972..d32614c00e48 100644 --- a/x/crisis/module.go +++ b/x/crisis/module.go @@ -94,7 +94,7 @@ func (AppModule) Name() string { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the crisis module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(*am.keeper)) } diff --git a/x/distribution/module.go b/x/distribution/module.go index e350e53b8907..7c1dc66ef475 100644 --- a/x/distribution/module.go +++ b/x/distribution/module.go @@ -119,7 +119,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the distribution module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/evidence/module.go b/x/evidence/module.go index bef19f5d44a3..96af56883c51 100644 --- a/x/evidence/module.go +++ b/x/evidence/module.go @@ -127,7 +127,7 @@ func (am AppModule) Name() string { } // Route returns the evidence module's message routing key. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/gov/module.go b/x/gov/module.go index 2a046a98fd37..8ae5b9388669 100644 --- a/x/gov/module.go +++ b/x/gov/module.go @@ -135,7 +135,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the gov module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index 4b162310d35b..3ab73573704f 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -105,7 +105,7 @@ func (AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route implements the AppModule interface -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/ibc/module.go b/x/ibc/module.go index 6d44cef373d0..507ed8658d37 100644 --- a/x/ibc/module.go +++ b/x/ibc/module.go @@ -108,7 +108,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the ibc module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(*am.keeper)) } diff --git a/x/mint/module.go b/x/mint/module.go index ed29f9c408e2..d02cc6fc6c90 100644 --- a/x/mint/module.go +++ b/x/mint/module.go @@ -100,7 +100,7 @@ func (AppModule) Name() string { func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the mint module. -func (AppModule) Route() sdk.Route { return nil } +func (AppModule) Route() *sdk.Route { return nil } // QuerierRoute returns the mint module's querier route name. func (AppModule) QuerierRoute() string { diff --git a/x/params/module.go b/x/params/module.go index 1ba3282060c6..e37b905ac393 100644 --- a/x/params/module.go +++ b/x/params/module.go @@ -84,7 +84,7 @@ func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONMarshaler, _ json.Raw return []abci.ValidatorUpdate{} } -func (AppModule) Route() sdk.Route { return nil } +func (AppModule) Route() *sdk.Route { return nil } // GenerateGenesisState performs a no-op. func (AppModule) GenerateGenesisState(simState *module.SimulationState) {} diff --git a/x/slashing/module.go b/x/slashing/module.go index c588a971ca6a..7dd55f981c97 100644 --- a/x/slashing/module.go +++ b/x/slashing/module.go @@ -110,7 +110,7 @@ func (AppModule) Name() string { func (am AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route returns the message routing key for the slashing module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/staking/module.go b/x/staking/module.go index 1b999444d078..777c4dab9db9 100644 --- a/x/staking/module.go +++ b/x/staking/module.go @@ -134,7 +134,7 @@ func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) { } // Route returns the message routing key for the staking module. -func (am AppModule) Route() sdk.Route { +func (am AppModule) Route() *sdk.Route { return sdk.NewRoute(RouterKey, NewHandler(am.keeper)) } diff --git a/x/upgrade/module.go b/x/upgrade/module.go index 98238c5a0b2a..6ef3c07a3dab 100644 --- a/x/upgrade/module.go +++ b/x/upgrade/module.go @@ -98,7 +98,7 @@ func NewAppModule(keeper Keeper) AppModule { func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {} // Route is empty, as we do not handle Messages (just proposals) -func (AppModule) Route() sdk.Route { return nil } +func (AppModule) Route() *sdk.Route { return nil } // QuerierRoute returns the route we respond to for abci queries func (AppModule) QuerierRoute() string { return QuerierKey } From a16b885c141f9700363708707eb46463efa96a38 Mon Sep 17 00:00:00 2001 From: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Date: Wed, 10 Jun 2020 20:23:53 +0200 Subject: [PATCH 17/17] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b1079727d1d7..706c4d55654d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,7 +62,7 @@ older clients. ### API Breaking Changes * [\#6231](https://github.com/cosmos/cosmos-sdk/pull/6231) Simplify `AppModule` interface, `Route` and `NewHandler` methods become only `Route` -and returns a new Route interface type. +and returns a new `Route` type. * [\#6212](https://github.com/cosmos/cosmos-sdk/pull/6212) Remove `Get*` prefixes from key construction functions * [\#6079](https://github.com/cosmos/cosmos-sdk/pull/6079) Remove `UpgradeOldPrivValFile` (deprecated in Tendermint Core v0.28). * (modules) [\#5664](https://github.com/cosmos/cosmos-sdk/pull/5664) Remove amino `Codec` from simulation `StoreDecoder`, which now returns a function closure in order to unmarshal the key-value pairs.