This repository has been archived by the owner on Jun 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 360
/
app.go
308 lines (271 loc) · 9.43 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
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 Berachain Foundation
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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)
}
polarisConfig := evmconfig.MustReadConfigFromAppOpts(appOpts)
if polarisConfig.OptimisticExecution {
baseAppOptions = append(baseAppOptions, baseapp.SetOptimisticExecution())
}
// Build the app using the app builder.
app.App = appBuilder.Build(db, traceStore, baseAppOptions...)
app.Polaris = polarruntime.New(app,
polarisConfig, 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.
// TODO: verify that the version of app CMS is the same as app.LastBlockHeight.
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()
}