11use std:: collections:: HashSet ;
22
33use bdk_chain:: {
4- bitcoin:: { Transaction , Txid } ,
4+ bitcoin:: { Block , Transaction , Txid } ,
5+ keychain:: LocalUpdate ,
56 local_chain:: CheckPoint ,
6- BlockId ,
7+ BlockId , ConfirmationHeightAnchor , ConfirmationTimeAnchor , TxGraph ,
78} ;
89use bitcoincore_rpc:: { bitcoincore_rpc_json:: GetBlockResult , Client , RpcApi } ;
910
@@ -12,13 +13,78 @@ pub enum BitcoindRpcItem {
1213 Block {
1314 cp : CheckPoint ,
1415 info : Box < GetBlockResult > ,
16+ block : Box < Block > ,
1517 } ,
1618 Mempool {
1719 cp : CheckPoint ,
1820 txs : Vec < ( Transaction , u64 ) > ,
1921 } ,
2022}
2123
24+ pub fn confirmation_height_anchor (
25+ info : & GetBlockResult ,
26+ _txid : Txid ,
27+ _tx_pos : usize ,
28+ ) -> ConfirmationHeightAnchor {
29+ ConfirmationHeightAnchor {
30+ anchor_block : BlockId {
31+ height : info. height as _ ,
32+ hash : info. hash ,
33+ } ,
34+ confirmation_height : info. height as _ ,
35+ }
36+ }
37+
38+ pub fn confirmation_time_anchor (
39+ info : & GetBlockResult ,
40+ _txid : Txid ,
41+ _tx_pos : usize ,
42+ ) -> ConfirmationTimeAnchor {
43+ ConfirmationTimeAnchor {
44+ anchor_block : BlockId {
45+ height : info. height as _ ,
46+ hash : info. hash ,
47+ } ,
48+ confirmation_height : info. height as _ ,
49+ confirmation_time : info. time as _ ,
50+ }
51+ }
52+
53+ impl BitcoindRpcItem {
54+ pub fn into_update < K , A , F > ( self , anchor : F ) -> LocalUpdate < K , A >
55+ where
56+ A : Clone + Ord + PartialOrd ,
57+ F : Fn ( & GetBlockResult , Txid , usize ) -> A ,
58+ {
59+ match self {
60+ BitcoindRpcItem :: Block { cp, info, block } => LocalUpdate {
61+ graph : {
62+ let mut g = TxGraph :: < A > :: new ( block. txdata ) ;
63+ for ( tx_pos, & txid) in info. tx . iter ( ) . enumerate ( ) {
64+ let _ = g. insert_anchor ( txid, anchor ( & info, txid, tx_pos) ) ;
65+ }
66+ g
67+ } ,
68+ ..LocalUpdate :: new ( cp)
69+ } ,
70+ BitcoindRpcItem :: Mempool { cp, txs } => LocalUpdate {
71+ graph : {
72+ let mut last_seens = Vec :: < ( Txid , u64 ) > :: with_capacity ( txs. len ( ) ) ;
73+ let mut g = TxGraph :: < A > :: new ( txs. into_iter ( ) . map ( |( tx, last_seen) | {
74+ last_seens. push ( ( tx. txid ( ) , last_seen) ) ;
75+ tx
76+ } ) ) ;
77+ for ( txid, seen_at) in last_seens {
78+ let _ = g. insert_seen_at ( txid, seen_at) ;
79+ }
80+ g
81+ } ,
82+ ..LocalUpdate :: new ( cp)
83+ } ,
84+ }
85+ }
86+ }
87+
2288pub struct BitcoindRpcIter < ' a > {
2389 client : & ' a Client ,
2490 fallback_height : u32 ,
@@ -57,6 +123,7 @@ impl<'a> BitcoindRpcIter<'a> {
57123 // get first item at fallback_height
58124 let info = client
59125 . get_block_info ( & client. get_block_hash ( self . fallback_height as _ ) ?) ?;
126+ let block = self . client . get_block ( & info. hash ) ?;
60127 let cp = CheckPoint :: new ( BlockId {
61128 height : info. height as _ ,
62129 hash : info. hash ,
@@ -66,6 +133,7 @@ impl<'a> BitcoindRpcIter<'a> {
66133 return Ok ( Some ( BitcoindRpcItem :: Block {
67134 cp,
68135 info : Box :: new ( info) ,
136+ block : Box :: new ( block) ,
69137 } ) ) ;
70138 }
71139 ( last_cp @ Some ( _) , last_info @ None ) => {
@@ -98,6 +166,7 @@ impl<'a> BitcoindRpcIter<'a> {
98166 continue ' main_loop;
99167 }
100168
169+ let block = self . client . get_block ( & info. hash ) ?;
101170 let cp = CheckPoint :: new_with_prev (
102171 BlockId {
103172 height : info. height as _ ,
@@ -113,6 +182,7 @@ impl<'a> BitcoindRpcIter<'a> {
113182 return Ok ( Some ( BitcoindRpcItem :: Block {
114183 cp,
115184 info : Box :: new ( info) ,
185+ block : Box :: new ( block) ,
116186 } ) ) ;
117187 }
118188 None => {
0 commit comments