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

     1  package wasm
     2  
     3  import (
     4  	"context"
     5  	"math/rand"
     6  
     7  	"github.com/fibonacci-chain/fbc/app/rpc/simulator"
     8  	"github.com/fibonacci-chain/fbc/libs/tendermint/global"
     9  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    10  
    11  	clictx "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context"
    12  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    13  	cdctypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
    14  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    15  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/module"
    16  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/upgrade"
    17  	simtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    18  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/base"
    19  	abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types"
    20  	"github.com/fibonacci-chain/fbc/x/wasm/client/rest"
    21  	"github.com/fibonacci-chain/fbc/x/wasm/keeper"
    22  	"github.com/fibonacci-chain/fbc/x/wasm/simulation"
    23  	"github.com/fibonacci-chain/fbc/x/wasm/types"
    24  	"github.com/fibonacci-chain/fbc/x/wasm/watcher"
    25  	"github.com/gorilla/mux"
    26  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var (
    31  	_ module.AppModuleAdapter      = AppModule{}
    32  	_ module.AppModuleBasicAdapter = AppModuleBasic{}
    33  	_ module.AppModuleSimulation   = AppModule{}
    34  	_ upgrade.UpgradeModule        = AppModule{}
    35  )
    36  
    37  // Module init related flags
    38  const (
    39  	flagWasmMemoryCacheSize    = "wasm.memory_cache_size"
    40  	flagWasmQueryGasLimit      = "wasm.query_gas_limit"
    41  	flagWasmSimulationGasLimit = "wasm.simulation_gas_limit"
    42  )
    43  
    44  // AppModuleBasic defines the basic application module used by the wasm module.
    45  type AppModuleBasic struct{}
    46  
    47  //func (b AppModuleBasic) RegisterLegacyAminoCodec(amino *codec.LegacyAmino) { //nolint:staticcheck
    48  //	RegisterCodec(amino)
    49  //}
    50  
    51  func (b AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx clictx.CLIContext, serveMux *runtime.ServeMux) {
    52  	err := types.RegisterQueryHandlerClient(context.Background(), serveMux, types.NewQueryClient(clientCtx))
    53  	if err != nil {
    54  		panic(err)
    55  	}
    56  }
    57  
    58  // Name returns the wasm module's name.
    59  func (AppModuleBasic) Name() string {
    60  	return ModuleName
    61  }
    62  
    63  // DefaultGenesis returns default genesis state as raw bytes for the wasm
    64  // module.
    65  //func (AppModuleBasic) DefaultGenesis(cdc codec.JSONCodec) json.RawMessage {
    66  //	return cdc.MustMarshalJSON(&GenesisState{
    67  //		Params: DefaultParams(),
    68  //	})
    69  //}
    70  
    71  // ValidateGenesis performs genesis state validation for the wasm module.
    72  //func (b AppModuleBasic) ValidateGenesis(marshaler codec.JSONCodec, config client.TxEncodingConfig, message json.RawMessage) error {
    73  //	var data GenesisState
    74  //	err := marshaler.UnmarshalJSON(message, &data)
    75  //	if err != nil {
    76  //		return err
    77  //	}
    78  //	return ValidateGenesis(data)
    79  //}
    80  
    81  // RegisterRESTRoutes registers the REST routes for the wasm module.
    82  func (AppModuleBasic) RegisterRESTRoutes(cliCtx clictx.CLIContext, rtr *mux.Router) {
    83  	rest.RegisterRoutes(cliCtx, rtr)
    84  }
    85  
    86  // GetTxCmd returns the root tx command for the wasm module.
    87  func (b AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
    88  	return nil
    89  }
    90  
    91  // GetQueryCmd returns no root query command for the wasm module.
    92  func (b AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
    93  	return nil
    94  }
    95  
    96  // RegisterInterfaces implements InterfaceModule
    97  func (b AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
    98  	types.RegisterInterfaces(registry)
    99  }
   100  
   101  // ____________________________________________________________________________
   102  
   103  // AppModule implements an application module for the wasm module.
   104  type AppModule struct {
   105  	AppModuleBasic
   106  	*base.BaseIBCUpgradeModule
   107  	cdc              codec.CodecProxy
   108  	keeper           *Keeper
   109  	permissionKeeper types.ContractOpsKeeper
   110  }
   111  
   112  // ConsensusVersion is a sequence number for state-breaking change of the
   113  // module. It should be incremented on each consensus-breaking change
   114  // introduced by the module. To avoid wrong/empty versions, the initial version
   115  // should be set to 1.
   116  func (AppModule) ConsensusVersion() uint64 { return 1 }
   117  
   118  // NewAppModule creates a new AppModule object
   119  func NewAppModule(cdc codec.CodecProxy, wasmkeeper *Keeper) AppModule {
   120  	m := AppModule{
   121  		AppModuleBasic: AppModuleBasic{},
   122  		cdc:            cdc,
   123  		keeper:         wasmkeeper,
   124  	}
   125  	m.BaseIBCUpgradeModule = base.NewBaseIBCUpgradeModule(m)
   126  	m.permissionKeeper = keeper.NewDefaultPermissionKeeper(wasmkeeper)
   127  	return m
   128  }
   129  
   130  func (am AppModule) RegisterServices(cfg module.Configurator) {
   131  	global.Manager = watcher.ParamsManager{}
   132  	simulator.NewWasmSimulator = NewWasmSimulator
   133  	types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.permissionKeeper))
   134  	if watcher.Enable() {
   135  		k := NewProxyKeeper()
   136  		types.RegisterQueryServer(cfg.QueryServer(), NewQuerier(&k))
   137  	} else {
   138  		types.RegisterQueryServer(cfg.QueryServer(), NewQuerier(am.keeper))
   139  	}
   140  }
   141  
   142  func (am AppModule) GetPermissionKeeper() types.ContractOpsKeeper {
   143  	return am.permissionKeeper
   144  }
   145  
   146  //func (am AppModule) LegacyQuerierHandler(amino *codec.LegacyAmino) sdk.Querier { //nolint:staticcheck
   147  //	return keeper.NewLegacyQuerier(am.keeper, am.keeper.QueryGasLimit())
   148  //}
   149  
   150  // RegisterInvariants registers the wasm module invariants.
   151  func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}
   152  
   153  // Route returns the message routing key for the wasm module.
   154  func (am AppModule) Route() string {
   155  	return RouterKey
   156  }
   157  
   158  // QuerierRoute returns the wasm module's querier route name.
   159  func (AppModule) QuerierRoute() string {
   160  	return QuerierRoute
   161  }
   162  
   163  //// InitGenesis performs genesis initialization for the wasm module. It returns
   164  //// no validator updates.
   165  //func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate {
   166  //	var genesisState GenesisState
   167  //	cdc.MustUnmarshalJSON(data, &genesisState)
   168  //	validators, err := InitGenesis(ctx, am.keeper, genesisState, am.validatorSetSource, am.Route().Handler())
   169  //	if err != nil {
   170  //		panic(err)
   171  //	}
   172  //	return validators
   173  //}
   174  
   175  // ExportGenesis returns the exported genesis state as raw bytes for the wasm
   176  // module.
   177  //func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.RawMessage {
   178  //	gs := ExportGenesis(ctx, am.keeper)
   179  //	return cdc.MustMarshalJSON(gs)
   180  //}
   181  
   182  // BeginBlock returns the begin blocker for the wasm module.
   183  func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {
   184  	watcher.NewHeight()
   185  	if tmtypes.DownloadDelta {
   186  		keeper.GetWasmParamsCache().SetNeedParamsUpdate()
   187  		keeper.GetWasmParamsCache().SetNeedBlockedUpdate()
   188  	}
   189  }
   190  
   191  // EndBlock returns the end blocker for the wasm module. It returns no validator
   192  // updates.
   193  func (AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
   194  	return []abci.ValidatorUpdate{}
   195  }
   196  
   197  // ____________________________________________________________________________
   198  
   199  // AppModuleSimulation functions
   200  
   201  // GenerateGenesisState creates a randomized GenState of the bank module.
   202  func (AppModule) GenerateGenesisState(simState *module.SimulationState) {
   203  	simulation.RandomizedGenState(simState)
   204  }
   205  
   206  // ProposalContents doesn't return any content functions for governance proposals.
   207  func (AppModule) ProposalContents(simState module.SimulationState) []simtypes.WeightedProposalContent {
   208  	return nil
   209  }
   210  
   211  // RandomizedParams creates randomized bank param changes for the simulator.
   212  func (am AppModule) RandomizedParams(r *rand.Rand) []simtypes.ParamChange {
   213  	aminoCdc := am.cdc.GetCdc()
   214  	return simulation.ParamChanges(r, *aminoCdc)
   215  }
   216  
   217  // RegisterStoreDecoder registers a decoder for supply module's types
   218  func (am AppModule) RegisterStoreDecoder(sdr sdk.StoreDecoderRegistry) {
   219  }
   220  
   221  // WeightedOperations returns the all the gov module operations with their respective weights.
   222  func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation {
   223  	return nil
   224  }
   225  
   226  // ____________________________________________________________________________
   227  
   228  // AddModuleInitFlags implements servertypes.ModuleInitFlags interface.
   229  func AddModuleInitFlags(startCmd *cobra.Command) {
   230  	defaults := DefaultWasmConfig()
   231  	startCmd.Flags().Uint32(flagWasmMemoryCacheSize, defaults.MemoryCacheSize, "Sets the size in MiB (NOT bytes) of an in-memory cache for Wasm modules. Set to 0 to disable.")
   232  	startCmd.Flags().Uint64(flagWasmQueryGasLimit, defaults.SmartQueryGasLimit, "Set the max gas that can be spent on executing a query with a Wasm contract")
   233  	startCmd.Flags().String(flagWasmSimulationGasLimit, "", "Set the max gas that can be spent when executing a simulation TX")
   234  }
   235  
   236  //// ReadWasmConfig reads the wasm specifig configuration
   237  //func ReadWasmConfig(opts servertypes.AppOptions) (types.WasmConfig, error) {
   238  //	cfg := types.DefaultWasmConfig()
   239  //	var err error
   240  //	if v := opts.Get(flagWasmMemoryCacheSize); v != nil {
   241  //		if cfg.MemoryCacheSize, err = cast.ToUint32E(v); err != nil {
   242  //			return cfg, err
   243  //		}
   244  //	}
   245  //	if v := opts.Get(flagWasmQueryGasLimit); v != nil {
   246  //		if cfg.SmartQueryGasLimit, err = cast.ToUint64E(v); err != nil {
   247  //			return cfg, err
   248  //		}
   249  //	}
   250  //	if v := opts.Get(flagWasmSimulationGasLimit); v != nil {
   251  //		if raw, ok := v.(string); ok && raw != "" {
   252  //			limit, err := cast.ToUint64E(v) // non empty string set
   253  //			if err != nil {
   254  //				return cfg, err
   255  //			}
   256  //			cfg.SimulationGasLimit = &limit
   257  //		}
   258  //	}
   259  //	// attach contract debugging to global "trace" flag
   260  //	if v := opts.Get(server.FlagTrace); v != nil {
   261  //		if cfg.ContractDebugMode, err = cast.ToBoolE(v); err != nil {
   262  //			return cfg, err
   263  //		}
   264  //	}
   265  //	return cfg, nil
   266  //}