Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ dictionary BackgroundSyncConfig {
u64 fee_rate_cache_update_interval_secs;
};

dictionary SyncTimeoutsConfig {
u64 onchain_wallet_sync_timeout_secs;
u64 lightning_wallet_sync_timeout_secs;
u64 fee_rate_cache_update_timeout_secs;
u64 tx_broadcast_timeout_secs;
u8 per_request_timeout_secs;
};

dictionary EsploraSyncConfig {
BackgroundSyncConfig? background_sync_config;
SyncTimeoutsConfig timeouts_config;
};

dictionary ElectrumSyncConfig {
BackgroundSyncConfig? background_sync_config;
SyncTimeoutsConfig timeouts_config;
};

dictionary LSPS2ServiceConfig {
Expand Down
7 changes: 4 additions & 3 deletions src/chain/bitcoind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use serde::Serialize;

use super::WalletSyncStatus;
use crate::config::{
BitcoindRestClientConfig, Config, FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, TX_BROADCAST_TIMEOUT_SECS,
BitcoindRestClientConfig, Config, DEFAULT_FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS,
DEFAULT_TX_BROADCAST_TIMEOUT_SECS,
};
use crate::fee_estimator::{
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
Expand Down Expand Up @@ -466,7 +467,7 @@ impl BitcoindChainSource {
macro_rules! get_fee_rate_update {
($estimation_fut:expr) => {{
let update_res = tokio::time::timeout(
Duration::from_secs(FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS),
Duration::from_secs(DEFAULT_FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS),
$estimation_fut,
)
.await
Expand Down Expand Up @@ -584,7 +585,7 @@ impl BitcoindChainSource {
for tx in &package {
let txid = tx.compute_txid();
let timeout_fut = tokio::time::timeout(
Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS),
Duration::from_secs(DEFAULT_TX_BROADCAST_TIMEOUT_SECS),
self.api_client.broadcast_transaction(tx),
);
match timeout_fut.await {
Expand Down
60 changes: 40 additions & 20 deletions src/chain/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use lightning::util::ser::Writeable;
use lightning_transaction_sync::ElectrumSyncClient;

use super::WalletSyncStatus;
use crate::config::{
Config, ElectrumSyncConfig, BDK_CLIENT_STOP_GAP, BDK_WALLET_SYNC_TIMEOUT_SECS,
FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS, TX_BROADCAST_TIMEOUT_SECS,
};
use crate::config::{Config, ElectrumSyncConfig, BDK_CLIENT_STOP_GAP};
use crate::error::Error;
use crate::fee_estimator::{
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
Expand All @@ -41,7 +38,6 @@ use crate::NodeMetrics;

const BDK_ELECTRUM_CLIENT_BATCH_SIZE: usize = 5;
const ELECTRUM_CLIENT_NUM_RETRIES: u8 = 3;
const ELECTRUM_CLIENT_TIMEOUT_SECS: u8 = 10;

pub(super) struct ElectrumChainSource {
server_url: String,
Expand Down Expand Up @@ -82,6 +78,7 @@ impl ElectrumChainSource {
pub(super) fn start(&self, runtime: Arc<Runtime>) -> Result<(), Error> {
self.electrum_runtime_status.write().unwrap().start(
self.server_url.clone(),
self.sync_config.clone(),
Arc::clone(&runtime),
Arc::clone(&self.config),
Arc::clone(&self.logger),
Expand Down Expand Up @@ -318,13 +315,14 @@ impl ElectrumRuntimeStatus {
}

pub(super) fn start(
&mut self, server_url: String, runtime: Arc<Runtime>, config: Arc<Config>,
logger: Arc<Logger>,
&mut self, server_url: String, sync_config: ElectrumSyncConfig, runtime: Arc<Runtime>,
config: Arc<Config>, logger: Arc<Logger>,
) -> Result<(), Error> {
match self {
Self::Stopped { pending_registered_txs, pending_registered_outputs } => {
let client = Arc::new(ElectrumRuntimeClient::new(
server_url.clone(),
server_url,
sync_config,
runtime,
config,
logger,
Expand Down Expand Up @@ -380,6 +378,7 @@ impl ElectrumRuntimeStatus {

struct ElectrumRuntimeClient {
electrum_client: Arc<ElectrumClient>,
sync_config: ElectrumSyncConfig,
bdk_electrum_client: Arc<BdkElectrumClient<Arc<ElectrumClient>>>,
tx_sync: Arc<ElectrumSyncClient<Arc<Logger>>>,
runtime: Arc<Runtime>,
Expand All @@ -389,11 +388,12 @@ struct ElectrumRuntimeClient {

impl ElectrumRuntimeClient {
fn new(
server_url: String, runtime: Arc<Runtime>, config: Arc<Config>, logger: Arc<Logger>,
server_url: String, sync_config: ElectrumSyncConfig, runtime: Arc<Runtime>,
config: Arc<Config>, logger: Arc<Logger>,
) -> Result<Self, Error> {
let electrum_config = ElectrumConfigBuilder::new()
.retry(ELECTRUM_CLIENT_NUM_RETRIES)
.timeout(Some(ELECTRUM_CLIENT_TIMEOUT_SECS))
.timeout(Some(sync_config.timeouts_config.per_request_timeout_secs))
.build();

let electrum_client = Arc::new(
Expand All @@ -409,7 +409,15 @@ impl ElectrumRuntimeClient {
Error::ConnectionFailed
})?,
);
Ok(Self { electrum_client, bdk_electrum_client, tx_sync, runtime, config, logger })
Ok(Self {
electrum_client,
sync_config,
bdk_electrum_client,
tx_sync,
runtime,
config,
logger,
})
}

async fn sync_confirmables(
Expand All @@ -419,8 +427,12 @@ impl ElectrumRuntimeClient {

let tx_sync = Arc::clone(&self.tx_sync);
let spawn_fut = self.runtime.spawn_blocking(move || tx_sync.sync(confirmables));
let timeout_fut =
tokio::time::timeout(Duration::from_secs(LDK_WALLET_SYNC_TIMEOUT_SECS), spawn_fut);
let timeout_fut = tokio::time::timeout(
Duration::from_secs(
self.sync_config.timeouts_config.lightning_wallet_sync_timeout_secs,
),
spawn_fut,
);

let res = timeout_fut
.await
Expand Down Expand Up @@ -461,8 +473,10 @@ impl ElectrumRuntimeClient {
true,
)
});
let wallet_sync_timeout_fut =
tokio::time::timeout(Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS), spawn_fut);
let wallet_sync_timeout_fut = tokio::time::timeout(
Duration::from_secs(self.sync_config.timeouts_config.onchain_wallet_sync_timeout_secs),
spawn_fut,
);

wallet_sync_timeout_fut
.await
Expand Down Expand Up @@ -490,8 +504,10 @@ impl ElectrumRuntimeClient {
let spawn_fut = self.runtime.spawn_blocking(move || {
bdk_electrum_client.sync(request, BDK_ELECTRUM_CLIENT_BATCH_SIZE, true)
});
let wallet_sync_timeout_fut =
tokio::time::timeout(Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS), spawn_fut);
let wallet_sync_timeout_fut = tokio::time::timeout(
Duration::from_secs(self.sync_config.timeouts_config.onchain_wallet_sync_timeout_secs),
spawn_fut,
);

wallet_sync_timeout_fut
.await
Expand All @@ -517,8 +533,10 @@ impl ElectrumRuntimeClient {

let spawn_fut =
self.runtime.spawn_blocking(move || electrum_client.transaction_broadcast(&tx));
let timeout_fut =
tokio::time::timeout(Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS), spawn_fut);
let timeout_fut = tokio::time::timeout(
Duration::from_secs(self.sync_config.timeouts_config.tx_broadcast_timeout_secs),
spawn_fut,
);

match timeout_fut.await {
Ok(res) => match res {
Expand Down Expand Up @@ -565,7 +583,9 @@ impl ElectrumRuntimeClient {
let spawn_fut = self.runtime.spawn_blocking(move || electrum_client.batch_call(&batch));

let timeout_fut = tokio::time::timeout(
Duration::from_secs(FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS),
Duration::from_secs(
self.sync_config.timeouts_config.fee_rate_cache_update_timeout_secs,
),
spawn_fut,
);

Expand Down
27 changes: 16 additions & 11 deletions src/chain/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ use lightning::util::ser::Writeable;
use lightning_transaction_sync::EsploraSyncClient;

use super::WalletSyncStatus;
use crate::config::{
Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP,
BDK_WALLET_SYNC_TIMEOUT_SECS, DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS,
FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS, LDK_WALLET_SYNC_TIMEOUT_SECS, TX_BROADCAST_TIMEOUT_SECS,
};
use crate::config::{Config, EsploraSyncConfig, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP};
use crate::fee_estimator::{
apply_post_estimation_adjustments, get_all_conf_targets, get_num_block_defaults_for_target,
OnchainFeeEstimator,
Expand Down Expand Up @@ -51,7 +47,8 @@ impl EsploraChainSource {
logger: Arc<Logger>, node_metrics: Arc<RwLock<NodeMetrics>>,
) -> Self {
let mut client_builder = esplora_client::Builder::new(&server_url);
client_builder = client_builder.timeout(DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS);
client_builder =
client_builder.timeout(sync_config.timeouts_config.per_request_timeout_secs as u64);

for (header_name, header_value) in &headers {
client_builder = client_builder.header(header_name, header_value);
Expand Down Expand Up @@ -183,14 +180,18 @@ impl EsploraChainSource {
if incremental_sync {
let sync_request = onchain_wallet.get_incremental_sync_request();
let wallet_sync_timeout_fut = tokio::time::timeout(
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
Duration::from_secs(
self.sync_config.timeouts_config.onchain_wallet_sync_timeout_secs,
),
self.esplora_client.sync(sync_request, BDK_CLIENT_CONCURRENCY),
);
get_and_apply_wallet_update!(wallet_sync_timeout_fut)
} else {
let full_scan_request = onchain_wallet.get_full_scan_request();
let wallet_sync_timeout_fut = tokio::time::timeout(
Duration::from_secs(BDK_WALLET_SYNC_TIMEOUT_SECS),
Duration::from_secs(
self.sync_config.timeouts_config.onchain_wallet_sync_timeout_secs,
),
self.esplora_client.full_scan(
full_scan_request,
BDK_CLIENT_STOP_GAP,
Expand Down Expand Up @@ -240,7 +241,9 @@ impl EsploraChainSource {
];

let timeout_fut = tokio::time::timeout(
Duration::from_secs(LDK_WALLET_SYNC_TIMEOUT_SECS),
Duration::from_secs(
self.sync_config.timeouts_config.lightning_wallet_sync_timeout_secs as u64,
),
self.tx_sync.sync(confirmables),
);
let now = Instant::now();
Expand Down Expand Up @@ -278,7 +281,9 @@ impl EsploraChainSource {
pub(crate) async fn update_fee_rate_estimates(&self) -> Result<(), Error> {
let now = Instant::now();
let estimates = tokio::time::timeout(
Duration::from_secs(FEE_RATE_CACHE_UPDATE_TIMEOUT_SECS),
Duration::from_secs(
self.sync_config.timeouts_config.fee_rate_cache_update_timeout_secs,
),
self.esplora_client.get_fee_estimates(),
)
.await
Expand Down Expand Up @@ -351,7 +356,7 @@ impl EsploraChainSource {
for tx in &package {
let txid = tx.compute_txid();
let timeout_fut = tokio::time::timeout(
Duration::from_secs(TX_BROADCAST_TIMEOUT_SECS),
Duration::from_secs(self.sync_config.timeouts_config.tx_broadcast_timeout_secs),
self.esplora_client.broadcast(tx),
);
match timeout_fut.await {
Expand Down
Loading
Loading