Skip to content

Commit

Permalink
feat(client): add cometBFT setting overrides on init (#29)
Browse files Browse the repository at this point in the history
* feat(client): add cometBFT setting overrides on init
  • Loading branch information
leeren authored Aug 23, 2024
1 parent cb55f8f commit 373cd59
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
5 changes: 5 additions & 0 deletions client/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ func bindInitFlags(flags *pflag.FlagSet, cfg *InitConfig) {
flags.BoolVar(&cfg.TrustedSync, "trusted-sync", cfg.TrustedSync, "Initialize trusted state-sync height and hash by querying the Story RPC")
flags.BoolVar(&cfg.Force, "force", cfg.Force, "Force initialization (overwrite existing files)")
flags.BoolVar(&cfg.Clean, "clean", cfg.Clean, "Delete home directory before initialization")
flags.StringVar(&cfg.RPCLaddr, "rpc-laddr", "", "Override the RPC listening address")
flags.StringVar(&cfg.ExternalAddress, "external-address", "", "Override the P2P external address")
flags.StringVar(&cfg.Seeds, "seeds", "", "Override the P2P seeds (comma-separated)")
flags.BoolVar(&cfg.SeedMode, "seed-mode", false, "Enable seed mode")
flags.StringVar(&cfg.Moniker, "moniker", cfg.Moniker, "Custom moniker name for this node")
}
63 changes: 53 additions & 10 deletions client/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,18 @@ import (

// InitConfig is the config for the init command.
type InitConfig struct {
HomeDir string
Network netconf.ID
TrustedSync bool
Force bool
Clean bool
Cosmos bool
ExecutionHash common.Hash
HomeDir string
Network netconf.ID
TrustedSync bool
Force bool
Clean bool
Cosmos bool
ExecutionHash common.Hash
RPCLaddr string
ExternalAddress string
Seeds string
SeedMode bool
Moniker string
}

// newInitCmd returns a new cobra command that initializes the files and folders required by story.
Expand Down Expand Up @@ -107,9 +112,9 @@ func InitFiles(ctx context.Context, initCfg InitConfig) error {
cfg = storycfg.LocalConfig
default:
cfg = storycfg.DefaultConfig()
cfg.HomeDir = homeDir
cfg.Network = network
}
cfg.HomeDir = homeDir

// Folders
folders := []struct {
Expand All @@ -133,9 +138,36 @@ func InitFiles(ctx context.Context, initCfg InitConfig) error {
log.Info(ctx, "Generated folder", "reason", folder.Name, "path", folder.Path)
}

// Add P2P seeds to comet config
if seeds := network.Static().ConsensusSeeds(); len(seeds) > 0 {
if initCfg.Moniker != "" {
comet.Moniker = initCfg.Moniker
log.Info(ctx, "Overriding node moniker", "moniker", comet.Moniker)
}

if initCfg.RPCLaddr != "" {
comet.RPC.ListenAddress = initCfg.RPCLaddr
log.Info(ctx, "Overriding RPC listen address", "address", comet.RPC.ListenAddress)
}

if initCfg.ExternalAddress != "" {
comet.P2P.ExternalAddress = initCfg.ExternalAddress
log.Info(ctx, "Overriding P2P external address", "address", comet.P2P.ExternalAddress)
}

// Handle P2P seeds with prioritization
if initCfg.Seeds != "" {
// If seeds are provided via the flag, use them
seeds := SplitAndTrim(initCfg.Seeds)
comet.P2P.Seeds = strings.Join(seeds, ",")
log.Info(ctx, "Overriding P2P seeds with provided flag", "seeds", comet.P2P.Seeds)
} else if networkSeeds := network.Static().ConsensusSeeds(); len(networkSeeds) > 0 {
// Otherwise, use the network's default seeds
comet.P2P.Seeds = strings.Join(networkSeeds, ",")
log.Info(ctx, "Using network's default P2P seeds", "seeds", comet.P2P.Seeds)
}

if initCfg.SeedMode {
comet.P2P.SeedMode = true
log.Info(ctx, "Seed mode enabled")
}

// Setup comet config
Expand Down Expand Up @@ -253,3 +285,14 @@ func prepareHomeDirectory(ctx context.Context, initCfg InitConfig, homeDir strin

return nil
}

func SplitAndTrim(input string) []string {
l := strings.Split(input, ",")
var ret []string
for _, r := range l {
if r = strings.TrimSpace(r); r != "" {
ret = append(ret, r)
}
}
return ret
}
16 changes: 16 additions & 0 deletions client/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ var (
APIAddress: "127.0.0.1:1317",
EnableUnsafeCORS: false,
Tracer: tracer.DefaultConfig(),
RPCLaddr: "tcp://127.0.0.1:26657",
ExternalAddress: "",
Seeds: "",
SeedMode: false,
}
LocalConfig = Config{
HomeDir: DefaultHomeDir(),
Expand All @@ -75,6 +79,10 @@ var (
APIAddress: "127.0.0.1:1317",
EnableUnsafeCORS: false,
Tracer: tracer.DefaultConfig(),
RPCLaddr: "tcp://127.0.0.1:26657",
ExternalAddress: "",
Seeds: "",
SeedMode: false,
}
)

Expand All @@ -96,6 +104,10 @@ func DefaultConfig() Config {
APIAddress: "127.0.0.1:1317",
EnableUnsafeCORS: false,
Tracer: tracer.DefaultConfig(),
RPCLaddr: "tcp://127.0.0.1:26657",
ExternalAddress: "",
Seeds: "",
SeedMode: false,
}
}

Expand Down Expand Up @@ -144,6 +156,10 @@ type Config struct {
APIAddress string
EnableUnsafeCORS bool
Tracer tracer.Config
RPCLaddr string
ExternalAddress string
Seeds string
SeedMode bool
}

// ConfigFile returns the default path to the toml story config file.
Expand Down

0 comments on commit 373cd59

Please sign in to comment.