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_minreq_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(&self, height: u32) -> Result<GetBlockStats> {
116                self.call("getblockstats", &[into_json(height)?])
117            }
118
119            pub fn get_block_stats_by_block_hash(&self, hash: &BlockHash) -> Result<GetBlockStats> {
120                self.call("getblockstats", &[into_json(hash)?])
121            }
122        }
123    };
124}
125
126/// Implements Bitcoin Core JSON-RPC API method `getchaintips`.
127#[macro_export]
128macro_rules! impl_client_v17__get_chain_tips {
129    () => {
130        impl Client {
131            pub fn get_chain_tips(&self) -> Result<GetChainTips> { self.call("getchaintips", &[]) }
132        }
133    };
134}
135
136/// Implements Bitcoin Core JSON-RPC API method `getchaintxstats`.
137#[macro_export]
138macro_rules! impl_client_v17__get_chain_tx_stats {
139    () => {
140        impl Client {
141            pub fn get_chain_tx_stats(&self) -> Result<GetChainTxStats> {
142                self.call("getchaintxstats", &[])
143            }
144        }
145    };
146}
147
148/// Implements Bitcoin Core JSON-RPC API method `getdifficulty`.
149#[macro_export]
150macro_rules! impl_client_v17__get_difficulty {
151    () => {
152        impl Client {
153            pub fn get_difficulty(&self) -> Result<GetDifficulty> {
154                self.call("getdifficulty", &[])
155            }
156        }
157    };
158}
159
160/// Implements Bitcoin Core JSON-RPC API method `getmempoolancestors`.
161#[macro_export]
162macro_rules! impl_client_v17__get_mempool_ancestors {
163    () => {
164        impl Client {
165            pub fn get_mempool_ancestors(&self, txid: Txid) -> Result<GetMempoolAncestors> {
166                // Equivalent to self.call("getmempoolancestors", &[into_json(txid)?, into_json(false)?])
167                self.call("getmempoolancestors", &[into_json(txid)?])
168            }
169
170            pub fn get_mempool_ancestors_verbose(
171                &self,
172                txid: Txid,
173            ) -> Result<GetMempoolAncestorsVerbose> {
174                self.call("getmempoolancestors", &[into_json(txid)?, into_json(true)?])
175            }
176        }
177    };
178}
179
180/// Implements Bitcoin Core JSON-RPC API method `getmempooldescendants`.
181#[macro_export]
182macro_rules! impl_client_v17__get_mempool_descendants {
183    () => {
184        impl Client {
185            pub fn get_mempool_descendants(&self, txid: Txid) -> Result<GetMempoolDescendants> {
186                // Equivalent to self.call("getmempooldescendants", &[into_json(txid)?, into_json(false)?])
187                self.call("getmempooldescendants", &[into_json(txid)?])
188            }
189
190            pub fn get_mempool_descendants_verbose(
191                &self,
192                txid: Txid,
193            ) -> Result<GetMempoolDescendantsVerbose> {
194                self.call("getmempooldescendants", &[into_json(txid)?, into_json(true)?])
195            }
196        }
197    };
198}
199
200/// Implements Bitcoin Core JSON-RPC API method `getmempoolentry`.
201#[macro_export]
202macro_rules! impl_client_v17__get_mempool_entry {
203    () => {
204        impl Client {
205            pub fn get_mempool_entry(&self, txid: Txid) -> Result<GetMempoolEntry> {
206                self.call("getmempoolentry", &[into_json(txid)?])
207            }
208        }
209    };
210}
211
212/// Implements Bitcoin Core JSON-RPC API method `getmempoolinfo`.
213#[macro_export]
214macro_rules! impl_client_v17__get_mempool_info {
215    () => {
216        impl Client {
217            pub fn get_mempool_info(&self) -> Result<GetMempoolInfo> {
218                self.call("getmempoolinfo", &[])
219            }
220        }
221    };
222}
223
224/// Implements Bitcoin Core JSON-RPC API method `getrawmempool`.
225#[macro_export]
226macro_rules! impl_client_v17__get_raw_mempool {
227    () => {
228        impl Client {
229            pub fn get_raw_mempool(&self) -> Result<GetRawMempool> {
230                // Equivalent to self.call("getrawmempool", &[into_json(false)?])
231                self.call("getrawmempool", &[])
232            }
233            pub fn get_raw_mempool_verbose(&self) -> Result<GetRawMempool> {
234                self.call("getrawmempool", &[into_json(true)?])
235            }
236        }
237    };
238}
239
240/// Implements Bitcoin Core JSON-RPC API method `gettxout`.
241#[macro_export]
242macro_rules! impl_client_v17__get_tx_out {
243    () => {
244        impl Client {
245            pub fn get_tx_out(&self, txid: Txid, vout: u64) -> Result<GetTxOut> {
246                self.call("gettxout", &[into_json(txid)?, into_json(vout)?])
247            }
248        }
249    };
250}
251
252/// Implements Bitcoin Core JSON-RPC API method `gettxoutproof`.
253#[macro_export]
254macro_rules! impl_client_v17__get_tx_out_proof {
255    () => {
256        impl Client {
257            pub fn get_tx_out_proof(&self, txids: &[Txid]) -> Result<String> {
258                self.call("gettxoutproof", &[into_json(txids)?])
259            }
260        }
261    };
262}
263
264/// Implements Bitcoin Core JSON-RPC API method `gettxoutsetinfo`.
265#[macro_export]
266macro_rules! impl_client_v17__get_tx_out_set_info {
267    () => {
268        impl Client {
269            pub fn get_tx_out_set_info(&self) -> Result<GetTxOutSetInfo> {
270                self.call("gettxoutsetinfo", &[])
271            }
272        }
273    };
274}
275
276/// Implements Bitcoin Core JSON-RPC API method `preciousblock`.
277#[macro_export]
278macro_rules! impl_client_v17__precious_block {
279    () => {
280        impl Client {
281            pub fn precious_block(&self, hash: BlockHash) -> Result<()> {
282                match self.call("preciousblock", &[into_json(hash)?]) {
283                    Ok(serde_json::Value::Null) => Ok(()),
284                    Ok(res) => Err(Error::Returned(res.to_string())),
285                    Err(err) => Err(err.into()),
286                }
287            }
288        }
289    };
290}
291
292/// Implements Bitcoin Core JSON-RPC API method `pruneblockchain`.
293#[macro_export]
294macro_rules! impl_client_v17__prune_blockchain {
295    () => {
296        impl Client {
297            /// Instructs the node to prune the blockchain up to a specified height or timestamp.
298            pub fn prune_blockchain(&self, target: u64) -> Result<PruneBlockchain> {
299                self.call("pruneblockchain", &[target.into()])
300            }
301        }
302    };
303}
304
305/// Implements Bitcoin Core JSON-RPC API method `savemempool`.
306#[macro_export]
307macro_rules! impl_client_v17__save_mempool {
308    () => {
309        impl Client {
310            pub fn save_mempool(&self) -> Result<()> {
311                match self.call("savemempool", &[]) {
312                    Ok(serde_json::Value::Null) => Ok(()),
313                    Ok(res) => Err(Error::Returned(res.to_string())),
314                    Err(err) => Err(err.into()),
315                }
316            }
317        }
318    };
319}
320
321/// Implements Bitcoin Core JSON-RPC API method `verifychain`.
322#[macro_export]
323macro_rules! impl_client_v17__verify_chain {
324    () => {
325        impl Client {
326            pub fn verify_chain(&self) -> Result<VerifyChain> { self.call("verifychain", &[]) }
327        }
328    };
329}
330
331/// Implements Bitcoin Core JSON-RPC API method `verifytxoutproof`.
332#[macro_export]
333macro_rules! impl_client_v17__verify_tx_out_proof {
334    () => {
335        impl Client {
336            // `proof` is the hex-encoded proof generated by `gettxoutproof`.
337            pub fn verify_tx_out_proof(&self, proof: &str) -> Result<VerifyTxOutProof> {
338                self.call("verifytxoutproof", &[into_json(proof)?])
339            }
340        }
341    };
342}