bitcoin_explorer/api/connected.rs
1//!
2//! implementation of methods that retrieve block info with outpoints connected
3//!
4use crate::api::{BitcoinDB, ConnectedBlock, ConnectedBlockIter, ConnectedTx, Txid};
5use crate::parser::errors::{OpError, OpResult};
6
7impl BitcoinDB {
8 ///
9 /// Get a block with inputs replaced by connected outputs.
10 ///
11 /// This function requires `txindex` to be set to `true` for `BitcoinDB`,
12 /// and requires that flag `txindex=1` has been enabled when
13 /// running Bitcoin Core.
14 ///
15 /// A transaction cannot be found using this function if it is
16 /// not yet indexed using `txindex`.
17 ///
18 /// # Caveat!!
19 ///
20 /// ## Performance Warning
21 ///
22 /// Slow! For massive computation, use `db.iter_connected_block()`.
23 ///
24 pub fn get_connected_block<T: ConnectedBlock>(&self, height: usize) -> OpResult<T> {
25 if !self.tx_db.is_open() {
26 return Err(OpError::from("TxDB not open"));
27 }
28 let tx = self.get_block(height)?;
29 T::connect(tx, &self.tx_db, &self.block_index, &self.blk_file)
30 }
31
32 ///
33 /// Get a transaction with outpoints replaced by outputs.
34 ///
35 /// This function requires `txindex` to be set to `true` for `BitcoinDB`,
36 /// and requires that flag `txindex=1` has been enabled when
37 /// running Bitcoin Core.
38 ///
39 /// A transaction cannot be found using this function if it is
40 /// not yet indexed using `txindex`.
41 ///
42 /// Format: `full (FConnectedTransaction)` / `simple (SConnectedTransaction)`.
43 ///
44 /// # Caveats
45 ///
46 /// ## Performance Warning
47 ///
48 /// Slow! For massive computation, use `db.iter_connected_block()`.
49 ///
50 pub fn get_connected_transaction<T: ConnectedTx>(&self, txid: &Txid) -> OpResult<T> {
51 if !self.tx_db.is_open() {
52 return Err(OpError::from("TxDB not open"));
53 }
54 let tx = self.get_transaction(txid)?;
55 T::connect(tx, &self.tx_db, &self.block_index, &self.blk_file)
56 }
57
58 ///
59 /// Iterate through all blocks for a given heights (excluded).
60 ///
61 /// Format: `full (FConnectedBlock)` / `simple (SConnectedBlock)`.
62 ///
63 /// This iterator use `unspent output` to track down the connected
64 /// outputs of each outpoints.
65 ///
66 /// ## Note
67 /// This does NOT require `txindex=true`.
68 ///
69 /// # Performance
70 ///
71 /// ## Using default feature:
72 /// Requires 4 GB memory, finishes in 2.5 hours from 0-70000 block.
73 ///
74 /// ## Using non-default feature
75 /// Requires 32 GB memory, finished in 30 minutes from 0-70000 block.
76 ///
77 /// # Example
78 ///
79 /// ```rust
80 /// use bitcoin_explorer::{BitcoinDB, FConnectedBlock, SConnectedBlock};
81 /// use std::path::Path;
82 ///
83 /// let path = Path::new("/Users/me/bitcoin");
84 ///
85 /// // launch without reading txindex
86 /// let db = BitcoinDB::new(path, false).unwrap();
87 ///
88 /// // iterate over block from 0 to 700000, (simple format)
89 /// for block in db.iter_connected_block::<SConnectedBlock>(700000) {
90 /// for tx in block.txdata {
91 /// println!("do something for this transaction");
92 /// }
93 /// }
94 /// ```
95 ///
96 pub fn iter_connected_block<TBlock>(&self, end: usize) -> ConnectedBlockIter<TBlock>
97 where
98 TBlock: 'static + ConnectedBlock + Send,
99 {
100 ConnectedBlockIter::new(self, end)
101 }
102}