code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/genesis/update.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 genesis
    17  
    18  import (
    19  	"encoding/base64"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/core/genesis"
    23  	"code.vegaprotocol.io/vega/core/nodewallets"
    24  	vgtm "code.vegaprotocol.io/vega/core/tendermint"
    25  	"code.vegaprotocol.io/vega/core/validators"
    26  	"code.vegaprotocol.io/vega/logging"
    27  	"code.vegaprotocol.io/vega/paths"
    28  
    29  	"github.com/jessevdk/go-flags"
    30  )
    31  
    32  type updateCmd struct {
    33  	Config nodewallets.Config
    34  
    35  	DryRun bool   `description:"Display the genesis file without writing it" long:"dry-run"`
    36  	TmHome string `description:"The home path of tendermint"                 long:"tm-home" short:"t"`
    37  }
    38  
    39  func (opts *updateCmd) Execute(_ []string) error {
    40  	log := logging.NewLoggerFromConfig(
    41  		logging.NewDefaultConfig(),
    42  	)
    43  	defer log.AtExit()
    44  
    45  	pass, err := genesisCmd.PassphraseFile.Get("node wallet", false)
    46  	if err != nil {
    47  		return err
    48  	}
    49  
    50  	vegaPaths := paths.New(genesisCmd.VegaHome)
    51  
    52  	if _, err := flags.NewParser(opts, flags.Default|flags.IgnoreUnknown).Parse(); err != nil {
    53  		return err
    54  	}
    55  
    56  	vegaKey, ethAddress, walletID, err := loadNodeWalletPubKey(opts.Config, vegaPaths, pass)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	tmConfig := vgtm.NewConfig(opts.TmHome)
    62  
    63  	pubKey, err := tmConfig.PublicValidatorKey()
    64  	if err != nil {
    65  		return err
    66  	}
    67  
    68  	b64TmPubKey := base64.StdEncoding.EncodeToString(pubKey.Bytes())
    69  	genesisState := genesis.DefaultState()
    70  	genesisState.Validators[b64TmPubKey] = validators.ValidatorData{
    71  		ID:              walletID,
    72  		VegaPubKey:      vegaKey.value,
    73  		VegaPubKeyIndex: vegaKey.index,
    74  		EthereumAddress: ethAddress,
    75  		TmPubKey:        b64TmPubKey,
    76  	}
    77  
    78  	genesisDoc, _, err := tmConfig.Genesis()
    79  	if err != nil {
    80  		return fmt.Errorf("couldn't get genesis file: %w", err)
    81  	}
    82  
    83  	if err := vgtm.AddAppStateToGenesis(genesisDoc, &genesisState); err != nil {
    84  		return fmt.Errorf("couldn't add app_state to genesis: %w", err)
    85  	}
    86  
    87  	if !opts.DryRun {
    88  		if err := tmConfig.SaveGenesis(genesisDoc); err != nil {
    89  			return fmt.Errorf("couldn't save genesis: %w", err)
    90  		}
    91  	}
    92  
    93  	prettifiedDoc, err := vgtm.Prettify(genesisDoc)
    94  	if err != nil {
    95  		return err
    96  	}
    97  	fmt.Println(prettifiedDoc)
    98  	return nil
    99  }