Skip to content

Commit 186d66b

Browse files
committed
wip acceptor contributes in tests
1 parent fbaad60 commit 186d66b

File tree

3 files changed

+257
-66
lines changed

3 files changed

+257
-66
lines changed

lightning/src/ln/channel.rs

+41-13
Original file line numberDiff line numberDiff line change
@@ -6892,10 +6892,24 @@ impl<SP: Deref> FundedChannel<SP> where
68926892
assert!(self.context.channel_state.is_monitor_update_in_progress());
68936893
self.context.channel_state.clear_monitor_update_in_progress();
68946894

6895+
// For channels established with V2 establishment we won't send a `tx_signatures` when we're in
6896+
// MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
6897+
// transaction and waits for us to do it).
6898+
let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6899+
if tx_signatures.is_some() {
6900+
if self.context.channel_state.is_their_tx_signatures_sent() {
6901+
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6902+
} else {
6903+
self.context.channel_state.set_our_tx_signatures_ready();
6904+
}
6905+
}
6906+
68956907
// If we're past (or at) the AwaitingChannelReady stage on an outbound (or V2-established) channel,
68966908
// try to (re-)broadcast the funding transaction as we may have declined to broadcast it when we
68976909
// first received the funding_signed.
68986910
let mut funding_broadcastable = None;
6911+
dbg!(&self.funding.funding_transaction);
6912+
dbg!(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)));
68996913
if let Some(funding_transaction) = &self.funding.funding_transaction {
69006914
if (self.funding.is_outbound() || self.is_v2_established()) &&
69016915
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
@@ -6931,17 +6945,6 @@ impl<SP: Deref> FundedChannel<SP> where
69316945
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
69326946
let mut pending_update_adds = Vec::new();
69336947
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
6934-
// For channels established with V2 establishment we won't send a `tx_signatures` when we're in
6935-
// MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
6936-
// transaction and waits for us to do it).
6937-
let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6938-
if tx_signatures.is_some() {
6939-
if self.context.channel_state.is_their_tx_signatures_sent() {
6940-
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6941-
} else {
6942-
self.context.channel_state.set_our_tx_signatures_ready();
6943-
}
6944-
}
69456948

69466949
if self.context.channel_state.is_peer_disconnected() {
69476950
self.context.monitor_pending_revoke_and_ack = false;
@@ -8246,7 +8249,7 @@ impl<SP: Deref> FundedChannel<SP> where
82468249
/// advanced state.
82478250
pub fn is_awaiting_initial_mon_persist(&self) -> bool {
82488251
if !self.is_awaiting_monitor_update() { return false; }
8249-
if matches!(
8252+
if self.context.channel_state.is_interactive_signing() || matches!(
82508253
self.context.channel_state, ChannelState::AwaitingChannelReady(flags)
82518254
if flags.clone().clear(AwaitingChannelReadyFlags::THEIR_CHANNEL_READY | FundedStateFlags::PEER_DISCONNECTED | FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS | AwaitingChannelReadyFlags::WAITING_FOR_BATCH).is_empty()
82528255
) {
@@ -10531,6 +10534,31 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1053110534
our_funding_inputs: our_funding_inputs.clone(),
1053210535
};
1053310536

10537+
// Optionally add change output
10538+
let change_script = signer_provider.get_destination_script(context.channel_keys_id)
10539+
.map_err(|_| ChannelError::close("Error getting change destination script".to_string()))?;
10540+
let change_value_opt = calculate_change_output_value(
10541+
funding.is_outbound(), dual_funding_context.our_funding_satoshis,
10542+
&our_funding_inputs, &vec![],
10543+
dual_funding_context.funding_feerate_sat_per_1000_weight,
10544+
change_script.minimal_non_dust().to_sat(),
10545+
).map_err(|_| ChannelError::close("Error calculating change output value".to_string()))?;
10546+
let mut our_funding_outputs = vec![];
10547+
if let Some(change_value) = change_value_opt {
10548+
let mut change_output = TxOut {
10549+
value: Amount::from_sat(change_value),
10550+
script_pubkey: change_script,
10551+
};
10552+
let change_output_weight = get_output_weight(&change_output.script_pubkey).to_wu();
10553+
let change_output_fee = fee_for_weight(dual_funding_context.funding_feerate_sat_per_1000_weight, change_output_weight);
10554+
let change_value_decreased_with_fee = change_value.saturating_sub(change_output_fee);
10555+
// Check dust limit again
10556+
if change_value_decreased_with_fee > context.holder_dust_limit_satoshis {
10557+
change_output.value = Amount::from_sat(change_value_decreased_with_fee);
10558+
our_funding_outputs.push(OutputOwned::Single(change_output));
10559+
}
10560+
}
10561+
1053410562
let interactive_tx_constructor = Some(InteractiveTxConstructor::new(
1053510563
InteractiveTxConstructorArgs {
1053610564
entropy_source,
@@ -10541,7 +10569,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
1054110569
funding_tx_locktime: dual_funding_context.funding_tx_locktime,
1054210570
is_initiator: false,
1054310571
inputs_to_contribute: our_funding_inputs,
10544-
outputs_to_contribute: Vec::new(),
10572+
outputs_to_contribute: our_funding_outputs,
1054510573
expected_remote_shared_funding_output: Some((funding.get_funding_redeemscript().to_p2wsh(), funding.get_value_satoshis())),
1054610574
}
1054710575
).map_err(|_| ChannelError::Close((

0 commit comments

Comments
 (0)