Skip to content

Commit 0a7a5f9

Browse files
committed
Combine common fields of AcceptChannel & AcceptChannelV2 into struct
1 parent d3ddf15 commit 0a7a5f9

9 files changed

+322
-235
lines changed

lightning/src/ln/async_signer_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ fn test_async_commitment_signature_for_funding_signed_0conf() {
197197

198198
// nodes[0] <-- accept_channel --- nodes[1]
199199
let accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
200-
assert_eq!(accept_channel.minimum_depth, 0, "Expected minimum depth of 0");
200+
assert_eq!(accept_channel.common_fields.minimum_depth, 0, "Expected minimum depth of 0");
201201
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
202202

203203
// nodes[0] --- funding_created --> nodes[1]

lightning/src/ln/channel.rs

+61-59
Original file line numberDiff line numberDiff line change
@@ -6654,58 +6654,58 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
66546654
if !matches!(self.context.channel_state, ChannelState::NegotiatingFunding(flags) if flags == NegotiatingFundingFlags::OUR_INIT_SENT) {
66556655
return Err(ChannelError::Close("Got an accept_channel message at a strange time".to_owned()));
66566656
}
6657-
if msg.dust_limit_satoshis > 21000000 * 100000000 {
6658-
return Err(ChannelError::Close(format!("Peer never wants payout outputs? dust_limit_satoshis was {}", msg.dust_limit_satoshis)));
6657+
if msg.common_fields.dust_limit_satoshis > 21000000 * 100000000 {
6658+
return Err(ChannelError::Close(format!("Peer never wants payout outputs? dust_limit_satoshis was {}", msg.common_fields.dust_limit_satoshis)));
66596659
}
66606660
if msg.channel_reserve_satoshis > self.context.channel_value_satoshis {
66616661
return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than ({})", msg.channel_reserve_satoshis, self.context.channel_value_satoshis)));
66626662
}
6663-
if msg.dust_limit_satoshis > self.context.holder_selected_channel_reserve_satoshis {
6664-
return Err(ChannelError::Close(format!("Dust limit ({}) is bigger than our channel reserve ({})", msg.dust_limit_satoshis, self.context.holder_selected_channel_reserve_satoshis)));
6663+
if msg.common_fields.dust_limit_satoshis > self.context.holder_selected_channel_reserve_satoshis {
6664+
return Err(ChannelError::Close(format!("Dust limit ({}) is bigger than our channel reserve ({})", msg.common_fields.dust_limit_satoshis, self.context.holder_selected_channel_reserve_satoshis)));
66656665
}
66666666
if msg.channel_reserve_satoshis > self.context.channel_value_satoshis - self.context.holder_selected_channel_reserve_satoshis {
66676667
return Err(ChannelError::Close(format!("Bogus channel_reserve_satoshis ({}). Must not be greater than channel value minus our reserve ({})",
66686668
msg.channel_reserve_satoshis, self.context.channel_value_satoshis - self.context.holder_selected_channel_reserve_satoshis)));
66696669
}
66706670
let full_channel_value_msat = (self.context.channel_value_satoshis - msg.channel_reserve_satoshis) * 1000;
6671-
if msg.htlc_minimum_msat >= full_channel_value_msat {
6672-
return Err(ChannelError::Close(format!("Minimum htlc value ({}) is full channel value ({})", msg.htlc_minimum_msat, full_channel_value_msat)));
6671+
if msg.common_fields.htlc_minimum_msat >= full_channel_value_msat {
6672+
return Err(ChannelError::Close(format!("Minimum htlc value ({}) is full channel value ({})", msg.common_fields.htlc_minimum_msat, full_channel_value_msat)));
66736673
}
66746674
let max_delay_acceptable = u16::min(peer_limits.their_to_self_delay, MAX_LOCAL_BREAKDOWN_TIMEOUT);
6675-
if msg.to_self_delay > max_delay_acceptable {
6676-
return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_delay_acceptable, msg.to_self_delay)));
6675+
if msg.common_fields.to_self_delay > max_delay_acceptable {
6676+
return Err(ChannelError::Close(format!("They wanted our payments to be delayed by a needlessly long period. Upper limit: {}. Actual: {}", max_delay_acceptable, msg.common_fields.to_self_delay)));
66776677
}
6678-
if msg.max_accepted_htlcs < 1 {
6678+
if msg.common_fields.max_accepted_htlcs < 1 {
66796679
return Err(ChannelError::Close("0 max_accepted_htlcs makes for a useless channel".to_owned()));
66806680
}
6681-
if msg.max_accepted_htlcs > MAX_HTLCS {
6682-
return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.max_accepted_htlcs, MAX_HTLCS)));
6681+
if msg.common_fields.max_accepted_htlcs > MAX_HTLCS {
6682+
return Err(ChannelError::Close(format!("max_accepted_htlcs was {}. It must not be larger than {}", msg.common_fields.max_accepted_htlcs, MAX_HTLCS)));
66836683
}
66846684

66856685
// Now check against optional parameters as set by config...
6686-
if msg.htlc_minimum_msat > peer_limits.max_htlc_minimum_msat {
6687-
return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.htlc_minimum_msat, peer_limits.max_htlc_minimum_msat)));
6686+
if msg.common_fields.htlc_minimum_msat > peer_limits.max_htlc_minimum_msat {
6687+
return Err(ChannelError::Close(format!("htlc_minimum_msat ({}) is higher than the user specified limit ({})", msg.common_fields.htlc_minimum_msat, peer_limits.max_htlc_minimum_msat)));
66886688
}
6689-
if msg.max_htlc_value_in_flight_msat < peer_limits.min_max_htlc_value_in_flight_msat {
6690-
return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.max_htlc_value_in_flight_msat, peer_limits.min_max_htlc_value_in_flight_msat)));
6689+
if msg.common_fields.max_htlc_value_in_flight_msat < peer_limits.min_max_htlc_value_in_flight_msat {
6690+
return Err(ChannelError::Close(format!("max_htlc_value_in_flight_msat ({}) is less than the user specified limit ({})", msg.common_fields.max_htlc_value_in_flight_msat, peer_limits.min_max_htlc_value_in_flight_msat)));
66916691
}
66926692
if msg.channel_reserve_satoshis > peer_limits.max_channel_reserve_satoshis {
66936693
return Err(ChannelError::Close(format!("channel_reserve_satoshis ({}) is higher than the user specified limit ({})", msg.channel_reserve_satoshis, peer_limits.max_channel_reserve_satoshis)));
66946694
}
6695-
if msg.max_accepted_htlcs < peer_limits.min_max_accepted_htlcs {
6696-
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.max_accepted_htlcs, peer_limits.min_max_accepted_htlcs)));
6695+
if msg.common_fields.max_accepted_htlcs < peer_limits.min_max_accepted_htlcs {
6696+
return Err(ChannelError::Close(format!("max_accepted_htlcs ({}) is less than the user specified limit ({})", msg.common_fields.max_accepted_htlcs, peer_limits.min_max_accepted_htlcs)));
66976697
}
6698-
if msg.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
6699-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
6698+
if msg.common_fields.dust_limit_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
6699+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is less than the implementation limit ({})", msg.common_fields.dust_limit_satoshis, MIN_CHAN_DUST_LIMIT_SATOSHIS)));
67006700
}
6701-
if msg.dust_limit_satoshis > MAX_CHAN_DUST_LIMIT_SATOSHIS {
6702-
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
6701+
if msg.common_fields.dust_limit_satoshis > MAX_CHAN_DUST_LIMIT_SATOSHIS {
6702+
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.common_fields.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
67036703
}
6704-
if msg.minimum_depth > peer_limits.max_minimum_depth {
6705-
return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", peer_limits.max_minimum_depth, msg.minimum_depth)));
6704+
if msg.common_fields.minimum_depth > peer_limits.max_minimum_depth {
6705+
return Err(ChannelError::Close(format!("We consider the minimum depth to be unreasonably large. Expected minimum: ({}). Actual: ({})", peer_limits.max_minimum_depth, msg.common_fields.minimum_depth)));
67066706
}
67076707

6708-
if let Some(ty) = &msg.channel_type {
6708+
if let Some(ty) = &msg.common_fields.channel_type {
67096709
if *ty != self.context.channel_type {
67106710
return Err(ChannelError::Close("Channel Type in accept_channel didn't match the one sent in open_channel.".to_owned()));
67116711
}
@@ -6721,7 +6721,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
67216721
}
67226722

67236723
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
6724-
match &msg.shutdown_scriptpubkey {
6724+
match &msg.common_fields.shutdown_scriptpubkey {
67256725
&Some(ref script) => {
67266726
// Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
67276727
if script.len() == 0 {
@@ -6740,32 +6740,32 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
67406740
}
67416741
} else { None };
67426742

6743-
self.context.counterparty_dust_limit_satoshis = msg.dust_limit_satoshis;
6744-
self.context.counterparty_max_htlc_value_in_flight_msat = cmp::min(msg.max_htlc_value_in_flight_msat, self.context.channel_value_satoshis * 1000);
6743+
self.context.counterparty_dust_limit_satoshis = msg.common_fields.dust_limit_satoshis;
6744+
self.context.counterparty_max_htlc_value_in_flight_msat = cmp::min(msg.common_fields.max_htlc_value_in_flight_msat, self.context.channel_value_satoshis * 1000);
67456745
self.context.counterparty_selected_channel_reserve_satoshis = Some(msg.channel_reserve_satoshis);
6746-
self.context.counterparty_htlc_minimum_msat = msg.htlc_minimum_msat;
6747-
self.context.counterparty_max_accepted_htlcs = msg.max_accepted_htlcs;
6746+
self.context.counterparty_htlc_minimum_msat = msg.common_fields.htlc_minimum_msat;
6747+
self.context.counterparty_max_accepted_htlcs = msg.common_fields.max_accepted_htlcs;
67486748

67496749
if peer_limits.trust_own_funding_0conf {
6750-
self.context.minimum_depth = Some(msg.minimum_depth);
6750+
self.context.minimum_depth = Some(msg.common_fields.minimum_depth);
67516751
} else {
6752-
self.context.minimum_depth = Some(cmp::max(1, msg.minimum_depth));
6752+
self.context.minimum_depth = Some(cmp::max(1, msg.common_fields.minimum_depth));
67536753
}
67546754

67556755
let counterparty_pubkeys = ChannelPublicKeys {
6756-
funding_pubkey: msg.funding_pubkey,
6757-
revocation_basepoint: RevocationBasepoint::from(msg.revocation_basepoint),
6758-
payment_point: msg.payment_point,
6759-
delayed_payment_basepoint: DelayedPaymentBasepoint::from(msg.delayed_payment_basepoint),
6760-
htlc_basepoint: HtlcBasepoint::from(msg.htlc_basepoint)
6756+
funding_pubkey: msg.common_fields.funding_pubkey,
6757+
revocation_basepoint: RevocationBasepoint::from(msg.common_fields.revocation_basepoint),
6758+
payment_point: msg.common_fields.payment_basepoint,
6759+
delayed_payment_basepoint: DelayedPaymentBasepoint::from(msg.common_fields.delayed_payment_basepoint),
6760+
htlc_basepoint: HtlcBasepoint::from(msg.common_fields.htlc_basepoint)
67616761
};
67626762

67636763
self.context.channel_transaction_parameters.counterparty_parameters = Some(CounterpartyChannelTransactionParameters {
6764-
selected_contest_delay: msg.to_self_delay,
6764+
selected_contest_delay: msg.common_fields.to_self_delay,
67656765
pubkeys: counterparty_pubkeys,
67666766
});
67676767

6768-
self.context.counterparty_cur_commitment_point = Some(msg.first_per_commitment_point);
6768+
self.context.counterparty_cur_commitment_point = Some(msg.common_fields.first_per_commitment_point);
67696769
self.context.counterparty_shutdown_scriptpubkey = counterparty_shutdown_scriptpubkey;
67706770

67716771
self.context.channel_state = ChannelState::NegotiatingFunding(
@@ -7278,25 +7278,27 @@ impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
72787278
let keys = self.context.get_holder_pubkeys();
72797279

72807280
msgs::AcceptChannel {
7281-
temporary_channel_id: self.context.channel_id,
7282-
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
7283-
max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
7281+
common_fields: msgs::CommonAcceptChannelFields {
7282+
temporary_channel_id: self.context.channel_id,
7283+
dust_limit_satoshis: self.context.holder_dust_limit_satoshis,
7284+
max_htlc_value_in_flight_msat: self.context.holder_max_htlc_value_in_flight_msat,
7285+
htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
7286+
minimum_depth: self.context.minimum_depth.unwrap(),
7287+
to_self_delay: self.context.get_holder_selected_contest_delay(),
7288+
max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
7289+
funding_pubkey: keys.funding_pubkey,
7290+
revocation_basepoint: keys.revocation_basepoint.to_public_key(),
7291+
payment_basepoint: keys.payment_point,
7292+
delayed_payment_basepoint: keys.delayed_payment_basepoint.to_public_key(),
7293+
htlc_basepoint: keys.htlc_basepoint.to_public_key(),
7294+
first_per_commitment_point,
7295+
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
7296+
Some(script) => script.clone().into_inner(),
7297+
None => Builder::new().into_script(),
7298+
}),
7299+
channel_type: Some(self.context.channel_type.clone()),
7300+
},
72847301
channel_reserve_satoshis: self.context.holder_selected_channel_reserve_satoshis,
7285-
htlc_minimum_msat: self.context.holder_htlc_minimum_msat,
7286-
minimum_depth: self.context.minimum_depth.unwrap(),
7287-
to_self_delay: self.context.get_holder_selected_contest_delay(),
7288-
max_accepted_htlcs: self.context.holder_max_accepted_htlcs,
7289-
funding_pubkey: keys.funding_pubkey,
7290-
revocation_basepoint: keys.revocation_basepoint.to_public_key(),
7291-
payment_point: keys.payment_point,
7292-
delayed_payment_basepoint: keys.delayed_payment_basepoint.to_public_key(),
7293-
htlc_basepoint: keys.htlc_basepoint.to_public_key(),
7294-
first_per_commitment_point,
7295-
shutdown_scriptpubkey: Some(match &self.context.shutdown_scriptpubkey {
7296-
Some(script) => script.clone().into_inner(),
7297-
None => Builder::new().into_script(),
7298-
}),
7299-
channel_type: Some(self.context.channel_type.clone()),
73007302
#[cfg(taproot)]
73017303
next_local_nonce: None,
73027304
}
@@ -8558,7 +8560,7 @@ mod tests {
85588560

85598561
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
85608562
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
8561-
accept_channel_msg.dust_limit_satoshis = 546;
8563+
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
85628564
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
85638565
node_a_chan.context.holder_dust_limit_satoshis = 1560;
85648566

@@ -8876,7 +8878,7 @@ mod tests {
88768878

88778879
// Node B --> Node A: accept channel, explicitly setting B's dust limit.
88788880
let mut accept_channel_msg = node_b_chan.accept_inbound_channel();
8879-
accept_channel_msg.dust_limit_satoshis = 546;
8881+
accept_channel_msg.common_fields.dust_limit_satoshis = 546;
88808882
node_a_chan.accept_channel(&accept_channel_msg, &config.channel_handshake_limits, &channelmanager::provided_init_features(&config)).unwrap();
88818883
node_a_chan.context.holder_dust_limit_satoshis = 1560;
88828884

@@ -10005,7 +10007,7 @@ mod tests {
1000510007
).unwrap();
1000610008

1000710009
let mut accept_channel_msg = channel_b.get_accept_channel_message();
10008-
accept_channel_msg.channel_type = Some(simple_anchors_channel_type.clone());
10010+
accept_channel_msg.common_fields.channel_type = Some(simple_anchors_channel_type.clone());
1000910011

1001010012
let res = channel_a.accept_channel(
1001110013
&accept_channel_msg, &config.channel_handshake_limits, &simple_anchors_init

lightning/src/ln/channelmanager.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6299,28 +6299,28 @@ where
62996299
let peer_state_mutex = per_peer_state.get(counterparty_node_id)
63006300
.ok_or_else(|| {
63016301
debug_assert!(false);
6302-
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.temporary_channel_id)
6302+
MsgHandleErrInternal::send_err_msg_no_close(format!("Can't find a peer matching the passed counterparty node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id)
63036303
})?;
63046304
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
63056305
let peer_state = &mut *peer_state_lock;
6306-
match peer_state.channel_by_id.entry(msg.temporary_channel_id) {
6306+
match peer_state.channel_by_id.entry(msg.common_fields.temporary_channel_id) {
63076307
hash_map::Entry::Occupied(mut phase) => {
63086308
match phase.get_mut() {
63096309
ChannelPhase::UnfundedOutboundV1(chan) => {
63106310
try_chan_phase_entry!(self, chan.accept_channel(&msg, &self.default_configuration.channel_handshake_limits, &peer_state.latest_features), phase);
63116311
(chan.context.get_value_satoshis(), chan.context.get_funding_redeemscript().to_v0_p2wsh(), chan.context.get_user_id())
63126312
},
63136313
_ => {
6314-
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected accept_channel message from peer with counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id));
6314+
return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got an unexpected accept_channel message from peer with counterparty_node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id));
63156315
}
63166316
}
63176317
},
6318-
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.temporary_channel_id))
6318+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close(format!("Got a message for a channel from the wrong node! No such channel for the passed counterparty_node_id {}", counterparty_node_id), msg.common_fields.temporary_channel_id))
63196319
}
63206320
};
63216321
let mut pending_events = self.pending_events.lock().unwrap();
63226322
pending_events.push_back((events::Event::FundingGenerationReady {
6323-
temporary_channel_id: msg.temporary_channel_id,
6323+
temporary_channel_id: msg.common_fields.temporary_channel_id,
63246324
counterparty_node_id: *counterparty_node_id,
63256325
channel_value_satoshis: value,
63266326
output_script,
@@ -8725,7 +8725,7 @@ where
87258725
fn handle_accept_channel_v2(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannelV2) {
87268726
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
87278727
"Dual-funded channels not supported".to_owned(),
8728-
msg.temporary_channel_id.clone())), *counterparty_node_id);
8728+
msg.common_fields.temporary_channel_id.clone())), *counterparty_node_id);
87298729
}
87308730

