github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/genutil/utils.go (about)

     1  package genutil
     2  
     3  import (
     4  	"encoding/json"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	cfg "github.com/fibonacci-chain/fbc/libs/tendermint/config"
     9  	"github.com/fibonacci-chain/fbc/libs/tendermint/crypto"
    10  	tmos "github.com/fibonacci-chain/fbc/libs/tendermint/libs/os"
    11  	"github.com/fibonacci-chain/fbc/libs/tendermint/p2p"
    12  	"github.com/fibonacci-chain/fbc/libs/tendermint/privval"
    13  	tmtypes "github.com/fibonacci-chain/fbc/libs/tendermint/types"
    14  )
    15  
    16  // ExportGenesisFile creates and writes the genesis configuration to disk. An
    17  // error is returned if building or writing the configuration to file fails.
    18  func ExportGenesisFile(genDoc *tmtypes.GenesisDoc, genFile string) error {
    19  	if err := genDoc.ValidateAndComplete(); err != nil {
    20  		return err
    21  	}
    22  
    23  	return genDoc.SaveAs(genFile)
    24  }
    25  
    26  // ExportGenesisFileWithTime creates and writes the genesis configuration to disk.
    27  // An error is returned if building or writing the configuration to file fails.
    28  func ExportGenesisFileWithTime(
    29  	genFile, chainID string, validators []tmtypes.GenesisValidator,
    30  	appState json.RawMessage, genTime time.Time,
    31  ) error {
    32  
    33  	genDoc := tmtypes.GenesisDoc{
    34  		GenesisTime: genTime,
    35  		ChainID:     chainID,
    36  		Validators:  validators,
    37  		AppState:    appState,
    38  	}
    39  
    40  	if err := genDoc.ValidateAndComplete(); err != nil {
    41  		return err
    42  	}
    43  
    44  	return genDoc.SaveAs(genFile)
    45  }
    46  
    47  func InitializeNodeValidatorFilesByIndex(config *cfg.Config, index int) (nodeID string, valPubKey crypto.PubKey, err error) {
    48  	nodeKey, err := p2p.LoadOrGenNodeKeyByIndex(config.NodeKeyFile(), index)
    49  	if err != nil {
    50  		return "", nil, err
    51  	}
    52  
    53  	return initValidatorFiles(config, nodeKey)
    54  }
    55  
    56  // InitializeNodeValidatorFiles creates private validator and p2p configuration files.
    57  func InitializeNodeValidatorFiles(config *cfg.Config) (nodeID string, valPubKey crypto.PubKey, err error) {
    58  	nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
    59  	if err != nil {
    60  		return "", nil, err
    61  	}
    62  
    63  	return initValidatorFiles(config, nodeKey)
    64  }
    65  
    66  func initValidatorFiles(config *cfg.Config, nodeKey *p2p.NodeKey) (nodeID string, valPubKey crypto.PubKey, err error) {
    67  
    68  	nodeID = string(nodeKey.ID())
    69  
    70  	pvKeyFile := config.PrivValidatorKeyFile()
    71  	if err := tmos.EnsureDir(filepath.Dir(pvKeyFile), 0777); err != nil {
    72  		return "", nil, err
    73  	}
    74  
    75  	pvStateFile := config.PrivValidatorStateFile()
    76  	if err := tmos.EnsureDir(filepath.Dir(pvStateFile), 0777); err != nil {
    77  		return "", nil, err
    78  	}
    79  
    80  	valPubKey, err = privval.LoadOrGenFilePV(pvKeyFile, pvStateFile).GetPubKey()
    81  	if err != nil {
    82  		return "", nil, err
    83  	}
    84  
    85  	return nodeID, valPubKey, nil
    86  }