Skip to content

Commit 8610326

Browse files
committed
Add gettxspendingprevout method, model and test
Add the struct for the RPC, the model, client macro, reexports and test.
1 parent e71a6b0 commit 8610326

File tree

19 files changed

+193
-14
lines changed

19 files changed

+193
-14
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: CC0-1.0
2+
3+
//! Macros for implementing JSON-RPC methods on a client.
4+
//!
5+
//! Specifically this is methods found under the `== Blockchain ==` section of the
6+
//! API docs of Bitcoin Core `v24`.
7+
//!
8+
//! All macros require `Client` to be in scope.
9+
//!
10+
//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11+
12+
/// Implements Bitcoin Core JSON-RPC API method `gettxspendingprevout`
13+
#[macro_export]
14+
macro_rules! impl_client_v24__get_tx_spending_prevout {
15+
() => {
16+
impl Client {
17+
pub fn get_tx_spending_prevout(
18+
&self,
19+
outputs: &[bitcoin::OutPoint],
20+
) -> Result<GetTxSpendingPrevout> {
21+
let json_outputs: Vec<_> = outputs.iter().map(|out| {
22+
serde_json::json!({
23+
"txid": out.txid.to_string(),
24+
"vout": out.vout,
25+
})
26+
}).collect();
27+
self.call("gettxspendingprevout", &[json_outputs.into()])
28+
}
29+
}
30+
};
31+
}

client/src/client_sync/v24/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//!
55
//! We ignore option arguments unless they effect the shape of the returned JSON data.
66
7+
pub mod blockchain;
8+
79
use std::collections::BTreeMap;
810
use std::path::Path;
911

@@ -48,6 +50,7 @@ crate::impl_client_v17__get_raw_mempool!();
4850
crate::impl_client_v17__get_tx_out!();
4951
crate::impl_client_v17__get_tx_out_proof!();
5052
crate::impl_client_v17__get_tx_out_set_info!();
53+
crate::impl_client_v24__get_tx_spending_prevout!();
5154
crate::impl_client_v17__precious_block!();
5255
crate::impl_client_v17__prune_blockchain!();
5356
crate::impl_client_v23__save_mempool!();

client/src/client_sync/v25/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ crate::impl_client_v17__get_raw_mempool!();
5050
crate::impl_client_v17__get_tx_out!();
5151
crate::impl_client_v17__get_tx_out_proof!();
5252
crate::impl_client_v17__get_tx_out_set_info!();
53+
crate::impl_client_v24__get_tx_spending_prevout!();
5354
crate::impl_client_v17__precious_block!();
5455
crate::impl_client_v17__prune_blockchain!();
5556
crate::impl_client_v23__save_mempool!();

client/src/client_sync/v26/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ crate::impl_client_v17__get_raw_mempool!();
5252
crate::impl_client_v17__get_tx_out!();
5353
crate::impl_client_v17__get_tx_out_proof!();
5454
crate::impl_client_v26__get_tx_out_set_info!();
55+
crate::impl_client_v24__get_tx_spending_prevout!();
5556
crate::impl_client_v17__precious_block!();
5657
crate::impl_client_v17__prune_blockchain!();
5758
crate::impl_client_v23__save_mempool!();

client/src/client_sync/v27/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ crate::impl_client_v17__get_raw_mempool!();
4848
crate::impl_client_v17__get_tx_out!();
4949
crate::impl_client_v17__get_tx_out_proof!();
5050
crate::impl_client_v26__get_tx_out_set_info!();
51+
crate::impl_client_v24__get_tx_spending_prevout!();
5152
crate::impl_client_v17__precious_block!();
5253
crate::impl_client_v17__prune_blockchain!();
5354
crate::impl_client_v23__save_mempool!();

client/src/client_sync/v28/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ crate::impl_client_v17__get_raw_mempool!();
5050
crate::impl_client_v17__get_tx_out!();
5151
crate::impl_client_v17__get_tx_out_proof!();
5252
crate::impl_client_v26__get_tx_out_set_info!();
53+
crate::impl_client_v24__get_tx_spending_prevout!();
5354
crate::impl_client_v17__precious_block!();
5455
crate::impl_client_v17__prune_blockchain!();
5556
crate::impl_client_v23__save_mempool!();

client/src/client_sync/v29/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ crate::impl_client_v17__get_raw_mempool!();
5050
crate::impl_client_v17__get_tx_out!();
5151
crate::impl_client_v17__get_tx_out_proof!();
5252
crate::impl_client_v26__get_tx_out_set_info!();
53+
crate::impl_client_v24__get_tx_spending_prevout!();
5354
crate::impl_client_v17__precious_block!();
5455
crate::impl_client_v17__prune_blockchain!();
5556
crate::impl_client_v23__save_mempool!();

