github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evidence/module.go (about)

     1  package evidence
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     9  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    10  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    11  	"github.com/fibonacci-chain/fbc/x/evidence/client"
    12  	"github.com/fibonacci-chain/fbc/x/evidence/client/cli"
    13  	"github.com/fibonacci-chain/fbc/x/evidence/client/rest"
    14  
    15  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    16  	"github.com/gorilla/mux"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  var (
    21  	_ module.AppModule      = AppModule{}
    22  	_ module.AppModuleBasic = AppModuleBasic{}
    23  
    24  	// TODO: Enable simulation once concrete types are defined.
    25  	// _ module.AppModuleSimulation = AppModuleSimulation{}
    26  )
    27  
    28  // ----------------------------------------------------------------------------
    29  // AppModuleBasic
    30  // ----------------------------------------------------------------------------
    31  
    32  // AppModuleBasic implements the AppModuleBasic interface for the evidence module.
    33  type AppModuleBasic struct {
    34  	evidenceHandlers []client.EvidenceHandler // client evidence submission handlers
    35  }
    36  
    37  func NewAppModuleBasic(evidenceHandlers ...client.EvidenceHandler) AppModuleBasic {
    38  	return AppModuleBasic{
    39  		evidenceHandlers: evidenceHandlers,
    40  	}
    41  }
    42  
    43  // Name returns the evidence module's name.
    44  func (AppModuleBasic) Name() string {
    45  	return ModuleName
    46  }
    47  
    48  // RegisterCodec registers the evidence module's types to the provided codec.
    49  func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
    50  	RegisterCodec(cdc)
    51  }
    52  
    53  // DefaultGenesis returns the evidence module's default genesis state.
    54  func (AppModuleBasic) DefaultGenesis() json.RawMessage {
    55  	return ModuleCdc.MustMarshalJSON(DefaultGenesisState())
    56  }
    57  
    58  // ValidateGenesis performs genesis state validation for the evidence module.
    59  func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
    60  	var gs GenesisState
    61  	if err := ModuleCdc.UnmarshalJSON(bz, &gs); err != nil {
    62  		return fmt.Errorf("failed to unmarshal %s genesis state: %w", ModuleName, err)
    63  	}
    64  
    65  	return gs.Validate()
    66  }
    67  
    68  // RegisterRESTRoutes registers the evidence module's REST service handlers.
    69  func (a AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
    70  	evidenceRESTHandlers := make([]rest.EvidenceRESTHandler, len(a.evidenceHandlers))
    71  
    72  	for i, evidenceHandler := range a.evidenceHandlers {
    73  		evidenceRESTHandlers[i] = evidenceHandler.RESTHandler(ctx)
    74  	}
    75  
    76  	rest.RegisterRoutes(ctx, rtr, evidenceRESTHandlers)
    77  }
    78  
    79  // GetTxCmd returns the evidence module's root tx command.
    80  func (a AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    81  	evidenceCLIHandlers := make([]*cobra.Command, len(a.evidenceHandlers))
    82  
    83  	for i, evidenceHandler := range a.evidenceHandlers {
    84  		evidenceCLIHandlers[i] = evidenceHandler.CLIHandler(cdc)
    85  	}
    86  
    87  	return cli.GetTxCmd(StoreKey, cdc, evidenceCLIHandlers)
    88  }
    89  
    90  // GetTxCmd returns the evidence module's root query command.
    91  func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    92  	return cli.GetQueryCmd(StoreKey, cdc)
    93  }
    94  
    95  // ----------------------------------------------------------------------------
    96  // AppModule
    97  // ----------------------------------------------------------------------------
    98  
    99  // AppModule implements the AppModule interface for the evidence module.
   100  type AppModule struct {
   101  	AppModuleBasic
   102  
   103  	keeper Keeper
   104  }
   105  
   106  func NewAppModule(keeper Keeper) AppModule {
   107  	return AppModule{
   108  		AppModuleBasic: NewAppModuleBasic(),
   109  		keeper:         keeper,
   110  	}
   111  }
   112  
   113  // Name returns the evidence module's name.
   114  func (am AppModule) Name() string {
   115  	return am.AppModuleBasic.Name()
   116  }
   117  
   118  // Route returns the evidence module's message routing key.
   119  func (AppModule) Route() string {
   120  	return RouterKey
   121  }
   122  
   123  // QuerierRoute returns the evidence module's query routing key.
   124  func (AppModule) QuerierRoute() string {
   125  	return QuerierRoute
   126  }
   127  
   128  // NewHandler returns the evidence module's message Handler.
   129  func (am AppModule) NewHandler() sdk.Handler {
   130  	return NewHandler(am.keeper)
   131  }
   132  
   133  // NewQuerierHandler returns the evidence module's Querier.
   134  func (am AppModule) NewQuerierHandler() sdk.Querier {
   135  	return NewQuerier(am.keeper)
   136  }
   137  
   138  // RegisterInvariants registers the evidence module's invariants.
   139  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}
   140  
   141  // InitGenesis performs the evidence module's genesis initialization It returns
   142  // no validator updates.
   143  func (am AppModule) InitGenesis(ctx sdk.Context, bz json.RawMessage) []abci.ValidatorUpdate {
   144  	var gs GenesisState
   145  	err := ModuleCdc.UnmarshalJSON(bz, &gs)
   146  	if err != nil {
   147  		panic(fmt.Sprintf("failed to unmarshal %s genesis state: %s", ModuleName, err))
   148  	}
   149  
   150  	InitGenesis(ctx, am.keeper, gs)
   151  	return []abci.ValidatorUpdate{}
   152  }
   153  
   154  // ExportGenesis returns the evidence module's exported genesis state as raw JSON bytes.
   155  func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
   156  	return ModuleCdc.MustMarshalJSON(ExportGenesis(ctx, am.keeper))
   157  }
   158  
   159  // BeginBlock executes all ABCI BeginBlock logic respective to the evidence module.
   160  func (am AppModule) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) {
   161  	BeginBlocker(ctx, req, am.keeper)
   162  }
   163  
   164  // EndBlock executes all ABCI EndBlock logic respective to the evidence module. It
   165  // returns no validator updates.
   166  func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   167  	return []abci.ValidatorUpdate{}
   168  }