Skip to content

Commit

Permalink
Merge pull request #72 from JackalLabs/viper-config
Browse files Browse the repository at this point in the history
Viper config
  • Loading branch information
TheMarstonConnell authored Sep 27, 2024
2 parents 7ccd39e + 2ba7a22 commit b6dc9f8
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 60 deletions.
16 changes: 16 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
Expand Down Expand Up @@ -156,6 +157,21 @@ func RootCmd() *cobra.Command {

r.PersistentFlags().String(types.FlagHome, types.DefaultHome, "sets the home directory for sequoia")
r.PersistentFlags().String(types.FlagLogLevel, types.DefaultLogLevel, "log level. info|error|debug")
r.PersistentFlags().Int("restart-attempt", defaultMaxRestartAttempt, "attempt to restart <restart-attempt> times when the provider fails to start")
r.PersistentFlags().String("domain", "http://example.com", "provider comain")
r.PersistentFlags().Int64("api_config.port", 3333, "port to serve api requests")
r.PersistentFlags().Int("api_config.ipfs_port", 4005, "port for IPFS")
r.PersistentFlags().String("api_config.ipfs_domain", "dns4/ipfs.example.com/tcp/4001", "IPFS domain")
r.PersistentFlags().Int64("proof_threads", 1000, "maximum threads for proofs")
r.PersistentFlags().String("data_directory", "$HOME/.sequoia/data", "directory to store database files")
r.PersistentFlags().Int64("queue_interval", 10, "seconds to wait until next cycle to flush the transaction queue")
r.PersistentFlags().Int64("proof_interval", 120, "seconds to wait until next cycle to post proofs")
r.PersistentFlags().Int64("total_bytes_offered", 1092616192, "maximum storage space to provide in bytes")

err := viper.BindPFlags(r.PersistentFlags())
if err != nil {
panic(err)
}

r.AddCommand(StartCmd(), wallet.WalletCmd(), InitCmd(), VersionCmd(), IPFSCmd(), SalvageCmd(), ShutdownCmd())

Expand Down
1 change: 0 additions & 1 deletion cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,5 @@ func StartCmd() *cobra.Command {
},
}

