|
1 | 1 | // SPDX-License-Identifier: CC0-1.0 |
2 | 2 |
|
3 | | -//! RPC set used by BDK. |
| 3 | +//! RPC methods for the async Bitcoin Core client. |
4 | 4 | //! All functions return the version nonspecific, strongly typed types. |
5 | 5 |
|
6 | 6 | use bitcoin::{block, Block, BlockHash, Transaction, Txid}; |
7 | 7 | use serde_json::value::RawValue; |
8 | 8 |
|
9 | | -use crate::client_async::{into_json, Client, Result}; |
| 9 | +use super::{into_json, Client, IntoModelError, Result}; |
10 | 10 | use crate::types::model::{GetBlockFilter, GetBlockHeaderVerbose, GetBlockVerboseOne}; |
11 | 11 |
|
12 | | -impl Client { |
| 12 | +/// Bitcoin Core RPC methods (v25 to v30). |
| 13 | +/// |
| 14 | +/// This trait exposes the Bitcoin Core RPC methods available on [`Client`]. Downstream users |
| 15 | +/// can define their own extension traits with additional methods without risk of |
| 16 | +/// name collision with the methods defined here. |
| 17 | +// `async fn` in traits produces non-`Send` futures by default; we suppress this lint because |
| 18 | +// `BitcoinRpcs` is intended only for use with concrete types (never `dyn BitcoinRpcs`). |
| 19 | +#[allow(async_fn_in_trait)] |
| 20 | +pub trait BitcoinRpcs { |
13 | 21 | /// Gets a block by blockhash. |
14 | | - pub async fn get_block(&self, hash: &BlockHash) -> Result<Block> { |
| 22 | + async fn get_block(&self, hash: &BlockHash) -> Result<Block>; |
| 23 | + |
| 24 | + /// Gets the block count. |
| 25 | + async fn get_block_count(&self) -> Result<u64>; |
| 26 | + |
| 27 | + /// Gets the block hash for a height. |
| 28 | + async fn get_block_hash(&self, height: u32) -> Result<BlockHash>; |
| 29 | + |
| 30 | + /// Gets the hash of the chain tip. |
| 31 | + async fn get_best_block_hash(&self) -> Result<BlockHash>; |
| 32 | + |
| 33 | + /// Gets the block header by blockhash. |
| 34 | + async fn get_block_header(&self, hash: &BlockHash) -> Result<block::Header>; |
| 35 | + |
| 36 | + /// Gets the block header with verbose output. |
| 37 | + async fn get_block_header_verbose(&self, hash: &BlockHash) -> Result<GetBlockHeaderVerbose>; |
| 38 | + |
| 39 | + /// Gets a block by blockhash with verbose set to 1. |
| 40 | + async fn get_block_verbose(&self, hash: &BlockHash) -> Result<GetBlockVerboseOne>; |
| 41 | + |
| 42 | + /// Gets the block filter for a blockhash. |
| 43 | + async fn get_block_filter(&self, hash: &BlockHash) -> Result<GetBlockFilter>; |
| 44 | + |
| 45 | + /// Gets the transaction IDs currently in the mempool. |
| 46 | + async fn get_raw_mempool(&self) -> Result<Vec<Txid>>; |
| 47 | + |
| 48 | + /// Gets the raw transaction by txid. |
| 49 | + async fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction>; |
| 50 | + |
| 51 | + /// Returns the version integer reported by the server (e.g. `250200` for v25.2.0). |
| 52 | + async fn server_version(&self) -> Result<usize>; |
| 53 | +} |
| 54 | + |
| 55 | +impl BitcoinRpcs for Client { |
| 56 | + async fn get_block(&self, hash: &BlockHash) -> Result<Block> { |
15 | 57 | let json: crate::types::v25::GetBlockVerboseZero = |
16 | 58 | self.call("getblock", &[into_json(hash)?, into_json(0)?]).await?; |
17 | | - Ok(json.into_model()?.0) |
| 59 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblock`", e))?.0) |
18 | 60 | } |
19 | 61 |
|
20 | | - /// Gets the block count. |
21 | | - pub async fn get_block_count(&self) -> Result<u64> { |
| 62 | + async fn get_block_count(&self) -> Result<u64> { |
22 | 63 | let json: crate::types::v25::GetBlockCount = self.call("getblockcount", &[]).await?; |
23 | 64 | Ok(json.into_model().0) |
24 | 65 | } |
25 | 66 |
|
26 | | - /// Gets the block hash for a height. |
27 | | - pub async fn get_block_hash(&self, height: u32) -> Result<BlockHash> { |
| 67 | + async fn get_block_hash(&self, height: u32) -> Result<BlockHash> { |
28 | 68 | let json: crate::types::v25::GetBlockHash = |
29 | 69 | self.call("getblockhash", &[into_json(height)?]).await?; |
30 | | - Ok(json.into_model()?.0) |
| 70 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblockhash`", e))?.0) |
31 | 71 | } |
32 | 72 |
|
33 | | - /// Gets the hash of the chain tip. |
34 | | - pub async fn get_best_block_hash(&self) -> Result<BlockHash> { |
| 73 | + async fn get_best_block_hash(&self) -> Result<BlockHash> { |
35 | 74 | let json: crate::types::v25::GetBestBlockHash = self.call("getbestblockhash", &[]).await?; |
36 | | - Ok(json.into_model()?.0) |
| 75 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getbestblockhash`", e))?.0) |
37 | 76 | } |
38 | 77 |
|
39 | | - /// Gets the block header by blockhash. |
40 | | - pub async fn get_block_header(&self, hash: &BlockHash) -> Result<block::Header> { |
| 78 | + async fn get_block_header(&self, hash: &BlockHash) -> Result<block::Header> { |
41 | 79 | let json: crate::types::v25::GetBlockHeader = |
42 | 80 | self.call("getblockheader", &[into_json(hash)?, into_json(false)?]).await?; |
43 | | - Ok(json.into_model()?.0) |
| 81 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblockheader`", e))?.0) |
44 | 82 | } |
45 | 83 |
|
46 | | - /// Gets the block header with verbose output. |
47 | | - pub async fn get_block_header_verbose( |
48 | | - &self, |
49 | | - hash: &BlockHash, |
50 | | - ) -> Result<GetBlockHeaderVerbose> { |
| 84 | + async fn get_block_header_verbose(&self, hash: &BlockHash) -> Result<GetBlockHeaderVerbose> { |
51 | 85 | let raw: Box<RawValue> = |
52 | 86 | self.call("getblockheader", &[into_json(hash)?, into_json(true)?]).await?; |
53 | 87 |
|
54 | 88 | if let Ok(json) = |
55 | 89 | serde_json::from_str::<crate::types::v29::GetBlockHeaderVerbose>(raw.get()) |
56 | 90 | { |
57 | | - Ok(json.into_model()?) |
| 91 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblockheader` verbose", e))?) |
58 | 92 | } else { |
59 | 93 | let json: crate::types::v25::GetBlockHeaderVerbose = serde_json::from_str(raw.get())?; |
60 | | - Ok(json.into_model()?) |
| 94 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblockheader` verbose", e))?) |
61 | 95 | } |
62 | 96 | } |
63 | 97 |
|
64 | | - /// Gets a block by blockhash with verbose set to 1. |
65 | | - pub async fn get_block_verbose(&self, hash: &BlockHash) -> Result<GetBlockVerboseOne> { |
| 98 | + async fn get_block_verbose(&self, hash: &BlockHash) -> Result<GetBlockVerboseOne> { |
66 | 99 | let raw: Box<RawValue> = self.call("getblock", &[into_json(hash)?, into_json(1)?]).await?; |
67 | 100 |
|
68 | 101 | if let Ok(json) = serde_json::from_str::<crate::types::v29::GetBlockVerboseOne>(raw.get()) { |
69 | | - Ok(json.into_model()?) |
| 102 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblock` verbose=1", e))?) |
70 | 103 | } else { |
71 | 104 | let json: crate::types::v25::GetBlockVerboseOne = serde_json::from_str(raw.get())?; |
72 | | - Ok(json.into_model()?) |
| 105 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblock` verbose=1", e))?) |
73 | 106 | } |
74 | 107 | } |
75 | 108 |
|
76 | | - /// Gets the block filter for a blockhash. |
77 | | - pub async fn get_block_filter(&self, hash: &BlockHash) -> Result<GetBlockFilter> { |
| 109 | + async fn get_block_filter(&self, hash: &BlockHash) -> Result<GetBlockFilter> { |
78 | 110 | let json: crate::types::v25::GetBlockFilter = |
79 | 111 | self.call("getblockfilter", &[into_json(hash)?]).await?; |
80 | | - Ok(json.into_model()?) |
| 112 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getblockfilter`", e))?) |
81 | 113 | } |
82 | 114 |
|
83 | | - /// Gets the transaction IDs currently in the mempool. |
84 | | - pub async fn get_raw_mempool(&self) -> Result<Vec<Txid>> { |
| 115 | + async fn get_raw_mempool(&self) -> Result<Vec<Txid>> { |
85 | 116 | let json: crate::types::v25::GetRawMempool = self.call("getrawmempool", &[]).await?; |
86 | | - Ok(json.into_model()?.0) |
| 117 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getrawmempool`", e))?.0) |
87 | 118 | } |
88 | 119 |
|
89 | | - /// Gets the raw transaction by txid. |
90 | | - pub async fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction> { |
| 120 | + async fn get_raw_transaction(&self, txid: &Txid) -> Result<Transaction> { |
91 | 121 | let json: crate::types::v25::GetRawTransaction = |
92 | 122 | self.call("getrawtransaction", &[into_json(txid)?]).await?; |
93 | | - Ok(json.into_model()?.0) |
| 123 | + Ok(json.into_model().map_err(|e| IntoModelError::new("`getrawtransaction`", e))?.0) |
94 | 124 | } |
95 | 125 |
|
96 | | - /// Returns the version integer reported by the server (e.g. `250200` for v25.2.0). |
97 | | - pub async fn server_version(&self) -> Result<usize> { |
| 126 | + async fn server_version(&self) -> Result<usize> { |
98 | 127 | // Use a minimal type to read only the `version` field; the shape of other fields |
99 | 128 | // (e.g. `warnings` changed from String to Vec<String> at v28) differs across the |
100 | 129 | // supported version range. |
|
0 commit comments