code.vegaprotocol.io/vega@v0.79.0/cmd/vega/commands/genesis/generate.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  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/genesis"
    24  	"code.vegaprotocol.io/vega/core/nodewallets"
    25  	vgtm "code.vegaprotocol.io/vega/core/tendermint"
    26  	"code.vegaprotocol.io/vega/core/validators"
    27  	vgrand "code.vegaprotocol.io/vega/libs/rand"
    28  	"code.vegaprotocol.io/vega/logging"
    29  	"code.vegaprotocol.io/vega/paths"
    30  
    31  	tmtypes "github.com/cometbft/cometbft/types"
    32  	"github.com/jessevdk/go-flags"
    33  )
    34  
    35  type generateCmd struct {
    36  	Config nodewallets.Config
    37  
    38  	DryRun bool   `description:"Display the genesis file without writing it" long:"dry-run"`
    39  	TmHome string `description:"The home path of tendermint"                 long:"tm-home" short:"t"`
    40  }
    41  
    42  func (opts *generateCmd) Execute(_ []string) error {
    43  	log := logging.NewLoggerFromConfig(
    44  		logging.NewDefaultConfig(),
    45  	)
    46  	defer log.AtExit()
    47  
    48  	pass, err := genesisCmd.PassphraseFile.Get("node wallet", false)
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	vegaPaths := paths.New(genesisCmd.VegaHome)
    54  
    55  	if _, err := flags.NewParser(opts, flags.Default|flags.IgnoreUnknown).Parse(); err != nil {
    56  		return err
    57  	}
    58  
    59  	vegaKey, ethAddress, walletID, err := loadNodeWalletPubKey(opts.Config, vegaPaths, pass)
    60  	if err != nil {
    61  		return err
    62  	}
    63  
    64  	tmConfig := vgtm.NewConfig(opts.TmHome)
    65  
    66  	pubKey, err := tmConfig.PublicValidatorKey()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	b64TmPubKey := base64.StdEncoding.EncodeToString(pubKey.Bytes())
    72  	genesisState := genesis.DefaultState()
    73  	genesisState.Validators[base64.StdEncoding.EncodeToString(pubKey.Bytes())] = validators.ValidatorData{
    74  		ID:              walletID,
    75  		VegaPubKey:      vegaKey.value,
    76  		VegaPubKeyIndex: vegaKey.index,
    77  		EthereumAddress: ethAddress,
    78  		TmPubKey:        b64TmPubKey,
    79  	}
    80  
    81  	genesisDoc := &tmtypes.GenesisDoc{
    82  		ChainID:         fmt.Sprintf("test-chain-%v", vgrand.RandomStr(6)),
    83  		GenesisTime:     time.Now().Round(0).UTC(),
    84  		ConsensusParams: tmtypes.DefaultConsensusParams(),
    85  		Validators: []tmtypes.GenesisValidator{
    86  			{
    87  				Address: pubKey.Address(),
    88  				PubKey:  pubKey,
    89  				Power:   10,
    90  			},
    91  		},
    92  	}
    93  
    94  	if err = vgtm.AddAppStateToGenesis(genesisDoc, &genesisState); err != nil {
    95  		return fmt.Errorf("couldn't add app_state to genesis: %w", err)
    96  	}
    97  
    98  	if !opts.DryRun {
    99  		if err := tmConfig.SaveGenesis(genesisDoc); err != nil {
   100  			return fmt.Errorf("couldn't save genesis: %w", err)
   101  		}
   102  	}
   103  
   104  	prettifiedDoc, err := vgtm.Prettify(genesisDoc)
   105  	if err != nil {
   106  		return err
   107  	}
   108  	fmt.Println(prettifiedDoc)
   109  	return nil
   110  }
   111  
   112  type vegaPubKey struct {
   113  	index uint32
   114  	value string
   115  }
   116  
   117  func loadNodeWalletPubKey(config nodewallets.Config, vegaPaths paths.Paths, registryPass string) (vegaKey *vegaPubKey, ethAddr, walletID string, err error) {
   118  	nw, err := nodewallets.GetNodeWallets(config, vegaPaths, registryPass)
   119  	if err != nil {
   120  		return nil, "", "", fmt.Errorf("couldn't get node wallets: %w", err)
   121  	}
   122  
   123  	if err := nw.Verify(); err != nil {
   124  		return nil, "", "", err
   125  	}
   126  
   127  	vegaPubKey := &vegaPubKey{
   128  		index: nw.Vega.Index(),
   129  		value: nw.Vega.PubKey().Hex(),
   130  	}
   131  
   132  	return vegaPubKey, nw.Ethereum.PubKey().Hex(), nw.Vega.ID().Hex(), nil
   133  }