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

     1  package genutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	abci "github.com/cometbft/cometbft/abci/types"
     8  	gwruntime "github.com/grpc-ecosystem/grpc-gateway/runtime"
     9  
    10  	modulev1 "cosmossdk.io/api/cosmos/genutil/module/v1"
    11  	"cosmossdk.io/core/appmodule"
    12  	"cosmossdk.io/core/genesis"
    13  	"cosmossdk.io/depinject"
    14  
    15  	"github.com/cosmos/cosmos-sdk/client"
    16  	"github.com/cosmos/cosmos-sdk/codec"
    17  	cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
    18  	sdk "github.com/cosmos/cosmos-sdk/types"
    19  	"github.com/cosmos/cosmos-sdk/types/module"
    20  	"github.com/cosmos/cosmos-sdk/x/genutil/types"
    21  )
    22  
    23  var (
    24  	_ module.AppModuleBasic = AppModule{}
    25  	_ module.HasABCIGenesis = AppModule{}
    26  
    27  	_ appmodule.AppModule = AppModule{}
    28  )
    29  
    30  // AppModuleBasic defines the basic application module used by the genutil module.
    31  type AppModuleBasic struct {
    32  	GenTxValidator types.MessageValidator
    33  }
    34  
    35  // NewAppModuleBasic creates AppModuleBasic, validator is a function used to validate genesis
    36  // transactions.
    37  func NewAppModuleBasic(validator types.MessageValidator) AppModuleBasic {
    38  	return AppModuleBasic{validator}
    39  }
    40  
    41  // Name returns the genutil module's name.
    42  func (AppModuleBasic) Name() string {
    43  	return types.ModuleName
    44  }
    45  
    46  // RegisterLegacyAminoCodec registers the genutil module's types on the given LegacyAmino codec.
    47  func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {}
    48  
    49  // RegisterInterfaces registers the module's interface types
    50  func (b AppModuleBasic) RegisterInterfaces(cdctypes.InterfaceRegistry) {}
    51  
    52  // DefaultGenesis returns default genesis state as raw bytes for the genutil
    53  // module.
    54  func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    55  	return cdc.MustMarshalJSON(types.DefaultGenesisState())
    56  }
    57  
    58  // ValidateGenesis performs genesis state validation for the genutil module.
    59  func (b AppModuleBasic) ValidateGenesis(cdc codec.JSONCodec, txEncodingConfig client.TxEncodingConfig, bz json.RawMessage) error {
    60  	var data types.GenesisState
    61  	if err := cdc.UnmarshalJSON(bz, &data); err != nil {
    62  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
    63  	}
    64  
    65  	return types.ValidateGenesis(&data, txEncodingConfig.TxJSONDecoder(), b.GenTxValidator)
    66  }
    67  
    68  // RegisterGRPCGatewayRoutes registers the gRPC Gateway routes for the genutil module.
    69  func (AppModuleBasic) RegisterGRPCGatewayRoutes(_ client.Context, _ *gwruntime.ServeMux) {
    70  }
    71  
    72  // AppModule implements an application module for the genutil module.
    73  type AppModule struct {
    74  	AppModuleBasic
    75  
    76  	accountKeeper    types.AccountKeeper
    77  	stakingKeeper    types.StakingKeeper
    78  	deliverTx        genesis.TxHandler
    79  	txEncodingConfig client.TxEncodingConfig
    80  }
    81  
    82  // NewAppModule creates a new AppModule object
    83  func NewAppModule(accountKeeper types.AccountKeeper,
    84  	stakingKeeper types.StakingKeeper, deliverTx genesis.TxHandler,
    85  	txEncodingConfig client.TxEncodingConfig,
    86  ) module.GenesisOnlyAppModule {
    87  	return module.NewGenesisOnlyAppModule(AppModule{
    88  		AppModuleBasic:   AppModuleBasic{},
    89  		accountKeeper:    accountKeeper,
    90  		stakingKeeper:    stakingKeeper,
    91  		deliverTx:        deliverTx,
    92  		txEncodingConfig: txEncodingConfig,
    93  	})
    94  }
    95  
    96  // IsOnePerModuleType implements the depinject.OnePerModuleType interface.
    97  func (AppModule) IsOnePerModuleType() {}
    98  
    99  // IsAppModule implements the appmodule.AppModule interface.
   100  func (AppModule) IsAppModule() {}
   101  
   102  // InitGenesis performs genesis initialization for the genutil module.
   103  func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   104  	var genesisState types.GenesisState
   105  	cdc.MustUnmarshalJSON(data, &genesisState)
   106  	validators, err := InitGenesis(ctx, am.stakingKeeper, am.deliverTx, genesisState, am.txEncodingConfig)
   107  	if err != nil {
   108  		panic(err)
   109  	}
   110  	return validators
   111  }
   112  
   113  // ExportGenesis returns the exported genesis state as raw bytes for the genutil
   114  // module.
   115  func (am AppModule) ExportGenesis(_ sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   116  	return am.DefaultGenesis(cdc)
   117  }
   118  
   119  // ConsensusVersion implements AppModule/ConsensusVersion.
   120  func (AppModule) ConsensusVersion() uint64 { return 1 }
   121  
   122  func init() {
   123  	appmodule.Register(&modulev1.Module{},
   124  		appmodule.Provide(ProvideModule),
   125  	)
   126  }
   127  
   128  // ModuleInputs defines the inputs needed for the genutil module.
   129  type ModuleInputs struct {
   130  	depinject.In
   131  
   132  	AccountKeeper types.AccountKeeper
   133  	StakingKeeper types.StakingKeeper
   134  	DeliverTx     genesis.TxHandler
   135  	Config        client.TxConfig
   136  }
   137  
   138  func ProvideModule(in ModuleInputs) appmodule.AppModule {
   139  	m := NewAppModule(in.AccountKeeper, in.StakingKeeper, in.DeliverTx, in.Config)
   140  	return m
   141  }