Skip to content

Commit

Permalink
Merge pull request #1749 from get10101/fix/on-off-chain-syncs
Browse files Browse the repository at this point in the history
Trigger on/off-chain syncs back to back in tests
  • Loading branch information
luckysori authored Dec 14, 2023
2 parents afd17fe + ac7cfe4 commit df76dee
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 58 deletions.
20 changes: 10 additions & 10 deletions crates/ln-dlc-node/src/tests/dlc/non_collaborative_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn force_close_ln_dlc_channel() {
.await
.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Act

Expand All @@ -55,8 +55,8 @@ async fn force_close_ln_dlc_channel() {
// transactions
mine(288).await.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Ensure publication of the glue and buffer transactions (otherwise we need to wait for the
// periodic task)
Expand All @@ -70,17 +70,17 @@ async fn force_close_ln_dlc_channel() {

// Assert

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Mining 288 blocks ensures that we get:
// - 144 required confirmations for the delayed output on the LN commitment transaction to be
// spendable.
// - 288 required confirmations for the CET to be published.
mine(288).await.unwrap();

coordinator.sync_on_chain().await.unwrap();
app.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
app.sync_wallets().await.unwrap();

// Ensure publication of CET (otherwise we need to wait for the periodic task)
sub_channel_manager_periodic_check(
Expand All @@ -95,9 +95,9 @@ async fn force_close_ln_dlc_channel() {
mine(1).await.unwrap();
tracing::info!("Mined 1 block");

coordinator.sync_on_chain().await.unwrap();
coordinator.sync_wallets().await.unwrap();
tracing::info!("Coordinator synced on-chain");
app.sync_on_chain().await.unwrap();
app.sync_wallets().await.unwrap();
tracing::info!("App synced on-chain");

let coordinator_on_chain_balance_after_force_close =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn ln_collab_close() {

// Mine one block to confirm the close transaction
bitcoind::mine(1).await.unwrap();
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Assert

Expand Down Expand Up @@ -134,7 +134,7 @@ async fn ln_force_close() {
.force_close_broadcasting_latest_txn(&channel_id, &coordinator.info.pubkey)
.unwrap();

payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

assert_eq!(payee.get_on_chain_balance().unwrap().confirmed, 0);
assert_eq!(payee.get_ldk_balance().available(), 0);
Expand All @@ -155,12 +155,12 @@ async fn ln_force_close() {
// Syncing the payee's wallet should now trigger a `SpendableOutputs` event
// corresponding to their revocable output in the commitment transaction, which they
// will subsequently spend in a new transaction paying to their on-chain wallet
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Mine one more block to confirm the transaction spending the payee's revocable output
// in the commitment transaction
bitcoind::mine(1).await.unwrap();
payee.sync_on_chain().await.unwrap();
payee.sync_wallets().await.unwrap();

// Assert

Expand Down
24 changes: 12 additions & 12 deletions crates/ln-dlc-node/src/tests/just_in_time_channel/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ pub(crate) async fn send_interceptable_payment(
let user_channel_id = liquidity_request.user_channel_id;
let jit_channel_fee = liquidity_request.fee_sats;

payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_before = payer.get_ldk_balance();
let coordinator_balance_before = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -310,9 +310,9 @@ pub(crate) async fn send_interceptable_payment(
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -346,9 +346,9 @@ pub(crate) async fn send_payment(
invoice_amount_sat: u64,
coordinator_just_in_time_channel_creation_outbound_liquidity: Option<u64>,
) -> Result<()> {
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_before = payer.get_ldk_balance();
let coordinator_balance_before = coordinator.get_ldk_balance();
Expand Down Expand Up @@ -394,9 +394,9 @@ pub(crate) async fn send_payment(
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.ldk_wallet().sync()?;
coordinator.ldk_wallet().sync()?;
payee.ldk_wallet().sync()?;
payer.sync_wallets().await?;
coordinator.sync_wallets().await?;
payee.sync_wallets().await?;

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down
17 changes: 11 additions & 6 deletions crates/ln-dlc-node/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,20 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
Ok((node, running))
}

/// Trigger on-chain wallet sync.
/// Trigger on-chain and off-chain wallet syncs.
///
/// We wrap the wallet sync with a `block_in_place` to avoid blocking the async task in
/// `tokio::test`s.
///
/// Because we use `block_in_place`, we must configure the `tokio::test`s with `flavor =
/// "multi_thread"`.
async fn sync_on_chain(&self) -> Result<()> {
block_in_place(|| self.ldk_wallet().sync())
async fn sync_wallets(&self) -> Result<()> {
block_in_place(|| {
self.sync_on_chain_wallet()?;
self.sync_lightning_wallet()?;

Ok(())
})
}

async fn fund(&self, amount: Amount) -> Result<()> {
Expand All @@ -238,7 +243,7 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
while self.get_confirmed_balance().await.unwrap() < expected_balance {
let interval = Duration::from_millis(200);

self.sync_on_chain().await.unwrap();
self.sync_wallets().await.unwrap();

tokio::time::sleep(interval).await;
tracing::debug!(
Expand Down Expand Up @@ -325,8 +330,8 @@ impl Node<TenTenOneInMemoryStorage, InMemoryStore> {
// We need to sync both parties, even if
// `trust_own_funding_0conf` is true for the creator
// of the channel (`self`)
self.sync_on_chain().await.unwrap();
peer.sync_on_chain().await.unwrap();
self.sync_wallets().await.unwrap();
peer.sync_wallets().await.unwrap();
}

tracing::debug!(
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/tests/multi_hop_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ async fn multi_hop_payment() {
let coordinator_balance_before = coordinator.get_ldk_balance();
let payee_balance_before = payee.get_ldk_balance();

payer.sync_on_chain().await.unwrap();
coordinator.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
coordinator.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

// Act

Expand All @@ -74,9 +74,9 @@ async fn multi_hop_payment() {
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.sync_on_chain().await.unwrap();
coordinator.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
coordinator.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

let payer_balance_after = payer.get_ldk_balance();
let coordinator_balance_after = coordinator.get_ldk_balance();
Expand Down
12 changes: 6 additions & 6 deletions crates/ln-dlc-node/src/tests/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ async fn estimate_payment_fee() {
tracing::info!("Waiting for channels to be discovered");

for _ in 0..5 {
payer.sync_on_chain().await.unwrap();
a.sync_on_chain().await.unwrap();
b.sync_on_chain().await.unwrap();
c.sync_on_chain().await.unwrap();
d.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
a.sync_wallets().await.unwrap();
b.sync_wallets().await.unwrap();
c.sync_wallets().await.unwrap();
d.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

payer.broadcast_node_announcement();
a.broadcast_node_announcement();
Expand Down
4 changes: 2 additions & 2 deletions crates/ln-dlc-node/src/tests/single_hop_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async fn single_hop_payment() {
// Assert

// Sync LN wallet after payment is claimed to update the balances
payer.sync_on_chain().await.unwrap();
payee.sync_on_chain().await.unwrap();
payer.sync_wallets().await.unwrap();
payee.sync_wallets().await.unwrap();

let payer_balance_after = payer.get_ldk_balance();
let payee_balance_after = payee.get_ldk_balance();
Expand Down
4 changes: 2 additions & 2 deletions crates/tests-e2e/examples/fund.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async fn fund_everything(faucet: &str, coordinator: &str, maker: &str) -> Result
bitcoind.fund(&maker_addr, Amount::ONE_BTC).await?;
bitcoind.mine(10).await?;
maker
.sync_on_chain()
.sync()
.await
.context("Failed to sync maker's on-chain wallet")?;

Expand Down Expand Up @@ -155,7 +155,7 @@ async fn fund_everything(faucet: &str, coordinator: &str, maker: &str) -> Result
bitcoind.mine(10).await?;

maker
.sync_on_chain()
.sync()
.await
.expect("to be able to sync on-chain wallet for maker");
let maker_balance = maker.get_balance().await?;
Expand Down
5 changes: 3 additions & 2 deletions crates/tests-e2e/src/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ impl Maker {
.is_ok_and(|r| r.status().is_success())
}

pub async fn sync_on_chain(&self) -> Result<()> {
/// Sync the on-chain and Lightning wallets.
pub async fn sync(&self) -> Result<()> {
let no_json: Option<()> = None;
self.post("/api/sync-on-chain", no_json).await?;
self.post("/api/sync", no_json).await?;
Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions crates/tests-e2e/tests/maker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async fn maker_can_open_channel_to_coordinator_and_send_payment() -> Result<()>
.await
.unwrap();
bitcoind.mine(1).await.unwrap();
maker.sync_on_chain().await.unwrap();
maker.sync().await.unwrap();

let balance_maker_before_channel = maker.get_balance().await?.offchain;

Expand All @@ -47,7 +47,7 @@ async fn maker_can_open_channel_to_coordinator_and_send_payment() -> Result<()>
// Mine one block to render the public channel is usable.
bitcoind.mine(1).await.unwrap();
coordinator.sync_wallet().await.unwrap();
maker.sync_on_chain().await.unwrap();
maker.sync().await.unwrap();

let balance_maker_after_channel = maker.get_balance().await?.offchain;

Expand Down
18 changes: 12 additions & 6 deletions maker/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn router(
.route("/api/channels", get(list_channels).post(create_channel))
.route("/api/connect", post(connect_to_peer))
.route("/api/pay-invoice/:invoice", post(pay_invoice))
.route("/api/sync-on-chain", post(sync_on_chain))
.route("/api/sync", post(sync))
.route("/api/position", get(get_position))
.route("/api/node", get(get_node_info))
.route("/metrics", get(get_metrics))
Expand Down Expand Up @@ -260,11 +260,17 @@ pub async fn pay_invoice(
Ok(())
}

pub async fn sync_on_chain(State(state): State<Arc<AppState>>) -> Result<(), AppError> {
spawn_blocking(move || state.node.ldk_wallet().sync())
.await
.expect("task to complete")
.map_err(|e| AppError::InternalServerError(format!("Could not sync wallet: {e:#}")))?;
/// Internal API for syncing the on-chain and Lightning wallets.
pub async fn sync(State(state): State<Arc<AppState>>) -> Result<(), AppError> {
spawn_blocking(move || {
state.node.sync_on_chain_wallet()?;
state.node.sync_lightning_wallet()?;

anyhow::Ok(())
})
.await
.expect("task to complete")
.map_err(|e| AppError::InternalServerError(format!("Could not sync wallet: {e:#}")))?;

Ok(())
}
Expand Down

0 comments on commit df76dee

Please sign in to comment.