-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.go
326 lines (274 loc) · 11.3 KB
/
app.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package app
import (
"fmt"
"io"
"net/http"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/server/api"
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
"github.com/cosmos/cosmos-sdk/x/crisis"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
_ "github.com/ethereum/go-ethereum/eth/tracers/native"
srvflags "github.com/evmos/ethermint/server/flags"
"github.com/rakyll/statik/fs"
"github.com/spf13/cast"
abci "github.com/tendermint/tendermint/abci/types"
tmjson "github.com/tendermint/tendermint/libs/json"
"github.com/tendermint/tendermint/libs/log"
tmos "github.com/tendermint/tendermint/libs/os"
dbm "github.com/tendermint/tm-db"
fxante "github.com/functionx/fx-core/v3/ante"
"github.com/functionx/fx-core/v3/app/keepers"
_ "github.com/functionx/fx-core/v3/docs/statik"
"github.com/functionx/fx-core/v3/server/grpc/base/gasprice"
gaspricelegacy "github.com/functionx/fx-core/v3/server/grpc/base/gasprice/legacy"
fxtypes "github.com/functionx/fx-core/v3/types"
"github.com/functionx/fx-core/v3/x/crosschain"
"github.com/functionx/fx-core/v3/x/crosschain/keeper"
crosschaintypes "github.com/functionx/fx-core/v3/x/crosschain/types"
)
var _ servertypes.Application = (*App)(nil)
// App extends an ABCI application, but with most of its parameters exported.
// They are exported for convenience in creating helper functions, as object
// capabilities aren't needed for testing.
type App struct {
*baseapp.BaseApp
*keepers.AppKeepers
interfaceRegistry types.InterfaceRegistry
mm *module.Manager
configurator module.Configurator
}
func New(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
skipUpgradeHeights map[int64]bool,
homePath string,
invCheckPeriod uint,
encodingConfig EncodingConfig,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *App {
appCodec := encodingConfig.Codec
legacyAmino := encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry
bApp := baseapp.NewBaseApp(
fxtypes.Name,
logger,
db,
encodingConfig.TxConfig.TxDecoder(),
baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetVersion(version.Version)
bApp.SetInterfaceRegistry(interfaceRegistry)
myApp := &App{
BaseApp: bApp,
interfaceRegistry: interfaceRegistry,
}
// Setup keepers
myApp.AppKeepers = keepers.NewAppKeeper(
appCodec,
bApp,
legacyAmino,
maccPerms,
myApp.BlockedModuleAccountAddrs(),
skipUpgradeHeights,
homePath,
invCheckPeriod,
appOpts,
)
// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
// we prefer to be more strict in what arguments the modules expect.
skipGenesisInvariants := cast.ToBool(appOpts.Get(crisis.FlagSkipGenesisInvariants))
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
myApp.mm = module.NewManager(appModules(myApp, encodingConfig, skipGenesisInvariants)...)
// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
// NOTE: staking module is required if HistoricalEntries param > 0
// NOTE: capability module's beginblocker must come before any modules using capabilities (e.g. IBC)
myApp.mm.SetOrderBeginBlockers(orderBeginBlockers()...)
myApp.mm.SetOrderEndBlockers(orderEndBlockers()...)
// NOTE: The genutils module must occur after staking so that pools are
// properly initialized with tokens from genesis accounts.
// NOTE: The genutils module must also occur after auth so that it can access the params from auth.
// NOTE: Capability module must occur first so that it can initialize any capabilities
// so that other modules that want to create or claim capabilities afterwards in InitChain
// can do so safely.
myApp.mm.SetOrderInitGenesis(orderInitBlockers()...)
myApp.mm.RegisterInvariants(&myApp.CrisisKeeper)
myApp.mm.RegisterRoutes(myApp.Router(), myApp.QueryRouter(), encodingConfig.Amino)
myApp.configurator = module.NewConfigurator(myApp.AppCodec(), myApp.MsgServiceRouter(), myApp.GRPCQueryRouter())
myApp.RegisterServices(myApp.configurator)
// initialize stores
myApp.MountKVStores(myApp.GetKVStoreKey())
myApp.MountTransientStores(myApp.GetTransientStoreKey())
myApp.MountMemoryStores(myApp.GetMemoryStoreKey())
maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted))
anteOptions := fxante.HandlerOptions{
AccountKeeper: myApp.AccountKeeper,
BankKeeper: myApp.BankKeeper,
EvmKeeper: myApp.EvmKeeper,
FeeMarketKeeper: myApp.FeeMarketKeeper,
IbcKeeper: myApp.IBCKeeper,
SignModeHandler: encodingConfig.TxConfig.SignModeHandler(),
SigGasConsumer: fxante.DefaultSigVerificationGasConsumer,
MaxTxGasWanted: maxGasWanted,
BypassMinFeeMsgTypes: cast.ToStringSlice(appOpts.Get(fxtypes.BypassMinFeeMsgTypesKey)),
MaxBypassMinFeeMsgGasUsage: cast.ToUint64(appOpts.Get(fxtypes.BypassMinFeeMsgMaxGasUsageKey)),
}
if err := anteOptions.Validate(); err != nil {
panic(fmt.Errorf("failed to ante options validate: %s", err))
}
myApp.SetAnteHandler(fxante.NewAnteHandler(anteOptions))
myApp.SetInitChainer(myApp.InitChainer)
myApp.SetBeginBlocker(myApp.BeginBlocker)
myApp.SetEndBlocker(myApp.EndBlocker)
myApp.setupUpgradeHandlers()
myApp.setupUpgradeStoreLoaders()
if loadLatest {
if err := myApp.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}
}
return myApp
}
// Name returns the name of the App
func (app *App) Name() string {
return app.BaseApp.Name()
}
// BeginBlocker application updates every begin block
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
}
// EndBlocker application updates every end block
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
}
// InitChainer application update at chain initialization
func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
return app.mm.InitGenesis(ctx, app.AppCodec(), genesisState)
}
// LoadHeight loads a particular height
func (app *App) LoadHeight(height int64) error {
return app.LoadVersion(height)
}
// ModuleAccountAddrs returns all the app's module account addresses.
func (app *App) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
}
return modAccAddrs
}
// BlockedModuleAccountAddrs returns all the app's blocked module account
// addresses.
func (app *App) BlockedModuleAccountAddrs() map[string]bool {
modAccAddrs := app.ModuleAccountAddrs()
// remove module accounts that are ALLOWED to received funds
// TODO: Blocked on updating to v0.46.x
// delete(modAccAddrs, authtypes.NewModuleAddress(grouptypes.ModuleName).String())
return modAccAddrs
}
// InterfaceRegistry returns InterfaceRegistry
func (app *App) InterfaceRegistry() types.InterfaceRegistry {
return app.interfaceRegistry
}
// SimulationManager implements the SimulationApp interface
func (app *App) SimulationManager() *module.SimulationManager {
return &module.SimulationManager{}
}
func (app *App) RegisterServices(cfg module.Configurator) {
for _, m := range app.mm.Modules {
m.RegisterServices(cfg)
}
gasprice.RegisterQueryServer(cfg.QueryServer(), gasprice.Querier{})
gaspricelegacy.RegisterQueryServer(cfg.QueryServer(), gaspricelegacy.Querier{}) // nolint:staticcheck
crosschaintypes.RegisterQueryServer(cfg.QueryServer(), app.CrosschainKeeper)
crosschaintypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerRouterImpl(app.CrosschainKeeper))
}
// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *App) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
clientCtx := apiSvr.ClientCtx
rpc.RegisterRoutes(clientCtx, apiSvr.Router)
// Register legacy tx routes.
authrest.RegisterTxRoutes(clientCtx, apiSvr.Router)
// Register new tx routes from grpc-gateway.
authtx.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register new tendermint queries routes from grpc-gateway.
tmservice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register gas price queries routes from grpc-gateway.
gasprice.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register crosschain queries routes from grpc-gateway.
crosschain.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// Register legacy and grpc-gateway routes for all modules.
ModuleBasics.RegisterRESTRoutes(clientCtx, apiSvr.Router)
ModuleBasics.RegisterGRPCGatewayRoutes(clientCtx, apiSvr.GRPCGatewayRouter)
// register swagger API from root so that other applications can override easily
if apiConfig.Swagger {
statikFS, err := fs.New()
if err != nil {
panic(err)
}
staticServer := http.FileServer(statikFS)
apiSvr.Router.PathPrefix("/swagger/").Handler(http.StripPrefix("/swagger/", staticServer))
}
}
// RegisterTxService implements the Application.RegisterTxService method.
func (app *App) RegisterTxService(clientCtx client.Context) {
authtx.RegisterTxService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.BaseApp.Simulate, app.interfaceRegistry)
}
// RegisterTendermintService implements the Application.RegisterTendermintService method.
func (app *App) RegisterTendermintService(clientCtx client.Context) {
tmservice.RegisterTendermintService(app.BaseApp.GRPCQueryRouter(), clientCtx, app.interfaceRegistry)
}
// TestingApp functions
// GetBaseApp implements the TestingApp interface.
func (app *App) GetBaseApp() *baseapp.BaseApp {
return app.BaseApp
}
// GetTxConfig implements the TestingApp interface.
func (app *App) GetTxConfig() client.TxConfig {
return MakeEncodingConfig().TxConfig
}
// GetModules implements the TestingApp interface.
func (app *App) GetModules() map[string]module.AppModule {
return app.mm.Modules
}
// EmptyAppOptions is a stub implementing AppOptions
type EmptyAppOptions struct{}
// Get implements AppOptions
func (ao EmptyAppOptions) Get(string) interface{} {
return nil
}
func (app *App) GetStakingKeeper() stakingkeeper.Keeper {
return app.StakingKeeper
}
func (app *App) GetIBCKeeper() *ibckeeper.Keeper {
return app.IBCKeeper
}
func (app *App) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
return app.ScopedIBCKeeper
}