github.com/Finschia/finschia-sdk@v0.48.1/x/authz/module/module.go (about)

     1  package authz
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"math/rand"
     7  
     8  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  	"github.com/spf13/cobra"
    10  
    11  	abci "github.com/tendermint/tendermint/abci/types"
    12  
    13  	sdkclient "github.com/Finschia/finschia-sdk/client"
    14  	"github.com/Finschia/finschia-sdk/codec"
    15  	cdctypes "github.com/Finschia/finschia-sdk/codec/types"
    16  	sdk "github.com/Finschia/finschia-sdk/types"
    17  	sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
    18  	"github.com/Finschia/finschia-sdk/types/module"
    19  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    20  	"github.com/Finschia/finschia-sdk/x/authz"
    21  	"github.com/Finschia/finschia-sdk/x/authz/client/cli"
    22  	"github.com/Finschia/finschia-sdk/x/authz/keeper"
    23  	"github.com/Finschia/finschia-sdk/x/authz/simulation"
    24  )
    25  
    26  var (
    27  	_ module.AppModule           = AppModule{}
    28  	_ module.AppModuleBasic      = AppModuleBasic{}
    29  	_ module.AppModuleSimulation = AppModule{}
    30  )
    31  
    32  // AppModuleBasic defines the basic application module used by the authz module.
    33  type AppModuleBasic struct {
    34  	cdc codec.Codec
    35  }
    36  
    37  // Name returns the authz module's name.
    38  func (AppModuleBasic) Name() string {
    39  	return authz.ModuleName
    40  }
    41  
    42  // RegisterServices registers a gRPC query service to respond to the
    43  // module-specific gRPC queries.
    44  func (am AppModule) RegisterServices(cfg module.Configurator) {
    45  	authz.RegisterQueryServer(cfg.QueryServer(), am.keeper)
    46  	authz.RegisterMsgServer(cfg.MsgServer(), am.keeper)
    47  }
    48  
    49  // RegisterLegacyAminoCodec registers the authz module's types for the given codec.
    50  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    51  	authz.RegisterLegacyAminoCodec(cdc)
    52  }
    53  
    54  // RegisterInterfaces registers the authz module's interface types
    55  func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
    56  	authz.RegisterInterfaces(registry)
    57  }
    58  
    59  // DefaultGenesis returns default genesis state as raw bytes for the authz
    60  // module.
    61  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    62  	return cdc.MustMarshalJSON(authz.DefaultGenesisState())
    63  }
    64  
    65  // ValidateGenesis performs genesis state validation for the authz module.
    66  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config sdkclient.TxEncodingConfig, bz json.RawMessage) error {
    67  	var data authz.GenesisState
    68  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    69  		return sdkerrors.Wrapf(err, "failed to unmarshal %s genesis state", authz.ModuleName)
    70  	}
    71  
    72  	return authz.ValidateGenesis(data)
    73  }
    74  
    75  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the authz module.
    76  func (a AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx sdkclient.Context, mux *runtime.ServeMux) {
    77  	if err := authz.RegisterQueryHandlerClient(context.Background(), mux, authz.NewQueryClient(clientCtx)); err != nil {
    78  		panic(err)
    79  	}
    80  }
    81  
    82  // GetQueryCmd returns the cli query commands for the authz module
    83  func (AppModuleBasic) GetQueryCmd() *cobra.Command {
    84  	return cli.GetQueryCmd()
    85  }
    86  
    87  // GetTxCmd returns the transaction commands for the authz module
    88  func (AppModuleBasic) GetTxCmd() *cobra.Command {
    89  	return cli.GetTxCmd()
    90  }
    91  
    92  // AppModule implements the sdk.AppModule interface
    93  type AppModule struct {
    94  	AppModuleBasic
    95  	keeper        keeper.Keeper
    96  	accountKeeper authz.AccountKeeper
    97  	bankKeeper    authz.BankKeeper
    98  	registry      cdctypes.InterfaceRegistry
    99  }
   100  
   101  // NewAppModule creates a new AppModule object
   102  func NewAppModule(cdc codec.Codec, keeper keeper.Keeper, ak authz.AccountKeeper, bk authz.BankKeeper, registry cdctypes.InterfaceRegistry) AppModule {
   103  	return AppModule{
   104  		AppModuleBasic: AppModuleBasic{cdc: cdc},
   105  		keeper:         keeper,
   106  		accountKeeper:  ak,
   107  		bankKeeper:     bk,
   108  		registry:       registry,
   109  	}
   110  }
   111  
   112  // Name returns the authz module's name.
   113  func (AppModule) Name() string {
   114  	return authz.ModuleName
   115  }
   116  
   117  // RegisterInvariants does nothing, there are no invariants to enforce
   118  func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
   119  
   120  // Route returns the message routing key for the staking module.
   121  func (am AppModule) Route() sdk.Route {
   122  	return sdk.NewRoute(authz.RouterKey, nil)
   123  }
   124  
   125  func (am AppModule) NewHandler() sdk.Handler {
   126  	return nil
   127  }
   128  
   129  // QuerierRoute returns the route we respond to for abci queries
   130  func (AppModule) QuerierRoute() string { return "" }
   131  
   132  // LegacyQuerierHandler returns the authz module sdk.Querier.
   133  func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
   134  	return nil
   135  }
   136  
   137  // InitGenesis performs genesis initialization for the authz module. It returns
   138  // no validator updates.
   139  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   140  	var genesisState authz.GenesisState
   141  	cdc.MustUnmarshalJSON(data, &genesisState)
   142  	am.keeper.InitGenesis(ctx, &genesisState)
   143  	return []abci.ValidatorUpdate{}
   144  }
   145  
   146  // ExportGenesis returns the exported genesis state as raw bytes for the authz
   147  // module.
   148  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   149  	gs := am.keeper.ExportGenesis(ctx)
   150  	return cdc.MustMarshalJSON(gs)
   151  }
   152  
   153  // ConsensusVersion implements AppModule/ConsensusVersion.
   154  func (AppModule) ConsensusVersion() uint64 { return 1 }
   155  
   156  // ____________________________________________________________________________
   157  
   158  // AppModuleSimulation functions
   159  
   160  // GenerateGenesisState creates a randomized GenState of the authz module.
   161  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   162  	simulation.RandomizedGenState(simState)
   163  }
   164  
   165  // ProposalContents returns all the authz content functions used to
   166  // simulate governance proposals.
   167  func (am AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
   168  	return nil
   169  }
   170  
   171  // RandomizedParams creates randomized authz param changes for the simulator.
   172  func (AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
   173  	return nil
   174  }
   175  
   176  // RegisterStoreDecoder registers a decoder for authz module's types
   177  func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   178  	sdr[keeper.StoreKey] = simulation.NewDecodeStore(am.cdc)
   179  }
   180  
   181  // WeightedOperations returns the all the gov module operations with their respective weights.
   182  func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   183  	return simulation.WeightedOperations(
   184  		simState.AppParams, simState.Cdc,
   185  		am.accountKeeper, am.bankKeeper, am.keeper, am.cdc,
   186  	)
   187  }