Skip to main content

corepc_client/client_sync/v17/
blockchain.rs

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 `v0.17`.
7//!
8//! All macros require `Client` to be in scope.
9//!
10//! See or use the `define_jsonrpc_bitreq_client!` macro to define a `Client`.
11
12/// Implements Bitcoin Core JSON-RPC API method `getblockchaininfo`.
13#[macro_export]
14macro_rules! impl_client_v17__get_blockchain_info {
15    () => {
16        impl Client {
17            pub fn get_blockchain_info(&self) -> Result<GetBlockchainInfo> {
18                self.call("getblockchaininfo", &[])
19            }
20        }
21    };
22}
23
24/// Implements Bitcoin Core JSON-RPC API method `getbestblockhash`.
25#[macro_export]
26macro_rules! impl_client_v17__get_best_block_hash {
27    () => {
28        impl Client {
29            /// Gets the blockhash of the current chain tip.
30            pub fn best_block_hash(&self) -> Result<bitcoin::BlockHash> {
31                let json = self.get_best_block_hash()?;
32                Ok(json.block_hash()?)
33            }
34
35            pub fn get_best_block_hash(&self) -> Result<GetBestBlockHash> {
36                self.call("getbestblockhash", &[])
37            }
38        }
39    };
40}
41
42/// Implements Bitcoin Core JSON-RPC API method `getblock`.
43#[macro_export]
44macro_rules! impl_client_v17__get_block {
45    () => {
46        impl Client {
47            /// Gets a block by blockhash.
48            pub fn get_block(&self, hash: BlockHash) -> Result<Block> {
49                let json = self.get_block_verbose_zero(hash)?;
50                Ok(json.block()?)
51            }
52
53            /// Gets a block by blockhash with verbose set to 0.
54            pub fn get_block_verbose_zero(&self, hash: BlockHash) -> Result<GetBlockVerboseZero> {
55                self.call("getblock", &[into_json(hash)?, 0.into()])
56            }
57
58            /// Gets a block by blockhash with verbose set to 1.
59            pub fn get_block_verbose_one(&self, hash: BlockHash) -> Result<GetBlockVerboseOne> {
60                self.call("getblock", &[into_json(hash)?, 1.into()])
61            }
62        }
63    };
64}
65
66/// Implements Bitcoin Core JSON-RPC API method `getblockcount`.
67#[macro_export]
68macro_rules! impl_client_v17__get_block_count {
69    () => {
70        impl Client {
71            pub fn get_block_count(&self) -> Result<GetBlockCount> {
72                self.call("getblockcount", &[])
73            }
74        }
75    };
76}
77
78/// Implements Bitcoin Core JSON-RPC API method `getblockhash`.
79#[macro_export]
80macro_rules! impl_client_v17__get_block_hash {
81    () => {
82        impl Client {
83            pub fn get_block_hash(&self, height: u64) -> Result<GetBlockHash> {
84                self.call("getblockhash", &[into_json(height)?])
85            }
86        }
87    };
88}
89
90/// Implements Bitcoin Core JSON-RPC API method `getblockheader`.
91#[macro_export]
92macro_rules! impl_client_v17__get_block_header {
93    () => {
94        impl Client {
95            pub fn get_block_header(&self, hash: &BlockHash) -> Result<GetBlockHeader> {
96                self.call("getblockheader", &[into_json(hash)?, into_json(false)?])
97            }
98
99            // This is the same as calling getblockheader with verbose==true.
100            pub fn get_block_header_verbose(
101                &self,
102                hash: &BlockHash,
103            ) -> Result<GetBlockHeaderVerbose> {
104                self.call("getblockheader", &[into_json(hash)?])
105            }
106        }
107    };
108}
109
110/// Implements Bitcoin Core JSON-RPC API method `getblockstats`.
111#[macro_export]
112macro_rules! impl_client_v17__get_block_stats {
113    () => {
114        impl Client {
115            pub fn get_block_stats_by_height(
116                &self,
117                height: u32,
118                stats: Option<&[&str]>,
119            ) -> Result<GetBlockStats> {
120                self.call("getblockstats", &[into_json(height)?, into_json(stats)?])
121            }
122
123            pub fn get_block_stats_by_block_hash(
124                &self,
125                hash: &BlockHash,
126                stats: Option<&[&str]>,
127            ) -> Result<GetBlockStats> {
128                self.call("getblockstats", &[into_json(hash)?, into_json(stats)?])
129            }
130        }
131    };
132}
133
134/// Implements Bitcoin Core JSON-RPC API method `getchaintips`.
135#[macro_export]
136macro_rules! impl_client_v17__get_chain_tips {
137    () => {
138        impl Client {
139            pub fn get_chain_tips(&self) -> Result<GetChainTips> { self.call("getchaintips", &[]) }
140        }
141    };
142}
143
144/// Implements Bitcoin Core JSON-RPC API method `getchaintxstats`.
145#[macro_export]
146macro_rules! impl_client_v17__get_chain_tx_stats {
147    () => {
148        impl Client {
149            pub fn get_chain_tx_stats(&self) -> Result<GetChainTxStats> {
150                self.call("getchaintxstats", &[])
151            }
152        }
153    };
154}
155
156/// Implements Bitcoin Core JSON-RPC API method `getdifficulty`.
157#[macro_export]
158macro_rules! impl_client_v17__get_difficulty {
159    () => {
160        impl Client {
161            pub fn get_difficulty(&self) -> Result<GetDifficulty> {
162                self.call("getdifficulty", &[])
163            }
164        }
165    };
166}
167
168/// Implements Bitcoin Core JSON-RPC API method `getmempoolancestors`.
169#[macro_export]
170macro_rules! impl_client_v17__get_mempool_ancestors {
171    () => {
172        impl Client {
173            pub fn get_mempool_ancestors(&self, txid: Txid) -> Result<GetMempoolAncestors> {
174                // Equivalent to self.call("getmempoolancestors", &[into_json(txid)?, into_json(false)?])
175                self.call("getmempoolancestors", &[into_json(txid)?])
176            }
177
178            pub fn get_mempool_ancestors_verbose(
179                &self,
180                txid: Txid,
181            ) -> Result<GetMempoolAncestorsVerbose> {
182                self.call("getmempoolancestors", &[into_json(txid)?, into_json(true)?])
183            }
184        }
185    };
186}
187
188/// Implements Bitcoin Core JSON-RPC API method `getmempooldescendants`.
189#[macro_export]
190macro_rules! impl_client_v17__get_mempool_descendants {
191    () => {
192        impl Client {
193            pub fn get_mempool_descendants(&self, txid: Txid) -> Result<GetMempoolDescendants> {
194                // Equivalent to self.call("getmempooldescendants", &[into_json(txid)?, into_json(false)?])
195                self.call("getmempooldescendants", &[into_json(txid)?])
196            }
197
198            pub fn get_mempool_descendants_verbose(
199                &self,
200                txid: Txid,
201            ) -> Result<GetMempoolDescendantsVerbose> {
202                self.call("getmempooldescendants", &[into_json(txid)?, into_json(true)?])
203            }
204        }
205    };
206}
207
208/// Implements Bitcoin Core JSON-RPC API method `getmempoolentry`.
209#[macro_export]
210macro_rules! impl_client_v17__get_mempool_entry {
211    () => {
212        impl Client {
213            pub fn get_mempool_entry(&self, txid: Txid) -> Result<GetMempoolEntry> {
214                self.call("getmempoolentry", &[into_json(txid)?])
215            }
216        }
217    };
218}
219
220/// Implements Bitcoin Core JSON-RPC API method `getmempoolinfo`.
221#[macro_export]
222macro_rules! impl_client_v17__get_mempool_info {
223    () => {
224        impl Client {
225            pub fn get_mempool_info(&self) -> Result<GetMempoolInfo> {
226                self.call("getmempoolinfo", &[])
227            }
228        }
229    };
230}
231
232/// Implements Bitcoin Core JSON-RPC API method `getrawmempool`.
233#[macro_export]
234macro_rules! impl_client_v17__get_raw_mempool {
235    () => {
236        impl Client {
237            pub fn get_raw_mempool(&self) -> Result<GetRawMempool> {
238                // Equivalent to self.call("getrawmempool", &[into_json(false)?])
239                self.call("getrawmempool", &[])
240            }
241
242            pub fn get_raw_mempool_verbose(&self) -> Result<GetRawMempoolVerbose> {
243                self.call("getrawmempool", &[into_json(true)?])
244            }
245        }
246    };
247}
248
249/// Implements Bitcoin Core JSON-RPC API method `gettxout`.
250#[macro_export]
251macro_rules! impl_client_v17__get_tx_out {
252    () => {
253        impl Client {
254            pub fn get_tx_out(&self, txid: Txid, vout: u64) -> Result<GetTxOut> {
255                self.call("gettxout", &[into_json(txid)?, into_json(vout)?])
256            }
257        }
258    };
259}
260
261/// Implements Bitcoin Core JSON-RPC API method `gettxoutproof`.
262#[macro_export]
263macro_rules! impl_client_v17__get_tx_out_proof {
264    () => {
265        impl Client {
266            pub fn get_tx_out_proof(&self, txids: &[Txid]) -> Result<String> {
267                self.call("gettxoutproof", &[into_json(txids)?])
268            }
269        }
270    };
271}
272
273/// Implements Bitcoin Core JSON-RPC API method `gettxoutsetinfo`.
274#[macro_export]
275macro_rules! impl_client_v17__get_tx_out_set_info {
276    () => {
277        impl Client {
278            pub fn get_tx_out_set_info(&self) -> Result<GetTxOutSetInfo> {
279                self.call("gettxoutsetinfo", &[])
280            }
281        }
282    };
283}
284
285/// Implements Bitcoin Core JSON-RPC API method `preciousblock`.
286#[macro_export]
287macro_rules! impl_client_v17__precious_block {
288    () => {
289        impl Client {
290            pub fn precious_block(&self, hash: BlockHash) -> Result<()> {
291                match self.call("preciousblock", &[into_json(hash)?]) {
292                    Ok(serde_json::Value::Null) => Ok(()),
293                    Ok(res) => Err(Error::Returned(res.to_string())),
294                    Err(err) => Err(err.into()),
295                }
296            }
297        }
298    };
299}
300
301/// Implements Bitcoin Core JSON-RPC API method `pruneblockchain`.
302#[macro_export]
303macro_rules! impl_client_v17__prune_blockchain {
304    () => {
305        impl Client {
306            /// Instructs the node to prune the blockchain up to a specified height or timestamp.
307            pub fn prune_blockchain(&self, target: u64) -> Result<PruneBlockchain> {
308                self.call("pruneblockchain", &[target.into()])
309            }
310        }
311    };
312}
313
314/// Implements Bitcoin Core JSON-RPC API method `savemempool`.
315#[macro_export]
316macro_rules! impl_client_v17__save_mempool {
317    () => {
318        impl Client {
319            pub fn save_mempool(&self) -> Result<()> {
320                match self.call("savemempool", &[]) {
321                    Ok(serde_json::Value::Null) => Ok(()),
322                    Ok(res) => Err(Error::Returned(res.to_string())),
323                    Err(err) => Err(err.into()),
324                }
325            }
326        }
327    };
328}
329
330/// Implements Bitcoin Core JSON-RPC API method `scantxoutset`
331#[macro_export]
332macro_rules! impl_client_v17__scan_tx_out_set {
333    () => {
334        impl Client {
335            /// Aborts an ongoing `scantxoutset` scan.
336            pub fn scan_tx_out_set_abort(&self) -> Result<ScanTxOutSetAbort> {
337                self.call("scantxoutset", &[into_json("abort")?])
338            }
339
340            /// Starts a scan of the UTXO set for specified descriptors.
341            pub fn scan_tx_out_set_start(
342                &self,
343                scan_objects: &[&str],
344            ) -> Result<ScanTxOutSetStart> {
345                self.call("scantxoutset", &[into_json("start")?, into_json(scan_objects)?])
346            }
347
348            /// Checks the status of an ongoing `scantxoutset` scan.
349            pub fn scan_tx_out_set_status(&self) -> Result<Option<ScanTxOutSetStatus>> {
350                self.call("scantxoutset", &[into_json("status")?])
351            }
352        }
353    };
354}
355
356/// Implements Bitcoin Core JSON-RPC API method `verifychain`
357#[macro_export]
358macro_rules! impl_client_v17__verify_chain {
359    () => {
360        impl Client {
361            pub fn verify_chain(&self) -> Result<VerifyChain> { self.call("verifychain", &[]) }
362        }
363    };
364}
365
366/// Implements Bitcoin Core JSON-RPC API method `verifytxoutproof`.
367#[macro_export]
368macro_rules! impl_client_v17__verify_tx_out_proof {
369    () => {
370        impl Client {
371            // `proof` is the hex-encoded proof generated by `gettxoutproof`.
372            pub fn verify_tx_out_proof(&self, proof: &str) -> Result<VerifyTxOutProof> {
373                self.call("verifytxoutproof", &[into_json(proof)?])
374            }
375        }
376    };
377}