Skip to content

Commit e2693f2

Browse files
authored
node: Use the env variable for run mode checks (wormhole-foundation#3923)
1 parent f86ebc6 commit e2693f2

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

node/cmd/guardiand/node.go

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ var (
244244

245245
// This is the externally reachable address advertised over gossip for guardian p2p and ccq p2p.
246246
gossipAdvertiseAddress *string
247+
248+
// env is the mode we are running in, Mainnet, Testnet or UnsafeDevnet.
249+
env common.Environment
247250
)
248251

249252
func init() {
@@ -490,13 +493,11 @@ func initConfig(cmd *cobra.Command, args []string) error {
490493
}
491494

492495
func runNode(cmd *cobra.Command, args []string) {
493-
if Build == "dev" && !*unsafeDevMode {
494-
fmt.Println("This is a development build. --unsafeDevMode must be enabled.")
495-
os.Exit(1)
496+
if *unsafeDevMode && *testnetMode {
497+
fmt.Println("Cannot be in unsafeDevMode and testnetMode at the same time.")
496498
}
497499

498500
// Determine execution mode
499-
var env common.Environment
500501
if *unsafeDevMode {
501502
env = common.UnsafeDevNet
502503
} else if *testnetMode {
@@ -505,11 +506,16 @@ func runNode(cmd *cobra.Command, args []string) {
505506
env = common.MainNet
506507
}
507508

508-
if *unsafeDevMode {
509+
if Build == "dev" && env != common.UnsafeDevNet {
510+
fmt.Println("This is a development build. --unsafeDevMode must be enabled.")
511+
os.Exit(1)
512+
}
513+
514+
if env == common.UnsafeDevNet {
509515
fmt.Print(devwarning)
510516
}
511517

512-
if *testnetMode || *unsafeDevMode {
518+
if env != common.MainNet {
513519
fmt.Println("Not locking in memory.")
514520
} else {
515521
common.LockMemory()
@@ -518,7 +524,7 @@ func runNode(cmd *cobra.Command, args []string) {
518524
common.SetRestrictiveUmask()
519525

520526
// Refuse to run as root in production mode.
521-
if !*unsafeDevMode && os.Geteuid() == 0 {
527+
if env != common.UnsafeDevNet && os.Geteuid() == 0 {
522528
fmt.Println("can't run as uid 0")
523529
os.Exit(1)
524530
}
@@ -537,7 +543,7 @@ func runNode(cmd *cobra.Command, args []string) {
537543
zapcore.AddSync(zapcore.Lock(os.Stderr)),
538544
zap.NewAtomicLevelAt(zapcore.Level(lvl))))
539545

540-
if *unsafeDevMode {
546+
if env == common.UnsafeDevNet {
541547
// Use the hostname as nodeName. For production, we don't want to do this to
542548
// prevent accidentally leaking sensitive hostnames.
543549
hostname, err := os.Hostname()
@@ -550,15 +556,11 @@ func runNode(cmd *cobra.Command, args []string) {
550556
logger = logger.Named(*nodeName)
551557
}
552558

553-
if *unsafeDevMode && *testnetMode {
554-
logger.Fatal("Cannot be in unsafeDevMode and testnetMode at the same time.")
555-
}
556-
557559
// Override the default go-log config, which uses a magic environment variable.
558560
ipfslog.SetAllLoggers(lvl)
559561

560562
// In devnet mode, we automatically set a number of flags that rely on deterministic keys.
561-
if *unsafeDevMode {
563+
if env == common.UnsafeDevNet {
562564
g0key, err := peer.IDFromPrivateKey(devnet.DeterministicP2PPrivKeyByIndex(0))
563565
if err != nil {
564566
panic(err)
@@ -602,7 +604,7 @@ func runNode(cmd *cobra.Command, args []string) {
602604

603605
// Verify flags
604606

605-
if *nodeKeyPath == "" && !*unsafeDevMode { // In devnet mode, keys are deterministically generated.
607+
if *nodeKeyPath == "" && env != common.UnsafeDevNet { // In devnet mode, keys are deterministically generated.
606608
logger.Fatal("Please specify --nodeKey")
607609
}
608610
if *guardianKeyPath == "" {
@@ -657,7 +659,7 @@ func runNode(cmd *cobra.Command, args []string) {
657659

658660
// Linea requires a couple of additional parameters.
659661
*lineaContract = checkEvmArgs(logger, *lineaRPC, *lineaContract, "linea", false)
660-
if (*lineaRPC != "") && (*lineaRollUpUrl == "" || *lineaRollUpContract == "") && !*unsafeDevMode {
662+
if (*lineaRPC != "") && (*lineaRollUpUrl == "" || *lineaRollUpContract == "") && env != common.UnsafeDevNet {
661663
logger.Fatal("If --lineaRPC is specified, --lineaRollUpUrl and --lineaRollUpContract must also be specified")
662664
}
663665

@@ -753,7 +755,7 @@ func runNode(cmd *cobra.Command, args []string) {
753755
}
754756

755757
// In devnet mode, we generate a deterministic guardian key and write it to disk.
756-
if *unsafeDevMode {
758+
if env == common.UnsafeDevNet {
757759
err := devnet.GenerateAndStoreDevnetGuardianKey(*guardianKeyPath)
758760
if err != nil {
759761
logger.Fatal("failed to generate devnet guardian key", zap.Error(err))
@@ -765,7 +767,7 @@ func runNode(cmd *cobra.Command, args []string) {
765767
defer db.Close()
766768

767769
// Guardian key
768-
gk, err := common.LoadGuardianKey(*guardianKeyPath, *unsafeDevMode)
770+
gk, err := common.LoadGuardianKey(*guardianKeyPath, env == common.UnsafeDevNet)
769771
if err != nil {
770772
logger.Fatal("failed to load guardian key", zap.Error(err))
771773
}
@@ -775,7 +777,7 @@ func runNode(cmd *cobra.Command, args []string) {
775777

776778
// Load p2p private key
777779
var p2pKey libp2p_crypto.PrivKey
778-
if *unsafeDevMode {
780+
if env == common.UnsafeDevNet {
779781
idx, err := devnet.GetDevnetIndex()
780782
if err != nil {
781783
logger.Fatal("Failed to parse hostname - are we running in devnet?")
@@ -887,7 +889,7 @@ func runNode(cmd *cobra.Command, args []string) {
887889
var hasTelemetryCredential bool = usingLoki
888890

889891
// Telemetry is enabled by default in mainnet/testnet. In devnet it is disabled by default
890-
if !*disableTelemetry && (!*unsafeDevMode || *unsafeDevMode && hasTelemetryCredential) {
892+
if !*disableTelemetry && (env != common.UnsafeDevNet || (env == common.UnsafeDevNet && hasTelemetryCredential)) {
891893
if !hasTelemetryCredential {
892894
logger.Fatal("Please specify --telemetryLokiURL or set --disableTelemetry=false")
893895
}
@@ -932,7 +934,7 @@ func runNode(cmd *cobra.Command, args []string) {
932934
ipfslog.SetPrimaryCore(logger.Core())
933935

934936
wormchainId := "wormchain"
935-
if *testnetMode {
937+
if env == common.TestNet {
936938
wormchainId = "wormchain-testnet-0"
937939
}
938940

@@ -951,7 +953,7 @@ func runNode(cmd *cobra.Command, args []string) {
951953
}
952954

953955
keyPathName := *accountantKeyPath
954-
if *unsafeDevMode {
956+
if env == common.UnsafeDevNet {
955957
idx, err := devnet.GetDevnetIndex()
956958
if err != nil {
957959
logger.Fatal("failed to get devnet index", zap.Error(err), zap.String("component", "gacct"))
@@ -987,7 +989,7 @@ func runNode(cmd *cobra.Command, args []string) {
987989
}
988990

989991
keyPathName := *accountantNttKeyPath
990-
if *unsafeDevMode {
992+
if env == common.UnsafeDevNet {
991993
idx, err := devnet.GetDevnetIndex()
992994
if err != nil {
993995
logger.Fatal("failed to get devnet index", zap.Error(err), zap.String("component", "gacct"))
@@ -1022,7 +1024,7 @@ func runNode(cmd *cobra.Command, args []string) {
10221024
}
10231025

10241026
wormchainKeyPathName := *gatewayRelayerKeyPath
1025-
if *unsafeDevMode {
1027+
if env == common.UnsafeDevNet {
10261028
idx, err := devnet.GetDevnetIndex()
10271029
if err != nil {
10281030
logger.Fatal("failed to get devnet index", zap.Error(err), zap.String("component", "gwrelayer"))
@@ -1466,7 +1468,7 @@ func runNode(cmd *cobra.Command, args []string) {
14661468
watcherConfigs = append(watcherConfigs, wc)
14671469
}
14681470

1469-
if *testnetMode || *unsafeDevMode {
1471+
if env == common.TestNet || env == common.UnsafeDevNet {
14701472
if shouldStart(sepoliaRPC) {
14711473
wc := &evm.WatcherConfig{
14721474
NetworkID: "sepolia",
@@ -1599,7 +1601,7 @@ func shouldStart(rpc *string) bool {
15991601
// checkEvmArgs verifies that the RPC and contract address parameters for an EVM chain make sense, given the environment.
16001602
// If we are in devnet mode and the contract address is not specified, it returns the deterministic one for tilt.
16011603
func checkEvmArgs(logger *zap.Logger, rpc string, contractAddr, chainLabel string, mainnetSupported bool) string {
1602-
if !*unsafeDevMode {
1604+
if env != common.UnsafeDevNet {
16031605
// In mainnet / testnet, if either parameter is specified, they must both be specified.
16041606
if (rpc == "") != (contractAddr == "") {
16051607
logger.Fatal(fmt.Sprintf("Both --%sContract and --%sRPC must be set or both unset", chainLabel, chainLabel))
@@ -1616,16 +1618,12 @@ func checkEvmArgs(logger *zap.Logger, rpc string, contractAddr, chainLabel strin
16161618
}
16171619
}
16181620
}
1619-
if contractAddr != "" && !mainnetSupported && mainnetMode() {
1621+
if contractAddr != "" && !mainnetSupported && env == common.MainNet {
16201622
logger.Fatal(fmt.Sprintf("Chain %s not supported in mainnet", chainLabel))
16211623
}
16221624
return contractAddr
16231625
}
16241626

1625-
func mainnetMode() bool {
1626-
return (!*unsafeDevMode && !*testnetMode)
1627-
}
1628-
16291627
// argsConsistent verifies that the arguments in the array are all set or all unset.
16301628
// Note that it doesn't validate the values, just whether they are blank or not.
16311629
func argsConsistent(args []string) bool {

0 commit comments

Comments
 (0)