Skip to content

Commit

Permalink
add store object
Browse files Browse the repository at this point in the history
  • Loading branch information
nkitlabs committed Feb 3, 2025
1 parent 6f6cafd commit 5c17809
Show file tree
Hide file tree
Showing 27 changed files with 743 additions and 425 deletions.
31 changes: 29 additions & 2 deletions relayer/log.go → cmd/log.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package relayer
package cmd

import (
"fmt"
"os"
"time"

zaplogfmt "github.com/jsternberg/zap-logfmt"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func newRootLogger(format string, logLevel string) (*zap.Logger, error) {
// newLogger creates a new root logger with the given log format and log level.
func newLogger(format string, logLevel string) (*zap.Logger, error) {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format("2006-01-02T15:04:05.000000Z07:00"))
Expand Down Expand Up @@ -51,3 +53,28 @@ func newRootLogger(format string, logLevel string) (*zap.Logger, error) {

return logger, nil
}

// initLogger initializes the logger with the given default log level.
func initLogger(defaultLogLevel string) (log *zap.Logger, err error) {
logFormat := viper.GetString("log-format")

logLevel := viper.GetString("log-level")
if viper.GetBool("debug") {
logLevel = "debug"
}
if logLevel == "" && defaultLogLevel != "" {
logLevel = defaultLogLevel
}

// initialize logger only if user run command "start" or log level is "debug"
if os.Args[1] == "start" || logLevel == "debug" {
log, err = newLogger(logFormat, logLevel)
if err != nil {
return nil, err
}
} else {
log = zap.NewNop()
}

return log, nil
}
32 changes: 25 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ import (
"go.uber.org/zap"

falcon "github.com/bandprotocol/falcon/relayer"
"github.com/bandprotocol/falcon/relayer/store"
)

const (
appName = "falcon"
defaultCoinType = 60

PassphraseEnvKey = "PASSPHRASE"
)

var defaultHome = filepath.Join(os.Getenv("HOME"), ".falcon")

// NewRootCmd returns the root command for falcon.
func NewRootCmd(log *zap.Logger) *cobra.Command {
app := falcon.NewApp(log, defaultHome, false, nil)
passphrase := os.Getenv(PassphraseEnvKey)
app := falcon.NewApp(log, defaultHome, false, nil, passphrase, nil)

// RootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Expand All @@ -45,15 +49,29 @@ func NewRootCmd(log *zap.Logger) *cobra.Command {
}

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error {
// retrieve log level from viper
logLevelViper := viper.GetString("log-level")
if viper.GetBool("debug") {
logLevelViper = "debug"
// set up store
app.Store = store.NewFileSystem(app.HomePath, app.Passphrase)

// load configuration
var err error
app.Config, err = app.Store.GetConfig()
if err != nil {
return err
}

logFormat := viper.GetString("log-format")
// retrieve log level from config
configLogLevel := ""
if app.Config != nil {
configLogLevel = app.Config.Global.LogLevel
}

// init log object
app.Log, err = initLogger(configLogLevel)
if err != nil {
return err
}

return app.Init(rootCmd.Context(), logLevelViper, logFormat)
return app.Init(rootCmd.Context())
}

rootCmd.PersistentPostRun = func(cmd *cobra.Command, _ []string) {
Expand Down
8 changes: 4 additions & 4 deletions internal/relayertest/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
_ "embed"
"time"

falcon "github.com/bandprotocol/falcon/relayer"
"github.com/bandprotocol/falcon/relayer/band"
"github.com/bandprotocol/falcon/relayer/chains"
"github.com/bandprotocol/falcon/relayer/chains/evm"
"github.com/bandprotocol/falcon/relayer/config"
)

//go:embed testdata/default_config.toml
Expand All @@ -19,8 +19,8 @@ var CustomCfgText string
//go:embed testdata/custom_config_with_time_str.toml
var CustomCfgTextWithTimeStr string

var CustomCfg = falcon.Config{
Global: falcon.GlobalConfig{
var CustomCfg = config.Config{
Global: config.GlobalConfig{
CheckingPacketInterval: 1 * time.Minute,
SyncTunnelsInterval: 5 * time.Minute,
MaxCheckingPacketPenaltyDuration: 1 * time.Hour,
Expand All @@ -32,7 +32,7 @@ var CustomCfg = falcon.Config{
Timeout: 3 * time.Second,
LivelinessCheckingInterval: 5 * time.Minute,
},
TargetChains: chains.ChainProviderConfigs{
TargetChains: config.ChainProviderConfigs{
"testnet": &evm.EVMChainProviderConfig{
BaseChainProviderConfig: chains.BaseChainProviderConfig{
Endpoints: []string{"http://localhost:8545"},
Expand Down
20 changes: 10 additions & 10 deletions internal/relayertest/mocks/band_chain_query.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions internal/relayertest/mocks/chain_provider_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package main

import (
"fmt"
"os"

"github.com/joho/godotenv"

"github.com/bandprotocol/falcon/cmd"
)

func main() {
// loading .env file
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
panic(fmt.Sprintf("Error due to loading .env file; %v", err))
}

if err := cmd.Execute(); err != nil {
os.Exit(1)
}
Expand Down
Loading

0 comments on commit 5c17809

Please sign in to comment.