0xfirefist 1 год назад
Родитель
Сommit
6e672be3a4
2 измененных файлов с 66 добавлено и 6 удалено
  1. 50 2
      apps/fortuna/src/command/run.rs
  2. 16 4
      apps/fortuna/src/metrics.rs

+ 50 - 2
apps/fortuna/src/command/run.rs

@@ -29,7 +29,18 @@ use {
         Result,
     },
     axum::Router,
-    ethers::types::Address,
+    ethers::{
+        middleware::Middleware,
+        providers::{
+            Http,
+            Provider,
+        },
+        signers::{
+            LocalWallet,
+            Signer,
+        },
+        types::Address,
+    },
     std::{
         collections::HashMap,
         net::SocketAddr,
@@ -230,12 +241,20 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
     let metrics_registry = Arc::new(metrics::Metrics::new());
 
     if let Some(keeper_private_key) = opts.load_keeper_private_key()? {
+        let keeper_address = keeper_private_key.parse::<LocalWallet>()?.address();
+
         spawn(run_keeper(
             chains.clone(),
             config.clone(),
             keeper_private_key,
             metrics_registry.clone(),
         ));
+
+        spawn(track_balance(
+            config.clone(),
+            keeper_address,
+            metrics_registry.clone(),
+        ));
     }
 
     spawn(track_hashchain(
@@ -243,12 +262,41 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
         opts.provider.clone(),
         metrics_registry.clone(),
     ));
-
     run_api(opts.addr.clone(), chains, metrics_registry.clone(), rx_exit).await?;
 
     Ok(())
 }
 
+pub async fn track_balance(
+    config: Config,
+    keeper_address: Address,
+    metrics_registry: Arc<metrics::Metrics>,
+) {
+    loop {
+        for (chain_id, chain_config) in &config.chains {
+            let provider = match Provider::<Http>::try_from(&chain_config.geth_rpc_addr) {
+                Ok(r) => r,
+                Err(_e) => continue,
+            };
+
+            let balance = match provider.get_balance(keeper_address, None).await {
+                Ok(r) => r.as_u128(),
+                Err(_e) => continue,
+            };
+            let balance = balance as f64 / 1e18;
+
+            metrics_registry
+                .balance
+                .get_or_create(&ProviderLabel {
+                    chain_id: chain_id.clone(),
+                    address:  keeper_address.to_string(),
+                })
+                // comment on why is this ok
+                .set(balance);
+        }
+    }
+}
+
 
 pub async fn track_hashchain(
     config: Config,

+ 16 - 4
apps/fortuna/src/metrics.rs

@@ -9,6 +9,7 @@ use {
         },
         registry::Registry,
     },
+    std::sync::atomic::AtomicU64,
     tokio::sync::RwLock,
 };
 
@@ -36,10 +37,7 @@ pub struct Metrics {
 
     pub current_sequence_number: Family<ProviderLabel, Gauge>,
     pub end_sequence_number:     Family<ProviderLabel, Gauge>,
-    // pub balance:           Family<Label, Gauge>,
-    // pub balance_threshold: Family<Label, Gauge>,
-    //
-    // pub rpc: Family<Label, Counter>,
+    pub balance:                 Family<ProviderLabel, Gauge<f64, AtomicU64>>,
     //
     pub requests:                Family<ProviderLabel, Counter>,
     pub requests_processed:      Family<ProviderLabel, Counter>,
@@ -48,6 +46,10 @@ pub struct Metrics {
     // why?
     // - it is not a value that increases or decreases over time. Not a counter or a gauge
     // - it can't fit in a histogram too. logging and then collecting it is better.
+    // NOTE: rpc is not part of metrics.
+    // why?
+    // - which metric type should we use to track it?
+    // - let's just use fetched latest safe block from logs
 }
 
 impl Metrics {
@@ -108,6 +110,15 @@ impl Metrics {
             reveals.clone(),
         );
 
+        let balance = Family::<ProviderLabel, Gauge<f64, AtomicU64>>::default();
+        metrics_registry.register(
+            // With the metric name.
+            "balance",
+            // And the metric help text.
+            "Balance of the keeper",
+            balance.clone(),
+        );
+
         Metrics {
             registry: RwLock::new(metrics_registry),
             request_counter: http_requests,
@@ -116,6 +127,7 @@ impl Metrics {
             requests,
             requests_processed,
             reveals,
+            balance,
         }
     }
 }