code.vegaprotocol.io/vega@v0.79.0/core/tendermint/config.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 tendermint
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"os"
    22  
    23  	"code.vegaprotocol.io/vega/core/genesis"
    24  	vgfs "code.vegaprotocol.io/vega/libs/fs"
    25  
    26  	tmconfig "github.com/cometbft/cometbft/config"
    27  	tmcrypto "github.com/cometbft/cometbft/crypto"
    28  	tmjson "github.com/cometbft/cometbft/libs/json"
    29  	tmos "github.com/cometbft/cometbft/libs/os"
    30  	"github.com/cometbft/cometbft/privval"
    31  	tmtypes "github.com/cometbft/cometbft/types"
    32  )
    33  
    34  type Config struct {
    35  	config *tmconfig.Config
    36  }
    37  
    38  func NewConfig(home string) *Config {
    39  	c := tmconfig.DefaultConfig()
    40  	c.SetRoot(os.ExpandEnv(home))
    41  	return &Config{
    42  		config: c,
    43  	}
    44  }
    45  
    46  func (c *Config) PublicValidatorKey() (tmcrypto.PubKey, error) {
    47  	privValKeyFile := c.config.PrivValidatorKeyFile()
    48  	if !tmos.FileExists(privValKeyFile) {
    49  		return nil, fmt.Errorf("file \"%s\" not found", privValKeyFile)
    50  	}
    51  	// read private validator
    52  	pv := privval.LoadFilePV(
    53  		c.config.PrivValidatorKeyFile(),
    54  		c.config.PrivValidatorStateFile(),
    55  	)
    56  
    57  	pubKey, err := pv.GetPubKey()
    58  	if err != nil {
    59  		return nil, fmt.Errorf("can't get tendermint public key: %w", err)
    60  	}
    61  
    62  	return pubKey, nil
    63  }
    64  
    65  func (c *Config) Genesis() (*tmtypes.GenesisDoc, *genesis.State, error) {
    66  	path := c.config.GenesisFile()
    67  	data, err := vgfs.ReadFile(path)
    68  	if err != nil {
    69  		return nil, nil, fmt.Errorf("couldn't read genesis file: %w", err)
    70  	}
    71  
    72  	doc := &tmtypes.GenesisDoc{}
    73  	err = tmjson.Unmarshal(data, doc)
    74  	if err != nil {
    75  		return nil, nil, fmt.Errorf("couldn't unmarshal the genesis document: %w", err)
    76  	}
    77  
    78  	state := &genesis.State{}
    79  
    80  	if len(doc.AppState) != 0 {
    81  		if err := json.Unmarshal(doc.AppState, state); err != nil {
    82  			return nil, nil, fmt.Errorf("couldn't unmarshal genesis state: %w", err)
    83  		}
    84  	}
    85  
    86  	return doc, state, nil
    87  }
    88  
    89  func (c *Config) SaveGenesis(doc *tmtypes.GenesisDoc) error {
    90  	path := c.config.GenesisFile()
    91  
    92  	if err := doc.SaveAs(path); err != nil {
    93  		return fmt.Errorf("couldn't save the genesis file: %w", err)
    94  	}
    95  
    96  	return nil
    97  }
    98  
    99  func AddAppStateToGenesis(doc *tmtypes.GenesisDoc, state *genesis.State) error {
   100  	rawGenesisState, err := json.Marshal(state)
   101  	if err != nil {
   102  		return fmt.Errorf("couldn't marshal the genesis state as JSON: %w", err)
   103  	}
   104  
   105  	doc.AppState = rawGenesisState
   106  
   107  	return nil
   108  }