Skip to content

Commit e730dac

Browse files
committed
feat: add batch_transaction_get_merkle
- adds the new batch method for `blockchain.transaction.get_merkle`.
1 parent 7de4cb7 commit e730dac

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

src/api.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ where
145145
(**self).transaction_get_merkle(txid, height)
146146
}
147147

148+
fn batch_transaction_get_merkle<'s, I>(
149+
&self,
150+
txids_and_heights: I,
151+
) -> Result<Vec<GetMerkleRes>, Error>
152+
where
153+
I: IntoIterator + Clone,
154+
I::Item: Borrow<&'s (Txid, usize)>,
155+
{
156+
(**self).batch_transaction_get_merkle(txids_and_heights)
157+
}
158+
148159
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
149160
(**self).txid_from_pos(height, tx_pos)
150161
}
@@ -362,6 +373,17 @@ pub trait ElectrumApi {
362373
/// Returns the merkle path for the transaction `txid` confirmed in the block at `height`.
363374
fn transaction_get_merkle(&self, txid: &Txid, height: usize) -> Result<GetMerkleRes, Error>;
364375

376+
/// Batch version of [`transaction_get_merkle`](#method.transaction_get_merkle).
377+
///
378+
/// Take a list of `(txid, height)`, for transactions with `txid` confirmed in the block at `height`.
379+
fn batch_transaction_get_merkle<'s, I>(
380+
&self,
381+
txids_and_heights: I,
382+
) -> Result<Vec<GetMerkleRes>, Error>
383+
where
384+
I: IntoIterator + Clone,
385+
I::Item: Borrow<&'s (Txid, usize)>;
386+
365387
/// Returns a transaction hash, given a block `height` and a `tx_pos` in the block.
366388
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error>;
367389

@@ -558,6 +580,17 @@ mod test {
558580
unreachable!()
559581
}
560582

583+
fn batch_transaction_get_merkle<'s, I>(
584+
&self,
585+
_: I,
586+
) -> Result<Vec<crate::GetMerkleRes>, crate::Error>
587+
where
588+
I: IntoIterator + Clone,
589+
I::Item: std::borrow::Borrow<&'s (bitcoin::Txid, usize)>,
590+
{
591+
unreachable!()
592+
}
593+
561594
fn txid_from_pos(&self, _: usize, _: usize) -> Result<bitcoin::Txid, super::Error> {
562595
unreachable!()
563596
}

src/batch.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ impl Batch {
6262
.push((String::from("blockchain.transaction.get"), params));
6363
}
6464

65+
/// Add one `blockchain.transaction.get_merkle` request to the batch queue
66+
pub fn transaction_get_merkle(&mut self, tx_hash_and_height: &(Txid, usize)) {
67+
let (tx_hash, height) = tx_hash_and_height;
68+
let params = vec![Param::String(format!("{:x}", tx_hash)), Param::Usize(*height)];
69+
self.calls
70+
.push((String::from("blockchain.transaction.get_merkle"), params));
71+
}
72+
6573
/// Add one `blockchain.estimatefee` request to the batch queue
6674
pub fn estimate_fee(&mut self, number: usize) {
6775
let params = vec![Param::Usize(number)];

src/client.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,22 @@ impl ElectrumApi for Client {
327327
impl_inner_call!(self, transaction_get_merkle, txid, height)
328328
}
329329

330+
#[inline]
331+
fn batch_transaction_get_merkle<'s, I>(
332+
&self,
333+
txids_and_heights: I,
334+
) -> Result<Vec<GetMerkleRes>, Error>
335+
where
336+
I: IntoIterator + Clone,
337+
I::Item: Borrow<&'s (Txid, usize)>,
338+
{
339+
impl_inner_call!(
340+
self,
341+
batch_transaction_get_merkle,
342+
txids_and_heights.clone()
343+
)
344+
}
345+
330346
#[inline]
331347
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
332348
impl_inner_call!(self, txid_from_pos, height, tx_pos)

src/raw_client.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,14 @@ impl<T: Read + Write> ElectrumApi for RawClient<T> {
11021102
Ok(serde_json::from_value(result)?)
11031103
}
11041104

1105+
fn batch_transaction_get_merkle<'s, I>(&self, txids_and_hashes: I) -> Result<Vec<GetMerkleRes>, Error>
1106+
where
1107+
I: IntoIterator + Clone,
1108+
I::Item: Borrow<&'s (Txid, usize)>
1109+
{
1110+
impl_batch_call!(self, txids_and_hashes, transaction_get_merkle)
1111+
}
1112+
11051113
fn txid_from_pos(&self, height: usize, tx_pos: usize) -> Result<Txid, Error> {
11061114
let params = vec![Param::Usize(height), Param::Usize(tx_pos)];
11071115
let req = Request::new_id(

0 commit comments

Comments
 (0)