integration_test/tests/blockchain.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,35 @@ fn blockchain__get_tx_out_set_info__modelled() {
302302
model.unwrap();
303303
}
304304

305+
#[test]
306+
#[cfg(not(feature = "v23_and_below"))]
307+
fn blockchain__get_tx_spending_prevout__modelled() {
308+
let node = Node::with_wallet(Wallet::Default, &[]);
309+
node.fund_wallet();
310+
311+
let (_address1, txid_1) = node.create_mempool_transaction();
312+
let (_address2, txid_2) = node.create_mempool_transaction();
313+
314+
let inputs = vec![
315+
bitcoin::OutPoint {
316+
txid: txid_1,
317+
vout: 0,
318+
},
319+
bitcoin::OutPoint {
320+
txid: txid_2,
321+
vout: 0,
322+
},
323+
];
324+
325+
let json: GetTxSpendingPrevout = node.client.get_tx_spending_prevout(&inputs).expect("gettxspendingprevout");
326+
let model: Result<mtype::GetTxSpendingPrevout, GetTxSpendingPrevoutError> = json.into_model();
327+
let spending_prevout = model.unwrap();
328+
329+
assert_eq!(spending_prevout.0.len(), 2);
330+
assert_eq!(spending_prevout.0[0].outpoint.txid, txid_1);
331+
assert_eq!(spending_prevout.0[0].outpoint.vout, 0);
332+
}
333+
305334
#[test]
306335
fn blockchain__precious_block() {
307336
let node = Node::with_wallet(Wallet::Default, &[]);

types/src/model/blockchain.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use alloc::collections::BTreeMap;
99

1010
use bitcoin::address::NetworkUnchecked;
1111
use bitcoin::{
12-
block, Address, Amount, Block, BlockHash, CompactTarget, FeeRate, Network, ScriptBuf, Target,
13-
TxMerkleNode, TxOut, Txid, Weight, Work, Wtxid,
12+
block, Address, Amount, Block, BlockHash, CompactTarget, FeeRate, Network, OutPoint, ScriptBuf,
13+
Target, TxMerkleNode, TxOut, Txid, Weight, Work, Wtxid,
1414
};
1515
use serde::{Deserialize, Serialize};
1616

@@ -626,6 +626,21 @@ pub struct GetTxOutSetInfo {
626626
pub total_amount: Amount,
627627
}
628628

629+
/// Models the result of JSON-RPC method `gettxspendingprevout`.
630+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
631+
#[serde(deny_unknown_fields)]
632+
pub struct GetTxSpendingPrevout(pub Vec<GetTxSpendingPrevoutItem>);
633+
634+
/// An individual result item from `gettxspendingprevout`.
635+
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
636+
#[serde(deny_unknown_fields)]
637+
pub struct GetTxSpendingPrevoutItem {
638+
/// The outpoint containing the transaction id and vout value of the checked output.
639+
pub outpoint: OutPoint,
640+
/// The transaction id of the mempool transaction spending this output (omitted if unspent).
641+
pub spending_txid: Option<Txid>,
642+
}
643+
629644
/// Models the result of JSON-RPC method `verifytxoutproof`.
630645
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
631646
#[serde(deny_unknown_fields)]

types/src/model/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ pub use self::{
2828
GetChainTxStats, GetDeploymentInfo, GetDescriptorActivity, GetDifficulty,
2929
GetMempoolAncestors, GetMempoolAncestorsVerbose, GetMempoolDescendants,
3030
GetMempoolDescendantsVerbose, GetMempoolEntry, GetMempoolInfo, GetRawMempool,
31-
GetRawMempoolVerbose, GetTxOut, GetTxOutSetInfo, MempoolEntry, MempoolEntryFees,
32-
ReceiveActivity, Softfork, SoftforkType, SpendActivity, VerifyTxOutProof,
31+
GetRawMempoolVerbose, GetTxOut, GetTxOutSetInfo, GetTxSpendingPrevout,
32+
GetTxSpendingPrevoutItem, MempoolEntry, MempoolEntryFees, ReceiveActivity, Softfork,
33+
SoftforkType, SpendActivity, VerifyTxOutProof,
3334
},
3435
generating::{Generate, GenerateBlock, GenerateToAddress, GenerateToDescriptor},
3536
mining::{

0 commit comments

Comments
 (0)