-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
pending_outputs
field on mempool Storage and responds to pendi…
…ng outputs requests when inserting new transactions into the mempool's verified set
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Pending [`transparent::Output`] tracker for [`AwaitOutput` requests](zebra_node_services::Mempool::Request::AwaitOutput). | ||
|
||
use std::{collections::HashMap, future::Future}; | ||
|
||
use tokio::sync::broadcast; | ||
|
||
use tower::BoxError; | ||
use zebra_chain::transparent; | ||
|
||
use zebra_node_services::mempool::Response; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct PendingOutputs(HashMap<transparent::OutPoint, broadcast::Sender<transparent::Output>>); | ||
|
||
impl PendingOutputs { | ||
/// Returns a future that will resolve to the `transparent::Output` pointed | ||
/// to by the given `transparent::OutPoint` when it is available. | ||
pub fn queue( | ||
&mut self, | ||
outpoint: transparent::OutPoint, | ||
) -> impl Future<Output = Result<Response, BoxError>> { | ||
let mut receiver = self | ||
.0 | ||
.entry(outpoint) | ||
.or_insert_with(|| { | ||
let (sender, _) = broadcast::channel(1); | ||
sender | ||
}) | ||
.subscribe(); | ||
|
||
async move { | ||
receiver | ||
.recv() | ||
.await | ||
.map(Some) | ||
.map(Response::UnspentOutput) | ||
.map_err(BoxError::from) | ||
} | ||
} | ||
|
||
/// Notify all requests waiting for the [`transparent::Output`] pointed to by | ||
/// the given [`transparent::OutPoint`] that the [`transparent::Output`] has | ||
/// arrived. | ||
#[inline] | ||
pub fn respond(&mut self, outpoint: &transparent::OutPoint, output: transparent::Output) { | ||
if let Some(sender) = self.0.remove(outpoint) { | ||
// Adding the outpoint as a field lets us cross-reference | ||
// with the trace of the verification that made the request. | ||
tracing::trace!(?outpoint, "found pending mempool output"); | ||
let _ = sender.send(output); | ||
} | ||
} | ||
|
||
/// Scan the set of waiting Output requests for channels where all receivers | ||
/// have been dropped and remove the corresponding sender. | ||
pub fn prune(&mut self) { | ||
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Check Cargo.lock is up to date
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Build and Deploy Zebra Internal Docs
Check failure on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Clippy (stable) Resultsmethods `prune` and `len` are never used
Check failure on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Clippy (stable) Resultsmethods `prune` and `len` are never used
Check failure on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Clippy (stable) Resultsmethods `prune` and `len` are never used
Check failure on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Build zebrad crate
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Install zebrad from lockfile without cache on ubuntu-latest
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / lightwalletd tip update / Create lwd-update-sync cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Zebra tip update / Create update-to-tip cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Zebra tip update / Create update-to-tip cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Zebra tip update / Create update-to-tip cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Zebra tip update / Create update-to-tip cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Zebra tip update / Create update-to-tip cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Generate checkpoints testnet / Create checkpoints-testnet cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Generate checkpoints testnet / Create checkpoints-testnet cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Generate checkpoints testnet / Create checkpoints-testnet cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Integration tests / Scan starts where left / Create scan-start-where-left cached state image
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Test beta on ubuntu-latest
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Test stable on ubuntu-latest
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Test stable on macos-latest
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Test stable on macos-latest
Check warning on line 56 in zebrad/src/components/mempool/pending_outputs.rs GitHub Actions / Test stable on windows-latest
|
||
self.0.retain(|_, chan| chan.receiver_count() > 0); | ||
} | ||
|
||
/// Returns the number of Outputs that are being waited on. | ||
pub fn len(&self) -> usize { | ||
self.0.len() | ||
} | ||
} |