Skip to content

Commit

Permalink
Merge pull request #1475 from fluidvanadium/handle_start_mempool_monitor
Browse files Browse the repository at this point in the history
Handle start mempool monitor
  • Loading branch information
zancas authored Oct 31, 2024
2 parents c260385 + 395c880 commit b6ce97d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ mod fast {
.await
.unwrap();

LightClient::start_mempool_monitor(recipient.clone());
LightClient::start_mempool_monitor(recipient.clone()).unwrap();
tokio::time::sleep(Duration::from_secs(5)).await;

let transactions = &recipient.transaction_summaries().await.0;
Expand Down Expand Up @@ -3446,7 +3446,7 @@ mod slow {
.await
.unwrap(),
);
LightClient::start_mempool_monitor(recipient_loaded.clone());
LightClient::start_mempool_monitor(recipient_loaded.clone()).unwrap();
// This seems to be long enough for the mempool monitor to kick in.
// One second is insufficient. Even if this fails, this can only ever be
// a false negative, giving us a balance of 100_000. Still, could be improved.
Expand Down
3 changes: 2 additions & 1 deletion zingocli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ pub fn command_loop(
let (resp_transmitter, resp_receiver) = channel::<String>();

std::thread::spawn(move || {
LightClient::start_mempool_monitor(lightclient.clone());
LightClient::start_mempool_monitor(lightclient.clone())
.expect("mempool monitor must start");

while let Ok((cmd, args)) = command_receiver.recv() {
let args: Vec<_> = args.iter().map(|s| s.as_ref()).collect();
Expand Down
41 changes: 34 additions & 7 deletions zingolib/src/lightclient/sync.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! TODO: Add Mod Description Here!
//! AWISOTT
//! LightClient sync stuff.
//! the difference between this and wallet/sync.rs is that these can interact with the network layer.
use futures::future::join_all;

Expand Down Expand Up @@ -43,6 +45,20 @@ use crate::{
wallet::{now, transaction_context::TransactionContext, utils::get_price},
};

#[allow(missing_docs)] // error types document themselves
#[derive(Debug, thiserror::Error)]
/// likely no errors here. but this makes clippy (and fv) happier
pub enum StartMempoolMonitorError {
#[error("Mempool Monitor is disabled.")]
Disabled,
#[error("could not read mempool monitor: {0}")]
CouldNotRead(String),
#[error("could not write mempool monitor: {0}")]
CouldNotWrite(String),
#[error("Mempool Monitor does not exist.")]
DoesNotExist,
}

impl LightClient {
/// TODO: Add Doc Comment Here!
pub async fn do_sync(&self, print_updates: bool) -> Result<SyncResult, String> {
Expand Down Expand Up @@ -150,14 +166,22 @@ impl LightClient {
*self.interrupt_sync.write().await = set_interrupt;
}

/// TODO: Add Doc Comment Here!
pub fn start_mempool_monitor(lc: Arc<LightClient>) {
/// a concurrent task
/// the mempool includes transactions waiting to be accepted to the chain
/// we query it through lightwalletd
/// and record any new data, using ConfirmationStatus::Mempool
pub fn start_mempool_monitor(lc: Arc<LightClient>) -> Result<(), StartMempoolMonitorError> {
if !lc.config.monitor_mempool {
return;
return Err(StartMempoolMonitorError::Disabled);
}

if lc.mempool_monitor.read().unwrap().is_some() {
return;
if lc
.mempool_monitor
.read()
.map_err(|e| StartMempoolMonitorError::CouldNotRead(e.to_string()))?
.is_some()
{
return Err(StartMempoolMonitorError::DoesNotExist);
}

let config = lc.config.clone();
Expand Down Expand Up @@ -278,7 +302,10 @@ impl LightClient {
});
});

*lc.mempool_monitor.write().unwrap() = Some(h);
*lc.mempool_monitor
.write()
.map_err(|e| StartMempoolMonitorError::CouldNotWrite(e.to_string()))? = Some(h);
Ok(())
}

/// Start syncing in batches with the max size, to manage memory consumption.
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/testutils/chain_generics/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ where
let check_mempool = !cfg!(feature = "ci");
if check_mempool {
for lightclient in [&ref_primary, &ref_secondary, &ref_tertiary] {
LightClient::start_mempool_monitor(lightclient.clone());
assert!(LightClient::start_mempool_monitor(lightclient.clone()).is_ok());
dbg!("mm started");
}
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
Expand Down

0 comments on commit b6ce97d

Please sign in to comment.