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