87318731
fn handle_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) {

lightning/src/ln/functional_test_utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1203,7 +1203,7 @@ pub fn open_zero_conf_channel<'a, 'b, 'c, 'd>(initiator: &'a Node<'b, 'c, 'd>, r
12031203
};
12041204

12051205
let accept_channel = get_event_msg!(receiver, MessageSendEvent::SendAcceptChannel, initiator.node.get_our_node_id());
1206-
assert_eq!(accept_channel.minimum_depth, 0);
1206+
assert_eq!(accept_channel.common_fields.minimum_depth, 0);
12071207
initiator.node.handle_accept_channel(&receiver.node.get_our_node_id(), &accept_channel);
12081208

12091209
let (temporary_channel_id, tx, _) = create_funding_transaction(&initiator, &receiver.node.get_our_node_id(), 100_000, 42);
@@ -1270,7 +1270,7 @@ pub fn exchange_open_accept_chan<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b:
12701270
};
12711271
}
12721272
let accept_channel_msg = get_event_msg!(node_b, MessageSendEvent::SendAcceptChannel, node_a.node.get_our_node_id());
1273-
assert_eq!(accept_channel_msg.temporary_channel_id, create_chan_id);
1273+
assert_eq!(accept_channel_msg.common_fields.temporary_channel_id, create_chan_id);
12741274
node_a.node.handle_accept_channel(&node_b.node.get_our_node_id(), &accept_channel_msg);
12751275
assert_ne!(node_b.node.list_channels().iter().find(|channel| channel.channel_id == create_chan_id).unwrap().user_channel_id, 0);
12761276

0 commit comments

Comments
 (0)