Skip to content

Commit 3902a7b

Browse files
committed
f - don't expose pending_funding
1 parent 8cba3da commit 3902a7b

File tree

3 files changed

+53
-61
lines changed

3 files changed

+53
-61
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,16 +1248,6 @@ impl<SP: Deref> Channel<SP> where
12481248
}
12491249
}
12501250

1251-
pub fn pending_funding(&self) -> &[FundingScope] {
1252-
match &self.phase {
1253-
ChannelPhase::Undefined => unreachable!(),
1254-
ChannelPhase::Funded(chan) => &chan.pending_funding,
1255-
ChannelPhase::UnfundedOutboundV1(_) => &[],
1256-
ChannelPhase::UnfundedInboundV1(_) => &[],
1257-
ChannelPhase::UnfundedV2(_) => &[],
1258-
}
1259-
}
1260-
12611251
pub fn unfunded_context_mut(&mut self) -> Option<&mut UnfundedChannelContext> {
12621252
match &mut self.phase {
12631253
ChannelPhase::Undefined => unreachable!(),
@@ -1560,6 +1550,25 @@ impl<SP: Deref> Channel<SP> where
15601550
}
15611551
}
15621552
}
1553+
1554+
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
1555+
/// Doesn't bother handling the
1556+
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
1557+
/// corner case properly.
1558+
pub fn get_available_balances<F: Deref>(
1559+
&self, fee_estimator: &LowerBoundedFeeEstimator<F>,
1560+
) -> AvailableBalances
1561+
where
1562+
F::Target: FeeEstimator,
1563+
{
1564+
match &self.phase {
1565+
ChannelPhase::Undefined => unreachable!(),
1566+
ChannelPhase::Funded(chan) => chan.get_available_balances(fee_estimator),
1567+
ChannelPhase::UnfundedOutboundV1(chan) => chan.context.get_available_balances_for_scope(&chan.funding, fee_estimator),
1568+
ChannelPhase::UnfundedInboundV1(chan) => chan.context.get_available_balances_for_scope(&chan.funding, fee_estimator),
1569+
ChannelPhase::UnfundedV2(chan) => chan.context.get_available_balances_for_scope(&chan.funding, fee_estimator),
1570+
}
1571+
}
15631572
}
15641573

15651574
impl<SP: Deref> From<OutboundV1Channel<SP>> for Channel<SP>
@@ -4133,31 +4142,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
41334142
outbound_details
41344143
}
41354144

