Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collecting all configs to one place and some refactoring #101

Merged
merged 8 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions conf.broker.yaml.template
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
log: info
name: <name>
system:
log: info
name: <name>

database:
postgres:
url: postgres://<user>:<pass>@<host>:<port>/<db>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com
arbitrum_sepolia:
- https://sepolia-rollup.arbitrum.io/rpc
- name: ethereum
nodes:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

- name: arbitrum_sepolia
nodes:
- https://sepolia-rollup.arbitrum.io/rpc

plugins:
uniswap:
Expand Down
24 changes: 14 additions & 10 deletions conf.remote.yaml.template
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
log: info
name: <name>
system:
log: info
name: <name>

database:
postgres:
url: postgres://<user>:<pass>@<host>:<port>/<db>

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com
arbitrum_sepolia:
- https://sepolia-rollup.arbitrum.io/rpc
- name: ethereum
nodes:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

- name: arbitrum_sepolia
nodes:
- https://sepolia-rollup.arbitrum.io/rpc

plugins:
uniswap:
Expand Down
34 changes: 19 additions & 15 deletions conf.standalone.yaml.template
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
log: info
name: <name>
system:
log: info
name: <name>

database:
postgres:
url: postgres://<user>:<pass>@<host>:<port>/<db>?sslmode=disable

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com
- name: ethereum
nodes:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

arbitrum:
- https://arbitrum-one.publicnode.com
- https://arbitrum.llamarpc.com
- wss://arbitrum-one.publicnode.com
- https://arbitrum-one.public.blastapi.io
- name: arbitrum:
nodes:
- https://arbitrum-one.publicnode.com
- https://arbitrum.llamarpc.com
- wss://arbitrum-one.publicnode.com
- https://arbitrum-one.public.blastapi.io

arbitrum_sepolia:
- https://sepolia-rollup.arbitrum.io/rpc
- name: arbitrum_sepolia
nodes:
- https://sepolia-rollup.arbitrum.io/rpc

plugins:
logs:
Expand Down
21 changes: 12 additions & 9 deletions conf.worker.yaml.template
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
log: info
name: <name>
system:
log: info
name: <name>

broker:
uri: wss://shinobi.brokers.kenshi.io

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com
- name: ethereum
nodes:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

arbitrum_sepolia:
- https://sepolia-rollup.arbitrum.io/rpc
- name: arbitrum_sepolia
nodes:
- https://sepolia-rollup.arbitrum.io/rpc

plugins:
uniswap:
Expand Down
131 changes: 127 additions & 4 deletions go.work.sum

Large diffs are not rendered by default.

23 changes: 13 additions & 10 deletions quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,21 +196,24 @@ You need a configuration file to get started. You can start with the following
config:

```yaml
log: info
name: <name>
system:
log: info
name: <name>

broker:
uri: wss://shinobi.brokers.kenshi.io

rpc:
ethereum:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

arbitrum_sepolia:
- https://sepolia-rollup.arbitrum.io/rpc
- name: ethereum
nodes:
- https://ethereum.publicnode.com
- https://eth.llamarpc.com
- wss://ethereum.publicnode.com
- https://eth.rpc.blxrbdn.com

- name: arbitrum_sepolia
nodes:
- https://sepolia-rollup.arbitrum.io/rpc

plugins:
uniswap:
Expand Down
14 changes: 9 additions & 5 deletions src/cmd/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ var brokerCmd = &cobra.Command{
Short: "Run the Unchained client in broker mode",
Long: `Run the Unchained client in broker mode`,
Run: func(cmd *cobra.Command, args []string) {
config.LoadConfig(configPath, secretsPath)
log.Start()
err := config.Load(configPath, secretsPath)
if err != nil {
panic(err)
}

log.Start(config.App.System.Log)
db.Start()
correctness.Setup()
correctness.New()
ethereum.Start()
uniswap.Setup()
logs.Setup()
uniswap.New()
logs.New()
gql.InstallHandlers()
net.StartServer()
},
Expand Down
19 changes: 10 additions & 9 deletions src/cmd/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ var consumerCmd = &cobra.Command{
Long: `Run the Unchained client in consumer mode`,

PreRun: func(cmd *cobra.Command, args []string) {
err := config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
if err != nil {
panic(err)
}
config.App.Broker.URI = cmd.Flags().Lookup("broker").Value.String()
},

Run: func(cmd *cobra.Command, args []string) {

config.LoadConfig(configPath, secretsPath)
log.Start()
err := config.Load(configPath, secretsPath)
if err != nil {
panic(err)
}

log.Start(config.App.System.Log)

log.Logger.
With("Version", constants.Version).
Expand All @@ -47,9 +48,9 @@ var consumerCmd = &cobra.Command{
bls.InitClientIdentity()
pos.Start()
db.Start()
uniswap.Setup()
correctness.Setup()
logs.Setup()
uniswap.New()
correctness.New()
logs.New()
client.StartClient()
consumers.StartConsumer()
client.Listen()
Expand Down
22 changes: 11 additions & 11 deletions src/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,16 @@ var workerCmd = &cobra.Command{
Long: `Run the Unchained client in worker mode`,

PreRun: func(cmd *cobra.Command, args []string) {
err := config.Config.BindPFlag("broker.uri", cmd.Flags().Lookup("broker"))
if err != nil {
panic(err)
}
config.App.Broker.URI = cmd.Flags().Lookup("broker").Value.String()
},

Run: func(cmd *cobra.Command, args []string) {
err := config.Load(configPath, secretsPath)
if err != nil {
panic(err)
}

config.LoadConfig(configPath, secretsPath)
log.Start()

log.Start(config.App.System.Log)
log.Logger.
With("Version", constants.Version).
With("Protocol", constants.ProtocolVersion).
Expand All @@ -47,10 +46,11 @@ var workerCmd = &cobra.Command{
pos.Start()
db.Start()
client.StartClient()
uniswap.Setup()
uniswap.Start()
logs.Setup()
logs.Start()

uniswap.Listen()
logs.New()
logs.Listen()

persistence.Start(contextPath)
client.Listen()
},
Expand Down
85 changes: 49 additions & 36 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,66 @@ package config

import (
"os"
"path"
"runtime"

petname "github.com/dustinkirkland/golang-petname"
"github.com/spf13/viper"
)
"github.com/KenshiTech/unchained/log"

func defaults() {
Config.SetDefault("name", petname.Generate(3, "-"))
Config.SetDefault("log", "info")
Config.SetDefault("rpc.ethereum", "https://ethereum.publicnode.com")
Config.SetDefault("rpc.arbitrum_sepolia", "https://sepolia-rollup.arbitrum.io/rpc")
Config.SetDefault("broker.bind", "0.0.0.0:9123")
Config.SetDefault("broker.uri", "wss://shinobi.brokers.kenshi.io")
Config.SetDefault("pos.chain", "arbitrum_sepolia")
Config.SetDefault("pos.address", "0x965e364987356785b7E89e2Fe7B70f5E5107332d")
Config.SetDefault("pos.base", int64(1))
}
"github.com/KenshiTech/unchained/constants"
"gopkg.in/yaml.v3"

var Config *viper.Viper
var Secrets *viper.Viper
"github.com/ilyakaznacheev/cleanenv"
)

func init() {
Config = viper.New()
Secrets = viper.New()
}
var App Config
var SecretFilePath string

func LoadConfig(configFileName string, secretsFileName string) {
defaults()
func Load(configPath, secretPath string) error {
if configPath == "" {
_, b, _, _ := runtime.Caller(0)
configPath = path.Join(b, "../..", "./config.yaml")
}

Config.SetConfigFile(configFileName)
err := Config.ReadInConfig()
if secretPath != "" {
SecretFilePath = secretPath
err := cleanenv.ReadConfig(secretPath, App.Secret)
if err != nil {
log.Logger.With("Error", err).Error("Can't read secret file")
return constants.ErrCantLoadSecret
}
}

err := cleanenv.ReadConfig(configPath, App)
if err != nil {
isNotExist := os.IsNotExist(err)
if !isNotExist {
panic(err)
}
log.Logger.With("Error", err).Error("Can't read config file")
return constants.ErrCantLoadConfig
}

err = Config.WriteConfig()
if err != nil {
panic(err)
}
err = cleanenv.ReadEnv(App)
if err != nil {
return err
}

return nil
}

func (s *Secret) Save() error {
yamlData, err := yaml.Marshal(&s)
if err != nil {
log.Logger.With("Error", err).Error("Can't marshal secrets to yaml")
return constants.ErrCantWriteSecret
}

Secrets.SetConfigFile(secretsFileName)
err = Secrets.MergeInConfig()
if SecretFilePath == "" {
log.Logger.With("Error", err).Error("SecretFilePath is not defined")
return constants.ErrCantWriteSecret
}

if err != nil && os.IsExist(err) {
panic(err)
err = os.WriteFile(SecretFilePath, yamlData, 0600)
if err != nil {
log.Logger.With("Error", err).Error("Can't write secret file")
return constants.ErrCantWriteSecret
}

return nil
}
Loading
Loading