forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
307 lines (263 loc) · 10.1 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
package app
import (
"context"
"regexp"
attestkeeper "github.com/omni-network/omni/halo/attest/keeper"
atypes "github.com/omni-network/omni/halo/attest/types"
"github.com/omni-network/omni/halo/comet"
"github.com/omni-network/omni/halo/evmslashing"
"github.com/omni-network/omni/halo/evmstaking"
"github.com/omni-network/omni/halo/evmupgrade"
registrykeeper "github.com/omni-network/omni/halo/registry/keeper"
rtypes "github.com/omni-network/omni/halo/registry/types"
valsynckeeper "github.com/omni-network/omni/halo/valsync/keeper"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/log"
evmengkeeper "github.com/omni-network/omni/octane/evmengine/keeper"
etypes "github.com/omni-network/omni/octane/evmengine/types"
abci "github.com/cometbft/cometbft/abci/types"
"cosmossdk.io/depinject"
sdklog "cosmossdk.io/log"
evidencekeeper "cosmossdk.io/x/evidence/keeper"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
utypes "cosmossdk.io/x/upgrade/types"
dbm "github.com/cosmos/cosmos-db"
"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"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
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"
distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
_ "cosmossdk.io/api/cosmos/tx/config/v1" // import for side-effects
_ "cosmossdk.io/x/evidence" // import for side-effects
_ "cosmossdk.io/x/upgrade" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/auth" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/auth/tx/config" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/bank" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/consensus" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/distribution" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/genutil" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/mint" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/slashing" // import for side-effects
_ "github.com/cosmos/cosmos-sdk/x/staking" // import for side-effects
)
const Name = "halo"
// 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 {
*runtime.App
appCodec codec.Codec
txConfig client.TxConfig
interfaceRegistry codectypes.InterfaceRegistry
// keepers
AccountKeeper authkeeper.AccountKeeper
BankKeeper bankkeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
SlashingKeeper slashingkeeper.Keeper
DistrKeeper distrkeeper.Keeper
ConsensusParamsKeeper consensuskeeper.Keeper
EVMEngKeeper *evmengkeeper.Keeper
AttestKeeper *attestkeeper.Keeper
ValSyncKeeper *valsynckeeper.Keeper
RegistryKeeper registrykeeper.Keeper
EvidenceKeeper evidencekeeper.Keeper
UpgradeKeeper *upgradekeeper.Keeper
SlashingEventProc evmslashing.EventProcessor
StakingEventProc evmstaking.EventProcessor
UpgradeEventProc evmupgrade.EventProcessor
}
// newApp returns a reference to an initialized App.
func newApp(
logger sdklog.Logger,
db dbm.DB,
engineCl ethclient.EngineClient,
voter atypes.Voter,
chainVerNamer atypes.ChainVerNameFunc,
chainNamer rtypes.ChainNameFunc,
feeRecProvider etypes.FeeRecipientProvider,
appOpts servertypes.AppOptions,
asyncAbort chan<- error,
baseAppOpts ...func(*baseapp.BaseApp),
) (*App, error) {
depCfg := depinject.Configs(
appConfig(),
depinject.Provide(diProviders...),
depinject.Supply(
logger,
engineCl,
chainVerNamer,
chainNamer,
voter,
feeRecProvider,
appOpts,
),
)
var (
app = new(App)
appBuilder = new(runtime.AppBuilder)
)
if err := depinject.Inject(depCfg,
&appBuilder,
&app.appCodec,
&app.txConfig,
&app.interfaceRegistry,
&app.AccountKeeper,
&app.BankKeeper,
&app.StakingKeeper,
&app.SlashingKeeper,
&app.DistrKeeper,
&app.ConsensusParamsKeeper,
&app.EVMEngKeeper,
&app.AttestKeeper,
&app.ValSyncKeeper,
&app.RegistryKeeper,
&app.EvidenceKeeper,
&app.UpgradeKeeper,
&app.SlashingEventProc,
&app.StakingEventProc,
&app.UpgradeEventProc,
); err != nil {
return nil, errors.Wrap(err, "dep inject")
}
// Wire provider.
app.EVMEngKeeper.SetVoteProvider(app.AttestKeeper)
app.AttestKeeper.SetValidatorProvider(app.ValSyncKeeper)
app.AttestKeeper.SetPortalRegistry(app.RegistryKeeper)
baseAppOpts = append(baseAppOpts, func(bapp *baseapp.BaseApp) {
// Use evm engine to create block proposals.
// Note that we do not check MaxTxBytes since all EngineEVM transaction MUST be included since we cannot
// postpone them to the next block. Nit: we could drop some vote extensions though...?
bapp.SetPrepareProposal(app.EVMEngKeeper.PrepareProposal)
// Route proposed messages to keepers for verification and external state updates.
bapp.SetProcessProposal(makeProcessProposalHandler(makeProcessProposalRouter(app), app.txConfig))
// Use attest keeper to extend votes.
bapp.SetExtendVoteHandler(app.AttestKeeper.ExtendVote)
bapp.SetVerifyVoteExtensionHandler(app.AttestKeeper.VerifyVoteExtension)
})
app.App = appBuilder.Build(db, nil, baseAppOpts...)
// Blocker overrides
{
// Workaround for official endblockers since valsync replaces staking endblocker, but cosmos panics if it's not there.
app.ModuleManager.OrderEndBlockers = endBlockers
app.SetEndBlocker(app.EndBlocker)
// Wrap upgrade module preblocker and do immediate shutdown if upgrade is needed.
app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
resp, err := app.PreBlocker(ctx, req)
if upgrade, ok := isErrOldBinary(err); ok {
// Dump last applied upgrade info to disk so cosmovisor can auto upgrade.
if err := dumpLastAppliedUpgradeInfo(ctx, app.UpgradeKeeper); err != nil {
log.Error(ctx, "Failed writing last applied upgrade info", err)
}
asyncAbort <- errors.Wrap(err, "⛔️ network already upgraded, switch halo binary", "upgrade", upgrade)
<-ctx.Done() // Wait for shutdown.
} else if upgrade, ok := isErrUpgradeNeeded(err); ok {
asyncAbort <- errors.Wrap(err, "⛔️ network upgrade needed now, switch halo binary", "upgrade", upgrade)
<-ctx.Done() // Wait for shutdown.
}
return resp, err //nolint:wrapcheck // Don't wrap this cosmos error.
})
}
// setUpgradeHandlers should be called before `Load()`
// because StoreLoad is sealed after that
if err := app.setUpgradeHandlers(); err != nil {
return nil, errors.Wrap(err, "set upgrade handlers")
}
if err := app.Load(true); err != nil {
return nil, errors.Wrap(err, "load app")
}
return app, nil
}
func (App) LegacyAmino() *codec.LegacyAmino {
return nil
}
func (App) ExportAppStateAndValidators(_ bool, _, _ []string) (servertypes.ExportedApp, error) {
return servertypes.ExportedApp{}, errors.New("not implemented")
}
// SimulationManager implements the SimulationApp interface.
func (App) SimulationManager() *module.SimulationManager {
return nil
}
// SetCometAPI sets the comet API client.
// TODO(corver): Figure out how to use depinject to set this.
func (a App) SetCometAPI(api comet.API) {
a.EVMEngKeeper.SetCometAPI(api)
}
// ClientContext returns a new client context with the app's codec and tx config.
func (a App) ClientContext(ctx context.Context) client.Context {
return client.Context{}.
WithInterfaceRegistry(a.interfaceRegistry).
WithTxConfig(a.txConfig).
WithChainID(a.ChainID()).
WithCmdContext(ctx).
WithCodec(a.appCodec)
}
// dumpLastAppliedUpgradeInfo dumps the last applied upgrade info to disk.
// This is a workaround for halovisor to auto upgrade binaries
// after snapsyncing to a post-upgrade state using a pre-upgrade (old) binary.
func dumpLastAppliedUpgradeInfo(ctx sdk.Context, keeper *upgradekeeper.Keeper) error {
name, height, err := keeper.GetLastCompletedUpgrade(ctx)
if err != nil {
return errors.Wrap(err, "get last completed upgrade")
}
// Note that we need to ensure that the next binary doesn't actually run any
// store loader upgrades on startup, it was already done during the upgrade.
// We therefore ensure height isn't current.
current := sdk.UnwrapSDKContext(ctx).BlockHeight()
if height >= current { // Sanity check that the upgrade was in the past.
return errors.New("unexpected last upgrade height [BUG]")
}
err = keeper.DumpUpgradeInfoToDisk(height, utypes.Plan{
Name: name,
Height: height,
})
if err != nil {
return errors.Wrap(err, "dump upgrade info")
}
return nil
}
// isErrOldBinary returns the last applied upgrade and true if the error is due to the
// upgrade module detecting the binary is too old.
func isErrOldBinary(err error) (string, bool) {
if err == nil {
return "", false
}
cause := errors.Cause(err)
reg := regexp.MustCompile(`wrong app version \d+, upgrade handler is missing for (.+) upgrade plan`)
if !reg.MatchString(cause.Error()) {
return "", false
}
matches := reg.FindStringSubmatch(cause.Error())
if len(matches) != 2 {
return "", false
}
return matches[1], true
}
func isErrUpgradeNeeded(err error) (string, bool) {
if err == nil {
return "", false
}
cause := errors.Cause(err)
reg := regexp.MustCompile(`UPGRADE "(.+)" NEEDED .+`)
if !reg.MatchString(cause.Error()) {
return "", false
}
matches := reg.FindStringSubmatch(cause.Error())
if len(matches) != 2 {
return "", false
}
return matches[1], true
}
var (
_ runtime.AppI = (*App)(nil)
_ servertypes.Application = (*App)(nil)
)