From 4d99b9f7468e5ce9883b51fd4a407afc6a5f1695 Mon Sep 17 00:00:00 2001 From: K-KAD <54041472+k-kaddal@users.noreply.github.com> Date: Tue, 25 Jun 2024 15:58:54 +0100 Subject: [PATCH] feat(relayer): adding a prometheus guage for relayer balance (#17659) Co-authored-by: jeff <113397187+cyberhorsey@users.noreply.github.com> Co-authored-by: xiaodino --- packages/relayer/pkg/mock/eth_client.go | 7 +++++ packages/relayer/processor/process_message.go | 30 +++++++++++++++++-- packages/relayer/processor/processor.go | 1 + packages/relayer/prometheus.go | 4 +++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/relayer/pkg/mock/eth_client.go b/packages/relayer/pkg/mock/eth_client.go index 8ff3330cac7..ecd08571fca 100644 --- a/packages/relayer/pkg/mock/eth_client.go +++ b/packages/relayer/pkg/mock/eth_client.go @@ -142,3 +142,10 @@ func (c *EthClient) SubscribeNewHead(ctx context.Context, ch chan<- *types.Heade func (c *EthClient) TransactionSender(ctx context.Context, tx *types.Transaction, blockHash common.Hash, txIndex uint) (common.Address, error) { return common.Address{}, nil } + +func (c *EthClient) BalanceAt( + ctx context.Context, + account common.Address, + blockNumber *big.Int) (*big.Int, error) { + return big.NewInt(100), nil +} diff --git a/packages/relayer/processor/process_message.go b/packages/relayer/processor/process_message.go index cb33d88d704..e4947b83813 100644 --- a/packages/relayer/processor/process_message.go +++ b/packages/relayer/processor/process_message.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "log/slog" + "math" "math/big" "strings" "time" @@ -394,6 +395,8 @@ func (p *Processor) sendProcessMessageCall( event *bridge.BridgeMessageSent, proof []byte, ) (*types.Receipt, error) { + defer p.logRelayerBalance(ctx) + received, err := p.destBridge.IsMessageReceived(nil, event.Message, proof) if err != nil { return nil, err @@ -447,9 +450,8 @@ func (p *Processor) sendProcessMessageCall( return nil, relayer.ErrUnprofitable } - // now simulate the transaction and lets confirm - // it is profitable + // now simulate the transaction and lets confirm it is profitable auth, err := bind.NewKeyedTransactorWithChainID(p.ecdsaKey, p.destChainId) if err != nil { return nil, err @@ -497,7 +499,6 @@ func (p *Processor) sendProcessMessageCall( uint64(event.Message.GasLimit), ) { slog.Error("can not process message after waiting for confirmations", "err", errUnprocessable) - return nil, errUnprocessable } @@ -553,6 +554,29 @@ func (p *Processor) sendProcessMessageCall( return receipt, nil } +// retrieve the balance of the relayer and set Prometheus +func (p *Processor) logRelayerBalance(ctx context.Context) { + balance, err := p.destEthClient.BalanceAt(ctx, p.relayerAddr, nil) + if err != nil { + slog.Warn("Failed to retrieve relayer balance", "error", err) + return + } + + balanceFloat := new(big.Float).SetInt(balance) + balanceEth := new(big.Float).Quo( + balanceFloat, + big.NewFloat(math.Pow10(18)), + ) + + slog.Info("Relayer balance", + "relayerAddress", p.relayerAddr, + "balance", balanceEth.Text('f', 18), + ) + + balanceEthFloat, _ := balanceEth.Float64() + relayer.RelayerKeyBalanceGauge.Set(balanceEthFloat) +} + // saveMessageStatusChangedEvent writes the MessageStatusChanged event to the // database after a message is processed func (p *Processor) saveMessageStatusChangedEvent( diff --git a/packages/relayer/processor/processor.go b/packages/relayer/processor/processor.go index 951da0e8b87..43302583357 100644 --- a/packages/relayer/processor/processor.go +++ b/packages/relayer/processor/processor.go @@ -60,6 +60,7 @@ type ethClient interface { ChainID(ctx context.Context) (*big.Int, error) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) (ethereum.Subscription, error) EstimateGas(ctx context.Context, msg ethereum.CallMsg) (uint64, error) + BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) } // hop is a struct which needs to be created based on the config parameters diff --git a/packages/relayer/prometheus.go b/packages/relayer/prometheus.go index 67f8b64f7cf..a586bd84c2d 100644 --- a/packages/relayer/prometheus.go +++ b/packages/relayer/prometheus.go @@ -146,4 +146,8 @@ var ( Name: "message_processed_events_after_retry_error_count", Help: "The total number of errors logged for MessageProcessed events after retries", }) + RelayerKeyBalanceGauge = promauto.NewGauge(prometheus.GaugeOpts{ + Name: "relayer_key_balance", + Help: "Current balance of the relayer key", + }) )