Skip to content

Commit

Permalink
abci,kwild: write in-memory cometbft confs to disk for inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
jchappelow committed Mar 14, 2024
1 parent 9d4dac6 commit ebd1ac9
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cmd/kwild/server/cometbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,19 @@ func newCometConfig(cfg *config.KwildConfig) *cmtCfg.Config {

chainRoot := filepath.Join(cfg.RootDir, abciDirName)
nodeCfg.SetRoot(chainRoot)
nodeCfg.Genesis = filepath.Join(cfg.RootDir, cometbft.GenesisJSONName)
// NOTE: The Genesis field is the one in cometbft's GenesisDoc, which is
// different from kwild's, which contains more fields (and not string
// int64). The documented genesis.json in kwild's root directory is:
// filepath.Join(cfg.RootDir, cometbft.GenesisJSONName)
// This file is only used to reflect the in-memory genesis config provided
// to cometbft via a GenesisDocProvider. It it is not used by cometbft.
nodeCfg.Genesis = filepath.Join(chainRoot, "config", cometbft.GenesisJSONName)
nodeCfg.P2P.AddrBook = cometbft.AddrBookPath(chainRoot)
// For the same reasons described for the genesis.json path above, clear the
// node and validator file fields since they are provided in-memory.
nodeCfg.PrivValidatorKey = ""
nodeCfg.PrivValidatorState = ""
nodeCfg.NodeKey = ""

return nodeCfg
}
Expand Down
1 change: 1 addition & 0 deletions internal/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ func (a *AbciApp) Commit(ctx context.Context, _ *abciTypes.RequestCommit) (*abci

// Info is part of the Info/Query connection.
func (a *AbciApp) Info(ctx context.Context, _ *abciTypes.RequestInfo) (*abciTypes.ResponseInfo, error) {
// TODO: check kwild_voting.height!!!!!!!!!!!!!!
height, err := a.metadataStore.GetBlockHeight(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get block height: %w", err)
Expand Down
1 change: 1 addition & 0 deletions internal/abci/cometbft/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const (
DataDir = "data"
GenesisJSONName = "genesis.json"
ConfigTOMLName = "config.toml"
AddrBookFileName = "addrbook.json"
)

Expand Down
37 changes: 37 additions & 0 deletions internal/abci/cometbft/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cometbft
import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/kwilteam/kwil-db/core/log"
"github.com/kwilteam/kwil-db/internal/abci/cometbft/privval"
Expand Down Expand Up @@ -128,8 +130,43 @@ func genesisDocProvider(genDoc *types.GenesisDoc) cometNodes.GenesisDocProvider
}
}

func writeCometBFTConfigs(conf *cometConfig.Config, genDoc *types.GenesisDoc) error {
// Save a copy the cometbft genesis and config files in the "abci/config"
// folder expected by the `cometbft` cli app, used for inspect, etc.
cometConfigPath := filepath.Join(conf.RootDir, "config")
os.MkdirAll(cometConfigPath, 0755)
cmtGenesisFile := filepath.Join(cometConfigPath, GenesisJSONName)
if err := genDoc.SaveAs(cmtGenesisFile); err != nil {
return fmt.Errorf("failed to write cometbft genesis.json formatted file to %v: %w", cmtGenesisFile, err)
}

// Now "abci/config/config.toml"
conf.RPC.TLSCertFile, conf.RPC.TLSKeyFile = "", "" // not needed for debugging and recovery
cmtConfigFile := filepath.Join(cometConfigPath, "config.toml")
cometConfig.WriteConfigFile(cmtConfigFile, conf)

cfgREADME := `This config folder is used to echo the in-memory CometBFT configuration
that is generated from kwild's config and genesis files. This folder and the files
within are useful for running certain debugging commands with the official cometbft
command line application (github.com/cometbft/cometbft/cmd/cometbft).
WARNING: These files are overwritten on kwild startup.`
cmtREADMEFile := filepath.Join(cometConfigPath, "README")
if _, err := os.Stat(cmtREADMEFile); errors.Is(err, os.ErrNotExist) {
err = os.WriteFile(cmtREADMEFile, []byte(cfgREADME), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to write cometbft config folder README: %v", err)
}
}

return nil
}

// NewCometBftNode creates a new CometBFT node.
func NewCometBftNode(app abciTypes.Application, conf *cometConfig.Config, genDoc *types.GenesisDoc, privateKey cometEd25519.PrivKey, atomicStore privval.AtomicReadWriter, log *log.Logger) (*CometBftNode, error) {
if err := writeCometBFTConfigs(conf, genDoc); err != nil {
return nil, fmt.Errorf("failed to write the effective cometbft config files: %w", err)
}

err := conf.ValidateBasic()
if err != nil {
Expand Down

0 comments on commit ebd1ac9

Please sign in to comment.