|
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::EsploraBlockchain;
|
11 | 11 | use bdk::database::BatchDatabase;
|
12 | 12 | use bdk::esplora_client;
|
13 | 13 | use bdk::wallet::AddressIndex;
|
14 | 14 | use bdk::{FeeRate, 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::{mpsc, Arc, Mutex, RwLock};
|
|
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 |
|
|
78 | 78 | watched_outputs,
|
79 | 79 | last_sync_height,
|
80 | 80 | tokio_runtime,
|
81 |
| - _config: config, |
| 81 | + config, |
82 | 82 | logger,
|
83 | 83 | }
|
84 | 84 | }
|
@@ -387,6 +387,61 @@ where
|
387 | 387 | }
|
388 | 388 | }
|
389 | 389 |
|
| 390 | +impl<D> Access for ChainAccess<D> |
| 391 | +where |
| 392 | + D: BatchDatabase, |
| 393 | +{ |
| 394 | + fn get_utxo( |
| 395 | + &self, genesis_hash: &BlockHash, short_channel_id: u64, |
| 396 | + ) -> Result<TxOut, AccessError> { |
| 397 | + if genesis_hash |
| 398 | + != &bitcoin::blockdata::constants::genesis_block(self.config.network) |
| 399 | + .header |
| 400 | + .block_hash() |
| 401 | + { |
| 402 | + return Err(AccessError::UnknownChain); |
| 403 | + } |
| 404 | + |
| 405 | + let locked_runtime = self.tokio_runtime.read().unwrap(); |
| 406 | + if locked_runtime.as_ref().is_none() { |
| 407 | + return Err(AccessError::UnknownTx); |
| 408 | + } |
| 409 | + |
| 410 | + let block_height = scid_utils::block_from_scid(&short_channel_id); |
| 411 | + let tx_index = scid_utils::tx_index_from_scid(&short_channel_id); |
| 412 | + let vout = scid_utils::vout_from_scid(&short_channel_id); |
| 413 | + |
| 414 | + let client_tokio = Arc::clone(&self.client); |
| 415 | + locked_runtime.as_ref().unwrap().block_on(async move { |
| 416 | + // TODO: migrate to https://github.com/bitcoindevkit/rust-esplora-client/pull/13 with |
| 417 | + // next release. |
| 418 | + let block_hash = client_tokio |
| 419 | + .get_header(block_height.into()) |
| 420 | + .await |
| 421 | + .map_err(|_| AccessError::UnknownTx)? |
| 422 | + .block_hash(); |
| 423 | + |
| 424 | + let txid = client_tokio |
| 425 | + .get_txid_at_block_index(&block_hash, tx_index as usize) |
| 426 | + .await |
| 427 | + .map_err(|_| AccessError::UnknownTx)? |
| 428 | + .ok_or(AccessError::UnknownTx)?; |
| 429 | + |
| 430 | + let tx = client_tokio |
| 431 | + .get_tx(&txid) |
| 432 | + .await |
| 433 | + .map_err(|_| AccessError::UnknownTx)? |
| 434 | + .ok_or(AccessError::UnknownTx)?; |
| 435 | + |
| 436 | + if let Some(tx_out) = tx.output.get(vout as usize) { |
| 437 | + return Ok(tx_out.clone()); |
| 438 | + } else { |
| 439 | + Err(AccessError::UnknownTx) |
| 440 | + } |
| 441 | + }) |
| 442 | + } |
| 443 | +} |
| 444 | + |
390 | 445 | fn num_blocks_from_conf_target(confirmation_target: ConfirmationTarget) -> usize {
|
391 | 446 | match confirmation_target {
|
392 | 447 | ConfirmationTarget::Background => 12,
|
|
0 commit comments