4136-
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
4137-
/// Doesn't bother handling the
4138-
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
4139-
/// corner case properly.
4140-
pub fn get_available_balances<F: Deref>(
4141-
&self, funding: &FundingScope, pending_funding: &[FundingScope],
4142-
fee_estimator: &LowerBoundedFeeEstimator<F>,
4143-
) -> AvailableBalances
4144-
where
4145-
F::Target: FeeEstimator,
4146-
{
4147-
core::iter::once(funding)
4148-
.chain(pending_funding.iter())
4149-
.map(|funding| self.get_available_balances_for_scope(funding, fee_estimator))
4150-
.reduce(|acc, e| {
4151-
AvailableBalances {
4152-
inbound_capacity_msat: acc.inbound_capacity_msat.min(e.inbound_capacity_msat),
4153-
outbound_capacity_msat: acc.outbound_capacity_msat.min(e.outbound_capacity_msat),
4154-
next_outbound_htlc_limit_msat: acc.next_outbound_htlc_limit_msat.min(e.next_outbound_htlc_limit_msat),
4155-
next_outbound_htlc_minimum_msat: acc.next_outbound_htlc_minimum_msat.max(e.next_outbound_htlc_minimum_msat),
4156-
}
4157-
})
4158-
.expect("At least one FundingScope is always provided")
4159-
}
4160-
41614145
fn get_available_balances_for_scope<F: Deref>(
41624146
&self, funding: &FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
41634147
) -> AvailableBalances
@@ -4949,7 +4933,7 @@ pub(super) struct DualFundingChannelContext {
49494933
// Counterparty designates channel data owned by the another channel participant entity.
49504934
pub(super) struct FundedChannel<SP: Deref> where SP::Target: SignerProvider {
49514935
pub funding: FundingScope,
4952-
pub(super) pending_funding: Vec<FundingScope>,
4936+
pending_funding: Vec<FundingScope>,
49534937
pub context: ChannelContext<SP>,
49544938
pub interactive_tx_signing_session: Option<InteractiveTxSigningSession>,
49554939
holder_commitment_point: HolderCommitmentPoint,
@@ -8814,13 +8798,24 @@ impl<SP: Deref> FundedChannel<SP> where
88148798
Ok(Some(res))
88158799
}
88168800

8817-
pub fn get_available_balances<F: Deref>(
8801+
pub(super) fn get_available_balances<F: Deref>(
88188802
&self, fee_estimator: &LowerBoundedFeeEstimator<F>,
88198803
) -> AvailableBalances
88208804
where
88218805
F::Target: FeeEstimator,
88228806
{
8823-
self.context.get_available_balances(&self.funding, &self.pending_funding, fee_estimator)
8807+
core::iter::once(&self.funding)
8808+
.chain(self.pending_funding.iter())
8809+
.map(|funding| self.context.get_available_balances_for_scope(funding, fee_estimator))
8810+
.reduce(|acc, e| {
8811+
AvailableBalances {
8812+
inbound_capacity_msat: acc.inbound_capacity_msat.min(e.inbound_capacity_msat),
8813+
outbound_capacity_msat: acc.outbound_capacity_msat.min(e.outbound_capacity_msat),
8814+
next_outbound_htlc_limit_msat: acc.next_outbound_htlc_limit_msat.min(e.next_outbound_htlc_limit_msat),
8815+
next_outbound_htlc_minimum_msat: acc.next_outbound_htlc_minimum_msat.max(e.next_outbound_htlc_minimum_msat),
8816+
}
8817+
})
8818+
.expect("At least one FundingScope is always provided")
88248819
}
88258820

88268821
fn build_commitment_no_status_check<L: Deref>(&mut self, logger: &L) -> ChannelMonitorUpdate where L::Target: Logger {

lightning/src/ln/channel_state.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use bitcoin::secp256k1::PublicKey;
1515

1616
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator};
1717
use crate::chain::transaction::OutPoint;
18-
use crate::ln::channel::{ChannelContext, FundingScope};
18+
use crate::ln::channel::Channel;
1919
use crate::ln::types::ChannelId;
2020
use crate::sign::SignerProvider;
2121
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
@@ -475,16 +475,17 @@ impl ChannelDetails {
475475
self.short_channel_id.or(self.outbound_scid_alias)
476476
}
477477

478-
pub(super) fn from_channel_context<SP: Deref, F: Deref>(
479-
context: &ChannelContext<SP>, funding: &FundingScope, pending_funding: &[FundingScope],
480-
best_block_height: u32, latest_features: InitFeatures,
478+
pub(super) fn from_channel<SP: Deref, F: Deref>(
479+
channel: &Channel<SP>, best_block_height: u32, latest_features: InitFeatures,
481480
fee_estimator: &LowerBoundedFeeEstimator<F>,
482481
) -> Self
483482
where
484483
SP::Target: SignerProvider,
485484
F::Target: FeeEstimator,
486485
{
487-
let balance = context.get_available_balances(funding, pending_funding, fee_estimator);
486+
let context = channel.context();
487+
let funding = channel.funding();
488+
let balance = channel.get_available_balances(fee_estimator);
488489
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
489490
funding.get_holder_counterparty_selected_channel_reserve_satoshis();
490491
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3726,7 +3726,7 @@ where
37263726
Ok(temporary_channel_id)
37273727
}
37283728

3729-
fn list_funded_channels_with_filter<Fn: FnMut(&(&ChannelId, &FundedChannel<SP>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
3729+
fn list_funded_channels_with_filter<Fn: FnMut(&(&ChannelId, &Channel<SP>)) -> bool + Copy>(&self, f: Fn) -> Vec<ChannelDetails> {
37303730
// Allocate our best estimate of the number of channels we have in the `res`
37313731
// Vec. Sadly the `short_to_chan_info` map doesn't cover channels without
37323732
// a scid or a scid alias. Therefore reallocations may still occur, but is
@@ -3741,12 +3741,11 @@ where
37413741
let peer_state = &mut *peer_state_lock;
37423742
res.extend(peer_state.channel_by_id.iter()
37433743
// Only `Channels` in the `Channel::Funded` phase can be considered funded.
3744-
.filter_map(|(chan_id, chan)| chan.as_funded().map(|chan| (chan_id, chan)))
3744+
.filter(|(_, chan)| chan.is_funded())
37453745
.filter(f)
37463746
.map(|(_channel_id, channel)| {
3747-
ChannelDetails::from_channel_context(
3748-
&channel.context, &channel.funding, &channel.pending_funding,
3749-
best_block_height, peer_state.latest_features.clone(),
3747+
ChannelDetails::from_channel(
3748+
channel, best_block_height, peer_state.latest_features.clone(),
37503749
&self.fee_estimator,
37513750
)
37523751
})
@@ -3771,12 +3770,10 @@ where
37713770
for (_cp_id, peer_state_mutex) in per_peer_state.iter() {
37723771
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
37733772
let peer_state = &mut *peer_state_lock;
3774-
for (context, funding, pending_funding) in peer_state.channel_by_id.iter()
3775-
.map(|(_, chan)| (chan.context(), chan.funding(), chan.pending_funding()))
3776-
{
3777-
let details = ChannelDetails::from_channel_context(
3778-
context, funding, pending_funding, best_block_height,
3779-
peer_state.latest_features.clone(), &self.fee_estimator,
3773+
for (_, channel) in peer_state.channel_by_id.iter() {
3774+
let details = ChannelDetails::from_channel(
3775+
channel, best_block_height, peer_state.latest_features.clone(),
3776+
&self.fee_estimator,
37803777
);
37813778
res.push(details);
37823779
}
@@ -3795,7 +3792,7 @@ where
37953792
// Note we use is_live here instead of usable which leads to somewhat confused
37963793
// internal/external nomenclature, but that's ok cause that's probably what the user
37973794
// really wanted anyway.
3798-
self.list_funded_channels_with_filter(|&(_, ref channel)| channel.context.is_live())
3795+
self.list_funded_channels_with_filter(|&(_, ref channel)| channel.context().is_live())
37993796
}
38003797

38013798
/// Gets the list of channels we have with a given counterparty, in random order.
@@ -3807,16 +3804,15 @@ where
38073804
let mut peer_state_lock = peer_state_mutex.lock().unwrap();
38083805
let peer_state = &mut *peer_state_lock;
38093806
let features = &peer_state.latest_features;
3810-
let context_to_details = |(context, funding, pending_funding)| {
3811-
ChannelDetails::from_channel_context(
3812-
context, funding, pending_funding, best_block_height, features.clone(),
3813-
&self.fee_estimator,
3807+
let channel_to_details = |channel| {
3808+
ChannelDetails::from_channel(
3809+
channel, best_block_height, features.clone(), &self.fee_estimator,
38143810
)
38153811
};
38163812
return peer_state.channel_by_id
38173813
.iter()
3818-
.map(|(_, chan)| (chan.context(), chan.funding(), chan.pending_funding()))
3819-
.map(context_to_details)
3814+
.map(|(_, chan)| (chan))
3815+
.map(channel_to_details)
38203816
.collect();
38213817
}
38223818
vec![]

0 commit comments

Comments
 (0)