github.com/Finschia/finschia-sdk@v0.49.1/x/genutil/module.go (about) 1 package genutil 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/grpc-ecosystem/grpc-gateway/runtime" 8 "github.com/spf13/cobra" 9 abci "github.com/tendermint/tendermint/abci/types" 10 11 "github.com/Finschia/finschia-sdk/client" 12 "github.com/Finschia/finschia-sdk/codec" 13 cdctypes "github.com/Finschia/finschia-sdk/codec/types" 14 sdk "github.com/Finschia/finschia-sdk/types" 15 "github.com/Finschia/finschia-sdk/types/module" 16 "github.com/Finschia/finschia-sdk/x/genutil/types" 17 ) 18 19 var ( 20 _ module.AppModuleGenesis = AppModule{} 21 _ module.AppModuleBasic = AppModuleBasic{} 22 ) 23 24 // AppModuleBasic defines the basic application module used by the genutil module. 25 type AppModuleBasic struct{} 26 27 // Name returns the genutil module's name. 28 func (AppModuleBasic) Name() string { 29 return types.ModuleName 30 } 31 32 // RegisterLegacyAminoCodec registers the genutil module's types on the given LegacyAmino codec. 33 func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} 34 35 // RegisterInterfaces registers the module's interface types 36 func (b AppModuleBasic) RegisterInterfaces(_ cdctypes.InterfaceRegistry) {} 37 38 // DefaultGenesis returns default genesis state as raw bytes for the genutil 39 // module. 40 func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage { 41 return cdc.MustMarshalJSON(types.DefaultGenesisState()) 42 } 43 44 // ValidateGenesis performs genesis state validation for the genutil module. 45 func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, txEncodingConfig client.TxEncodingConfig, bz json.RawMessage) error { 46 var data types.GenesisState 47 if err := cdc.UnmarshalJSON(bz, &data); err != nil { 48 return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err) 49 } 50 51 return types.ValidateGenesis(&data, txEncodingConfig.TxJSONDecoder()) 52 } 53 54 // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the genutil module. 55 func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *runtime.ServeMux) { 56 } 57 58 // GetTxCmd returns no root tx command for the genutil module. 59 func (AppModuleBasic) GetTxCmd() *cobra.Command { return nil } 60 61 // GetQueryCmd returns no root query command for the genutil module. 62 func (AppModuleBasic) GetQueryCmd() *cobra.Command { return nil } 63 64 // AppModule implements an application module for the genutil module. 65 type AppModule struct { 66 AppModuleBasic 67 68 accountKeeper types.AccountKeeper 69 stakingKeeper types.StakingKeeper 70 deliverTx deliverTxfn 71 txEncodingConfig client.TxEncodingConfig 72 } 73 74 // NewAppModule creates a new AppModule object 75 func NewAppModule(accountKeeper types.AccountKeeper, 76 stakingKeeper types.StakingKeeper, deliverTx deliverTxfn, 77 txEncodingConfig client.TxEncodingConfig, 78 ) module.AppModule { 79 return module.NewGenesisOnlyAppModule(AppModule{ 80 AppModuleBasic: AppModuleBasic{}, 81 accountKeeper: accountKeeper, 82 stakingKeeper: stakingKeeper, 83 deliverTx: deliverTx, 84 txEncodingConfig: txEncodingConfig, 85 }) 86 } 87 88 // InitGenesis performs genesis initialization for the genutil module. It returns 89 // no validator updates. 90 func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { 91 var genesisState types.GenesisState 92 cdc.MustUnmarshalJSON(data, &genesisState) 93 validators, err := InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig) 94 if err != nil { 95 panic(err) 96 } 97 return validators 98 } 99 100 // ExportGenesis returns the exported genesis state as raw bytes for the genutil 101 // module. 102 func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMessage { 103 return am.DefaultGenesis(cdc) 104 } 105 106 // ConsensusVersion implements AppModule/ConsensusVersion. 107 func (AppModule) ConsensusVersion() uint64 { return 1 }