-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pcli: 💿 crate can be used as a library
currently, types and interfaces defined in the workspace's `pcli` crate are only available within the `pcli` binary itself. this means that the client functionality provided by this crate cannot be depended on as a library, or otherwise used in unit tests. this state of affairs is a significant part of why our tests for pcli are contained in the `crates/bin/pcli/tests/network_integration.rs` test suite, which rely on shelling out via a `std::process::Command` wrapper and invoking the compiled `pcli` binary. to facilitate future unit testing, this commit introduces a `lib.rs` file to the `pcli` crate. the `App` structure is moved there as part of this change, but is not changed in this commit.
- Loading branch information
Showing
2 changed files
with
82 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#![deny(clippy::unwrap_used)] | ||
#![allow(clippy::clone_on_copy)] | ||
|
||
use { | ||
crate::{command::*, config::PcliConfig}, | ||
anyhow::Result, | ||
futures::StreamExt, | ||
penumbra_proto::{ | ||
box_grpc_svc::BoxGrpcService, custody::v1::custody_service_client::CustodyServiceClient, | ||
view::v1::view_service_client::ViewServiceClient, | ||
}, | ||
penumbra_view::ViewClient, | ||
}; | ||
|
||
pub mod command; | ||
pub mod config; | ||
pub mod opt; | ||
pub mod warning; | ||
|
||
mod dex_utils; | ||
mod network; | ||
mod terminal; | ||
mod transaction_view_ext; | ||
|
||
const CONFIG_FILE_NAME: &str = "config.toml"; | ||
const VIEW_FILE_NAME: &str = "pcli-view.sqlite"; | ||
|
||
#[derive(Debug)] | ||
pub struct App { | ||
/// view will be `None` when a command indicates that it can be run offline via | ||
/// `.offline()` and Some(_) otherwise. Assuming `.offline()` has been implemenented | ||
/// correctly, this can be unwrapped safely. | ||
pub view: Option<ViewServiceClient<BoxGrpcService>>, | ||
pub custody: CustodyServiceClient<BoxGrpcService>, | ||
pub governance_custody: CustodyServiceClient<BoxGrpcService>, | ||
pub config: PcliConfig, | ||
} | ||
|
||
impl App { | ||
pub fn view(&mut self) -> &mut impl ViewClient { | ||
self.view.as_mut().expect("view service initialized") | ||
} | ||
|
||
pub async fn sync(&mut self) -> Result<()> { | ||
let mut status_stream = | ||
ViewClient::status_stream(self.view.as_mut().expect("view service initialized")) | ||
.await?; | ||
|
||
// Pull out the first message from the stream, which has the current state, and use | ||
// it to set up a progress bar. | ||
let initial_status = status_stream | ||
.next() | ||
.await | ||
.transpose()? | ||
.ok_or_else(|| anyhow::anyhow!("view service did not report sync status"))?; | ||
|
||
eprintln!( | ||
"Scanning blocks from last sync height {} to latest height {}", | ||
initial_status.full_sync_height, initial_status.latest_known_block_height, | ||
); | ||
|
||
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle}; | ||
let progress_bar = ProgressBar::with_draw_target( | ||
initial_status.latest_known_block_height - initial_status.full_sync_height, | ||
ProgressDrawTarget::stdout(), | ||
) | ||
.with_style( | ||
ProgressStyle::default_bar() | ||
.template("[{elapsed}] {bar:50.cyan/blue} {pos:>7}/{len:7} {per_sec} ETA: {eta}"), | ||
); | ||
progress_bar.set_position(0); | ||
|
||
while let Some(status) = status_stream.next().await.transpose()? { | ||
progress_bar.set_position(status.full_sync_height - initial_status.full_sync_height); | ||
} | ||
progress_bar.finish(); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters