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

     1  package crisis
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  	"github.com/spf13/cobra"
    10  	abci "github.com/tendermint/tendermint/abci/types"
    11  
    12  	"github.com/Finschia/finschia-sdk/client"
    13  	"github.com/Finschia/finschia-sdk/codec"
    14  	codectypes "github.com/Finschia/finschia-sdk/codec/types"
    15  	"github.com/Finschia/finschia-sdk/telemetry"
    16  	sdk "github.com/Finschia/finschia-sdk/types"
    17  	"github.com/Finschia/finschia-sdk/types/module"
    18  	"github.com/Finschia/finschia-sdk/x/crisis/client/cli"
    19  	"github.com/Finschia/finschia-sdk/x/crisis/keeper"
    20  	"github.com/Finschia/finschia-sdk/x/crisis/types"
    21  )
    22  
    23  var (
    24  	_ module.AppModule         = AppModule{}
    25  	_ module.AppModuleBasic    = AppModuleBasic{}
    26  	_ module.EndBlockAppModule = AppModule{}
    27  )
    28  
    29  // Module init related flags
    30  const (
    31  	FlagSkipGenesisInvariants = "x-crisis-skip-assert-invariants"
    32  )
    33  
    34  // AppModuleBasic defines the basic application module used by the crisis module.
    35  type AppModuleBasic struct{}
    36  
    37  // Name returns the crisis module's name.
    38  func (AppModuleBasic) Name() string {
    39  	return types.ModuleName
    40  }
    41  
    42  // RegisterLegacyAminoCodec registers the crisis module's types on the given LegacyAmino codec.
    43  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    44  	types.RegisterLegacyAminoCodec(cdc)
    45  }
    46  
    47  // DefaultGenesis returns default genesis state as raw bytes for the crisis
    48  // module.
    49  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    50  	return cdc.MustMarshalJSON(types.DefaultGenesisState())
    51  }
    52  
    53  // ValidateGenesis performs genesis state validation for the crisis module.
    54  func (AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, config client.TxEncodingConfig, bz json.RawMessage) error {
    55  	var data types.GenesisState
    56  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    57  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    58  	}
    59  
    60  	return types.ValidateGenesis(&data)
    61  }
    62  
    63  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the capability module.
    64  func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *runtime.ServeMux) {}
    65  
    66  // GetTxCmd returns the root tx command for the crisis module.
    67  func (b AppModuleBasic) GetTxCmd() *cobra.Command {
    68  	return cli.NewTxCmd()
    69  }
    70  
    71  // GetQueryCmd returns no root query command for the crisis module.
    72  func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil }
    73  
    74  // RegisterInterfaces registers interfaces and implementations of the crisis
    75  // module.
    76  func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    77  	types.RegisterInterfaces(registry)
    78  }
    79  
    80  // AppModule implements an application module for the crisis module.
    81  type AppModule struct {
    82  	AppModuleBasic
    83  
    84  	// NOTE: We store a reference to the keeper here so that after a module
    85  	// manager is created, the invariants can be properly registered and
    86  	// executed.
    87  	keeper *keeper.Keeper
    88  
    89  	skipGenesisInvariants bool
    90  }
    91  
    92  // NewAppModule creates a new AppModule object. If initChainAssertInvariants is set,
    93  // we will call keeper.AssertInvariants during InitGenesis (it may take a significant time)
    94  // - which doesn't impact the chain security unless 66+% of validators have a wrongly
    95  // modified genesis file.
    96  func NewAppModule(keeper *keeper.Keeper, skipGenesisInvariants bool) AppModule {
    97  	return AppModule{
    98  		AppModuleBasic: AppModuleBasic{},
    99  		keeper:         keeper,
   100  
   101  		skipGenesisInvariants: skipGenesisInvariants,
   102  	}
   103  }
   104  
   105  // AddModuleInitFlags implements servertypes.ModuleInitFlags interface.
   106  func AddModuleInitFlags(startCmd *cobra.Command) {
   107  	startCmd.Flags().Bool(FlagSkipGenesisInvariants, false, "Skip x/crisis invariants check on startup")
   108  }
   109  
   110  // Name returns the crisis module's name.
   111  func (AppModule) Name() string {
   112  	return types.ModuleName
   113  }
   114  
   115  // RegisterInvariants performs a no-op.
   116  func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
   117  
   118  // Route returns the message routing key for the crisis module.
   119  func (am AppModule) Route() sdk.Route {
   120  	return sdk.NewRoute(RouterKey, NewHandler(*am.keeper))
   121  }
   122  
   123  // QuerierRoute returns no querier route.
   124  func (AppModule) QuerierRoute() string { return "" }
   125  
   126  // LegacyQuerierHandler returns no sdk.Querier.
   127  func (AppModule) LegacyQuerierHandler(*codec.LegacyAmino) sdk.Querier { return nil }
   128  
   129  // RegisterServices registers module services.
   130  func (am AppModule) RegisterServices(cfg module.Configurator) {
   131  	types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
   132  
   133  	// m := keeper.NewMigrator(*am.keeper)
   134  	// if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   135  	// 	panic(fmt.Sprintf("failed to migrate x/crisis from version 1 to 2: %v", err))
   136  	// }
   137  }
   138  
   139  // InitGenesis performs genesis initialization for the crisis module. It returns
   140  // no validator updates.
   141  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   142  	start := time.Now()
   143  	var genesisState types.GenesisState
   144  	cdc.MustUnmarshalJSON(data, &genesisState)
   145  	telemetry.MeasureSince(start, "InitGenesis", "crisis", "unmarshal")
   146  
   147  	am.keeper.InitGenesis(ctx, &genesisState)
   148  	if !am.skipGenesisInvariants {
   149  		am.keeper.AssertInvariants(ctx)
   150  	}
   151  	return []abci.ValidatorUpdate{}
   152  }
   153  
   154  // ExportGenesis returns the exported genesis state as raw bytes for the crisis
   155  // module.
   156  func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   157  	gs := am.keeper.ExportGenesis(ctx)
   158  	return cdc.MustMarshalJSON(gs)
   159  }
   160  
   161  // ConsensusVersion implements AppModule/ConsensusVersion.
   162  func (AppModule) ConsensusVersion() uint64 { return 1 }
   163  
   164  // EndBlock returns the end blocker for the crisis module. It returns no validator
   165  // updates.
   166  func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   167  	EndBlocker(ctx, *am.keeper)
   168  	return []abci.ValidatorUpdate{}
   169  }