Skip to content

Commit

Permalink
Merge pull request #2077 from dunxen/2023-02-splitchannelstate
Browse files Browse the repository at this point in the history
Split prefunded `Channel` into `Inbound`/`Outbound` channels
  • Loading branch information
TheBlueMatt committed Jun 16, 2023
2 parents 74a9ed9 + d957f36 commit ae9e96e
Show file tree
Hide file tree
Showing 8 changed files with 4,049 additions and 3,776 deletions.
4 changes: 2 additions & 2 deletions lightning/src/ln/chanmon_update_fail_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1456,12 +1456,12 @@ fn monitor_failed_no_reestablish_response() {
{
let mut node_0_per_peer_lock;
let mut node_0_peer_state_lock;
get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, channel_id).context.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
}
{
let mut node_1_per_peer_lock;
let mut node_1_peer_state_lock;
get_channel_ref!(nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, channel_id).announcement_sigs_state = AnnouncementSigsState::PeerReceived;
get_channel_ref!(nodes[1], nodes[0], node_1_per_peer_lock, node_1_peer_state_lock, channel_id).context.announcement_sigs_state = AnnouncementSigsState::PeerReceived;
}

// Route the payment and deliver the initial commitment_signed (with a monitor update failure
Expand Down
6,950 changes: 3,522 additions & 3,428 deletions lightning/src/ln/channel.rs

Large diffs are not rendered by default.

731 changes: 439 additions & 292 deletions lightning/src/ln/channelmanager.rs

Large diffs are not rendered by default.

30 changes: 26 additions & 4 deletions lightning/src/ln/functional_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,36 @@ macro_rules! get_channel_ref {
}
}

#[cfg(test)]
macro_rules! get_outbound_v1_channel_ref {
($node: expr, $counterparty_node: expr, $per_peer_state_lock: ident, $peer_state_lock: ident, $channel_id: expr) => {
{
$per_peer_state_lock = $node.node.per_peer_state.read().unwrap();
$peer_state_lock = $per_peer_state_lock.get(&$counterparty_node.node.get_our_node_id()).unwrap().lock().unwrap();
$peer_state_lock.outbound_v1_channel_by_id.get_mut(&$channel_id).unwrap()
}
}
}

#[cfg(test)]
macro_rules! get_inbound_v1_channel_ref {
($node: expr, $counterparty_node: expr, $per_peer_state_lock: ident, $peer_state_lock: ident, $channel_id: expr) => {
{
$per_peer_state_lock = $node.node.per_peer_state.read().unwrap();
$peer_state_lock = $per_peer_state_lock.get(&$counterparty_node.node.get_our_node_id()).unwrap().lock().unwrap();
$peer_state_lock.inbound_v1_channel_by_id.get_mut(&$channel_id).unwrap()
}
}
}

#[cfg(test)]
macro_rules! get_feerate {
($node: expr, $counterparty_node: expr, $channel_id: expr) => {
{
let mut per_peer_state_lock;
let mut peer_state_lock;
let chan = get_channel_ref!($node, $counterparty_node, per_peer_state_lock, peer_state_lock, $channel_id);
chan.get_feerate_sat_per_1000_weight()
chan.context.get_feerate_sat_per_1000_weight()
}
}
}
Expand All @@ -802,7 +824,7 @@ macro_rules! get_opt_anchors {
let mut per_peer_state_lock;
let mut peer_state_lock;
let chan = get_channel_ref!($node, $counterparty_node, per_peer_state_lock, peer_state_lock, $channel_id);
chan.opt_anchors()
chan.context.opt_anchors()
}
}
}
Expand Down Expand Up @@ -2237,10 +2259,10 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
let peer_state = per_peer_state.get(&$prev_node.node.get_our_node_id())
.unwrap().lock().unwrap();
let channel = peer_state.channel_by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap();
if let Some(prev_config) = channel.prev_config() {
if let Some(prev_config) = channel.context.prev_config() {
prev_config.forwarding_fee_base_msat
} else {
channel.config().forwarding_fee_base_msat
channel.context.config().forwarding_fee_base_msat
}
};
expect_payment_forwarded!($node, $next_node, $prev_node, Some(fee as u64), false, false);
Expand Down
80 changes: 43 additions & 37 deletions lightning/src/ln/functional_tests.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lightning/src/ln/onion_route_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ fn test_onion_failure() {
let short_channel_id = channels[1].0.contents.short_channel_id;
let amt_to_forward = nodes[1].node.per_peer_state.read().unwrap().get(&nodes[2].node.get_our_node_id())
.unwrap().lock().unwrap().channel_by_id.get(&channels[1].2).unwrap()
.get_counterparty_htlc_minimum_msat() - 1;
.context.get_counterparty_htlc_minimum_msat() - 1;
let mut bogus_route = route.clone();
let route_len = bogus_route.paths[0].hops.len();
bogus_route.paths[0].hops[route_len-1].fee_msat = amt_to_forward;
Expand Down
26 changes: 15 additions & 11 deletions lightning/src/ln/payment_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,9 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
let mut peer_state = per_peer_state.get(&nodes[2].node.get_our_node_id())
.unwrap().lock().unwrap();
let mut channel = peer_state.channel_by_id.get_mut(&chan_id_2).unwrap();
let mut new_config = channel.config();
let mut new_config = channel.context.config();
new_config.forwarding_fee_base_msat += 100_000;
channel.update_config(&new_config);
channel.context.update_config(&new_config);
new_route.paths[0].hops[0].fee_msat += 100_000;
}

