@@ -12,7 +12,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
1212
1313use base64:: prelude:: BASE64_STANDARD ;
1414use base64:: Engine ;
15- use bitcoin:: { BlockHash , FeeRate , Network , Transaction , Txid } ;
15+ use bitcoin:: { BlockHash , FeeRate , Network , OutPoint , Transaction , Txid } ;
1616use lightning:: chain:: chaininterface:: ConfirmationTarget as LdkConfirmationTarget ;
1717use lightning:: chain:: Listen ;
1818use lightning:: util:: ser:: Writeable ;
@@ -120,7 +120,7 @@ impl BitcoindChainSource {
120120 }
121121 }
122122
123- pub ( super ) fn as_utxo_source ( & self ) -> Arc < dyn UtxoSource > {
123+ pub ( super ) fn as_utxo_source ( & self ) -> UtxoSourceClient {
124124 self . api_client . utxo_source ( )
125125 }
126126
@@ -625,6 +625,68 @@ impl BitcoindChainSource {
625625 }
626626}
627627
628+ #[ derive( Clone ) ]
629+ pub ( crate ) enum UtxoSourceClient {
630+ Rpc ( Arc < RpcClient > ) ,
631+ Rest ( Arc < RestClient > ) ,
632+ }
633+
634+ impl std:: ops:: Deref for UtxoSourceClient {
635+ type Target = Self ;
636+ fn deref ( & self ) -> & Self {
637+ self
638+ }
639+ }
640+
641+ impl BlockSource for UtxoSourceClient {
642+ fn get_header < ' a > (
643+ & ' a self ,
644+ header_hash : & ' a BlockHash ,
645+ height_hint : Option < u32 > ,
646+ ) -> AsyncBlockSourceResult < ' a , BlockHeaderData > {
647+ match self {
648+ Self :: Rpc ( client) => client. get_header ( header_hash, height_hint) ,
649+ Self :: Rest ( client) => client. get_header ( header_hash, height_hint) ,
650+ }
651+ }
652+
653+ fn get_block < ' a > (
654+ & ' a self ,
655+ header_hash : & ' a BlockHash ,
656+ ) -> AsyncBlockSourceResult < ' a , BlockData > {
657+ match self {
658+ Self :: Rpc ( client) => client. get_block ( header_hash) ,
659+ Self :: Rest ( client) => client. get_block ( header_hash) ,
660+ }
661+ }
662+
663+ fn get_best_block (
664+ & self ,
665+ ) -> AsyncBlockSourceResult < ' _ , ( BlockHash , Option < u32 > ) > {
666+ match self {
667+ Self :: Rpc ( client) => client. get_best_block ( ) ,
668+ Self :: Rest ( client) => client. get_best_block ( ) ,
669+ }
670+ }
671+ }
672+
673+
674+ impl UtxoSource for UtxoSourceClient {
675+ fn get_block_hash_by_height < ' a > ( & ' a self , block_height : u32 ) -> AsyncBlockSourceResult < ' a , BlockHash > {
676+ match self {
677+ Self :: Rpc ( client) => client. get_block_hash_by_height ( block_height) ,
678+ Self :: Rest ( client) => client. get_block_hash_by_height ( block_height) ,
679+ }
680+ }
681+
682+ fn is_output_unspent < ' a > ( & ' a self , outpoint : OutPoint ) -> AsyncBlockSourceResult < ' a , bool > {
683+ match self {
684+ Self :: Rpc ( client) => client. is_output_unspent ( outpoint) ,
685+ Self :: Rest ( client) => client. is_output_unspent ( outpoint) ,
686+ }
687+ }
688+ }
689+
628690pub enum BitcoindClient {
629691 Rpc {
630692 rpc_client : Arc < RpcClient > ,
@@ -686,12 +748,10 @@ impl BitcoindClient {
686748 }
687749 }
688750
689- pub ( crate ) fn utxo_source ( & self ) -> Arc < dyn UtxoSource > {
751+ fn utxo_source ( & self ) -> UtxoSourceClient {
690752 match self {
691- BitcoindClient :: Rpc { rpc_client, .. } => Arc :: clone ( rpc_client) as Arc < dyn UtxoSource > ,
692- BitcoindClient :: Rest { rest_client, .. } => {
693- Arc :: clone ( rest_client) as Arc < dyn UtxoSource >
694- } ,
753+ Self :: Rpc { rpc_client, .. } => UtxoSourceClient :: Rpc ( Arc :: clone ( & rpc_client) ) ,
754+ Self :: Rest { rest_client, .. } => UtxoSourceClient :: Rest ( Arc :: clone ( & rest_client) ) ,
695755 }
696756 }
697757
0 commit comments