Skip to content

Commit 0c29015

Browse files
committed
Move general channel behaviour into new ChannelInterface trait
In preparation for the introduction of `InboundV1Channel` and `OutboundV1Channel`, we move common channel behaviour and helpers to a new `ChannelInterface` trait. `Channel` and the upcoming prefunded channel types implement `ChannelInterface` and only need to provide a way to access their inner `ChannelContext`, mutably, and immutably, so that these can be called by default methods of the trait. Further work will pull items which are unique to certain channel types out of `ChannelContext` and into that channel struct as a field.
1 parent aec133d commit 0c29015

File tree

7 files changed

+1135
-998
lines changed

7 files changed

+1135
-998
lines changed

lightning/src/ln/channel.rs

Lines changed: 1127 additions & 940 deletions
Large diffs are not rendered by default.

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::events::{Event, EventHandler, EventsProvider, MessageSendEvent, Messa
4040
// Since this struct is returned in `list_channels` methods, expose it here in case users want to
4141
// construct one themselves.
4242
use crate::ln::{inbound_payment, PaymentHash, PaymentPreimage, PaymentSecret};
43-
use crate::ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch};
43+
use crate::ln::channel::{Channel, ChannelInterface, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch};
4444
use crate::ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, NodeFeatures};
4545
#[cfg(any(feature = "_test_utils", test))]
4646
use crate::ln::features::InvoiceFeatures;
@@ -1358,56 +1358,6 @@ impl ChannelDetails {
13581358
pub fn get_outbound_payment_scid(&self) -> Option<u64> {
13591359
self.short_channel_id.or(self.outbound_scid_alias)
13601360
}
1361-
1362-
fn from_channel<Signer: WriteableEcdsaChannelSigner>(channel: &Channel<Signer>,
1363-
best_block_height: u32, latest_features: InitFeatures) -> Self {
1364-
1365-
let balance = channel.get_available_balances();
1366-
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
1367-
channel.get_holder_counterparty_selected_channel_reserve_satoshis();
1368-
ChannelDetails {
1369-
channel_id: channel.channel_id(),
1370-
counterparty: ChannelCounterparty {
1371-
node_id: channel.get_counterparty_node_id(),
1372-
features: latest_features,
1373-
unspendable_punishment_reserve: to_remote_reserve_satoshis,
1374-
forwarding_info: channel.counterparty_forwarding_info(),
1375-
// Ensures that we have actually received the `htlc_minimum_msat` value
1376-
// from the counterparty through the `OpenChannel` or `AcceptChannel`
1377-
// message (as they are always the first message from the counterparty).
1378-
// Else `Channel::get_counterparty_htlc_minimum_msat` could return the
1379-
// default `0` value set by `Channel::new_outbound`.
1380-
outbound_htlc_minimum_msat: if channel.have_received_message() {
1381-
Some(channel.get_counterparty_htlc_minimum_msat()) } else { None },
1382-
outbound_htlc_maximum_msat: channel.get_counterparty_htlc_maximum_msat(),
1383-
},
1384-
funding_txo: channel.get_funding_txo(),
1385-
// Note that accept_channel (or open_channel) is always the first message, so
1386-
// `have_received_message` indicates that type negotiation has completed.
1387-
channel_type: if channel.have_received_message() { Some(channel.get_channel_type().clone()) } else { None },
1388-
short_channel_id: channel.get_short_channel_id(),
1389-
outbound_scid_alias: if channel.is_usable() { Some(channel.outbound_scid_alias()) } else { None },
1390-
inbound_scid_alias: channel.latest_inbound_scid_alias(),
1391-
channel_value_satoshis: channel.get_value_satoshis(),
1392-
feerate_sat_per_1000_weight: Some(channel.get_feerate_sat_per_1000_weight()),
1393-
unspendable_punishment_reserve: to_self_reserve_satoshis,
1394-
balance_msat: balance.balance_msat,
1395-
inbound_capacity_msat: balance.inbound_capacity_msat,
1396-
outbound_capacity_msat: balance.outbound_capacity_msat,
1397-
next_outbound_htlc_limit_msat: balance.next_outbound_htlc_limit_msat,
1398-
user_channel_id: channel.get_user_id(),
1399-
confirmations_required: channel.minimum_depth(),
1400-
confirmations: Some(channel.get_funding_tx_confirmations(best_block_height)),
1401-
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
1402-
is_outbound: channel.is_outbound(),
1403-
is_channel_ready: channel.is_usable(),
1404-
is_usable: channel.is_live(),
1405-
is_public: channel.should_announce(),
1406-
inbound_htlc_minimum_msat: Some(channel.get_holder_htlc_minimum_msat()),
1407-
inbound_htlc_maximum_msat: channel.get_holder_htlc_maximum_msat(),
1408-
config: Some(channel.config()),
1409-
}
1410-
}
14111361
}
14121362

