From 0af76e57823a627a69935c8e4db356675687aa58 Mon Sep 17 00:00:00 2001 From: redshiftzero Date: Mon, 16 Sep 2024 13:14:45 -0400 Subject: [PATCH] pmonitor: get current `RateData` --- Cargo.lock | 1 + crates/bin/pmonitor/Cargo.toml | 1 + crates/bin/pmonitor/src/main.rs | 46 +++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b49a86662a..09eaaeb88b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5923,6 +5923,7 @@ dependencies = [ "serde_json", "tokio", "toml 0.7.8", + "tonic", "tracing", "url", ] diff --git a/crates/bin/pmonitor/Cargo.toml b/crates/bin/pmonitor/Cargo.toml index 53060d64cf..7e9d12cfd8 100644 --- a/crates/bin/pmonitor/Cargo.toml +++ b/crates/bin/pmonitor/Cargo.toml @@ -34,3 +34,4 @@ serde = {workspace = true, features = ["derive"]} serde_json = {workspace = true} futures = {workspace = true} pcli = { path = "../pcli" } +tonic = {workspace = true, features = ["tls-webpki-roots", "tls"]} diff --git a/crates/bin/pmonitor/src/main.rs b/crates/bin/pmonitor/src/main.rs index 928b1751db..1f737065a2 100644 --- a/crates/bin/pmonitor/src/main.rs +++ b/crates/bin/pmonitor/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use camino::Utf8PathBuf; use clap::{self, Parser}; use directories::ProjectDirs; @@ -7,6 +7,7 @@ use penumbra_asset::STAKING_TOKEN_ASSET_ID; use std::fs; use std::process::Command as ProcessCommand; use std::str::FromStr; +use tonic::transport::{Channel, ClientTlsConfig}; use url::Url; use pcli::config::PcliConfig; @@ -19,9 +20,10 @@ use penumbra_proto::view::v1::{ }; use penumbra_proto::{ core::component::compact_block::v1::CompactBlockRequest, + core::component::stake::v1::query_service_client::QueryServiceClient as StakeQueryServiceClient, penumbra::core::component::compact_block::v1::query_service_client::QueryServiceClient as CompactBlockQueryServiceClient, }; -use penumbra_stake::rate::{BaseRateData, RateData}; +use penumbra_stake::rate::RateData; use penumbra_stake::DelegationToken; use penumbra_view::{ViewClient, ViewServer}; @@ -161,6 +163,21 @@ impl Opt { compact_block.try_into() } + /// Stolen from pcli + pub async fn pd_channel(&self, grpc_url: Url) -> anyhow::Result { + match grpc_url.scheme() { + "http" => Ok(Channel::from_shared(grpc_url.to_string())? + .connect() + .await?), + "https" => Ok(Channel::from_shared(grpc_url.to_string())? + .tls_config(ClientTlsConfig::new())? + .connect() + .await?), + other => Err(anyhow::anyhow!("unknown url scheme {other}")) + .with_context(|| format!("could not connect to {}", grpc_url)), + } + } + /// Execute the specified command. pub async fn exec(&self) -> Result<()> { let opt = self; @@ -224,13 +241,6 @@ impl Opt { Ok(()) } Command::Audit {} => { - // todo: fix this - let dummy_base_rate = BaseRateData { - epoch_index: 0, - base_reward_rate: 0u128.into(), - base_exchange_rate: 1_0000_0000u128.into(), - }; - // Parse all the wallets to get the FVKs let mut fvks = Vec::new(); let mut configs = Vec::new(); @@ -253,6 +263,9 @@ impl Opt { .await?; let genesis_filtered_block = genesis::scan_genesis_block(genesis_compact_block, fvks).await?; + let mut stake_client = StakeQueryServiceClient::new( + self.pd_channel(configs[0].grpc_url.clone()).await?, + ); // Sync each wallet to the latest block height and check the balances. for (config, path) in configs.iter().zip(paths.iter()) { @@ -298,13 +311,14 @@ impl Opt { .sum::(); // We need to convert the amount to the UM-equivalent amount using the appropriate rate data - let dummy_rate_data = RateData { - identity_key: delegation_token.validator(), - validator_reward_rate: 0u128.into(), - validator_exchange_rate: dummy_base_rate.base_exchange_rate, - }; - let um_equivalent_balance = - dummy_rate_data.unbonded_amount(total_amount); + let rate_data: RateData = stake_client + .current_validator_rate(tonic::Request::new( + (delegation_token.validator()).into(), + )) + .await? + .into_inner() + .try_into()?; + let um_equivalent_balance = rate_data.unbonded_amount(total_amount); total_um_equivalent_amount += um_equivalent_balance; }; }