Skip to content

Commit

Permalink
Respond to inbound TransactionsById with mempool content (#2725)
Browse files Browse the repository at this point in the history
* reply to inbound `TransactionsById` requests

* apply style/redability suggestions and fix typo

Co-authored-by: Deirdre Connolly <[email protected]>

Co-authored-by: Deirdre Connolly <[email protected]>
Co-authored-by: Deirdre Connolly <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2021
1 parent 8e65e82 commit 65e308d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
18 changes: 8 additions & 10 deletions zebrad/src/components/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,14 @@ impl Service<zn::Request> for Inbound {
.map_ok(zn::Response::Blocks)
.boxed()
}
zn::Request::TransactionsById(_transactions) => {
// `zcashd` returns a list of found transactions, followed by a
// `NotFound` message if any transactions are missing. `zcashd`
// says that Simplified Payment Verification (SPV) clients rely on
// this behaviour - are there any of them on the Zcash network?
// https://github.com/zcash/zcash/blob/e7b425298f6d9a54810cb7183f00be547e4d9415/src/main.cpp#L5632
// We'll implement this request once we have a mempool:
// https://en.bitcoin.it/wiki/Protocol_documentation#getdata
debug!("ignoring unimplemented request");
async { Ok(zn::Response::Nil) }.boxed()
zn::Request::TransactionsById(transactions) => {
let request = mempool::Request::TransactionsById(transactions);
self.mempool.clone().oneshot(request).map_ok(|resp| match resp {
mempool::Response::Transactions(transactions) => zn::Response::Transactions(transactions),
_ => unreachable!("Mempool component should always respond to a `TransactionsById` request with a `Transactions` response"),
})
.boxed()

}
zn::Request::FindBlocks { known_blocks, stop } => {
let request = zs::Request::FindBlockHashes { known_blocks, stop };
Expand Down
30 changes: 22 additions & 8 deletions zebrad/src/components/inbound/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use tower::ServiceExt;
use std::collections::HashSet;

use super::mempool::{unmined_transactions_in_blocks, Mempool};

use tokio::sync::oneshot;
use tower::builder::ServiceBuilder;
use tower::{builder::ServiceBuilder, ServiceExt};

use zebra_chain::{
parameters::Network,
Expand All @@ -14,7 +14,7 @@ use zebra_network::{Request, Response};
use zebra_state::Config as StateConfig;

#[tokio::test]
async fn mempool_requests_for_transaction_ids() {
async fn mempool_requests_for_transactions() {
let network = Network::Mainnet;
let consensus_config = ConsensusConfig::default();
let state_config = StateConfig::ephemeral();
Expand All @@ -23,11 +23,8 @@ async fn mempool_requests_for_transaction_ids() {
let state_service = ServiceBuilder::new().buffer(1).service(state);
let mut mempool_service = Mempool::new(network);

let added_transaction_ids: Vec<UnminedTxId> =
add_some_stuff_to_mempool(&mut mempool_service, network)
.iter()
.map(|t| t.id)
.collect();
let added_transactions = add_some_stuff_to_mempool(&mut mempool_service, network);
let added_transaction_ids: Vec<UnminedTxId> = added_transactions.iter().map(|t| t.id).collect();

let (block_verifier, transaction_verifier) =
zebra_consensus::chain::init(consensus_config.clone(), network, state_service.clone())
Expand All @@ -45,7 +42,9 @@ async fn mempool_requests_for_transaction_ids() {
mempool_service,
));

// Test `Request::MempoolTransactionIds`
let request = inbound_service
.clone()
.oneshot(Request::MempoolTransactionIds)
.await;
match request {
Expand All @@ -54,6 +53,21 @@ async fn mempool_requests_for_transaction_ids() {
"`MempoolTransactionIds` requests should always respond `Ok(Vec<UnminedTxId>)`"
),
};

// Test `Request::TransactionsById`
let hash_set = added_transaction_ids
.iter()
.copied()
.collect::<HashSet<_>>();

let request = inbound_service
.oneshot(Request::TransactionsById(hash_set))
.await;

match request {
Ok(Response::Transactions(response)) => assert_eq!(response, added_transactions),
_ => unreachable!("`TransactionsById` requests should always respond `Ok(Vec<UnminedTx>)`"),
};
}

fn add_some_stuff_to_mempool(mempool_service: &mut Mempool, network: Network) -> Vec<UnminedTx> {
Expand Down

0 comments on commit 65e308d

Please sign in to comment.