14131363
/// Used by [`ChannelManager::list_recent_payments`] to express the status of recent payments.
@@ -1974,8 +1924,7 @@ where
19741924
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
19751925
let peer_state = &mut *peer_state_lock;
19761926
for (_channel_id, channel) in peer_state.channel_by_id.iter().filter(f) {
1977-
let details = ChannelDetails::from_channel(channel, best_block_height,
1978-
peer_state.latest_features.clone());
1927+
let details = channel.get_channel_details(best_block_height, peer_state.latest_features.clone());
19791928
res.push(details);
19801929
}
19811930
}
@@ -2014,7 +1963,7 @@ where
20141963
return peer_state.channel_by_id
20151964
.iter()
20161965
.map(|(_, channel)|
2017-
ChannelDetails::from_channel(channel, best_block_height, features.clone()))
1966+
channel.get_channel_details(best_block_height, features.clone()))
20181967
.collect();
20191968
}
20201969
vec![]

lightning/src/ln/functional_test_utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::chain::channelmonitor::ChannelMonitor;
1616
use crate::chain::transaction::OutPoint;
1717
use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, PaymentFailureReason};
1818
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
19+
use crate::ln::channel::ChannelInterface;
1920
use crate::ln::channelmanager::{AChannelManager, ChainParameters, ChannelManager, ChannelManagerReadArgs, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, PaymentId, MIN_CLTV_EXPIRY_DELTA};
2021
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate};
2122
use crate::routing::router::{self, PaymentParameters, Route};

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::chain::transaction::OutPoint;
2020
use crate::sign::{ChannelSigner, EcdsaChannelSigner, EntropySource};
2121
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentPurpose, ClosureReason, HTLCDestination, PaymentFailureReason};
2222
use crate::ln::{PaymentPreimage, PaymentSecret, PaymentHash};
23-
use crate::ln::channel::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT};
23+
use crate::ln::channel::{ChannelInterface, commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT};
2424
use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, PaymentSendFailure, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
2525
use crate::ln::channel::{Channel, ChannelError};
2626
use crate::ln::{chan_utils, onion_utils};

lightning/src/ln/monitor_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::chain::chaininterface::LowerBoundedFeeEstimator;
1919
#[cfg(anchors)]
2020
use crate::events::bump_transaction::BumpTransactionEvent;
2121
use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider, ClosureReason, HTLCDestination};
22-
use crate::ln::channel;
22+
use crate::ln::channel::{self, ChannelInterface};
2323
#[cfg(anchors)]
2424
use crate::ln::chan_utils;
2525
#[cfg(anchors)]

lightning/src/ln/onion_route_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::chain::channelmonitor::{CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCK
1515
use crate::sign::{EntropySource, NodeSigner, Recipient};
1616
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason};
1717
use crate::ln::{PaymentHash, PaymentSecret};
18-
use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
18+
use crate::ln::channel::{ChannelInterface, EXPIRE_PREV_CONFIG_TICKS};
1919
use crate::ln::channelmanager::{HTLCForwardInfo, FailureCode, CLTV_FAR_FAR_AWAY, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA, PendingAddHTLCInfo, PendingHTLCInfo, PendingHTLCRouting, PaymentId, RecipientOnionFields};
2020
use crate::ln::onion_utils;
2121
use crate::routing::gossip::{NetworkUpdate, RoutingFees};

lightning/src/ln/payment_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::chain::channelmonitor::{ANTI_REORG_DELAY, HTLC_FAIL_BACK_BUFFER, LATE
1616
use crate::sign::EntropySource;
1717
use crate::chain::transaction::OutPoint;
1818
use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, PathFailure, PaymentFailureReason};
19-
use crate::ln::channel::EXPIRE_PREV_CONFIG_TICKS;
19+
use crate::ln::channel::{ChannelInterface, EXPIRE_PREV_CONFIG_TICKS};
2020
use crate::ln::channelmanager::{BREAKDOWN_TIMEOUT, ChannelManager, MPP_TIMEOUT_TICKS, MIN_CLTV_EXPIRY_DELTA, PaymentId, PaymentSendFailure, IDEMPOTENCY_TIMEOUT_TICKS, RecentPaymentDetails, RecipientOnionFields};
2121
use crate::ln::features::InvoiceFeatures;
2222
use crate::ln::msgs;

0 commit comments

Comments
 (0)