Skip to content

Commit

Permalink
config: relayer updates (#23)
Browse files Browse the repository at this point in the history
* config updates

* nit file path logic

* clean relayer config yml
  • Loading branch information
shaspitz authored Feb 23, 2024
1 parent 5fd8abf commit ae1dceb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 94 deletions.
96 changes: 51 additions & 45 deletions standard/bridge-v1/cmd/relayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
optionConfig = &cli.StringFlag{
Name: "config",
Usage: "path to relayer config file",
Required: true,
Required: false, // Can also set config via env var
EnvVars: []string{"STANDARD_BRIDGE_RELAYER_CONFIG"},
}
)
Expand All @@ -53,28 +53,51 @@ func main() {
}
}

func loadConfigFromEnv() config {
cfg := config{
PrivKeyFilePath: os.Getenv("PRIVATE_KEY_FILE_PATH"),
LogLevel: os.Getenv("LOG_LEVEL"),
L1RPCUrl: os.Getenv("L1_RPC_URL"),
SettlementRPCUrl: os.Getenv("SETTLEMENT_RPC_URL"),
L1ContractAddr: os.Getenv("L1_CONTRACT_ADDR"),
SettlementContractAddr: os.Getenv("SETTLEMENT_CONTRACT_ADDR"),
}
return cfg
}

func loadConfigFromFile(cfg *config, filePath string) error {
buf, err := os.ReadFile(filePath)
if err != nil {
return fmt.Errorf("failed to read config file at: %s, %w", filePath, err)
}
if err := yaml.Unmarshal(buf, cfg); err != nil {
return fmt.Errorf("failed to unmarshal config file at: %s, %w", filePath, err)
}
return nil
}

func setupLogging(logLevel string) {
lvl, err := zerolog.ParseLevel(logLevel)
if err != nil {
log.Fatal().Err(err).Msg("failed to parse log level")
}
zerolog.SetGlobalLevel(lvl)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}

type config struct {
PrivKeyFile string `yaml:"priv_key_file" json:"priv_key_file"`
HTTPPort int `yaml:"http_port" json:"http_port"`
PrivKeyFilePath string `yaml:"priv_key_file_path" json:"priv_key_file_path"`
LogLevel string `yaml:"log_level" json:"log_level"`
L1RPCUrl string `yaml:"l1_rpc_url" json:"l1_rpc_url"`
SettlementRPCUrl string `yaml:"settlement_rpc_url" json:"settlement_rpc_url"`
L1ContractAddr string `yaml:"l1_contract_addr" json:"l1_contract_addr"`
SettlementContractAddr string `yaml:"settlement_contract_addr" json:"settlement_contract_addr"`
PgHost string `yaml:"pg_host" json:"pg_host"`
PgPort int `yaml:"pg_port" json:"pg_port"`
PgUser string `yaml:"pg_user" json:"pg_user"`
PgPassword string `yaml:"pg_password" json:"pg_password"`
PgDbname string `yaml:"pg_dbname" json:"pg_dbname"`
}

