Skip to content

Commit

Permalink
Mock different channel closes (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-21 authored Dec 16, 2024
1 parent c4987d4 commit 2aac09f
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions mock/breez-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,10 @@ impl BreezServices {
"pay.err.connectivity" | "pe.connectivity" => {
*PAYMENT_OUTCOME.lock().await = PaymentOutcome::ServiceConnectivity
}
"mimic.activities" | "ma" => self.simulate_activities(),
"mimic.pay2addr" | "mp" => self.simulate_payments(PaymentType::Sent, 10, true).await,
"channels.close_largest" | "cclose" => close_channel_with_largest_balance().await,
"channels.confirm_pending_closes" | "cconf" => confirm_pending_channel_closes().await,
"channels.confirm_pending_closes" | "cconf" => confirm_pending_channel_closes(),
"swaps.start" | "ss" => self.start_swap(req.amount_msat / 1_000).await?,
"swaps.confirm_onchain" | "sc" => self.confirm_swap_onchain().await?,
"swaps.redeem" | "sr" => {
Expand Down Expand Up @@ -1200,6 +1201,31 @@ impl BreezServices {
Ok(())
}

fn simulate_activities(&self) {
self.simulate_channle_closes();
}

fn simulate_channle_closes(&self) {
close_channel(Channel {
capacity_msat: 10_000_000,
local_balance_msat: 0,
});
close_channel(Channel {
capacity_msat: 10_000_000,
local_balance_msat: 5_000_000,
});
confirm_pending_channel_closes();

close_channel(Channel {
capacity_msat: 20_000_000,
local_balance_msat: 0,
});
close_channel(Channel {
capacity_msat: 20_000_000,
local_balance_msat: 7_000_000,
});
}

async fn simulate_payments(
&self,
payment_type: PaymentType,
Expand Down Expand Up @@ -1433,31 +1459,35 @@ async fn close_channel_with_largest_balance() {
.map(|(i, _)| i);

if let Some(i) = max_index {
let c = channels.remove(i);
CHANNELS_PENDING_CLOSE.lock().unwrap().push(c.clone());
let channel = channels.remove(i);
close_channel(channel);
}
}

let now = Utc::now().timestamp();
PAYMENTS.lock().unwrap().push(Payment {
id: now.to_string(),
payment_type: PaymentType::ClosedChannel,
payment_time: now,
amount_msat: c.local_balance_msat,
fee_msat: 0,
status: PaymentStatus::Pending,
error: None,
description: None,
details: PaymentDetails::ClosedChannel {
data: ClosedChannelPaymentDetails {
state: ChannelState::PendingClose,
funding_txid: TX_ID_DUMMY.to_string(),
short_channel_id: Some("mock_short_channel_id".to_string()),
closing_txid: Some(TX_ID_DUMMY.to_string()),
},
fn close_channel(channel: Channel) {
let now = Utc::now().timestamp();
PAYMENTS.lock().unwrap().push(Payment {
id: now.to_string(),
payment_type: PaymentType::ClosedChannel,
payment_time: now,
amount_msat: channel.local_balance_msat,
fee_msat: 0,
status: PaymentStatus::Pending,
error: None,
description: None,
details: PaymentDetails::ClosedChannel {
data: ClosedChannelPaymentDetails {
state: ChannelState::PendingClose,
funding_txid: TX_ID_DUMMY.to_string(),
short_channel_id: Some("mock_short_channel_id".to_string()),
closing_txid: Some(TX_ID_DUMMY.to_string()),
},
metadata: None,
})
}
},
metadata: None,
});
CHANNELS_PENDING_CLOSE.lock().unwrap().push(channel);
}

fn get_onchain_balance_msat() -> u64 {
CHANNELS_CLOSED
.lock()
Expand All @@ -1476,7 +1506,7 @@ fn get_pending_onchain_balance_msat() -> u64 {
.sum()
}

async fn confirm_pending_channel_closes() {
fn confirm_pending_channel_closes() {
CHANNELS_CLOSED
.lock()
.unwrap()
Expand Down

0 comments on commit 2aac09f

Please sign in to comment.