From 4d55f7f3aeed8cbdb62f45c7397bbaad8845cb9c Mon Sep 17 00:00:00 2001 From: Brian Balser Date: Wed, 16 Aug 2023 08:19:49 -0400 Subject: [PATCH] Change solana payers_balance function record metric if payers is configured to be monitored --- mobile_packet_verifier/src/burner.rs | 24 +++++++++---------- solana/src/lib.rs | 35 ++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/mobile_packet_verifier/src/burner.rs b/mobile_packet_verifier/src/burner.rs index 583911274..aadb5a9fe 100644 --- a/mobile_packet_verifier/src/burner.rs +++ b/mobile_packet_verifier/src/burner.rs @@ -81,8 +81,18 @@ where }, ) in payer_totals.into_iter() { - tracing::info!(%total_dcs, %payer, "Burning DC"); + let payer_balance = self + .solana + .payer_balance(&payer) + .await + .map_err(BurnError::SolanaError)?; + + if payer_balance < total_dcs { + tracing::warn!(%payer, %payer_balance, %total_dcs, "Payer does not have enough balance to burn dcs"); + continue; + } + tracing::info!(%total_dcs, %payer, "Burning DC"); if self .solana .burn_data_credits(&payer, total_dcs) @@ -123,18 +133,6 @@ where ) .await?; } - - // Fetch the balance after - - metrics::gauge!( - "balance", - self - .solana - .payer_balance(&payer) - .await - .map_err(BurnError::SolanaError)? as f64, - "payer" => payer.to_string() - ); } Ok(()) diff --git a/solana/src/lib.rs b/solana/src/lib.rs index aba0b2323..d4d2f83a0 100644 --- a/solana/src/lib.rs +++ b/solana/src/lib.rs @@ -17,8 +17,8 @@ use solana_sdk::{ signer::Signer, transaction::Transaction, }; -use std::collections::HashMap; use std::convert::Infallible; +use std::{collections::HashMap, str::FromStr}; use std::{ sync::Arc, time::{SystemTime, SystemTimeError}, @@ -43,7 +43,7 @@ pub enum SolanaRpcError { #[error("Solana rpc error: {0}")] RpcClientError(#[from] ClientError), #[error("Anchor error: {0}")] - AnchorError(#[from] anchor_lang::error::Error), + AnchorError(Box), #[error("Solana program error: {0}")] ProgramError(#[from] solana_sdk::program_error::ProgramError), #[error("Parse pubkey error: {0}")] @@ -54,6 +54,14 @@ pub enum SolanaRpcError { SystemTimeError(#[from] SystemTimeError), #[error("Failed to read keypair file")] FailedToReadKeypairError, + #[error("crypto error: {0}")] + Crypto(#[from] helium_crypto::Error), +} + +impl From for SolanaRpcError { + fn from(err: anchor_lang::error::Error) -> Self { + Self::AnchorError(Box::new(err)) + } } #[derive(Debug, Deserialize)] @@ -63,6 +71,18 @@ pub struct Settings { burn_keypair: String, dc_mint: String, dnt_mint: String, + #[serde(default)] + payers_to_monitor: Vec, +} + +impl Settings { + pub fn payers_to_monitor(&self) -> Result, SolanaRpcError> { + self.payers_to_monitor + .iter() + .map(|payer| PublicKeyBinary::from_str(payer)) + .collect::>() + .map_err(SolanaRpcError::from) + } } pub struct SolanaRpc { @@ -70,6 +90,7 @@ pub struct SolanaRpc { program_cache: BurnProgramCache, cluster: String, keypair: [u8; 64], + payers_to_monitor: Vec, } impl SolanaRpc { @@ -90,6 +111,7 @@ impl SolanaRpc { provider, program_cache, keypair: keypair.to_bytes(), + payers_to_monitor: settings.payers_to_monitor()?, })) } } @@ -110,6 +132,15 @@ impl SolanaNetwork for SolanaRpc { return Ok(0); }; let account_layout = spl_token::state::Account::unpack(account_data.as_slice())?; + + if self.payers_to_monitor.contains(payer) { + metrics::gauge!( + "balance", + account_layout.amount as f64, + "payer" => payer.to_string() + ); + } + Ok(account_layout.amount) }