Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split prefunded Channel into Inbound/Outbound channels #2077

Merged
merged 25 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
883afb3
Move `Channel` fields into `ChannelContext` struct
dunxen Mar 7, 2023
1503ebb
Move `Channel::opt_anchors` to `ChannelContext` impl & move some util…
dunxen Jun 7, 2023
1ee0a66
Move `Channel::get_update_time_counter` and some other methods
dunxen Jun 7, 2023
ede8324
Move `Channel::channel_id` and some other methods to `ChannelContext`…
dunxen Jun 7, 2023
497aeb0
Move `Channel::build_commitment_transaction` to `ChannelContext` impl
dunxen Jun 7, 2023
0d739ee
Move `Channel::build_holder_transaction_keys` and some other methods
dunxen Jun 7, 2023
3ff94fa
Move `Channel::get_feerate_sat_per_1000_weight` and other methods
dunxen Jun 7, 2023
2774aa2
Prepare some methods for upcoming moves to `ChannelContext`
dunxen Jun 13, 2023
ed6a5bb
Move `Channel::get_*_pending_htlc_stats` to `ChannelContext` impl
dunxen Jun 13, 2023
08ee72b
Move `Channel::commit_tx_fee_msat` to file-level utilities
dunxen Jun 13, 2023
9f4e714
Move `Channel::next_*_commit_tx_fee_msat` methods to `ChannelContext`…
dunxen Jun 13, 2023
60706d6
Move `Channel::get_available_balances` to `ChannelContext` impl
dunxen Jun 13, 2023
25c1ad8
Convert `ChannelDetails::from_channel` to `ChannelDetails::from_chann…
dunxen Jun 7, 2023
e3f0c55
Make `ChannelManager::issue_channel_close_events` take a `ChannelCont…
dunxen Jun 13, 2023
1012526
Move channel constants up
dunxen Mar 30, 2023
883e056
Introduce `InboundV1Channel` & `OutboundV1Channel`
dunxen Apr 5, 2023
e6c2f04
Move outbound channel constructor into `OutboundV1Channel` impl
dunxen Jun 7, 2023
baadeb7
Move inbound channel constructor into `InboundV1Channel` impl
dunxen Jun 7, 2023
2ea27e0
Move `Channel::force_shutdown` to `ChannelContext` impl
dunxen Jun 13, 2023
4ad67cf
Refactor channel map update macros for use with `ChannelContext`
dunxen Jun 7, 2023
4b1e286
Create and use methods for counting channels
dunxen Jun 7, 2023
4a0cd5c
Move outbound channel methods into `OutboundV1Channel`'s impl
dunxen Jun 7, 2023
637e03a
Move inbound channel methods into `InboundV1Channel`'s impl
dunxen Jun 7, 2023
8f93e2d
Rename `InboundV1Channel::new_from_req` to `InboundV1Channel::new`
dunxen Jun 7, 2023
d957f36
Rename `inbound_is_awaiting_accept()` to `is_awaiting_accept()`
dunxen Jun 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No big deal, but this was added two commits ago, removed in the previous, and added back here.

($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