-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
start.go
1051 lines (895 loc) · 35.9 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package server
import (
"bufio"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"net"
"os"
"path/filepath"
"runtime/pprof"
"strings"
"time"
"github.com/cometbft/cometbft/abci/server"
cmtstate "github.com/cometbft/cometbft/api/cometbft/state/v1"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
cmtcfg "github.com/cometbft/cometbft/config"
cmtcrypto "github.com/cometbft/cometbft/crypto"
cmted25519 "github.com/cometbft/cometbft/crypto/ed25519"
cmtjson "github.com/cometbft/cometbft/libs/json"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
"github.com/cometbft/cometbft/proxy"
rpchttp "github.com/cometbft/cometbft/rpc/client/http"
"github.com/cometbft/cometbft/rpc/client/local"
sm "github.com/cometbft/cometbft/state"
"github.com/cometbft/cometbft/store"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/hashicorp/go-metrics"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"
pruningtypes "cosmossdk.io/store/pruning/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/api"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servergrpc "github.com/cosmos/cosmos-sdk/server/grpc"
servercmtlog "github.com/cosmos/cosmos-sdk/server/log"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/telemetry"
"github.com/cosmos/cosmos-sdk/types/mempool"
"github.com/cosmos/cosmos-sdk/version"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
)
const (
// CometBFT full-node start flags
flagWithComet = "with-comet"
flagAddress = "address"
flagTransport = "transport"
flagTraceStore = "trace-store"
flagCPUProfile = "cpu-profile"
FlagMinGasPrices = "minimum-gas-prices"
FlagQueryGasLimit = "query-gas-limit"
FlagHaltHeight = "halt-height"
FlagHaltTime = "halt-time"
FlagInterBlockCache = "inter-block-cache"
FlagUnsafeSkipUpgrades = "unsafe-skip-upgrades"
FlagTrace = "trace"
FlagInvCheckPeriod = "inv-check-period"
FlagPruning = "pruning"
FlagPruningKeepRecent = "pruning-keep-recent"
FlagPruningInterval = "pruning-interval"
FlagIndexEvents = "index-events"
FlagMinRetainBlocks = "min-retain-blocks"
FlagIAVLCacheSize = "iavl-cache-size"
FlagDisableIAVLFastNode = "iavl-disable-fastnode"
FlagShutdownGrace = "shutdown-grace"
// state sync-related flags
FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval"
FlagStateSyncSnapshotKeepRecent = "state-sync.snapshot-keep-recent"
// api-related flags
FlagAPIEnable = "api.enable"
FlagAPISwagger = "api.swagger"
FlagAPIAddress = "api.address"
FlagAPIMaxOpenConnections = "api.max-open-connections"
FlagRPCReadTimeout = "api.rpc-read-timeout"
FlagRPCWriteTimeout = "api.rpc-write-timeout"
FlagRPCMaxBodyBytes = "api.rpc-max-body-bytes"
FlagAPIEnableUnsafeCORS = "api.enabled-unsafe-cors"
// gRPC-related flags
flagGRPCOnly = "grpc-only"
flagGRPCEnable = "grpc.enable"
flagGRPCAddress = "grpc.address"
// mempool flags
FlagMempoolMaxTxs = "mempool.max-txs"
// testnet keys
KeyIsTestnet = "is-testnet"
KeyNewChainID = "new-chain-ID"
KeyNewOpAddr = "new-operator-addr"
KeyNewValAddr = "new-validator-addr"
KeyUserPubKey = "user-pub-key"
KeyTriggerTestnetUpgrade = "trigger-testnet-upgrade"
)
// StartCmdOptions defines options that can be customized in `StartCmdWithOptions`,
type StartCmdOptions[T types.Application] struct {
// DBOpener can be used to customize db opening, for example customize db options or support different db backends,
// default to the builtin db opener.
DBOpener func(rootDir string, backendType dbm.BackendType) (corestore.KVStoreWithBatch, error)
// PostSetup can be used to setup extra services under the same cancellable context,
// it's not called in stand-alone mode, only for in-process mode.
PostSetup func(app T, svrCtx *Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error
// PostSetupStandalone can be used to setup extra services under the same cancellable context,
PostSetupStandalone func(app T, svrCtx *Context, clientCtx client.Context, ctx context.Context, g *errgroup.Group) error
// AddFlags add custom flags to start cmd
AddFlags func(cmd *cobra.Command)
// StartCommandHandler can be used to customize the start command handler
StartCommandHandler func(svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator[T], withCMT bool, opts StartCmdOptions[T]) error
}
// StartCmd runs the service passed in, either stand-alone or in-process with
// CometBFT.
func StartCmd[T types.Application](appCreator types.AppCreator[T]) *cobra.Command {
return StartCmdWithOptions(appCreator, StartCmdOptions[T]{})
}
// StartCmdWithOptions runs the service passed in, either stand-alone or in-process with
// CometBFT.
func StartCmdWithOptions[T types.Application](appCreator types.AppCreator[T], opts StartCmdOptions[T]) *cobra.Command {
if opts.DBOpener == nil {
opts.DBOpener = OpenDB
}
if opts.StartCommandHandler == nil {
opts.StartCommandHandler = start
}
cmd := &cobra.Command{
Use: "start",
Short: "Run the full node",
Long: `Run the full node application with CometBFT in or out of process. By
default, the application will run with CometBFT in process.
Pruning options can be provided via the '--pruning' flag or alternatively with '--pruning-keep-recent', and
'pruning-interval' together.
For '--pruning' the options are as follows:
default: the last 362880 states are kept, pruning at 10 block intervals
nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node)
everything: 2 latest states will be kept; pruning at 10 block intervals.
custom: allow pruning options to be manually specified through 'pruning-keep-recent', and 'pruning-interval'
Node halting configurations exist in the form of two flags: '--halt-height' and '--halt-time'. During
the ABCI Commit phase, the node will check if the current block height is greater than or equal to
the halt-height or if the current block time is greater than or equal to the halt-time. If so, the
node will attempt to gracefully shutdown and the block will not be committed. In addition, the node
will not be able to commit subsequent blocks.
For profiling and benchmarking purposes, CPU profiling can be enabled via the '--cpu-profile' flag
which accepts a path for the resulting pprof file.
The node may be started in a 'query only' mode where only the gRPC and JSON HTTP
API services are enabled via the 'grpc-only' flag. In this mode, CometBFT is
bypassed and can be used when legacy queries are needed after an on-chain upgrade
is performed. Note, when enabled, gRPC will also be automatically enabled.
`,
RunE: func(cmd *cobra.Command, _ []string) error {
serverCtx := GetServerContextFromCmd(cmd)
_, err := GetPruningOptionsFromFlags(serverCtx.Viper)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
withCMT, _ := cmd.Flags().GetBool(flagWithComet)
if !withCMT {
serverCtx.Logger.Info("starting ABCI without CometBFT")
}
err = wrapCPUProfile(serverCtx, func() error {
return opts.StartCommandHandler(serverCtx, clientCtx, appCreator, withCMT, opts)
})
serverCtx.Logger.Debug("received quit signal")
graceDuration, _ := cmd.Flags().GetDuration(FlagShutdownGrace)
if graceDuration > 0 {
serverCtx.Logger.Info("graceful shutdown start", FlagShutdownGrace, graceDuration)
<-time.After(graceDuration)
serverCtx.Logger.Info("graceful shutdown complete")
}
return err
},
}
addStartNodeFlags(cmd, opts)
return cmd
}
func start[T types.Application](svrCtx *Context, clientCtx client.Context, appCreator types.AppCreator[T], withCmt bool, opts StartCmdOptions[T]) error {
svrCfg, err := getAndValidateConfig(svrCtx)
if err != nil {
return err
}
app, appCleanupFn, err := startApp[T](svrCtx, appCreator, opts)
if err != nil {
return err
}
defer appCleanupFn()
metrics, err := startTelemetry(svrCfg)
if err != nil {
return err
}
emitServerInfoMetrics()
if !withCmt {
return startStandAlone[T](svrCtx, svrCfg, clientCtx, app, metrics, opts)
}
return startInProcess[T](svrCtx, svrCfg, clientCtx, app, metrics, opts)
}
func startStandAlone[T types.Application](svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app T, metrics *telemetry.Metrics, opts StartCmdOptions[T]) error {
addr := svrCtx.Viper.GetString(flagAddress)
transport := svrCtx.Viper.GetString(flagTransport)
cmtApp := NewCometABCIWrapper(app)
svr, err := server.NewServer(addr, transport, cmtApp)
if err != nil {
return fmt.Errorf("error creating listener: %w", err)
}
svr.SetLogger(servercmtlog.CometLoggerWrapper{Logger: svrCtx.Logger.With("module", "abci-server")})
g, ctx := getCtx(svrCtx, false)
// Add the tx service to the gRPC router. We only need to register this
// service if API or gRPC is enabled, and avoid doing so in the general
// case, because it spawns a new local CometBFT RPC client.
if svrCfg.API.Enable || svrCfg.GRPC.Enable {
// create tendermint client
// assumes the rpc listen address is where tendermint has its rpc server
rpcclient, err := rpchttp.New(svrCtx.Config.RPC.ListenAddress)
if err != nil {
return err
}
// re-assign for making the client available below
// do not use := to avoid shadowing clientCtx
clientCtx = clientCtx.WithClient(rpcclient)
// use the provided clientCtx to register the services
app.RegisterTxService(clientCtx)
app.RegisterTendermintService(clientCtx)
app.RegisterNodeService(clientCtx, svrCfg)
}
grpcSrv, clientCtx, err := startGrpcServer(ctx, g, svrCfg.GRPC, clientCtx, svrCtx, app)
if err != nil {
return err
}
err = startAPIServer(ctx, g, svrCfg, clientCtx, svrCtx, app, svrCtx.Config.RootDir, grpcSrv, metrics)
if err != nil {
return err
}
if opts.PostSetupStandalone != nil {
if err := opts.PostSetupStandalone(app, svrCtx, clientCtx, ctx, g); err != nil {
return err
}
}
g.Go(func() error {
if err := svr.Start(); err != nil {
svrCtx.Logger.Error("failed to start out-of-process ABCI server", "err", err)
return err
}
// Wait for the calling process to be canceled or close the provided context,
// so we can gracefully stop the ABCI server.
<-ctx.Done()
svrCtx.Logger.Info("stopping the ABCI server...")
return svr.Stop()
})
return g.Wait()
}
func startInProcess[T types.Application](svrCtx *Context, svrCfg serverconfig.Config, clientCtx client.Context, app T,
metrics *telemetry.Metrics, opts StartCmdOptions[T],
) error {
cmtCfg := svrCtx.Config
gRPCOnly := svrCtx.Viper.GetBool(flagGRPCOnly)
g, ctx := getCtx(svrCtx, true)
if gRPCOnly {
// TODO: Generalize logic so that gRPC only is really in startStandAlone
svrCtx.Logger.Info("starting node in gRPC only mode; CometBFT is disabled")
svrCfg.GRPC.Enable = true
} else {
svrCtx.Logger.Info("starting node with ABCI CometBFT in-process")
tmNode, cleanupFn, err := startCmtNode(ctx, cmtCfg, app, svrCtx)
if err != nil {
return err
}
defer cleanupFn()
// Add the tx service to the gRPC router. We only need to register this
// service if API or gRPC is enabled, and avoid doing so in the general
// case, because it spawns a new local CometBFT RPC client.
if svrCfg.API.Enable || svrCfg.GRPC.Enable {
// Re-assign for making the client available below do not use := to avoid
// shadowing the clientCtx variable.
clientCtx = clientCtx.WithClient(local.New(tmNode))
app.RegisterTxService(clientCtx)
app.RegisterTendermintService(clientCtx)
app.RegisterNodeService(clientCtx, svrCfg)
}
}
grpcSrv, clientCtx, err := startGrpcServer(ctx, g, svrCfg.GRPC, clientCtx, svrCtx, app)
if err != nil {
return err
}
err = startAPIServer(ctx, g, svrCfg, clientCtx, svrCtx, app, cmtCfg.RootDir, grpcSrv, metrics)
if err != nil {
return err
}
if opts.PostSetup != nil {
if err := opts.PostSetup(app, svrCtx, clientCtx, ctx, g); err != nil {
return err
}
}
// wait for signal capture and gracefully return
// we are guaranteed to be waiting for the "ListenForQuitSignals" goroutine.
return g.Wait()
}
// TODO: Move nodeKey into being created within the function.
func startCmtNode(
ctx context.Context,
cfg *cmtcfg.Config,
app types.Application,
svrCtx *Context,
) (tmNode *node.Node, cleanupFn func(), err error) {
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return nil, cleanupFn, err
}
pv, err := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), app.ValidatorKeyProvider())
if err != nil {
return nil, cleanupFn, err
}
cmtApp := NewCometABCIWrapper(app)
tmNode, err = node.NewNode(
ctx,
cfg,
pv,
nodeKey,
proxy.NewLocalClientCreator(cmtApp),
getGenDocProvider(cfg),
cmtcfg.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
servercmtlog.CometLoggerWrapper{Logger: svrCtx.Logger},
)
if err != nil {
return tmNode, cleanupFn, err
}
if err := tmNode.Start(); err != nil {
return tmNode, cleanupFn, err
}
cleanupFn = func() {
if tmNode != nil && tmNode.IsRunning() {
_ = tmNode.Stop()
}
}
return tmNode, cleanupFn, nil
}
func getAndValidateConfig(svrCtx *Context) (serverconfig.Config, error) {
config, err := serverconfig.GetConfig(svrCtx.Viper)
if err != nil {
return config, err
}
if err := config.ValidateBasic(); err != nil {
return config, err
}
return config, nil
}
// getGenDocProvider returns a function which returns the genesis doc from the genesis file.
func getGenDocProvider(cfg *cmtcfg.Config) func() (node.ChecksummedGenesisDoc, error) {
return func() (node.ChecksummedGenesisDoc, error) {
defaultGenesisDoc := node.ChecksummedGenesisDoc{
Sha256Checksum: []byte{},
}
appGenesis, err := genutiltypes.AppGenesisFromFile(cfg.GenesisFile())
if err != nil {
return defaultGenesisDoc, err
}
gen, err := appGenesis.ToGenesisDoc()
if err != nil {
return defaultGenesisDoc, err
}
genbz, err := gen.AppState.MarshalJSON()
if err != nil {
return defaultGenesisDoc, err
}
bz, err := json.Marshal(genbz)
if err != nil {
return defaultGenesisDoc, err
}
sum := sha256.Sum256(bz)
return node.ChecksummedGenesisDoc{
GenesisDoc: gen,
Sha256Checksum: sum[:],
}, nil
}
}
// SetupTraceWriter sets up the trace writer and returns a cleanup function.
func SetupTraceWriter(logger log.Logger, traceWriterFile string) (traceWriter io.WriteCloser, cleanup func(), err error) {
// clean up the traceWriter when the server is shutting down
cleanup = func() {}
traceWriter, err = openTraceWriter(traceWriterFile)
if err != nil {
return traceWriter, cleanup, err
}
// if flagTraceStore is not used then traceWriter is nil
if traceWriter != nil {
cleanup = func() {
if err = traceWriter.Close(); err != nil {
logger.Error("failed to close trace writer", "err", err)
}
}
}
return traceWriter, cleanup, nil
}
func startGrpcServer(
ctx context.Context,
g *errgroup.Group,
config serverconfig.GRPCConfig,
clientCtx client.Context,
svrCtx *Context,
app types.Application,
) (*grpc.Server, client.Context, error) {
if !config.Enable {
// return grpcServer as nil if gRPC is disabled
return nil, clientCtx, nil
}
_, _, err := net.SplitHostPort(config.Address)
if err != nil {
return nil, clientCtx, err
}
maxSendMsgSize := config.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = serverconfig.DefaultGRPCMaxSendMsgSize
}
maxRecvMsgSize := config.MaxRecvMsgSize
if maxRecvMsgSize == 0 {
maxRecvMsgSize = serverconfig.DefaultGRPCMaxRecvMsgSize
}
// if gRPC is enabled, configure gRPC client for gRPC gateway
grpcClient, err := grpc.NewClient(
config.Address,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithDefaultCallOptions(
grpc.ForceCodec(codec.NewProtoCodec(clientCtx.InterfaceRegistry).GRPCCodec()),
grpc.MaxCallRecvMsgSize(maxRecvMsgSize),
grpc.MaxCallSendMsgSize(maxSendMsgSize),
),
)
if err != nil {
return nil, clientCtx, err
}
clientCtx = clientCtx.WithGRPCClient(grpcClient)
svrCtx.Logger.Debug("gRPC client assigned to client context", "target", config.Address)
grpcSrv, err := servergrpc.NewGRPCServer(clientCtx, app, config)
if err != nil {
return nil, clientCtx, err
}
// Start the gRPC server in a goroutine. Note, the provided ctx will ensure
// that the server is gracefully shut down.
g.Go(func() error {
return servergrpc.StartGRPCServer(ctx, svrCtx.Logger.With("module", "grpc-server"), config, grpcSrv)
})
return grpcSrv, clientCtx, nil
}
func startAPIServer(
ctx context.Context,
g *errgroup.Group,
svrCfg serverconfig.Config,
clientCtx client.Context,
svrCtx *Context,
app types.Application,
home string,
grpcSrv *grpc.Server,
metrics *telemetry.Metrics,
) error {
if !svrCfg.API.Enable {
return nil
}
clientCtx = clientCtx.WithHomeDir(home)
apiSrv := api.New(clientCtx, svrCtx.Logger.With("module", "api-server"), grpcSrv)
app.RegisterAPIRoutes(apiSrv, svrCfg.API)
if svrCfg.Telemetry.Enabled {
apiSrv.SetTelemetry(metrics)
}
g.Go(func() error {
return apiSrv.Start(ctx, svrCfg)
})
return nil
}
func startTelemetry(cfg serverconfig.Config) (*telemetry.Metrics, error) {
return telemetry.New(cfg.Telemetry)
}
// wrapCPUProfile starts CPU profiling, if enabled, and executes the provided
// callbackFn in a separate goroutine, then will wait for that callback to
// return.
//
// NOTE: We expect the caller to handle graceful shutdown and signal handling.
func wrapCPUProfile(svrCtx *Context, callbackFn func() error) error {
if cpuProfile := svrCtx.Viper.GetString(flagCPUProfile); cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return err
}
svrCtx.Logger.Info("starting CPU profiler", "profile", cpuProfile)
if err := pprof.StartCPUProfile(f); err != nil {
return err
}
defer func() {
svrCtx.Logger.Info("stopping CPU profiler", "profile", cpuProfile)
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
svrCtx.Logger.Info("failed to close cpu-profile file", "profile", cpuProfile, "err", err.Error())
}
}()
}
return callbackFn()
}
// emitServerInfoMetrics emits server info related metrics using application telemetry.
func emitServerInfoMetrics() {
var ls []metrics.Label
versionInfo := version.NewInfo()
if len(versionInfo.GoVersion) > 0 {
ls = append(ls, telemetry.NewLabel("go", versionInfo.GoVersion))
}
if len(versionInfo.CosmosSdkVersion) > 0 {
ls = append(ls, telemetry.NewLabel("version", versionInfo.CosmosSdkVersion))
}
if len(ls) == 0 {
return
}
telemetry.SetGaugeWithLabels([]string{"server", "info"}, 1, ls)
}
func getCtx(svrCtx *Context, block bool) (*errgroup.Group, context.Context) {
ctx, cancelFn := context.WithCancel(context.Background())
g, ctx := errgroup.WithContext(ctx)
// listen for quit signals so the calling parent process can gracefully exit
ListenForQuitSignals(g, block, cancelFn, svrCtx.Logger)
return g, ctx
}
func startApp[T types.Application](svrCtx *Context, appCreator types.AppCreator[T], opts StartCmdOptions[T]) (app T, cleanupFn func(), err error) {
traceWriter, traceCleanupFn, err := SetupTraceWriter(svrCtx.Logger, svrCtx.Viper.GetString(flagTraceStore))
if err != nil {
return app, traceCleanupFn, err
}
home := svrCtx.Config.RootDir
db, err := opts.DBOpener(home, GetAppDBBackend(svrCtx.Viper))
if err != nil {
return app, traceCleanupFn, err
}
if isTestnet, ok := svrCtx.Viper.Get(KeyIsTestnet).(bool); ok && isTestnet {
var appPtr *T
appPtr, err = testnetify[T](svrCtx, appCreator, db, traceWriter)
if err != nil {
return app, traceCleanupFn, err
}
app = *appPtr
} else {
app = appCreator(svrCtx.Logger, db, traceWriter, svrCtx.Viper)
}
cleanupFn = func() {
traceCleanupFn()
if localErr := app.Close(); localErr != nil {
svrCtx.Logger.Error(localErr.Error())
}
}
return app, cleanupFn, nil
}
// InPlaceTestnetCreator utilizes the provided chainID and operatorAddress as well as the local private validator key to
// control the network represented in the data folder. This is useful to create testnets nearly identical to your
// mainnet environment.
func InPlaceTestnetCreator[T types.Application](testnetAppCreator types.AppCreator[T]) *cobra.Command {
opts := StartCmdOptions[T]{}
if opts.DBOpener == nil {
opts.DBOpener = OpenDB
}
if opts.StartCommandHandler == nil {
opts.StartCommandHandler = start
}
cmd := &cobra.Command{
Use: "in-place-testnet <newChainID> <newOperatorAddress>",
Short: "Create and start a testnet from current local state",
Long: `Create and start a testnet from current local state.
After utilizing this command the network will start. If the network is stopped,
the normal "start" command should be used. Re-using this command on state that
has already been modified by this command could result in unexpected behavior.
Additionally, the first block may take up to one minute to be committed, depending
on how old the block is. For instance, if a snapshot was taken weeks ago and we want
to turn this into a testnet, it is possible lots of pending state needs to be committed
(expiring locks, etc.). It is recommended that you should wait for this block to be committed
before stopping the daemon.
If the --trigger-testnet-upgrade flag is set, the upgrade handler specified by the flag will be run
on the first block of the testnet.
Regardless of whether the flag is set or not, if any new stores are introduced in the daemon being run,
those stores will be registered in order to prevent panics. Therefore, you only need to set the flag if
you want to test the upgrade handler itself.
`,
Example: "in-place-testnet localosmosis osmo12smx2wdlyttvyzvzg54y2vnqwq2qjateuf7thj",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
_, err := GetPruningOptionsFromFlags(serverCtx.Viper)
if err != nil {
return err
}
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
withCMT, _ := cmd.Flags().GetBool(flagWithComet)
if !withCMT {
serverCtx.Logger.Info("starting ABCI without CometBFT")
}
newChainID := args[0]
newOperatorAddress := args[1]
skipConfirmation, _ := cmd.Flags().GetBool("skip-confirmation")
if !skipConfirmation {
// Confirmation prompt to prevent accidental modification of state.
reader := bufio.NewReader(os.Stdin)
fmt.Println("This operation will modify state in your data folder and cannot be undone. Do you want to continue? (y/n)")
text, _ := reader.ReadString('\n')
response := strings.TrimSpace(strings.ToLower(text))
if response != "y" && response != "yes" {
fmt.Println("Operation canceled.")
return nil
}
}
// Set testnet keys to be used by the application.
// This is done to prevent changes to existing start API.
serverCtx.Viper.Set(KeyIsTestnet, true)
serverCtx.Viper.Set(KeyNewChainID, newChainID)
serverCtx.Viper.Set(KeyNewOpAddr, newOperatorAddress)
err = wrapCPUProfile(serverCtx, func() error {
return opts.StartCommandHandler(serverCtx, clientCtx, testnetAppCreator, withCMT, opts)
})
serverCtx.Logger.Debug("received quit signal")
graceDuration, _ := cmd.Flags().GetDuration(FlagShutdownGrace)
if graceDuration > 0 {
serverCtx.Logger.Info("graceful shutdown start", FlagShutdownGrace, graceDuration)
<-time.After(graceDuration)
serverCtx.Logger.Info("graceful shutdown complete")
}
return err
},
}
addStartNodeFlags(cmd, opts)
cmd.Flags().String(KeyTriggerTestnetUpgrade, "", "If set (example: \"v21\"), triggers the v21 upgrade handler to run on the first block of the testnet")
cmd.Flags().Bool("skip-confirmation", false, "Skip the confirmation prompt")
return cmd
}
// testnetify modifies both state and blockStore, allowing the provided operator address and local validator key to control the network
// that the state in the data folder represents. The chainID of the local genesis file is modified to match the provided chainID.
func testnetify[T types.Application](ctx *Context, testnetAppCreator types.AppCreator[T], db corestore.KVStoreWithBatch, traceWriter io.WriteCloser) (*T, error) {
config := ctx.Config
newChainID, ok := ctx.Viper.Get(KeyNewChainID).(string)
if !ok {
return nil, fmt.Errorf("expected string for key %s", KeyNewChainID)
}
// Modify app genesis chain ID and save to genesis file.
genFilePath := config.GenesisFile()
appGen, err := genutiltypes.AppGenesisFromFile(genFilePath)
if err != nil {
return nil, err
}
appGen.ChainID = newChainID
if err := appGen.ValidateAndComplete(); err != nil {
return nil, err
}
if err := appGen.SaveAs(genFilePath); err != nil {
return nil, err
}
// Regenerate addrbook.json to prevent peers on old network from causing error logs.
addrBookPath := filepath.Join(config.RootDir, "config", "addrbook.json")
if err := os.Remove(addrBookPath); err != nil && !os.IsNotExist(err) {
return nil, fmt.Errorf("failed to remove existing addrbook.json: %w", err)
}
emptyAddrBook := []byte("{}")
if err := os.WriteFile(addrBookPath, emptyAddrBook, 0o600); err != nil {
return nil, fmt.Errorf("failed to create empty addrbook.json: %w", err)
}
// Load the comet genesis doc provider.
genDocProvider := node.DefaultGenesisDocProviderFunc(config)
// Initialize blockStore and stateDB.
blockStoreDB, err := cmtcfg.DefaultDBProvider(&cmtcfg.DBContext{ID: "blockstore", Config: config})
if err != nil {
return nil, err
}
blockStore := store.NewBlockStore(blockStoreDB)
stateDB, err := cmtcfg.DefaultDBProvider(&cmtcfg.DBContext{ID: "state", Config: config})
if err != nil {
return nil, err
}
defer blockStore.Close()
defer stateDB.Close()
privValidator, err := pvm.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(), func() (cmtcrypto.PrivKey, error) {
return cmted25519.GenPrivKey(), nil
}) // TODO: make this modular
if err != nil {
return nil, err
}
userPubKey, err := privValidator.GetPubKey()
if err != nil {
return nil, err
}
validatorAddress := userPubKey.Address()
stateStore := sm.NewStore(stateDB, sm.StoreOptions{
DiscardABCIResponses: config.Storage.DiscardABCIResponses,
})
state, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genDocProvider, "")
if err != nil {
return nil, err
}
ctx.Viper.Set(KeyNewValAddr, validatorAddress)
ctx.Viper.Set(KeyUserPubKey, userPubKey)
testnetApp := testnetAppCreator(ctx.Logger, db, traceWriter, ctx.Viper)
// We need to create a temporary proxyApp to get the initial state of the application.
// Depending on how the node was stopped, the application height can differ from the blockStore height.
// This height difference changes how we go about modifying the state.
cmtApp := NewCometABCIWrapper(testnetApp)
_, context := getCtx(ctx, true)
clientCreator := proxy.NewLocalClientCreator(cmtApp)
metrics := node.DefaultMetricsProvider(cmtcfg.DefaultConfig().Instrumentation)
_, _, _, _, _, proxyMetrics, _, _ := metrics(genDoc.ChainID) //nolint: dogsled // function from comet
proxyApp := proxy.NewAppConns(clientCreator, proxyMetrics)
if err := proxyApp.Start(); err != nil {
return nil, fmt.Errorf("error starting proxy app connections: %w", err)
}
res, err := proxyApp.Query().Info(context, proxy.InfoRequest)
if err != nil {
return nil, fmt.Errorf("error calling Info: %w", err)
}
err = proxyApp.Stop()
if err != nil {
return nil, err
}
appHash := res.LastBlockAppHash
appHeight := res.LastBlockHeight
var block *cmttypes.Block
switch {
case appHeight == blockStore.Height():
block, _ = blockStore.LoadBlock(blockStore.Height())
// If the state's last blockstore height does not match the app and blockstore height, we likely stopped with the halt height flag.
if state.LastBlockHeight != appHeight {
state.LastBlockHeight = appHeight
block.AppHash = appHash
state.AppHash = appHash
} else {
// Node was likely stopped via SIGTERM, delete the next block's seen commit
err := blockStoreDB.Delete([]byte(fmt.Sprintf("SC:%v", blockStore.Height()+1)))
if err != nil {
return nil, err
}
}
case blockStore.Height() > state.LastBlockHeight:
// This state usually occurs when we gracefully stop the node.
err = blockStore.DeleteLatestBlock()
if err != nil {
return nil, err
}
block, _ = blockStore.LoadBlock(blockStore.Height())
default:
// If there is any other state, we just load the block
block, _ = blockStore.LoadBlock(blockStore.Height())
}
block.ChainID = newChainID
state.ChainID = newChainID
block.LastBlockID = state.LastBlockID
block.LastCommit.BlockID = state.LastBlockID
// Create a vote from our validator
vote := cmttypes.Vote{
Type: cmtproto.PrecommitType,
Height: state.LastBlockHeight,
Round: 0,
BlockID: state.LastBlockID,
Timestamp: time.Now(),
ValidatorAddress: validatorAddress,
ValidatorIndex: 0,
Signature: []byte{},
}
// Sign the vote, and copy the proto changes from the act of signing to the vote itself
voteProto := vote.ToProto()
err = privValidator.SignVote(newChainID, voteProto, false)
if err != nil {
return nil, err
}
vote.Signature = voteProto.Signature
vote.Timestamp = voteProto.Timestamp
// Modify the block's lastCommit to be signed only by our validator
block.LastCommit.Signatures[0].ValidatorAddress = validatorAddress
block.LastCommit.Signatures[0].Signature = vote.Signature
block.LastCommit.Signatures = []cmttypes.CommitSig{block.LastCommit.Signatures[0]}
// Load the seenCommit of the lastBlockHeight and modify it to be signed from our validator
seenCommit := blockStore.LoadSeenCommit(state.LastBlockHeight)
seenCommit.BlockID = state.LastBlockID
seenCommit.Round = vote.Round
seenCommit.Signatures[0].Signature = vote.Signature
seenCommit.Signatures[0].ValidatorAddress = validatorAddress
seenCommit.Signatures[0].Timestamp = vote.Timestamp
seenCommit.Signatures = []cmttypes.CommitSig{seenCommit.Signatures[0]}
err = blockStore.SaveSeenCommit(state.LastBlockHeight, seenCommit)
if err != nil {
return nil, err
}
// Create ValidatorSet struct containing just our validator.
newVal := &cmttypes.Validator{
Address: validatorAddress,
PubKey: userPubKey,
VotingPower: 900000000000000,
}
newValSet := &cmttypes.ValidatorSet{
Validators: []*cmttypes.Validator{newVal},
Proposer: newVal,
}
// Replace all valSets in state to be the valSet with just our validator.
state.Validators = newValSet
state.LastValidators = newValSet
state.NextValidators = newValSet
state.LastHeightValidatorsChanged = blockStore.Height()
err = stateStore.Save(state)
if err != nil {
return nil, err
}
// Create a ValidatorsInfo struct to store in stateDB.
valSet, err := state.Validators.ToProto()
if err != nil {
return nil, err
}
valInfo := &cmtstate.ValidatorsInfo{
ValidatorSet: valSet,
LastHeightChanged: state.LastBlockHeight,
}
buf, err := valInfo.Marshal()
if err != nil {
return nil, err
}
// Modify Validators stateDB entry.
err = stateDB.Set([]byte(fmt.Sprintf("validatorsKey:%v", blockStore.Height())), buf)
if err != nil {
return nil, err
}
// Modify LastValidators stateDB entry.
err = stateDB.Set([]byte(fmt.Sprintf("validatorsKey:%v", blockStore.Height()-1)), buf)
if err != nil {
return nil, err
}
// Modify NextValidators stateDB entry.
err = stateDB.Set([]byte(fmt.Sprintf("validatorsKey:%v", blockStore.Height()+1)), buf)
if err != nil {
return nil, err
}
// Since we modified the chainID, we set the new genesisDoc in the stateDB.
b, err := cmtjson.Marshal(genDoc)
if err != nil {
return nil, err
}
if err := stateDB.SetSync([]byte("genesisDoc"), b); err != nil {
return nil, err
}
return &testnetApp, err
}
// addStartNodeFlags should be added to any CLI commands that start the network.