github.com/cosmos/cosmos-sdk@v0.50.10/x/consensus/module.go (about)

     1  package consensus
     2  
     3  import (
     4  	"context"
     5  
     6  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     7  	"google.golang.org/grpc"
     8  
     9  	modulev1 "cosmossdk.io/api/cosmos/consensus/module/v1"
    10  	"cosmossdk.io/core/appmodule"
    11  	"cosmossdk.io/core/event"
    12  	storetypes "cosmossdk.io/core/store"
    13  	"cosmossdk.io/depinject"
    14  
    15  	"github.com/cosmos/cosmos-sdk/baseapp"
    16  	"github.com/cosmos/cosmos-sdk/client"
    17  	"github.com/cosmos/cosmos-sdk/codec"
    18  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    19  	"github.com/cosmos/cosmos-sdk/runtime"
    20  	"github.com/cosmos/cosmos-sdk/types/module"
    21  	authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
    22  	"github.com/cosmos/cosmos-sdk/x/consensus/keeper"
    23  	"github.com/cosmos/cosmos-sdk/x/consensus/types"
    24  	govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
    25  )
    26  
    27  // ConsensusVersion defines the current x/consensus module consensus version.
    28  const ConsensusVersion = 1
    29  
    30  var (
    31  	_ module.AppModuleBasic = AppModule{}
    32  
    33  	_ appmodule.AppModule   = AppModule{}
    34  	_ appmodule.HasServices = AppModule{}
    35  )
    36  
    37  // AppModuleBasic defines the basic application module used by the consensus module.
    38  type AppModuleBasic struct {
    39  	cdc codec.Codec
    40  }
    41  
    42  // Name returns the consensus module's name.
    43  func (AppModuleBasic) Name() string { return types.ModuleName }
    44  
    45  // RegisterLegacyAminoCodec registers the consensus module's types on the LegacyAmino codec.
    46  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    47  	types.RegisterLegacyAminoCodec(cdc)
    48  }
    49  
    50  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes
    51  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *gwruntime.ServeMux) {
    52  	if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
    53  		panic(err)
    54  	}
    55  }
    56  
    57  // RegisterInterfaces registers interfaces and implementations of the bank module.
    58  func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    59  	types.RegisterInterfaces(registry)
    60  }
    61  
    62  // AppModule implements an application module
    63  type AppModule struct {
    64  	AppModuleBasic
    65  
    66  	keeper keeper.Keeper
    67  }
    68  
    69  // IsOnePerModuleType implements the depinject.OnePerModuleType interface.
    70  func (am AppModule) IsOnePerModuleType() {}
    71  
    72  // IsAppModule implements the appmodule.AppModule interface.
    73  func (am AppModule) IsAppModule() {}
    74  
    75  // RegisterServices registers module services.
    76  func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
    77  	types.RegisterMsgServer(registrar, am.keeper)
    78  	types.RegisterQueryServer(registrar, am.keeper)
    79  	return nil
    80  }
    81  
    82  // NewAppModule creates a new AppModule object
    83  func NewAppModule(cdc codec.Codec, keeper keeper.Keeper) AppModule {
    84  	return AppModule{
    85  		AppModuleBasic: AppModuleBasic{cdc: cdc},
    86  		keeper:         keeper,
    87  	}
    88  }
    89  
    90  // ConsensusVersion implements AppModule/ConsensusVersion.
    91  func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }
    92  
    93  func init() {
    94  	appmodule.Register(
    95  		&modulev1.Module{},
    96  		appmodule.Provide(ProvideModule),
    97  	)
    98  }
    99  
   100  type ModuleInputs struct {
   101  	depinject.In
   102  
   103  	Config       *modulev1.Module
   104  	Cdc          codec.Codec
   105  	StoreService storetypes.KVStoreService
   106  	EventManager event.Service
   107  }
   108  
   109  type ModuleOutputs struct {
   110  	depinject.Out
   111  
   112  	Keeper        keeper.Keeper
   113  	Module        appmodule.AppModule
   114  	BaseAppOption runtime.BaseAppOption
   115  }
   116  
   117  func ProvideModule(in ModuleInputs) ModuleOutputs {
   118  	// default to governance authority if not provided
   119  	authority := authtypes.NewModuleAddress(govtypes.ModuleName)
   120  	if in.Config.Authority != "" {
   121  		authority = authtypes.NewModuleAddressOrBech32Address(in.Config.Authority)
   122  	}
   123  
   124  	k := keeper.NewKeeper(in.Cdc, in.StoreService, authority.String(), in.EventManager)
   125  	m := NewAppModule(in.Cdc, k)
   126  	baseappOpt := func(app *baseapp.BaseApp) {
   127  		app.SetParamStore(k.ParamsStore)
   128  	}
   129  
   130  	return ModuleOutputs{
   131  		Keeper:        k,
   132  		Module:        m,
   133  		BaseAppOption: baseappOpt,
   134  	}
   135  }