Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC module wiring with uber-go dig #9529

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 156 additions & 0 deletions app/cli/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package cli

import (
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/app/internal"

"github.com/cosmos/cosmos-sdk/app/query"

"github.com/cosmos/cosmos-sdk/container"

tmcli "github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
sdk "github.com/cosmos/cosmos-sdk/types"

"go.uber.org/dig"

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

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/app"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/config"
"github.com/cosmos/cosmos-sdk/server"
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/cosmos/cosmos-sdk/x/crisis"
)

type Options struct {
AppName string
Description string
DefaultAppConfig *app.Config
DefaultHome string
EnvPrefix string
}

func Run(options Options) {
err := container.Compose(
func(in inputs) {
rootCmd := makeRootCmd(in, options)
if err := svrcmd.Execute(rootCmd, string(in.DefaultHome)); err != nil {
switch e := err.(type) {
case server.ErrorCode:
os.Exit(e.Code)

default:
os.Exit(1)
}
}
},

// Provide default home
container.Provide(func() client.DefaultHome { return client.DefaultHome(options.DefaultHome) }),
// Provide codec
app.CodecProvider,
query.Module,
internal.AppConfigProvider(options.DefaultAppConfig),
)
if err != nil {
fmt.Printf("ERROR: %v\n", err)
os.Exit(-1)
}
}

type inputs struct {
dig.In

DefaultHome client.DefaultHome
RootCommands []*cobra.Command `group:"root"`
Codec codec.JSONCodec `optional:"true"`
InterfaceRegistry codectypes.InterfaceRegistry `optional:"true"`
Amino *codec.LegacyAmino `optional:"true"`
TxConfig client.TxConfig `optional:"true"`
AccountRetriever client.AccountRetriever `optional:"true"`
}

func makeRootCmd(in inputs, options Options) *cobra.Command {
initClientCtx := client.Context{}.
WithInput(os.Stdin).
WithHomeDir(options.DefaultHome).
WithViper(options.EnvPrefix)
if in.Codec != nil {
initClientCtx = initClientCtx.WithJSONCodec(in.Codec)
}
if in.InterfaceRegistry != nil {
initClientCtx = initClientCtx.WithInterfaceRegistry(in.InterfaceRegistry)
}
if in.TxConfig != nil {
initClientCtx = initClientCtx.WithTxConfig(in.TxConfig)
}
if in.AccountRetriever != nil {
initClientCtx = initClientCtx.WithAccountRetriever(in.AccountRetriever)
}
if in.Amino != nil {
initClientCtx = initClientCtx.WithLegacyAmino(in.Amino)
}

rootCmd := &cobra.Command{
Use: options.AppName,
Short: options.Description,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// set the default command outputs
cmd.SetOut(cmd.OutOrStdout())
cmd.SetErr(cmd.ErrOrStderr())

initClientCtx = client.ReadHomeFlag(initClientCtx, cmd)

initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
return err
}

if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}

return server.InterceptConfigsPreRunHandler(cmd)
},
}

cfg := sdk.GetConfig()
cfg.Seal()

rootCmd.AddCommand(in.RootCommands...)

rootCmd.AddCommand(
//TODO: AddGenesisAccountCmd(options.DefaultHome),
tmcli.NewCompletionCmd(rootCmd, true),
//TODO: testnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
config.Cmd(),
)

// TODO:
//server.AddCommands(rootCmd, options.DefaultHome, appProvider.AppCreator, appProvider.AppExportor, addModuleInitFlags)

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
rpc.StatusCommand(),
keys.Commands(options.DefaultHome),
)

// TODO: rootCmd.AddCommand(server.RosettaCommand(clientCtx.InterfaceRegistry, clientCtx.JSONCodec))

return rootCmd
}

func addModuleInitFlags(startCmd *cobra.Command) {
crisis.AddModuleInitFlags(startCmd)
}
67 changes: 67 additions & 0 deletions app/compat/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package compat

import (
"context"
"encoding/json"

grpc1 "github.com/gogo/protobuf/grpc"
"google.golang.org/grpc"

abci "github.com/tendermint/tendermint/abci/types"

"github.com/cosmos/cosmos-sdk/app"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
)

func AppModuleHandler(id app.ModuleID, module module.AppModule) app.Handler {
cfg := &configurator{}
module.RegisterServices(cfg)
return app.Handler{
ID: id,
InitGenesis: func(ctx context.Context, jsonCodec codec.JSONCodec, message json.RawMessage) []abci.ValidatorUpdate {
return module.InitGenesis(types.UnwrapSDKContext(ctx), jsonCodec, message)
},
BeginBlocker: func(ctx context.Context, req abci.RequestBeginBlock) {
module.BeginBlock(types.UnwrapSDKContext(ctx), req)
},
EndBlocker: func(ctx context.Context, req abci.RequestEndBlock) []abci.ValidatorUpdate {
return module.EndBlock(types.UnwrapSDKContext(ctx), req)
},
MsgServices: cfg.msgServices,
QueryServices: cfg.queryServices,
}
}

type configurator struct {
msgServices []app.ServiceImpl
queryServices []app.ServiceImpl
}

func (c *configurator) MsgServer() grpc1.Server {
return &serviceRegistrar{impls: c.msgServices}
}

func (c *configurator) QueryServer() grpc1.Server {
return &serviceRegistrar{impls: c.queryServices}
}

func (c *configurator) RegisterMigration(moduleName string, forVersion uint64, handler module.MigrationHandler) error {
return nil
}

var _ module.Configurator = &configurator{}

type serviceRegistrar struct {
impls []app.ServiceImpl
}

func (s *serviceRegistrar) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
s.impls = append(s.impls, app.ServiceImpl{
Desc: desc,
Impl: impl,
})
}

var _ grpc.ServiceRegistrar = &serviceRegistrar{}
Loading