github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/module_adapter.go (about) 1 package wasm 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "path/filepath" 7 "sync" 8 9 store "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/store/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 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/server" 15 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 16 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types/upgrade" 17 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 18 tmcli "github.com/fibonacci-chain/fbc/libs/tendermint/libs/cli" 19 types2 "github.com/fibonacci-chain/fbc/libs/tendermint/types" 20 "github.com/fibonacci-chain/fbc/x/wasm/client/cli" 21 "github.com/fibonacci-chain/fbc/x/wasm/keeper" 22 "github.com/fibonacci-chain/fbc/x/wasm/types" 23 "github.com/gorilla/mux" 24 "github.com/spf13/cast" 25 "github.com/spf13/cobra" 26 "github.com/spf13/viper" 27 ) 28 29 const SupportedFeatures = keeper.SupportedFeatures 30 31 func (b AppModuleBasic) RegisterCodec(amino *codec.Codec) { 32 RegisterCodec(amino) 33 } 34 35 func (b AppModuleBasic) DefaultGenesis() json.RawMessage { 36 return nil 37 } 38 39 func (b AppModuleBasic) ValidateGenesis(message json.RawMessage) error { 40 return nil 41 } 42 43 func (b AppModuleBasic) GetTxCmdV2(cdc *codec.CodecProxy, reg cdctypes.InterfaceRegistry) *cobra.Command { 44 return cli.NewTxCmd(cdc, reg) 45 } 46 47 func (b AppModuleBasic) GetQueryCmdV2(cdc *codec.CodecProxy, reg cdctypes.InterfaceRegistry) *cobra.Command { 48 return cli.NewQueryCmd(cdc, reg) 49 } 50 51 func (b AppModuleBasic) RegisterRouterForGRPC(cliCtx clictx.CLIContext, r *mux.Router) { 52 53 } 54 func (am AppModule) NewHandler() sdk.Handler { 55 return NewHandler(keeper.NewDefaultPermissionKeeper(am.keeper)) 56 } 57 58 func (am AppModule) NewQuerierHandler() sdk.Querier { 59 return keeper.NewLegacyQuerier(am.keeper, am.keeper.QueryGasLimit()) 60 } 61 62 // InitGenesis performs genesis initialization for the wasm module. It returns 63 // no validator updates. 64 func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate { 65 // Note: use RegisterTask instead 66 67 //var genesisState GenesisState 68 //ModuleCdc.MustUnmarshalJSON(data, &genesisState) 69 //validators, err := InitGenesis(ctx, am.keeper, genesisState, am.NewHandler()) 70 //if err != nil { 71 // panic(err) 72 //} 73 //return validators 74 return nil 75 } 76 77 func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage { 78 if !types2.HigherThanEarth(ctx.BlockHeight()) { 79 return nil 80 } 81 gs := ExportGenesis(ctx, am.keeper) 82 return ModuleCdc.MustMarshalJSON(gs) 83 } 84 85 func (am AppModule) RegisterTask() upgrade.HeightTask { 86 return upgrade.NewHeightTask( 87 0, func(ctx sdk.Context) error { 88 if am.Sealed() { 89 return nil 90 } 91 _, err := InitGenesis(ctx, am.keeper, GenesisState{Params: DefaultParams()}, am.NewHandler()) 92 return err 93 }) 94 } 95 96 var ( 97 defaultDenyFilter store.StoreFilter = func(module string, h int64, s store.CommitKVStore) bool { 98 return module == ModuleName 99 } 100 101 defaultCommitFilter store.StoreFilter = func(module string, h int64, s store.CommitKVStore) bool { 102 if module != ModuleName { 103 return false 104 } 105 106 if h == types2.GetEarthHeight() { 107 if s != nil { 108 s.SetUpgradeVersion(h) 109 } 110 return false 111 } 112 113 if types2.HigherThanEarth(h) { 114 return false 115 } 116 117 return true 118 } 119 defaultPruneFilter store.StoreFilter = func(module string, h int64, s store.CommitKVStore) bool { 120 if module != ModuleName { 121 return false 122 } 123 124 if types2.HigherThanEarth(h) { 125 return false 126 } 127 128 return true 129 } 130 defaultVersionFilter store.VersionFilter = func(h int64) func(cb func(name string, version int64)) { 131 if h < 0 { 132 return func(cb func(name string, version int64)) {} 133 } 134 135 return func(cb func(name string, version int64)) { 136 cb(ModuleName, types2.GetEarthHeight()) 137 } 138 } 139 ) 140 141 func (am AppModule) CommitFilter() *store.StoreFilter { 142 if am.UpgradeHeight() == 0 { 143 return &defaultDenyFilter 144 } 145 return &defaultCommitFilter 146 } 147 148 func (am AppModule) PruneFilter() *store.StoreFilter { 149 if am.UpgradeHeight() == 0 { 150 return &defaultDenyFilter 151 } 152 return &defaultPruneFilter 153 } 154 155 func (am AppModule) VersionFilter() *store.VersionFilter { 156 return &defaultVersionFilter 157 } 158 159 func (am AppModule) UpgradeHeight() int64 { 160 return types2.GetEarthHeight() 161 } 162 163 var ( 164 once sync.Once 165 gWasmConfig types.WasmConfig 166 gWasmDir string 167 ) 168 169 func WasmDir() string { 170 once.Do(Init) 171 return gWasmDir 172 } 173 174 func WasmConfig() types.WasmConfig { 175 once.Do(Init) 176 return gWasmConfig 177 } 178 179 func Init() { 180 wasmConfig, err := ReadWasmConfig() 181 if err != nil { 182 panic(fmt.Sprintf("error while reading wasm config: %s", err)) 183 } 184 gWasmConfig = wasmConfig 185 gWasmDir = filepath.Join(viper.GetString(tmcli.HomeFlag), "data") 186 187 } 188 189 // ReadWasmConfig reads the wasm specifig configuration 190 func ReadWasmConfig() (types.WasmConfig, error) { 191 cfg := types.DefaultWasmConfig() 192 var err error 193 if v := viper.Get(flagWasmMemoryCacheSize); v != nil { 194 if cfg.MemoryCacheSize, err = cast.ToUint32E(v); err != nil { 195 return cfg, err 196 } 197 } 198 if v := viper.Get(flagWasmQueryGasLimit); v != nil { 199 if cfg.SmartQueryGasLimit, err = cast.ToUint64E(v); err != nil { 200 return cfg, err 201 } 202 } 203 if v := viper.Get(flagWasmSimulationGasLimit); v != nil { 204 if raw, ok := v.(string); ok && raw != "" { 205 limit, err := cast.ToUint64E(v) // non empty string set 206 if err != nil { 207 return cfg, err 208 } 209 cfg.SimulationGasLimit = &limit 210 } 211 } 212 // attach contract debugging to global "trace" flag 213 if v := viper.Get(server.FlagTrace); v != nil { 214 if cfg.ContractDebugMode, err = cast.ToBoolE(v); err != nil { 215 return cfg, err 216 } 217 } 218 return cfg, nil 219 }