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

     1  package upgrade
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	ocabci "github.com/Finschia/ostracon/abci/types"
     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  	sdk "github.com/Finschia/finschia-sdk/types"
    16  	"github.com/Finschia/finschia-sdk/types/module"
    17  	"github.com/Finschia/finschia-sdk/x/upgrade/client/cli"
    18  	"github.com/Finschia/finschia-sdk/x/upgrade/keeper"
    19  	"github.com/Finschia/finschia-sdk/x/upgrade/types"
    20  )
    21  
    22  func init() {
    23  	types.RegisterLegacyAminoCodec(codec.NewLegacyAmino())
    24  }
    25  
    26  var (
    27  	_ module.AppModule           = AppModule{}
    28  	_ module.AppModuleBasic      = AppModuleBasic{}
    29  	_ module.BeginBlockAppModule = AppModule{}
    30  )
    31  
    32  // AppModuleBasic implements the sdk.AppModuleBasic interface
    33  type AppModuleBasic struct{}
    34  
    35  // Name returns the ModuleName
    36  func (AppModuleBasic) Name() string {
    37  	return types.ModuleName
    38  }
    39  
    40  // RegisterLegacyAminoCodec registers the upgrade types on the LegacyAmino codec
    41  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
    42  	types.RegisterLegacyAminoCodec(cdc)
    43  }
    44  
    45  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the upgrade module.
    46  func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *runtime.ServeMux) {
    47  	if err := types.RegisterQueryHandlerClient(context.Background(), mux, types.NewQueryClient(clientCtx)); err != nil {
    48  		panic(err)
    49  	}
    50  }
    51  
    52  // GetQueryCmd returns the cli query commands for this module
    53  func (AppModuleBasic) GetQueryCmd() *cobra.Command {
    54  	return cli.GetQueryCmd()
    55  }
    56  
    57  // GetTxCmd returns the transaction commands for this module
    58  func (AppModuleBasic) GetTxCmd() *cobra.Command {
    59  	return cli.GetTxCmd()
    60  }
    61  
    62  func (b AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
    63  	types.RegisterInterfaces(registry)
    64  }
    65  
    66  // AppModule implements the sdk.AppModule interface
    67  type AppModule struct {
    68  	AppModuleBasic
    69  	keeper keeper.Keeper
    70  }
    71  
    72  // NewAppModule creates a new AppModule object
    73  func NewAppModule(keeper keeper.Keeper) AppModule {
    74  	return AppModule{
    75  		AppModuleBasic: AppModuleBasic{},
    76  		keeper:         keeper,
    77  	}
    78  }
    79  
    80  // RegisterInvariants does nothing, there are no invariants to enforce
    81  func (AppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
    82  
    83  // Route is empty, as we do not handle Messages (just proposals)
    84  func (AppModule) Route() sdk.Route { return sdk.Route{} }
    85  
    86  // QuerierRoute returns the route we respond to for abci queries
    87  func (AppModule) QuerierRoute() string { return types.QuerierKey }
    88  
    89  // LegacyQuerierHandler registers a query handler to respond to the module-specific queries
    90  func (am AppModule) LegacyQuerierHandler(legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
    91  	return keeper.NewQuerier(am.keeper, legacyQuerierCdc)
    92  }
    93  
    94  // RegisterServices registers a GRPC query service to respond to the
    95  // module-specific GRPC queries.
    96  func (am AppModule) RegisterServices(cfg module.Configurator) {
    97  	types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
    98  
    99  	/* m := keeper.NewMigrator(am.keeper)
   100  	if err := cfg.RegisterMigration(types.ModuleName, 1, m.Migrate1to2); err != nil {
   101  		panic(fmt.Sprintf("failed to migrate x/upgrade from version 1 to 2: %v", err))
   102  	} */
   103  }
   104  
   105  // InitGenesis is ignored, no sense in serializing future upgrades
   106  func (am AppModule) InitGenesis(_ sdk.Context, _ codec.JSONCodec, _ json.RawMessage) []abci.ValidatorUpdate {
   107  	return []abci.ValidatorUpdate{}
   108  }
   109  
   110  // DefaultGenesis is an empty object
   111  func (AppModuleBasic) DefaultGenesis(_ codec.JSONCodec) json.RawMessage {
   112  	return []byte("{}")
   113  }
   114  
   115  // ValidateGenesis is always successful, as we ignore the value
   116  func (AppModuleBasic) ValidateGenesis(_ codec.JSONCodec, config client.TxEncodingConfig, _ json.RawMessage) error {
   117  	return nil
   118  }
   119  
   120  // ExportGenesis is always empty, as InitGenesis does nothing either
   121  func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   122  	return am.DefaultGenesis(cdc)
   123  }
   124  
   125  // ConsensusVersion implements AppModule/ConsensusVersion.
   126  func (AppModule) ConsensusVersion() uint64 { return 1 }
   127  
   128  // BeginBlock calls the upgrade module hooks
   129  //
   130  // CONTRACT: this is registered in BeginBlocker *before* all other modules' BeginBlock functions
   131  func (am AppModule) BeginBlock(ctx sdk.Context, req ocabci.RequestBeginBlock) {
   132  	BeginBlocker(am.keeper, ctx, req)
   133  }