func checkConfig(cfg *config) error {
if cfg.PrivKeyFile == "" {
return fmt.Errorf("priv_key_file is required")
}

if cfg.HTTPPort == 0 {
cfg.HTTPPort = defaultHTTPPort
if cfg.PrivKeyFilePath == "" {
return fmt.Errorf("priv_key_file_path is required")
}

if cfg.LogLevel == "" {
Expand All @@ -97,67 +120,50 @@ func checkConfig(cfg *config) error {
return fmt.Errorf("preconf_contract_addr is required")
}

if cfg.PgHost == "" || cfg.PgPort == 0 || cfg.PgUser == "" || cfg.PgPassword == "" || cfg.PgDbname == "" {
return fmt.Errorf("pg_host, pg_port, pg_user, pg_password, pg_dbname are required")
}

return nil
}

func start(c *cli.Context) error {
cfg := loadConfigFromEnv()

configFilePath := c.String(optionConfig.Name)

var cfg config
buf, err := os.ReadFile(configFilePath)
if err != nil {
log.Fatal().Err(err).Msg("failed to read config file at: " + configFilePath)
}

if err := yaml.Unmarshal(buf, &cfg); err != nil {
log.Fatal().Err(err).Msg("failed to unmarshal config file at: " + configFilePath)
if configFilePath == "" {
log.Info().Msg("env var config will be used")
} else {
log.Info().Str("config_file", configFilePath).Msg(
"overriding env var config with file")
if err := loadConfigFromFile(&cfg, configFilePath); err != nil {
log.Fatal().Err(err).Msg("failed to load config provided as file")
}
}

if err := checkConfig(&cfg); err != nil {
log.Fatal().Err(err).Msg("invalid config")
}

lvl, err := zerolog.ParseLevel(cfg.LogLevel)
if err != nil {
log.Fatal().Err(err).Msg("failed to parse log level")
}

zerolog.SetGlobalLevel(lvl)
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
setupLogging(cfg.LogLevel)

privKeyFile := cfg.PrivKeyFile
privKeyFilePath := cfg.PrivKeyFilePath

if strings.HasPrefix(privKeyFile, "~/") {
if strings.HasPrefix(privKeyFilePath, "~/") {
homeDir, err := os.UserHomeDir()
if err != nil {
log.Err(err).Msg("failed to get user home dir")
}
privKeyFile = filepath.Join(homeDir, privKeyFile[2:])
privKeyFilePath = filepath.Join(homeDir, privKeyFilePath[2:])
}

privKey, err := crypto.LoadECDSA(privKeyFile)
privKey, err := crypto.LoadECDSA(privKeyFilePath)
if err != nil {
log.Err(err).Msg("failed to load private key")
}

r := relayer.NewRelayer(&relayer.Options{
PrivateKey: privKey,
HTTPPort: cfg.HTTPPort,
L1RPCUrl: cfg.L1RPCUrl,
SettlementRPCUrl: cfg.SettlementRPCUrl,
L1ContractAddr: common.HexToAddress(cfg.L1ContractAddr),
SettlementContractAddr: common.HexToAddress(cfg.SettlementContractAddr),
PgHost: cfg.PgHost,
PgPort: cfg.PgPort,
PgUser: cfg.PgUser,
PgPassword: cfg.PgPassword,
PgDbname: cfg.PgDbname,
})

interruptSigChan := make(chan os.Signal, 1)
Expand Down
8 changes: 1 addition & 7 deletions standard/bridge-v1/example_config/relayer_config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
priv_key_file: "example_config/relayer_key" # Relative path working dir
http_port: 8080
priv_key_file_path: "example_config/relayer_key"
log_level: "debug"
l1_rpc_url: "http://l1-bootnode:8545"
settlement_rpc_url: "http://sl-bootnode:8545"
l1_contract_addr: "0x1a18dfEc4f2B66207b1Ad30aB5c7A0d62Ef4A40b"
settlement_contract_addr: "0xc1f93bE11D7472c9B9a4d87B41dD0a491F1fbc75"
pg_host: "localhost"
pg_port: 5432
pg_user: "user"
pg_password: "password"
pg_dbname: "dbname"
42 changes: 0 additions & 42 deletions standard/bridge-v1/pkg/relayer/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,10 @@ import (

type Options struct {
PrivateKey *ecdsa.PrivateKey
HTTPPort int
SettlementRPCUrl string
L1RPCUrl string
L1ContractAddr common.Address
SettlementContractAddr common.Address
PgHost string
PgPort int
PgUser string
PgPassword string
PgDbname string
}

type Relayer struct {
Expand All @@ -38,22 +32,8 @@ type Relayer struct {
}

func NewRelayer(opts *Options) *Relayer {

r := &Relayer{}

// TODO: db

// db, err := initDB(opts)
// if err != nil {
// log.Fatal("failed to init db", err)
// }
// r.db = db

// st, err := store.NewStore(db)
// if err != nil {
// log.Fatal("failed to init store", err)
// }

pubKey := &opts.PrivateKey.PublicKey
pubKeyBytes := crypto.FromECDSAPub(pubKey)
hash := sha3.NewLegacyKeccak256()
Expand Down Expand Up @@ -180,25 +160,3 @@ func (r *Relayer) TryCloseAll() (err error) {
return errors.New(msg)
}
}

// func initDB(opts *Options) (db *sql.DB, err error) {
// // Connection string
// psqlInfo := fmt.Sprintf(
// "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
// opts.PgHost, opts.PgPort, opts.PgUser, opts.PgPassword, opts.PgDbname,
// )

// // Open a connection
// db, err = sql.Open("postgres", psqlInfo)
// if err != nil {
// return nil, err
// }

// // Check the connection
// err = db.Ping()
// if err != nil {
// return nil, err
// }

// return db, err
// }

0 comments on commit ae1dceb

Please sign in to comment.