cmd.Flags().Int("restart-attempt", defaultMaxRestartAttempt, "attempt to restart <restart-attempt> times when the provider fails to start")
return cmd
}
3 changes: 1 addition & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ func ReadConfig(data []byte) (*Config, error) {
// not using a default config to detect badger ds users
config := Config{}

err := yaml.Unmarshal(data, &config)
if err != nil {
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, err
}

Expand Down
39 changes: 34 additions & 5 deletions config/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import (
"errors"
"os"
"path"

"github.com/rs/zerolog/log"
"github.com/spf13/viper"
)

const ConfigFileName = "config.yaml"
const (
ConfigName = "config"
ConfigType = "yaml"
ConfigFileName = ConfigName + "." + ConfigType
)

func createIfNotExists(directory string, fileName string, contents []byte) (bool, error) {
filePath := path.Join(directory, fileName)
Expand Down Expand Up @@ -62,7 +69,6 @@ func ReadConfigFile(directory string) (*Config, error) {
if err != nil {
return nil, err
}

return config, nil
}

Expand All @@ -74,10 +80,33 @@ func Init(home string) (*Config, error) {
return nil, err
}

err = createFiles(directory)
if err != nil {
viper.SetConfigName(ConfigName)
viper.SetConfigType(ConfigType)
viper.AddConfigPath(directory)

if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
err := createFiles(directory)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
/*
config, err := ReadConfigFile(directory)
if err != nil {
return nil, err
}
*/
var config Config

if err := viper.Unmarshal(&config); err != nil {
return nil, err
}

return ReadConfigFile(directory)
log.Debug().Object("config", config)

return &config, nil
}
157 changes: 110 additions & 47 deletions config/types.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,87 @@
package config

import (
"github.com/desmos-labs/cosmos-go-wallet/types"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)

type Seed struct {
SeedPhrase string `json:"seed_phrase"`
DerivationPath string `json:"derivation_path"`
}

// required for the mapstructure tag
type ChainConfig struct {
Bech32Prefix string `yaml:"bech32_prefix" mapstructure:"bech32_prefix"`
RPCAddr string `yaml:"rpc_addr" mapstructure:"rpc_addr"`
GRPCAddr string `yaml:"grpc_addr" mapstructure:"grpc_addr"`
GasPrice string `yaml:"gas_price" mapstructure:"gas_price"`
GasAdjustment float64 `yaml:"gas_adjustment" mapstructure:"gas_adjustment"`
}

type Config struct {
QueueInterval int64 `yaml:"queue_interval"`
ProofInterval int64 `yaml:"proof_interval"`
StrayManagerCfg StrayManagerConfig `yaml:"stray_manager"`
ChainCfg types.ChainConfig `yaml:"chain_config"`
Ip string `yaml:"domain"`
TotalSpace int64 `yaml:"total_bytes_offered"`
DataDirectory string `yaml:"data_directory"`
APICfg APIConfig `yaml:"api_config"`
ProofThreads int64 `yaml:"proof_threads"`
BlockStoreConfig BlockStoreConfig `yaml:"block_store_config"`
QueueInterval int64 `yaml:"queue_interval" mapstructure:"queue_interval"`
ProofInterval int64 `yaml:"proof_interval" mapstructure:"proof_interval"`
StrayManagerCfg StrayManagerConfig `yaml:"stray_manager" mapstructure:"stray_manager"`
ChainCfg ChainConfig `yaml:"chain_config" mapstructure:"chain_config"`
Ip string `yaml:"domain" mapstructure:"domain"`
TotalSpace int64 `yaml:"total_bytes_offered" mapstructure:"total_bytes_offered"`
DataDirectory string `yaml:"data_directory" mapstructure:"data_directory"`
APICfg APIConfig `yaml:"api_config" mapstructure:"api_config"`
ProofThreads int16 `yaml:"proof_threads" mapstructure:"proof_threads"`
BlockStoreConfig BlockStoreConfig `yaml:"block_store_config" mapstructure:"block_store_config"`
}

func DefaultQueueInterval() int64 {
return 10
}

func DefaultProofInterval() int64 {
return 120
}

func DefaultIP() string {
return "https://example.com"
}

func DefaultTotalSpace() int64 {
return 1092616192
}

func DefaultDataDirectory() string {
return "$HOME/.sequoia/data"
}

func DefaultProofThreads() int16 {
return 1000
}

type StrayManagerConfig struct {
CheckInterval int64 `yaml:"check_interval"`
RefreshInterval int64 `yaml:"refresh_interval"`
HandCount int `yaml:"hands"`
CheckInterval int64 `yaml:"check_interval" mapstructure:"check_interval"`
RefreshInterval int64 `yaml:"refresh_interval" mapstructure:"refresh_interval"`
HandCount int `yaml:"hands" mapstructure:"hands"`
}

func DefaultStrayManagerConfig() StrayManagerConfig {
return StrayManagerConfig{
CheckInterval: 30,
RefreshInterval: 120,
HandCount: 2,
}
}

type APIConfig struct {
Port int64 `yaml:"port"`
IPFSPort int `yaml:"ipfs_port"`
IPFSDomain string `yaml:"ipfs_domain"`
Port int64 `yaml:"port" mapstructure:"port"`
IPFSPort int `yaml:"ipfs_port" mapstructure:"ipfs_port"`
IPFSDomain string `yaml:"ipfs_domain" mapstructure:"ipfs_domain"`
}

func DefaultAPIConfig() APIConfig {
return APIConfig{
Port: 3333,
IPFSPort: 4005,
IPFSDomain: "dns4/ipfs.example.com/tcp/4001",
}
}

const (
Expand All @@ -42,9 +92,16 @@ const (
type BlockStoreConfig struct {
// *choosing badgerdb as block store will need to use the same directory
// for data directory
Directory string `yaml:"directory"`
Directory string `yaml:"directory" mapstructure:"directory"`
// data store options: flatfs, badgerdb
Type string `yaml:"type"`
Type string `yaml:"type" mapstructure:"type"`
}

func DefaultBlockStoreConfig() BlockStoreConfig {
return BlockStoreConfig{
Directory: "$HOME/.sequoia/blockstore",
Type: OptFlatFS,
}
}

// LegacyWallet handles keys from earlier versions of storage providers.
Expand All @@ -56,35 +113,28 @@ type LegacyWallet struct {
Address string `json:"address"`
}

func DefaultChainConfig() ChainConfig {
return ChainConfig{
RPCAddr: "http://localhost:26657",
GRPCAddr: "localhost:9090",
GasPrice: "0.02ujkl",
GasAdjustment: 1.5,
Bech32Prefix: "jkl",
}
}

func DefaultConfig() *Config {
return &Config{
QueueInterval: 10,
ProofInterval: 120,
StrayManagerCfg: StrayManagerConfig{
CheckInterval: 30,
RefreshInterval: 120,
HandCount: 2,
},
ChainCfg: types.ChainConfig{
RPCAddr: "http://localhost:26657",
GRPCAddr: "localhost:9090",
GasPrice: "0.02ujkl",
GasAdjustment: 1.5,
Bech32Prefix: "jkl",
},
Ip: "https://example.com",
TotalSpace: 1092616192, // 1 gib default
DataDirectory: "$HOME/.sequoia/data",
APICfg: APIConfig{
Port: 3333,
IPFSPort: 4005,
IPFSDomain: "dns4/ipfs.example.com/tcp/4001",
},
ProofThreads: 1000,
BlockStoreConfig: BlockStoreConfig{
Directory: "$HOME/.sequoia/blockstore",
Type: OptFlatFS,
},
QueueInterval: DefaultQueueInterval(),
ProofInterval: DefaultProofInterval(),
StrayManagerCfg: DefaultStrayManagerConfig(),
ChainCfg: DefaultChainConfig(),
Ip: DefaultIP(),
TotalSpace: DefaultTotalSpace(), // 1 gib default
DataDirectory: DefaultDataDirectory(),
APICfg: DefaultAPIConfig(),
ProofThreads: DefaultProofThreads(),
BlockStoreConfig: DefaultBlockStoreConfig(),
}
}

Expand All @@ -104,6 +154,19 @@ func (c Config) MarshalZerologObject(e *zerolog.Event) {
Int64("APIPort", c.APICfg.Port).
Int("APIIPFSPort", c.APICfg.IPFSPort).
Str("APIIPFSDomain", c.APICfg.IPFSDomain).
Int64("ProofThreads", c.ProofThreads).
Int16("ProofThreads", c.ProofThreads).
Str("BlockstoreBackend", c.BlockStoreConfig.Type)
}

func init() {
viper.SetDefault("QueueInterval", DefaultQueueInterval())
viper.SetDefault("ProofInterval", DefaultProofInterval())
viper.SetDefault("StrayManagerCfg", DefaultStrayManagerConfig())
viper.SetDefault("ChainCfg", DefaultChainConfig())
viper.SetDefault("Ip", DefaultIP())
viper.SetDefault("TotalSpace", DefaultTotalSpace()) // 1 gib defaul
viper.SetDefault("DataDirectory", DefaultDataDirectory())
viper.SetDefault("APICfg", DefaultAPIConfig())
viper.SetDefault("ProofThreads", DefaultProofThreads())
viper.SetDefault("BlockStoreConfig", DefaultBlockStoreConfig())
}
5 changes: 3 additions & 2 deletions config/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

sequoiaWallet "github.com/JackalLabs/sequoia/wallet"
bip39 "github.com/cosmos/go-bip39"
"github.com/desmos-labs/cosmos-go-wallet/types"
"github.com/desmos-labs/cosmos-go-wallet/wallet"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -94,7 +95,7 @@ func InitWallet(home string) (*wallet.Wallet, error) {
legacyWallet, err := detectLegacyWallet(home)
if err == nil {
log.Info().Msg("legacy wallet detected")
return sequoiaWallet.CreateWalletPrivKey(legacyWallet.Key, config.ChainCfg)
return sequoiaWallet.CreateWalletPrivKey(legacyWallet.Key, types.ChainConfig(config.ChainCfg))
}

err = createWallet(directory)
Expand All @@ -112,7 +113,7 @@ func InitWallet(home string) (*wallet.Wallet, error) {
return nil, err
}

return sequoiaWallet.CreateWallet(seed.SeedPhrase, seed.DerivationPath, config.ChainCfg)
return sequoiaWallet.CreateWallet(seed.SeedPhrase, seed.DerivationPath, types.ChainConfig(config.ChainCfg))
}

// returns LegacyWallet if "priv_storkey.json" is found at sequoia home directory,
Expand Down
2 changes: 1 addition & 1 deletion proofs/proofs.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (p *Prover) Stop() {
p.running = false
}

func NewProver(wallet *wallet.Wallet, q *queue.Queue, io FileSystem, interval int64, threads int64, chunkSize int) *Prover {
func NewProver(wallet *wallet.Wallet, q *queue.Queue, io FileSystem, interval int64, threads int16, chunkSize int) *Prover {
p := Prover{
running: false,
wallet: wallet,
Expand Down
4 changes: 2 additions & 2 deletions proofs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Prover struct {
processed time.Time
interval int64
io FileSystem
threads int64
currentThreads int64
threads int16
currentThreads int16
chunkSize int
}

Expand Down

0 comments on commit b6dc9f8

Please sign in to comment.