Skip to content

Commit

Permalink
feat(relayer): adding a prometheus guage for relayer balance (#17659)
Browse files Browse the repository at this point in the history
Co-authored-by: jeff <[email protected]>
Co-authored-by: xiaodino <[email protected]>
  • Loading branch information
3 people authored Jun 25, 2024
1 parent 03f686c commit 4d99b9f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/relayer/pkg/mock/eth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
30 changes: 27 additions & 3 deletions packages/relayer/processor/process_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"log/slog"
"math"
"math/big"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/relayer/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/relayer/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
)

0 comments on commit 4d99b9f

Please sign in to comment.