code.vegaprotocol.io/vega@v0.79.0/cmd/data-node/commands/init.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  	"math"
    23  	"time"
    24  
    25  	"code.vegaprotocol.io/vega/datanode/config"
    26  	"code.vegaprotocol.io/vega/datanode/config/encoding"
    27  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    28  	"code.vegaprotocol.io/vega/logging"
    29  	"code.vegaprotocol.io/vega/paths"
    30  
    31  	"github.com/jessevdk/go-flags"
    32  )
    33  
    34  type InitCmd struct {
    35  	config.VegaHomeFlag
    36  
    37  	Force            bool   `description:"Erase exiting vega configuration at the specified path" long:"force"     short:"f"`
    38  	RetentionProfile string `choice:"archive"                                                     choice:"minimal" choice:"conservative" default:"archive" description:"Set which mode to initialise the data node with, will affect retention policies" long:"retention-profile" short:"r"`
    39  }
    40  
    41  var initCmd InitCmd
    42  
    43  func (opts *InitCmd) Usage() string {
    44  	return "<ChainID> [options]"
    45  }
    46  
    47  func (opts *InitCmd) Execute(args []string) error {
    48  	logger := logging.NewLoggerFromConfig(logging.NewDefaultConfig())
    49  	defer logger.AtExit()
    50  
    51  	if len(args) != 1 {
    52  		return errors.New("expected <chain ID>")
    53  	}
    54  
    55  	chainID := args[0]
    56  
    57  	vegaPaths := paths.New(opts.VegaHome)
    58  
    59  	cfgLoader, err := config.InitialiseLoader(vegaPaths)
    60  	if err != nil {
    61  		return fmt.Errorf("couldn't initialise configuration loader: %w", err)
    62  	}
    63  
    64  	configExists, err := cfgLoader.ConfigExists()
    65  	if err != nil {
    66  		return fmt.Errorf("couldn't verify configuration presence: %w", err)
    67  	}
    68  
    69  	if configExists && !opts.Force {
    70  		return fmt.Errorf("configuration already exists at `%s` please remove it first or re-run using -f", cfgLoader.ConfigFilePath())
    71  	}
    72  
    73  	if configExists && opts.Force {
    74  		cfgLoader.Remove()
    75  	}
    76  
    77  	cfg := config.NewDefaultConfig()
    78  
    79  	if opts.RetentionProfile == "archive" {
    80  		cfg.NetworkHistory.Store.HistoryRetentionBlockSpan = math.MaxInt64
    81  		cfg.SQLStore.RetentionPeriod = sqlstore.RetentionPeriodArchive
    82  		cfg.NetworkHistory.Initialise.TimeOut = encoding.Duration{Duration: 96 * time.Hour}
    83  		cfg.NetworkHistory.Initialise.MinimumBlockCount = -1
    84  	}
    85  
    86  	if opts.RetentionProfile == "minimal" {
    87  		cfg.SQLStore.RetentionPeriod = sqlstore.RetentionPeriodLite
    88  		cfg.NetworkHistory.Initialise.TimeOut = encoding.Duration{Duration: 1 * time.Minute}
    89  		cfg.NetworkHistory.Initialise.MinimumBlockCount = 1
    90  	}
    91  	cfg.ChainID = chainID
    92  
    93  	if err := cfgLoader.Save(&cfg); err != nil {
    94  		return fmt.Errorf("couldn't save configuration file: %w", err)
    95  	}
    96  
    97  	logger.Info("configuration generated successfully", logging.String("path", cfgLoader.ConfigFilePath()))
    98  
    99  	return nil
   100  }
   101  
   102  func Init(ctx context.Context, parser *flags.Parser) error {
   103  	initCmd = InitCmd{}
   104  
   105  	short := "init <chain ID>"
   106  	long := "Generate the minimal configuration required for a vega data-node to start. The Chain ID is required."
   107  
   108  	_, err := parser.AddCommand("init", short, long, &initCmd)
   109  	return err
   110  }