code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/node.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  	"runtime/debug"
    23  	"time"
    24  
    25  	"code.vegaprotocol.io/vega/cmd/vega/commands/node"
    26  	"code.vegaprotocol.io/vega/core/config"
    27  	"code.vegaprotocol.io/vega/core/evtforward"
    28  	"code.vegaprotocol.io/vega/libs/memory"
    29  	"code.vegaprotocol.io/vega/logging"
    30  	"code.vegaprotocol.io/vega/paths"
    31  
    32  	"github.com/jessevdk/go-flags"
    33  )
    34  
    35  type StartCmd struct {
    36  	config.Passphrase `description:"A file contain the passphrase to decrypt the node wallet" long:"nodewallet-passphrase-file"`
    37  	config.VegaHomeFlag
    38  	config.Config
    39  
    40  	TendermintHome string `description:"Directory for tendermint config and data (default: $HOME/.cometbft)" long:"tendermint-home"`
    41  
    42  	Network    string `description:"The network to start this node with"               long:"network"`
    43  	NetworkURL string `description:"The URL to a genesis file to start this node with" long:"network-url"`
    44  }
    45  
    46  var startCmd StartCmd
    47  
    48  const namedLogger = "core"
    49  
    50  func (cmd *StartCmd) Execute([]string) error {
    51  	log := logging.NewLoggerFromConfig(
    52  		logging.NewDefaultConfig())
    53  	logCore := log.Named(namedLogger)
    54  
    55  	defer func() {
    56  		log.AtExit()
    57  		logCore.AtExit()
    58  	}()
    59  
    60  	// we define this option to parse the cli args each time the config is
    61  	// loaded. So that we can respect the cli flag precedence.
    62  	parseFlagOpt := func(cfg *config.Config) error {
    63  		_, err := flags.NewParser(cfg, flags.Default|flags.IgnoreUnknown).Parse()
    64  		return err
    65  	}
    66  
    67  	vegaPaths := paths.New(cmd.VegaHome)
    68  
    69  	if len(cmd.Network) > 0 && len(cmd.NetworkURL) > 0 {
    70  		return errors.New("--network-url and --network cannot be set together")
    71  	}
    72  
    73  	// this is to migrate all validators configuration at once
    74  	// and set the event forwarder to the appropriate value
    75  	migrateConfig := func(cnf *config.Config) {
    76  		sevenDays := 24 * 7 * time.Hour
    77  		if cnf.EvtForward.KeepHashesDurationForTestOnlyDoNotChange.Duration == sevenDays {
    78  			cnf.EvtForward.KeepHashesDurationForTestOnlyDoNotChange.Duration = evtforward.DefaultKeepHashesDuration
    79  		}
    80  	}
    81  
    82  	confWatcher, err := config.NewWatcher(context.Background(), logCore, vegaPaths, migrateConfig, config.Use(parseFlagOpt))
    83  	if err != nil {
    84  		return err
    85  	}
    86  
    87  	// only try to get the passphrase if the node is started
    88  	// as a validator
    89  	var pass string
    90  	if confWatcher.Get().IsValidator() {
    91  		pass, err = cmd.Get("node wallet", false)
    92  		if err != nil {
    93  			return err
    94  		}
    95  	}
    96  
    97  	// setup max memory usage
    98  	memFactor, err := confWatcher.Get().GetMaxMemoryFactor()
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	// only set max memory if user didn't require 100%
   104  	if memFactor != 1 {
   105  		totalMem, err := memory.TotalMemory()
   106  		if err != nil {
   107  			return fmt.Errorf("failed to get total memory: %w", err)
   108  		}
   109  		debug.SetMemoryLimit(int64(float64(totalMem) * memFactor))
   110  	}
   111  
   112  	if len(startCmd.TendermintHome) <= 0 {
   113  		startCmd.TendermintHome = "$HOME/.cometbft"
   114  	}
   115  
   116  	return (&node.Command{
   117  		Log: logCore,
   118  	}).Run(
   119  		confWatcher,
   120  		vegaPaths,
   121  		pass,
   122  		cmd.TendermintHome,
   123  		cmd.NetworkURL,
   124  		cmd.Network,
   125  		log,
   126  	)
   127  }
   128  
   129  func Start(ctx context.Context, parser *flags.Parser) error {
   130  	startCmd = StartCmd{
   131  		Config: config.NewDefaultConfig(),
   132  	}
   133  	cmd, err := parser.AddCommand("start", "Start a vega instance", "Runs a vega node", &startCmd)
   134  	if err != nil {
   135  		return err
   136  	}
   137  
   138  	// Print nested groups under parent's name using `::` as the separator.
   139  	for _, parent := range cmd.Groups() {
   140  		for _, grp := range parent.Groups() {
   141  			grp.ShortDescription = parent.ShortDescription + "::" + grp.ShortDescription
   142  		}
   143  	}
   144  	return nil
   145  }
   146  
   147  func Node(ctx context.Context, parser *flags.Parser) error {
   148  	startCmd = StartCmd{
   149  		Config: config.NewDefaultConfig(),
   150  	}
   151  	cmd, err := parser.AddCommand("node", "deprecated, see vega start instead", "deprecated, use vega start instead", &startCmd)
   152  	if err != nil {
   153  		return err
   154  	}
   155  
   156  	// Print nested groups under parent's name using `::` as the separator.
   157  	for _, parent := range cmd.Groups() {
   158  		for _, grp := range parent.Groups() {
   159  			grp.ShortDescription = parent.ShortDescription + "::" + grp.ShortDescription
   160  		}
   161  	}
   162  	return nil
   163  }