Skip to content

Commit

Permalink
Implement ADR 033 (#157)
Browse files Browse the repository at this point in the history
* WIP on module refactor, ADR 028, 033

* WIP

* WIP

* got it working

* refactor

* cleanup

* cleanup

* prevent re-entrancy

* add authz middleware hook

* use Modules instead of ModuleBasics

* Update types/module/server/router.go

Co-authored-by: Robert Zaremba <[email protected]>

* lint

* docs and cleanup

* Update types/module/server/router.go

Co-authored-by: Cory <[email protected]>

* docs and cleanup

Co-authored-by: Robert Zaremba <[email protected]>
Co-authored-by: Cory <[email protected]>
  • Loading branch information
3 people authored Dec 4, 2020
1 parent 7f9fd48 commit e81ef35
Show file tree
Hide file tree
Showing 32 changed files with 803 additions and 368 deletions.
30 changes: 25 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ import (

"github.com/CosmWasm/wasmd/x/wasm"
wasmclient "github.com/CosmWasm/wasmd/x/wasm/client"

newmodule "github.com/regen-network/regen-ledger/types/module"
servermodule "github.com/regen-network/regen-ledger/types/module/server"
data "github.com/regen-network/regen-ledger/x/data/module"
ecocredit "github.com/regen-network/regen-ledger/x/ecocredit/module"
)
Expand Down Expand Up @@ -123,11 +126,16 @@ var (
evidence.AppModuleBasic{},
transfer.AppModuleBasic{},
vesting.AppModuleBasic{},
data.AppModuleBasic{},
wasm.AppModuleBasic{},
ecocredit.AppModuleBasic{},
ecocredit.Module{},
data.Module{},
)

NewModules = []newmodule.Module{
ecocredit.Module{},
data.Module{},
}

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
Expand Down Expand Up @@ -213,7 +221,7 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
minttypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, capabilitytypes.StoreKey,
data.StoreKey, ecocredit.StoreKey, wasm.StoreKey,
wasm.StoreKey,
)

tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -345,6 +353,20 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
&stakingKeeper, govRouter,
)

/* New Module Wiring START */
newModuleManager := servermodule.NewManager(app.BaseApp, codec.NewProtoCodec(interfaceRegistry))

err := newModuleManager.RegisterModules(NewModules)
if err != nil {
panic(err)
}

err = newModuleManager.CompleteInitialization()
if err != nil {
panic(err)
}
/* New Module Wiring END */

app.mm = module.NewManager(
genutil.NewAppModule(
app.AccountKeeper, app.StakingKeeper, app.BaseApp.DeliverTx,
Expand All @@ -365,9 +387,7 @@ func NewRegenApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest
ibc.NewAppModule(app.IBCKeeper),
params.NewAppModule(app.ParamsKeeper),
transferModule,
data.NewAppModule(keys[data.StoreKey]),
wasm.NewAppModule(app.wasmKeeper),
ecocredit.NewAppModule(keys[ecocredit.StoreKey]),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down
104 changes: 0 additions & 104 deletions testutil/server/configurator/configurator.go

This file was deleted.

15 changes: 0 additions & 15 deletions testutil/server/configurator/helper.go

This file was deleted.

78 changes: 0 additions & 78 deletions testutil/server/configurator/router.go

This file was deleted.

2 changes: 1 addition & 1 deletion testutil/server/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type FixtureFactory interface {
// independent of the backend.
type Fixture interface {

// Context is the context.Context to be used with gRPC generated cient code.
// Context is the context.Context to be used with gRPC generated client code.
Context() context.Context

// TxConn is the grpc.ClientConnInterface to be used when constructing Msg service clients.
Expand Down
13 changes: 13 additions & 0 deletions types/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package types

import "crypto/sha256"

func AddressHash(prefix string, contents []byte) []byte {
preImage := []byte(prefix)
if len(contents) != 0 {
preImage = append(preImage, 0)
preImage = append(preImage, contents...)
}
sum := sha256.Sum256(preImage)
return sum[:20]
}
10 changes: 10 additions & 0 deletions types/id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package types

type ModuleID struct {
ModuleName string
Path []byte
}

func (m ModuleID) Address() []byte {
return AddressHash(m.ModuleName, m.Path)
}
15 changes: 15 additions & 0 deletions types/module/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package module

import "github.com/cosmos/cosmos-sdk/codec/types"

// Module is the base module type that all modules (client and server) must satisfy.
type Module interface {
Name() string
}

// TypeModule is an interface that modules should implement to register types.
type TypeModule interface {
Module

RegisterInterfaces(types.InterfaceRegistry)
}
20 changes: 20 additions & 0 deletions types/module/client/cli/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cli

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/regen-network/regen-ledger/types/module"
"github.com/spf13/cobra"
)

// Module is an interface that modules should implement to integrate with the CLI framework.
type Module interface {
module.TypeModule

DefaultGenesis(codec.JSONMarshaler) json.RawMessage
ValidateGenesis(codec.JSONMarshaler, client.TxEncodingConfig, json.RawMessage) error
GetTxCmd() *cobra.Command
GetQueryCmd() *cobra.Command
}
14 changes: 14 additions & 0 deletions types/module/client/grpc_gateway/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package grpc_gateway

import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/regen-network/regen-ledger/types/module"
)

// Module is an interface that modules should implement to register grpc-gateway routes.
type Module interface {
module.Module

RegisterGRPCGatewayRoutes(client.Context, *runtime.ServeMux)
}
Loading

0 comments on commit e81ef35

Please sign in to comment.