forked from berachain/polaris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
299 lines (263 loc) · 9.03 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
// SPDX-License-Identifier: BUSL-1.1
//
// Copyright (C) 2023, Berachain Foundation. All rights reserved.
// Use of this software is govered by the Business Source License included
// in the LICENSE file of this repository and at www.mariadb.com/bsl11.
//
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER
// VERSIONS OF THE LICENSED WORK.
//
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE).
//
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
// TITLE.
package testapp
import (
"io"
"os"
"path/filepath"
dbm "github.com/cosmos/cosmos-db"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
evidencekeeper "cosmossdk.io/x/evidence/keeper"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
evmv1alpha1 "github.com/berachain/polaris/cosmos/api/polaris/evm/v1alpha1"
evmconfig "github.com/berachain/polaris/cosmos/config"
ethcryptocodec "github.com/berachain/polaris/cosmos/crypto/codec"
signinglib "github.com/berachain/polaris/cosmos/lib/signing"
polarruntime "github.com/berachain/polaris/cosmos/runtime"
"github.com/berachain/polaris/cosmos/runtime/ante"
"github.com/berachain/polaris/cosmos/runtime/miner"
evmkeeper "github.com/berachain/polaris/cosmos/x/evm/keeper"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
"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"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
consensuskeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
crisiskeeper "github.com/cosmos/cosmos-sdk/x/crisis/keeper"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
)
//nolint:gochecknoinits // from sdk.
func init() {
userHomeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
DefaultNodeHome = filepath.Join(userHomeDir, ".polard")
}
// DefaultNodeHome default home directories for the application daemon.
var DefaultNodeHome string
var (
_ runtime.AppI = (*SimApp)(nil)
_ servertypes.Application = (*SimApp)(nil)
)
// SimApp 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 SimApp struct {
*runtime.App
*polarruntime.Polaris
legacyAmino *codec.LegacyAmino
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry
// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
MintKeeper mintkeeper.Keeper
DistrKeeper distrkeeper.Keeper
GovKeeper *govkeeper.Keeper
CrisisKeeper *crisiskeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
ConsensusParamsKeeper consensuskeeper.Keeper
// polaris required keeper
EVMKeeper *evmkeeper.Keeper
}
// NewPolarisApp returns a reference to an initialized SimApp.
//
//nolint:funlen // from sdk.
func NewPolarisApp(
logger log.Logger,
db dbm.DB,
traceStore io.Writer,
loadLatest bool,
bech32Prefix string,
appOpts servertypes.AppOptions,
baseAppOptions ...func(*baseapp.BaseApp),
) *SimApp {
var (
app = &SimApp{}
appBuilder *runtime.AppBuilder
// merge the AppConfig and other configuration in one config
appConfig = depinject.Configs(
MakeAppConfig(bech32Prefix),
depinject.Provide(
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedEthereumTransaction],
signinglib.ProvideNoopGetSigners[*evmv1alpha1.WrappedPayloadEnvelope],
),
depinject.Supply(
// supply the application options
appOpts,
// supply the logger
logger,
// ADVANCED CONFIGURATION\
PolarisConfigFn(evmconfig.MustReadConfigFromAppOpts(appOpts)),
PrecompilesToInject(app),
QueryContextFn(app),
//
// AUTH
//
// For providing a custom function required in auth to generate custom account types
// add it below. By default the auth module uses simulation.RandomGenesisAccounts.
//
// authtypes.RandomGenesisAccountsFn(simulation.RandomGenesisAccounts),
// For providing a custom a base account type add it below.
// By default the auth module uses authtypes.ProtoBaseAccount().
//
// func() sdk.AccountI { return authtypes.ProtoBaseAccount() },
//
// MINT
//
// For providing a custom inflation function for x/mint add here your
// custom function that implements the minttypes.InflationCalculationFn
// interface.
),
)
)
if err := depinject.Inject(appConfig,
&appBuilder,
&app.appCodec,
&app.legacyAmino,
&app.txConfig,
&app.interfaceRegistry,
&app.AccountKeeper,
&app.BankKeeper,
&app.StakingKeeper,
&app.SlashingKeeper,
&app.MintKeeper,
&app.DistrKeeper,
&app.GovKeeper,
&app.CrisisKeeper,
&app.UpgradeKeeper,
&app.EvidenceKeeper,
&app.ConsensusParamsKeeper,
&app.EVMKeeper,
); err != nil {
panic(err)
}
// Build the app using the app builder.
app.App = appBuilder.Build(db, traceStore, baseAppOptions...)
app.Polaris = polarruntime.New(app,
evmconfig.MustReadConfigFromAppOpts(appOpts), app.Logger(), app.EVMKeeper.Host, nil,
)
// Build cosmos ante handler for non-evm transactions.
cosmHandler, err := authante.NewAnteHandler(
authante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
FeegrantKeeper: nil,
SigGasConsumer: ante.EthSecp256k1SigVerificationGasConsumer,
SignModeHandler: app.txConfig.SignModeHandler(),
TxFeeChecker: func(ctx sdk.Context, tx sdk.Tx) (sdk.Coins, int64, error) {
return nil, 0, nil
},
},
)
if err != nil {
panic(err)
}
// Setup Polaris Runtime.
if err = app.Polaris.Build(
app, cosmHandler, app.EVMKeeper, miner.DefaultAllowedMsgs,
); err != nil {
panic(err)
}
// register streaming services
if err = app.RegisterStreamingServices(appOpts, app.kvStoreKeys()); err != nil {
panic(err)
}
/**** Module Options ****/
app.ModuleManager.RegisterInvariants(app.CrisisKeeper)
// RegisterUpgradeHandlers is used for registering any on-chain upgrades.
app.RegisterUpgradeHandlers()
// Register eth_secp256k1 keys
ethcryptocodec.RegisterInterfaces(app.interfaceRegistry)
// Load the app.
if err = app.Load(loadLatest); err != nil {
panic(err)
}
// Load the last state of the polaris evm.
if err = app.Polaris.LoadLastState(
app.CommitMultiStore(), uint64(app.LastBlockHeight()),
); err != nil {
panic(err)
}
return app
}
// Name returns the name of the App.
func (app *SimApp) Name() string { return app.BaseApp.Name() }
// LegacyAmino returns SimApp's amino codec.
//
// NOTE: This is solely to be used for testing purposes as it may be desirable
// for modules to register their own custom testing types.
func (app *SimApp) LegacyAmino() *codec.LegacyAmino {
return app.legacyAmino
}
func (app *SimApp) kvStoreKeys() map[string]*storetypes.KVStoreKey {
keys := make(map[string]*storetypes.KVStoreKey)
for _, k := range app.GetStoreKeys() {
if kv, ok := k.(*storetypes.KVStoreKey); ok {
keys[kv.Name()] = kv
}
}
return keys
}
// SimulationManager implements the SimulationApp interface.
func (app *SimApp) SimulationManager() *module.SimulationManager {
return nil
}
// RegisterAPIRoutes registers all application module routes with the provided
// API server.
func (app *SimApp) RegisterAPIRoutes(apiSvr *api.Server, apiConfig config.APIConfig) {
app.App.RegisterAPIRoutes(apiSvr, apiConfig)
// register swagger API in app.go so that other applications can override easily
if err := server.RegisterSwaggerAPI(
apiSvr.ClientCtx, apiSvr.Router, apiConfig.Swagger,
); err != nil {
panic(err)
}
if err := app.Polaris.SetupServices(apiSvr.ClientCtx); err != nil {
panic(err)
}
}
// Close shuts down the application.
func (app *SimApp) Close() error {
if pl := app.Polaris; pl != nil {
return pl.Close()
}
return app.BaseApp.Close()
}