Skip to content

Commit

Permalink
adjustments to reuse code from getrawtransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoplg committed Dec 13, 2024
1 parent d2f968f commit e4f8752
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 49 deletions.
35 changes: 13 additions & 22 deletions zebra-rpc/src/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,15 +852,20 @@ where
.transactions
.iter()
.map(|tx| {
TransactionObject::new(
tx.clone(),
height,
confirmations
.try_into()
.expect("should be less than max block height, i32::MAX"),
)
let GetRawTransaction::Object(tx_obj) =
GetRawTransaction::from_transaction(
tx.clone(),
Some(height),
confirmations
.try_into()
.expect("should be less than max block height, i32::MAX"),
true,
)
else {
unreachable!("an Object must be returned when verbose is true");
};
GetBlockTransaction::Object(tx_obj)
})
.map(GetBlockTransaction::Object)
.collect(),
_ => unreachable!("unmatched response to a transaction_ids_for_block request"),
};
Expand Down Expand Up @@ -2041,20 +2046,6 @@ pub struct TransactionObject {
// TODO: many fields not yet supported
}

impl TransactionObject {
/// Creates a new [`TransactionObject`].
fn new(tx: Arc<Transaction>, height: block::Height, confirmations: u32) -> Self {
TransactionObject {
hex: tx.into(),
height: height
.0
.try_into()
.expect("valid block heights are limited to i32::MAX"),
confirmations,
}
}
}

impl Default for TransactionObject {
fn default() -> Self {
Self {
Expand Down
35 changes: 8 additions & 27 deletions zebra-rpc/src/methods/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,31 +242,12 @@ async fn rpc_getblock() {
);
}

// With verbosity=2, the RPC calls getrawtransaction which queries the
// mempool, which we need to mock since we used a MockService for it. This
// function returns a future that mocks that query. This is similar to what
// we use in the getrawtransaction test, but here we don't bother checking
// if the request is correct.
let make_mempool_req = || {
let mut mempool = mempool.clone();
async move {
mempool
.expect_request_that(|_request| true)
.await
.respond(mempool::Response::Transactions(vec![]));
}
};

// Make height calls with verbosity=2 and check response
for (i, block) in blocks.iter().enumerate() {
let get_block_req = rpc.get_block(i.to_string(), Some(2u8));

// Run both the getblock request and the mocked mempool request.
// This assumes a single mempool query, i.e. that there is a single
// transaction the block. If the test vectors changes and the block
// has more than one transaction, this will need to be adjusted.
let (response, _) = futures::join!(get_block_req, make_mempool_req());
let get_block = response.expect("We should have a GetBlock struct");
let get_block = rpc
.get_block(i.to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");

let (expected_nonce, expected_final_sapling_root) =
get_block_data(&read_state, block.clone(), i).await;
Expand Down Expand Up @@ -310,10 +291,10 @@ async fn rpc_getblock() {

// Make hash calls with verbosity=2 and check response
for (i, block) in blocks.iter().enumerate() {
let get_block_req = rpc.get_block(blocks[i].hash().to_string(), Some(2u8));

let (response, _) = futures::join!(get_block_req, make_mempool_req());
let get_block = response.expect("We should have a GetBlock struct");
let get_block = rpc
.get_block(blocks[i].hash().to_string(), Some(2u8))
.await
.expect("We should have a GetBlock struct");

let (expected_nonce, expected_final_sapling_root) =
get_block_data(&read_state, block.clone(), i).await;
Expand Down

0 comments on commit e4f8752

Please sign in to comment.