1
1
use crate :: logger:: {
2
2
log_error, log_given_level, log_internal, log_trace, FilesystemLogger , Logger ,
3
3
} ;
4
- use crate :: { Config , Error } ;
4
+ use crate :: { scid_utils , Config , Error } ;
5
5
6
6
use lightning:: chain:: chaininterface:: { BroadcasterInterface , ConfirmationTarget , FeeEstimator } ;
7
7
use lightning:: chain:: WatchedOutput ;
8
- use lightning:: chain:: { Confirm , Filter } ;
8
+ use lightning:: chain:: { Access , AccessError , Confirm , Filter } ;
9
9
10
10
use bdk:: blockchain:: { Blockchain , EsploraBlockchain } ;
11
11
use bdk:: database:: BatchDatabase ;
12
12
use bdk:: esplora_client;
13
13
use bdk:: wallet:: AddressIndex ;
14
14
use bdk:: { SignOptions , SyncOptions } ;
15
15
16
- use bitcoin:: { Script , Transaction , Txid } ;
16
+ use bitcoin:: { BlockHash , Script , Transaction , TxOut , Txid } ;
17
17
18
18
use std:: collections:: HashSet ;
19
19
use std:: sync:: { Arc , Mutex , RwLock } ;
@@ -33,15 +33,15 @@ where
33
33
D : BatchDatabase ,
34
34
{
35
35
blockchain : EsploraBlockchain ,
36
- _client : Arc < esplora_client:: AsyncClient > ,
36
+ client : Arc < esplora_client:: AsyncClient > ,
37
37
wallet : Mutex < bdk:: Wallet < D > > ,
38
38
queued_transactions : Mutex < Vec < Txid > > ,
39
39
watched_transactions : Mutex < Vec < Txid > > ,
40
40
queued_outputs : Mutex < Vec < WatchedOutput > > ,
41
41
watched_outputs : Mutex < Vec < WatchedOutput > > ,
42
42
last_sync_height : tokio:: sync:: Mutex < Option < u32 > > ,
43
43
tokio_runtime : RwLock < Option < Arc < tokio:: runtime:: Runtime > > > ,
44
- _config : Arc < Config > ,
44
+ config : Arc < Config > ,
45
45
logger : Arc < FilesystemLogger > ,
46
46
}
47
47
@@ -68,15 +68,15 @@ where
68
68
let client = Arc :: new ( client_builder. build_async ( ) . unwrap ( ) ) ;
69
69
Self {
70
70
blockchain,
71
- _client : client,
71
+ client,
72
72
wallet,
73
73
queued_transactions,
74
74
watched_transactions,
75
75
queued_outputs,
76
76
watched_outputs,
77
77
last_sync_height,
78
78
tokio_runtime,
79
- _config : config,
79
+ config,
80
80
logger,
81
81
}
82
82
}
@@ -361,6 +361,61 @@ where
361
361
}
362
362
}
363
363
364
+ impl < D > Access for ChainAccess < D >
365
+ where
366
+ D : BatchDatabase ,
367
+ {
368
+ fn get_utxo (
369
+ & self , genesis_hash : & BlockHash , short_channel_id : u64 ,
370
+ ) -> Result < TxOut , AccessError > {
371
+ if genesis_hash
372
+ != & bitcoin:: blockdata:: constants:: genesis_block ( self . config . network )
373
+ . header
374
+ . block_hash ( )
375
+ {
376
+ return Err ( AccessError :: UnknownChain ) ;
377
+ }
378
+
379
+ let locked_runtime = self . tokio_runtime . read ( ) . unwrap ( ) ;
380
+ if locked_runtime. as_ref ( ) . is_none ( ) {
381
+ return Err ( AccessError :: UnknownTx ) ;
382
+ }
383
+
384
+ let block_height = scid_utils:: block_from_scid ( & short_channel_id) ;
385
+ let tx_index = scid_utils:: tx_index_from_scid ( & short_channel_id) ;
386
+ let vout = scid_utils:: vout_from_scid ( & short_channel_id) ;
387
+
388
+ let client_tokio = Arc :: clone ( & self . client ) ;
389
+ locked_runtime. as_ref ( ) . unwrap ( ) . block_on ( async move {
390
+ // TODO: migrate to https://github.com/bitcoindevkit/rust-esplora-client/pull/13 with
391
+ // next release.
392
+ let block_hash = client_tokio
393
+ . get_header ( block_height. into ( ) )
394
+ . await
395
+ . map_err ( |_| AccessError :: UnknownTx ) ?
396
+ . block_hash ( ) ;
397
+
398
+ let txid = client_tokio
399
+ . get_txid_at_block_index ( & block_hash, tx_index as usize )
400
+ . await
401
+ . map_err ( |_| AccessError :: UnknownTx ) ?
402
+ . ok_or ( AccessError :: UnknownTx ) ?;
403
+
404
+ let tx = client_tokio
405
+ . get_tx ( & txid)
406
+ . await
407
+ . map_err ( |_| AccessError :: UnknownTx ) ?
408
+ . ok_or ( AccessError :: UnknownTx ) ?;
409
+
410
+ if let Some ( tx_out) = tx. output . get ( vout as usize ) {
411
+ return Ok ( tx_out. clone ( ) ) ;
412
+ } else {
413
+ Err ( AccessError :: UnknownTx )
414
+ }
415
+ } )
416
+ }
417
+ }
418
+
364
419
fn num_blocks_from_conf_target ( confirmation_target : ConfirmationTarget ) -> usize {
365
420
match confirmation_target {
366
421
ConfirmationTarget :: Background => 12 ,
0 commit comments