Skip to content

Commit

Permalink
ctx, port, and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
boojamya committed Mar 4, 2024
1 parent 9775f7e commit e0370b4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 40 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ Installation
```shell
git clone https://github.com/strangelove-ventures/noble-cctp-relayer
cd noble-cctp-relayer
go install
make install
```

Running the relayer
```shell
noble-cctp-relayer start --config ./config/sample-app-config.yaml
```
Sample configs can be found in config/.
Sample configs can be found in [config](config).
### Promethius Metrics

By default, metrics are exported at on port :2112/metrics (`http://localhost:2112/metrics`). You can customize the port using the `--metrics-port` flag.

| **Exported Metric** | **Description** | **Type** |
|-----------------------------|------------------------------------------------------------------------------------------------------------------------------------------|----------|
| cctp_relayer_wallet_balance | Current balance of a relayer wallet in Wei. Noble balances are not currently exported. MsgReceiveMessage is free to submit on Noble. | Gauge |


### API
Simple API to query message state cache
```shell
Expand Down Expand Up @@ -50,7 +59,3 @@ abigen --abi ethereum/abi/MessageTransmitter.json --pkg contracts- --type Messag

### Useful links
[Goerli USDC faucet](https://usdcfaucet.com/)

[Goerli ETH faucet](https://goerlifaucet.com/)


3 changes: 1 addition & 2 deletions cmd/appstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type AppState struct {

ConfigPath string

// Depreciated in favor of LogLevel
Debug bool

LogLevel string
Expand Down Expand Up @@ -48,7 +47,7 @@ func (a *AppState) InitLogger() {
level = zerolog.ErrorLevel
}

// a.Debug is Depreciatred!
// a.Debug ovverrides a.loglevel
if a.Debug {
a.Logger = log.NewLogger(os.Stdout, log.LevelOption(zerolog.DebugLevel))
} else {
Expand Down
16 changes: 9 additions & 7 deletions cmd/flags.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

const (
flagConfigPath = "config"
// depreciated
flagVerbose = "verbose"
flagLogLevel = "log-level"
flagJSON = "json"
flagConfigPath = "config"
flagVerbose = "verbose"
flagLogLevel = "log-level"
flagJSON = "json"
flagMetricsPort = "metrics-port"
)

func addAppPersistantFlags(cmd *cobra.Command, a *AppState) *cobra.Command {
cmd.PersistentFlags().StringVar(&a.ConfigPath, flagConfigPath, defaultConfigPath, "file path of config file")
cmd.PersistentFlags().BoolVarP(&a.Debug, flagVerbose, "v", false, "use this flag to set log level to `debug`")
cmd.PersistentFlags().MarkDeprecated(flagVerbose, "depericated")
cmd.PersistentFlags().BoolVarP(&a.Debug, flagVerbose, "v", false, fmt.Sprintf("use this flag to set log level to `debug` (overrides %s flag)", flagLogLevel))
cmd.PersistentFlags().StringVar(&a.LogLevel, flagLogLevel, "info", "log level (debug, info, warn, error)")
cmd.PersistentFlags().Int16P(flagMetricsPort, "p", 2112, "customize Prometheus metrics port")
return cmd

}
Expand Down
9 changes: 7 additions & 2 deletions cmd/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ func Start(a *AppState) *cobra.Command {

registeredDomains := make(map[types.Domain]types.Chain)

metrics := relayer.InitPromMetrics()
port, err := cmd.Flags().GetInt16(flagMetricsPort)
if err != nil {
logger.Error("Invalid port", "error", err)
}

metrics := relayer.InitPromMetrics(port)

for name, cfg := range cfg.Chains {
c, err := cfg.Chain(name)
Expand All @@ -58,7 +63,7 @@ func Start(a *AppState) *cobra.Command {
}

if err := c.InitializeBroadcaster(cmd.Context(), logger, sequenceMap); err != nil {
logger.Error("Error initializing broadcaster", "err: ", err)
logger.Error("Error initializing broadcaster", "error", err)
os.Exit(1)
}

Expand Down
56 changes: 35 additions & 21 deletions ethereum/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,38 +134,52 @@ func (e *Ethereum) WalletBalanceMetric(ctx context.Context, logger log.Logger, m
logger = logger.With("metric", "wallet blance", "chain", e.name, "domain", e.domain)
queryRate := 30 // seconds

// report balance right away, then query bal every `queryRate`
firstTime := true
createClient := true

var err error
client, _ := ethclient.DialContext(ctx, e.rpcURL)
var client *ethclient.Client

for {
if !firstTime {
time.Sleep(time.Duration(queryRate) * time.Second)
firstTime = false
defer func() {
if client != nil {
client.Close()
}
if createClient {
client, err = ethclient.DialContext(ctx, e.rpcURL)
}()

for {
select {
case <-ctx.Done():
return

default:

if !firstTime {
time.Sleep(time.Duration(queryRate) * time.Second)
firstTime = false
}
if createClient {
client, err = ethclient.DialContext(ctx, e.rpcURL)
if err != nil {
logger.Info(fmt.Sprintf("error dialing eth client. Will try again in %d sec", queryRate), "error", err)
createClient = true
continue
}
}

account := common.HexToAddress(e.minterAddress)
balance, err := client.BalanceAt(ctx, account, nil)
if err != nil {
logger.Info(fmt.Sprintf("error dialing eth client. Will try again in %d sec", queryRate), "error", err)
logger.Info(fmt.Sprintf("error querying balance. Will try again in %d sec", queryRate), "error", err)
createClient = true
continue
}
}
floatBalance := new(big.Float).SetInt(balance)
float64Value, _ := floatBalance.Float64()

account := common.HexToAddress(e.minterAddress)
balance, err := client.BalanceAt(ctx, account, nil)
if err != nil {
logger.Info(fmt.Sprintf("error querying balance. Will try again in %d sec", queryRate), "error", err)
createClient = true
continue
}
floatBalance := new(big.Float).SetInt(balance)
float64Value, _ := floatBalance.Float64()
m.SetWalletBalance(e.name, e.minterAddress, float64Value)

m.SetWalletBalance(e.name, e.minterAddress, float64Value)

createClient = false
createClient = false
}
}
}
5 changes: 3 additions & 2 deletions relayer/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package relayer

import (
"fmt"
"log"
"net/http"

Expand All @@ -12,7 +13,7 @@ type PromMetrics struct {
WalletBalance *prometheus.GaugeVec
}

func InitPromMetrics() *PromMetrics {
func InitPromMetrics(port int16) *PromMetrics {
reg := prometheus.NewRegistry()

// labels
Expand All @@ -32,7 +33,7 @@ func InitPromMetrics() *PromMetrics {
// Expose /metrics HTTP endpoint
go func() {
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Fatal(http.ListenAndServe(":2112", nil))
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
}()

return m
Expand Down

0 comments on commit e0370b4

Please sign in to comment.