code.vegaprotocol.io/vega@v0.79.0/visor/commands.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 visor
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"strings"
    22  
    23  	"code.vegaprotocol.io/vega/visor/utils"
    24  )
    25  
    26  const (
    27  	dataNodeArg              = "datanode"
    28  	homeFlagName             = "--home"
    29  	noHistorySegmentFoundMsg = "no history segments found"
    30  )
    31  
    32  var jsonOutputFlag = []string{"--output", "json"}
    33  
    34  type latestSegmentCommanndOutput struct {
    35  	LatestSegment struct {
    36  		Height int64 `json:"to_height"`
    37  	}
    38  }
    39  
    40  var ErrNoHistorySegmentFound = errors.New(noHistorySegmentFoundMsg)
    41  
    42  func latestDataNodeHistorySegment(binary string, binaryArgs Args) (*latestSegmentCommanndOutput, error) {
    43  	args := []string{}
    44  
    45  	if binaryArgs.Exists(dataNodeArg) {
    46  		args = append(args, dataNodeArg)
    47  	}
    48  
    49  	args = append(args, []string{"network-history", "latest-history-segment", "--output=json"}...)
    50  	args = append(args, binaryArgs.GetFlagWithArg(homeFlagName)...)
    51  
    52  	var output latestSegmentCommanndOutput
    53  	_, err := utils.ExecuteBinary(binary, append(args, jsonOutputFlag...), &output)
    54  	if err != nil {
    55  		if strings.Contains(err.Error(), noHistorySegmentFoundMsg) {
    56  			return nil, ErrNoHistorySegmentFound
    57  		}
    58  
    59  		return nil, err
    60  	}
    61  
    62  	return &output, nil
    63  }
    64  
    65  type versionCommandOutput struct {
    66  	Version string `json:"version"`
    67  	Hash    string `json:"hash"`
    68  }
    69  
    70  func ensureBinaryVersion(binary, version string) error {
    71  	var output versionCommandOutput
    72  
    73  	if _, err := utils.ExecuteBinary(binary, append([]string{"version"}, jsonOutputFlag...), &output); err != nil {
    74  		return err
    75  	}
    76  
    77  	if output.Version != version {
    78  		return fmt.Errorf("wrong binary version provided - provided: %s, want: %s", output.Version, version)
    79  	}
    80  
    81  	return nil
    82  }