Expand Down Expand Up @@ -1409,7 +1409,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
channel_1.get_short_channel_id().unwrap()
channel_1.context.get_short_channel_id().unwrap()
);
assert_eq!(chan_1_used_liquidity, None);
}
Expand All @@ -1421,7 +1421,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
channel_2.get_short_channel_id().unwrap()
channel_2.context.get_short_channel_id().unwrap()
);

assert_eq!(chan_2_used_liquidity, None);
Expand All @@ -1446,7 +1446,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
channel_1.get_short_channel_id().unwrap()
channel_1.context.get_short_channel_id().unwrap()
);
// First hop accounts for expected 1000 msat fee
assert_eq!(chan_1_used_liquidity, Some(501000));
Expand All @@ -1459,7 +1459,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
channel_2.get_short_channel_id().unwrap()
channel_2.context.get_short_channel_id().unwrap()
);

assert_eq!(chan_2_used_liquidity, Some(500000));
Expand All @@ -1485,7 +1485,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_1_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
channel_1.get_short_channel_id().unwrap()
channel_1.context.get_short_channel_id().unwrap()
);
assert_eq!(chan_1_used_liquidity, None);
}
Expand All @@ -1497,7 +1497,7 @@ fn test_trivial_inflight_htlc_tracking(){
let chan_2_used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[2].node.get_our_node_id()),
channel_2.get_short_channel_id().unwrap()
channel_2.context.get_short_channel_id().unwrap()
);
assert_eq!(chan_2_used_liquidity, None);
}
Expand Down Expand Up @@ -1538,7 +1538,7 @@ fn test_holding_cell_inflight_htlcs() {
let used_liquidity = inflight_htlcs.used_liquidity_msat(
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
channel.get_short_channel_id().unwrap()
channel.context.get_short_channel_id().unwrap()
);

assert_eq!(used_liquidity, Some(2000000));
Expand Down Expand Up @@ -1635,7 +1635,9 @@ fn do_test_intercepted_payment(test: InterceptTest) {

// Check for unknown channel id error.
let unknown_chan_id_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &[42; 32], nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable { err: format!("Channel with id {} not found for the passed counterparty node_id {}", log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });
assert_eq!(unknown_chan_id_err , APIError::ChannelUnavailable {
err: format!("Funded channel with id {} not found for the passed counterparty node_id {}. Channel may still be opening.",
log_bytes!([42; 32]), nodes[2].node.get_our_node_id()) });

if test == InterceptTest::Fail {
// Ensure we can fail the intercepted payment back.
Expand All @@ -1659,7 +1661,9 @@ fn do_test_intercepted_payment(test: InterceptTest) {
// Check that we'll fail as expected when sending to a channel that isn't in `ChannelReady` yet.
let temp_chan_id = nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 0, 42, None).unwrap();
let unusable_chan_err = nodes[1].node.forward_intercepted_htlc(intercept_id, &temp_chan_id, nodes[2].node.get_our_node_id(), expected_outbound_amount_msat).unwrap_err();
assert_eq!(unusable_chan_err , APIError::ChannelUnavailable { err: format!("Channel with id {} not fully established", log_bytes!(temp_chan_id)) });
assert_eq!(unusable_chan_err , APIError::ChannelUnavailable {
err: format!("Funded channel with id {} not found for the passed counterparty node_id {}. Channel may still be opening.",
log_bytes!(temp_chan_id), nodes[2].node.get_our_node_id()) });
assert_eq!(nodes[1].node.get_and_clear_pending_msg_events().len(), 1);

// Open the just-in-time channel so the payment can then be forwarded.
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/shutdown_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ fn do_test_closing_signed_reinit_timeout(timeout_step: TimeoutStep) {
{
let mut node_0_per_peer_lock;
let mut node_0_peer_state_lock;
get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).closing_fee_limits.as_mut().unwrap().1 *= 10;
get_channel_ref!(nodes[0], nodes[1], node_0_per_peer_lock, node_0_peer_state_lock, chan_id).context.closing_fee_limits.as_mut().unwrap().1 *= 10;
}
nodes[1].node.handle_closing_signed(&nodes[0].node.get_our_node_id(), &node_0_closing_signed);
let node_1_closing_signed = get_event_msg!(nodes[1], MessageSendEvent::SendClosingSigned, nodes[0].node.get_our_node_id());
Expand Down

0 comments on commit ae9e96e

Please sign in to comment.