avoid using async blocks to avoid lifetime bug with generators

This commit is contained in:
Jane Lusby 2021-01-27 14:44:22 -08:00 committed by teor
parent 685a592399
commit 4d6ef89248
1 changed files with 15 additions and 14 deletions

View File

@ -239,23 +239,24 @@ impl Service<zn::Request> for Inbound {
// //
// We can't use `call_all` here, because it leaks buffer slots: // We can't use `call_all` here, because it leaks buffer slots:
// https://github.com/tower-rs/tower/blob/master/tower/src/util/call_all/common.rs#L112 // https://github.com/tower-rs/tower/blob/master/tower/src/util/call_all/common.rs#L112
let mut state = self.state.clone(); use futures::stream::TryStreamExt;
async move { hashes
let mut blocks = Vec::new(); .into_iter()
for hash in hashes { .map(|hash| zs::Request::Block(hash.into()))
let request = zs::Request::Block(hash.into()); .map(|request| self.state.clone().oneshot(request))
// we can't use ServiceExt::oneshot here, due to lifetime issues .collect::<futures::stream::FuturesOrdered<_>>()
match state.ready_and().await?.call(request).await? { .try_filter_map(|response| async move {
zs::Response::Block(Some(block)) => blocks.push(block), Ok(match response {
zs::Response::Block(Some(block)) => Some(block),
// `zcashd` ignores missing blocks in GetData responses, // `zcashd` ignores missing blocks in GetData responses,
// rather than including them in a trailing `NotFound` // rather than including them in a trailing `NotFound`
// message // message
zs::Response::Block(None) => {} zs::Response::Block(None) => None,
_ => unreachable!("wrong response from state"), _ => unreachable!("wrong response from state"),
} })
} })
Ok(zn::Response::Blocks(blocks)) .try_collect::<Vec<_>>()
} .map_ok(zn::Response::Blocks)
.boxed() .boxed()
} }
zn::Request::TransactionsByHash(_transactions) => { zn::Request::TransactionsByHash(_transactions) => {