forked from omni-network/omni
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.go
347 lines (284 loc) · 9.55 KB
/
start.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package app
import (
"context"
"time"
"github.com/omni-network/omni/halo/comet"
halocfg "github.com/omni-network/omni/halo/config"
"github.com/omni-network/omni/lib/buildinfo"
cprovider "github.com/omni-network/omni/lib/cchain/provider"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/lib/tracer"
etypes "github.com/omni-network/omni/octane/evmengine/types"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
"github.com/cometbft/cometbft/proxy"
rpclocal "github.com/cometbft/cometbft/rpc/client/local"
cmttypes "github.com/cometbft/cometbft/types"
"github.com/ethereum/go-ethereum/common"
"cosmossdk.io/store"
pruningtypes "cosmossdk.io/store/pruning/types"
"cosmossdk.io/store/snapshots"
snapshottypes "cosmossdk.io/store/snapshots/types"
storetypes "cosmossdk.io/store/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/server"
sdktelemetry "github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/mempool"
)
// Config wraps the halo (app) and comet (client) configurations.
type Config struct {
halocfg.Config
Comet cmtcfg.Config
}
// BackendType returns the halo config backend type
// or the comet backend type otherwise.
func (c Config) BackendType() dbm.BackendType {
if c.Config.BackendType == "" {
return dbm.BackendType(c.Comet.DBBackend)
}
return dbm.BackendType(c.Config.BackendType)
}
// Run runs the halo client until the context is canceled.
//
//nolint:contextcheck // Explicit new stop context.
func Run(ctx context.Context, cfg Config) error {
async, stopFunc, err := Start(ctx, cfg)
if err != nil {
return err
}
select {
case <-ctx.Done():
log.Info(ctx, "Shutdown detected, stopping...")
case err := <-async:
return err
}
// Use a fresh context for stopping (only allow 5 seconds).
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return stopFunc(stopCtx)
}
// Start starts the halo client returning a stop function or an error.
//
// Note that the original context used to start the app must be canceled first
// before calling the stop function and a fresh context should be passed into the stop function.
func Start(ctx context.Context, cfg Config) (<-chan error, func(context.Context) error, error) {
log.Info(ctx, "Starting halo consensus client")
if err := cfg.Verify(); err != nil {
return nil, nil, errors.Wrap(err, "verify halo config")
}
buildinfo.Instrument(ctx)
tracerIDs := tracer.Identifiers{Network: cfg.Network, Service: "halo", Instance: cfg.Comet.Moniker}
stopTracer, err := tracer.Init(ctx, tracerIDs, cfg.Tracer)
if err != nil {
return nil, nil, err
}
if err := enableSDKTelemetry(); err != nil {
return nil, nil, errors.Wrap(err, "enable cosmos-sdk telemetry")
}
privVal, err := loadPrivVal(cfg)
if err != nil {
return nil, nil, errors.Wrap(err, "load validator key")
}
db, err := dbm.NewDB("application", cfg.BackendType(), cfg.DataDir())
if err != nil {
return nil, nil, errors.Wrap(err, "create db")
}
baseAppOpts, err := makeBaseAppOpts(cfg)
if err != nil {
return nil, nil, errors.Wrap(err, "make base app opts")
}
engineCl, err := newEngineClient(ctx, cfg, cfg.Network, privVal.Key.PubKey)
if err != nil {
return nil, nil, err
}
voter, err := newVoterLoader(privVal.Key.PrivKey) // Construct a lazy voter loader
if err != nil {
return nil, nil, err
}
//nolint:contextcheck // False positive
app, err := newApp(
newSDKLogger(ctx),
db,
engineCl,
voter,
netconf.ChainVersionNamer(cfg.Network),
burnEVMFees{},
baseAppOpts...,
)
if err != nil {
return nil, nil, errors.Wrap(err, "create app")
}
app.EVMEngKeeper.SetBuildDelay(cfg.EVMBuildDelay)
app.EVMEngKeeper.SetBuildOptimistic(cfg.EVMBuildOptimistic)
cmtNode, err := newCometNode(ctx, &cfg.Comet, app, privVal)
if err != nil {
return nil, nil, errors.Wrap(err, "create comet node")
}
rpcClient := rpclocal.New(cmtNode)
cmtAPI := comet.NewAPI(rpcClient)
app.SetCometAPI(cmtAPI)
cProvider := cprovider.NewABCIProvider(rpcClient, cfg.Network, netconf.ChainVersionNamer(cfg.Network))
async := make(chan error, 1)
go func() {
err := voter.LazyLoad(
ctx,
cfg.Network,
engineCl,
cfg.RPCEndpoints,
cProvider,
privVal.Key.PrivKey,
cfg.VoterStateFile(),
cmtAPI,
)
if err != nil {
async <- err
}
}()
log.Info(ctx, "Starting CometBFT", "listeners", cmtNode.Listeners())
if err := cmtNode.Start(); err != nil {
return nil, nil, errors.Wrap(err, "start comet node")
}
// Return async and stop functions.
// Note that the original context used to start the app must be canceled first.
// And a fresh context should be passed into the stop function.
return async, func(ctx context.Context) error {
voter.WaitDone()
if err := cmtNode.Stop(); err != nil {
return errors.Wrap(err, "stop comet node")
}
cmtNode.Wait()
// Note that cometBFT doesn't shut down cleanly. It leaves a bunch of goroutines running...
if err := stopTracer(ctx); err != nil {
return errors.Wrap(err, "stop tracer")
}
return nil
}, nil
}
func newCometNode(ctx context.Context, cfg *cmtcfg.Config, app *App, privVal cmttypes.PrivValidator,
) (*node.Node, error) {
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return nil, errors.Wrap(err, "load or gen node key", "key_file", cfg.NodeKeyFile())
}
cmtLog, err := newCmtLogger(ctx, cfg.LogLevel)
if err != nil {
return nil, err
}
wrapper := newABCIWrapper(
server.NewCometABCIWrapper(app),
app.EVMEngKeeper.PostFinalize,
func() storetypes.CacheMultiStore {
return app.CommitMultiStore().CacheMultiStore()
},
)
cmtNode, err := node.NewNode(cfg,
privVal,
nodeKey,
proxy.NewLocalClientCreator(wrapper),
node.DefaultGenesisDocProviderFunc(cfg),
cmtcfg.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
cmtLog,
)
if err != nil {
return nil, errors.Wrap(err, "create node")
}
return cmtNode, nil
}
func makeBaseAppOpts(cfg Config) ([]func(*baseapp.BaseApp), error) {
chainID, err := chainIDFromGenesis(cfg)
if err != nil {
return nil, err
}
snapshotStore, err := newSnapshotStore(cfg)
if err != nil {
return nil, err
}
snapshotOptions := snapshottypes.NewSnapshotOptions(cfg.SnapshotInterval, uint32(cfg.SnapshotKeepRecent))
pruneOpts := pruningtypes.NewPruningOptionsFromString(cfg.PruningOption)
if cfg.PruningOption == pruningtypes.PruningOptionDefault {
// Override the default cosmosSDK pruning values with much more aggressive defaults
// since historical state isn't very important for most use-cases.
pruneOpts = pruningtypes.NewCustomPruningOptions(defaultPruningKeep, defaultPruningInterval)
}
return []func(*baseapp.BaseApp){
// baseapp.SetOptimisticExecution(), // TODO(corver): Enable this.
baseapp.SetChainID(chainID),
baseapp.SetMinRetainBlocks(cfg.MinRetainBlocks),
baseapp.SetPruning(pruneOpts),
baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager()),
baseapp.SetSnapshot(snapshotStore, snapshotOptions),
baseapp.SetMempool(mempool.NoOpMempool{}),
}, nil
}
func newSnapshotStore(cfg Config) (*snapshots.Store, error) {
db, err := dbm.NewDB("metadata", cfg.BackendType(), cfg.SnapshotDir())
if err != nil {
return nil, errors.Wrap(err, "create snapshot db")
}
ss, err := snapshots.NewStore(db, cfg.SnapshotDir())
if err != nil {
return nil, errors.Wrap(err, "create snapshot store")
}
return ss, nil
}
func chainIDFromGenesis(cfg Config) (string, error) {
genDoc, err := node.DefaultGenesisDocProviderFunc(&cfg.Comet)()
if err != nil {
return "", errors.Wrap(err, "load genesis doc")
}
return genDoc.ChainID, nil
}
// newEngineClient returns a new engine API client.
func newEngineClient(ctx context.Context, cfg Config, network netconf.ID, pubkey crypto.PubKey) (ethclient.EngineClient, error) {
if network == netconf.Simnet {
return ethclient.NewEngineMock(
ethclient.WithMockSelfDelegation(pubkey, 1),
ethclient.WithPortalRegister(netconf.SimnetNetwork()),
)
}
jwtBytes, err := ethclient.LoadJWTHexFile(cfg.EngineJWTFile)
if err != nil {
return nil, errors.Wrap(err, "load engine JWT file")
}
engineCl, err := ethclient.NewAuthClient(ctx, cfg.EngineEndpoint, jwtBytes)
if err != nil {
return nil, errors.Wrap(err, "create engine client")
}
return engineCl, nil
}
// enableSDKTelemetry enables prometheus based cosmos-sdk telemetry.
func enableSDKTelemetry() error {
const farFuture = time.Hour * 24 * 365 * 10 // 10 years ~= infinity.
_, err := sdktelemetry.New(sdktelemetry.Config{
ServiceName: "cosmos",
Enabled: true,
PrometheusRetentionTime: int64(farFuture.Seconds()), // Prometheus metrics never expire once created in-app.
})
if err != nil {
return errors.Wrap(err, "enable cosmos-sdk telemetry")
}
return nil
}
var (
_ etypes.FeeRecipientProvider = &burnEVMFees{}
// burnAddress is used as the EVM fee recipient resulting in burned execution fees.
burnAddress = common.HexToAddress("0x000000000000000000000000000000000000dEaD")
)
// burnEVMFees is a fee recipient provider that burns all execution fees.
type burnEVMFees struct{}
func (burnEVMFees) LocalFeeRecipient() common.Address {
return burnAddress
}
func (burnEVMFees) VerifyFeeRecipient(address common.Address) error {
if address != burnAddress {
return errors.New("fee recipient not the burn address", "addr", address.Hex())
}
return nil
}