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