|
5 | 5 | // http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
|
6 | 6 | // accordance with one or both of these licenses.
|
7 | 7 |
|
| 8 | +use crate::config::TX_BROADCAST_TIMEOUT_SECS; |
8 | 9 | use crate::error::Error;
|
9 |
| -use crate::logger::{log_error, LdkLogger, Logger}; |
| 10 | +use crate::logger::{log_bytes, log_error, log_trace, LdkLogger, Logger}; |
10 | 11 |
|
11 | 12 | use lightning::chain::{Filter, WatchedOutput};
|
| 13 | +use lightning::util::ser::Writeable; |
12 | 14 | use lightning_transaction_sync::ElectrumSyncClient;
|
13 | 15 |
|
14 | 16 | use bdk_electrum::BdkElectrumClient;
|
15 | 17 |
|
16 | 18 | use electrum_client::Client as ElectrumClient;
|
17 | 19 | use electrum_client::ConfigBuilder as ElectrumConfigBuilder;
|
| 20 | +use electrum_client::ElectrumApi; |
18 | 21 |
|
19 |
| -use bitcoin::{Script, Txid}; |
| 22 | +use bitcoin::{Script, Transaction, Txid}; |
20 | 23 |
|
21 | 24 | use std::sync::Arc;
|
| 25 | +use std::time::Duration; |
22 | 26 |
|
23 | 27 | const ELECTRUM_CLIENT_NUM_RETRIES: u8 = 3;
|
24 | 28 | const ELECTRUM_CLIENT_TIMEOUT_SECS: u8 = 20;
|
@@ -60,6 +64,48 @@ impl ElectrumRuntimeClient {
|
60 | 64 | );
|
61 | 65 | Ok(Self { electrum_client, bdk_electrum_client, tx_sync, runtime, logger })
|
62 | 66 | }
|
| 67 | + |
| 68 | + pub(crate) async fn broadcast(&self, tx: Transaction) { |
| 69 | + let electrum_client = Arc::clone(&self.electrum_client); |
| 70 | + |
| 71 | + let txid = tx.compute_txid(); |
| 72 | + let tx_bytes = tx.encode(); |
| 73 | + |
| 74 | + let spawn_fut = |
| 75 | + self.runtime.spawn_blocking(move || electrum_client.transaction_broadcast(&tx)); |
| 76 | + |
| 77 | + let timeout_fut = |
| 78 | + tokio::time::timeout(Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS), spawn_fut); |
| 79 | + |
| 80 | + match timeout_fut.await { |
| 81 | + Ok(res) => match res { |
| 82 | + Ok(_) => { |
| 83 | + log_trace!(self.logger, "Successfully broadcast transaction {}", txid); |
| 84 | + }, |
| 85 | + Err(e) => { |
| 86 | + log_error!(self.logger, "Failed to broadcast transaction {}: {}", txid, e); |
| 87 | + log_trace!( |
| 88 | + self.logger, |
| 89 | + "Failed broadcast transaction bytes: {}", |
| 90 | + log_bytes!(tx_bytes) |
| 91 | + ); |
| 92 | + }, |
| 93 | + }, |
| 94 | + Err(e) => { |
| 95 | + log_error!( |
| 96 | + self.logger, |
| 97 | + "Failed to broadcast transaction due to timeout {}: {}", |
| 98 | + txid, |
| 99 | + e |
| 100 | + ); |
| 101 | + log_trace!( |
| 102 | + self.logger, |
| 103 | + "Failed broadcast transaction bytes: {}", |
| 104 | + log_bytes!(tx_bytes) |
| 105 | + ); |
| 106 | + }, |
| 107 | + } |
| 108 | + } |
63 | 109 | }
|
64 | 110 |
|
65 | 111 | impl Filter for ElectrumRuntimeClient {
|
|
0 commit comments