From 2e6f66587d6a1f38823798502bba8793cdc433eb Mon Sep 17 00:00:00 2001 From: Roman Date: Thu, 4 Jan 2024 22:34:46 -0700 Subject: [PATCH] feat: complete JSON config (#15) * fix: proper error message when token in is too small * go work sum * feat: config path * feat: full JSON config * lint --- app/main.go | 19 ++++++++--------- app/sqs_config.go | 27 +++++++++--------------- chaininfo/client/chain_client.go | 27 ------------------------ config.json | 35 ++++++++++++++++---------------- domain/router.go | 18 ++++++++-------- 5 files changed, 46 insertions(+), 80 deletions(-) diff --git a/app/main.go b/app/main.go index 54f1e378..bcf90abf 100644 --- a/app/main.go +++ b/app/main.go @@ -37,8 +37,12 @@ func main() { panic(err) } - dbHost := viper.GetString(`database.host`) - dbPort := viper.GetString(`database.port`) + // Unmarshal the config into your Config struct + var config Config + if err := viper.Unmarshal(&config); err != nil { + fmt.Println("Error unmarshalling config:", err) + return + } // Handle SIGINT and SIGTERM signals to initiate shutdown exitChan := make(chan os.Signal, 1) @@ -52,7 +56,7 @@ func main() { }() redisClient := redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%s", dbHost, dbPort), + Addr: fmt.Sprintf("%s:%s", config.StorageHost, config.StoragePort), Password: "", // no password set DB: 0, // use default DB }) @@ -63,10 +67,7 @@ func main() { panic(err) } - chainID := viper.GetString(`chain.id`) - chainNodeURI := viper.GetString(`chain.node_uri`) - - chainClient, err := client.NewClient(chainID, chainNodeURI) + chainClient, err := client.NewClient(config.ChainID, config.ChainGRPCGatewayEndpoint) if err != nil { panic(err) } @@ -79,8 +80,6 @@ func main() { panic(err) } - config := DefaultConfig - encCfg := app.MakeEncodingConfig() // logger @@ -90,7 +89,7 @@ func main() { } logger.Info("Starting sidecar query server") - sidecarQueryServer, err := NewSideCarQueryServer(encCfg.Marshaler, *config.Router, dbHost, dbPort, config.ServerAddress, config.ChainGRPCGatewayEndpoint, config.ServerTimeoutDurationSecs, logger) + sidecarQueryServer, err := NewSideCarQueryServer(encCfg.Marshaler, *config.Router, config.StorageHost, config.StoragePort, config.ServerAddress, config.ChainGRPCGatewayEndpoint, config.ServerTimeoutDurationSecs, logger) if err != nil { panic(err) } diff --git a/app/sqs_config.go b/app/sqs_config.go index ad88ddd1..a9f05fc2 100644 --- a/app/sqs_config.go +++ b/app/sqs_config.go @@ -6,9 +6,6 @@ import ( // Config defines the config for the sidecar query server. type Config struct { - // IsEnabled defines if the sidecar query server is enabled. - IsEnabled bool `mapstructure:"enabled"` - // Storage defines the storage host and port. StorageHost string `mapstructure:"db-host"` StoragePort string `mapstructure:"db-port"` @@ -23,18 +20,14 @@ type Config struct { LoggerLevel string `mapstructure:"logger-level"` ChainGRPCGatewayEndpoint string `mapstructure:"grpc-gateway-endpoint"` + ChainID string `mapstructure:"chain-id"` // Router encapsulates the router config. Router *domain.RouterConfig `mapstructure:"router"` } -const groupOptName = "osmosis-sqs" - // DefaultConfig defines the default config for the sidecar query server. var DefaultConfig = Config{ - - IsEnabled: false, - StorageHost: "localhost", StoragePort: "6379", @@ -46,16 +39,16 @@ var DefaultConfig = Config{ LoggerLevel: "info", ChainGRPCGatewayEndpoint: "http://localhost:26657", + ChainID: "osmosis-1", Router: &domain.RouterConfig{ - PreferredPoolIDs: []uint64{}, - MaxPoolsPerRoute: 4, - MaxRoutes: 5, - MaxSplitRoutes: 3, - MaxSplitIterations: 10, - MinOSMOLiquidity: 10000, // 10_000 OSMO - RouteUpdateHeightInterval: 0, - RouteCacheEnabled: false, - RouteCacheExpirySeconds: 600, // 10 minutes + PreferredPoolIDs: []uint64{}, + MaxPoolsPerRoute: 4, + MaxRoutes: 5, + MaxSplitRoutes: 3, + MaxSplitIterations: 10, + MinOSMOLiquidity: 10000, // 10_000 OSMO + RouteCacheEnabled: false, + RouteCacheExpirySeconds: 600, // 10 minutes }, } diff --git a/chaininfo/client/chain_client.go b/chaininfo/client/chain_client.go index 51241dad..5b0b190b 100644 --- a/chaininfo/client/chain_client.go +++ b/chaininfo/client/chain_client.go @@ -5,11 +5,7 @@ import ( rpchttp "github.com/cometbft/cometbft/rpc/client/http" "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" "github.com/spf13/viper" - "google.golang.org/grpc" - "google.golang.org/grpc/encoding" - "google.golang.org/grpc/encoding/proto" clpoolmodel "github.com/osmosis-labs/osmosis/v21/x/concentrated-liquidity/model" cwpoolmodel "github.com/osmosis-labs/osmosis/v21/x/cosmwasmpool/model" @@ -24,7 +20,6 @@ type Client interface { } type chainClient struct { - context client.Context rpcClient *rpchttp.HTTP } @@ -40,30 +35,8 @@ func NewClient(chainID string, nodeURI string) (Client, error) { clpoolmodel.RegisterInterfaces(interfaceRegistry) cwpoolmodel.RegisterInterfaces(interfaceRegistry) - clientCtx := client.Context{}. - WithCodec(codec.NewProtoCodec(interfaceRegistry)). - WithChainID(chainID) - - // If grpc is enabled, configure grpc client for grpc gateway. - grpcClient, err := grpc.Dial( - viper.GetString(`chain.node_grpc`), // TODO: get from config - // nolint: staticcheck - grpc.WithInsecure(), - grpc.WithDefaultCallOptions( - grpc.ForceCodec(encoding.GetCodec(proto.Name)), - grpc.MaxCallRecvMsgSize(10485760), - grpc.MaxCallSendMsgSize(2147483647), - ), - ) - if err != nil { - return nil, err - } - - clientCtx = clientCtx.WithGRPCClient(grpcClient) - return &chainClient{ rpcClient: rpcClient, - context: clientCtx, }, nil } diff --git a/config.json b/config.json index 1fc07b2a..303eedb9 100644 --- a/config.json +++ b/config.json @@ -1,22 +1,23 @@ { "debug": true, - "server": { - "address": ":9092" - }, - "context":{ - "timeout":2 - }, - "database": { - "host": "0.0.0.0", - "port": "6379", - "user": "user", - "pass": "password", - "name": "article" - }, - "chain": { - "id": "osmosis-1", - "node_uri": "tcp://0.0.0.0:26657", - "node_grpc": "0.0.0.0:9090" + "db-host": "localhost", + "db-port": "6379", + "server-address": ":9092", + "timeout-duration-secs": 2, + "logger-filename": "sqs.log", + "logger-is-production": true, + "logger-level": "info", + "grpc-gateway-endpoint": "http://localhost:26657", + "chain-id": "osmosis-1", + "router": { + "preferred-pool-ids": [], + "max-pools-per-route": 4, + "max-routes": 20, + "max-split-routes": 3, + "max-split-iterations": 10, + "min-osmo-liquidity": 10000, + "route-cache-enabled": true, + "route-cache-expiry-seconds": 600 } } \ No newline at end of file diff --git a/domain/router.go b/domain/router.go index b6094eca..318d78c8 100644 --- a/domain/router.go +++ b/domain/router.go @@ -55,15 +55,15 @@ type Quote interface { } type RouterConfig struct { - PreferredPoolIDs []uint64 `mapstructure:"preferred_pool_ids"` - MaxPoolsPerRoute int `mapstructure:"max_pools_per_route"` - MaxRoutes int `mapstructure:"max_routes"` - MaxSplitRoutes int `mapstructure:"max_split_routes"` - MaxSplitIterations int `mapstructure:"max_split_iterations"` + PreferredPoolIDs []uint64 `mapstructure:"preferred-pool-ids"` + MaxPoolsPerRoute int `mapstructure:"max-pools-per-route"` + MaxRoutes int `mapstructure:"max-routes"` + MaxSplitRoutes int `mapstructure:"max-split-routes"` + MaxSplitIterations int `mapstructure:"max-split-iterations"` // Denominated in OSMO (not uosmo) - MinOSMOLiquidity int `mapstructure:"min_osmo_liquidity"` - RouteUpdateHeightInterval int `mapstructure:"route_update_height_interval"` - RouteCacheEnabled bool `mapstructure:"route_cache_enabled"` + MinOSMOLiquidity int `mapstructure:"min-osmo-liquidity"` + RouteUpdateHeightInterval int `mapstructure:"route-update-height-interval"` + RouteCacheEnabled bool `mapstructure:"route-cache-enabled"` // The number of seconds to cache routes for before expiry. - RouteCacheExpirySeconds uint64 `mapstructure:"route_cache_expiry_seconds"` + RouteCacheExpirySeconds uint64 `mapstructure:"route-cache-expiry-seconds"` }