@@ -67,7 +67,7 @@ use crate::util::scid_utils::scid_from_parts;
6767use crate::io;
6868use crate::prelude::*;
6969use core::{cmp,mem,fmt};
70- use core::ops::Deref;
70+ use core::ops::{ Deref, DerefMut} ;
7171#[cfg(any(test, fuzzing, debug_assertions))]
7272use crate::sync::Mutex;
7373use crate::sign::type_resolver::ChannelSignerType;
@@ -760,10 +760,12 @@ impl<'a, L: Deref> Logger for WithChannelContext<'a, L> where L::Target: Logger
760760 }
761761}
762762
763- impl<'a, 'b, L: Deref> WithChannelContext<'a, L>
763+ impl<'a, L: Deref> WithChannelContext<'a, L>
764764where L::Target: Logger {
765- pub(super) fn from<S: Deref>(logger: &'a L, context: &'b ChannelContext<S>, payment_hash: Option<PaymentHash>) -> Self
766- where S::Target: SignerProvider
765+ pub(super) fn from<C, S: Deref>(logger: &'a L, context: C, payment_hash: Option<PaymentHash>) -> Self
766+ where
767+ C: Deref<Target = ChannelContext<S>>,
768+ S::Target: SignerProvider,
767769 {
768770 WithChannelContext {
769771 logger,
@@ -1155,23 +1157,33 @@ impl<SP: Deref> Channel<SP> where
11551157 SP::Target: SignerProvider,
11561158 <SP::Target as SignerProvider>::EcdsaSigner: ChannelSigner,
11571159{
1158- pub fn context(&self) -> &ChannelContext<SP> {
1159- match &self.phase {
1160+ pub fn context(&self) -> ScopedChannelContext< &ChannelContext<SP>, &FundingScope, SP> {
1161+ let (funding, context) = match &self.phase {
11601162 ChannelPhase::Undefined => unreachable!(),
1161- ChannelPhase::Funded(chan) => &chan.context,
1162- ChannelPhase::UnfundedOutboundV1(chan) => &chan.context,
1163- ChannelPhase::UnfundedInboundV1(chan) => &chan.context,
1164- ChannelPhase::UnfundedV2(chan) => &chan.context,
1163+ ChannelPhase::Funded(chan) => (&chan.funding, &chan.context),
1164+ ChannelPhase::UnfundedOutboundV1(chan) => (&chan.funding, &chan.context),
1165+ ChannelPhase::UnfundedInboundV1(chan) => (&chan.funding, &chan.context),
1166+ ChannelPhase::UnfundedV2(chan) => (&chan.funding, &chan.context),
1167+ };
1168+
1169+ ScopedChannelContext {
1170+ funding,
1171+ context,
11651172 }
11661173 }
11671174
1168- pub fn context_mut(&mut self) -> &mut ChannelContext<SP> {
1169- match &mut self.phase {
1175+ pub fn context_mut(&mut self) -> ScopedChannelContext< &mut ChannelContext<SP>, &mut FundingScope, SP> {
1176+ let (funding, context) = match &mut self.phase {
11701177 ChannelPhase::Undefined => unreachable!(),
1171- ChannelPhase::Funded(chan) => &mut chan.context,
1172- ChannelPhase::UnfundedOutboundV1(chan) => &mut chan.context,
1173- ChannelPhase::UnfundedInboundV1(chan) => &mut chan.context,
1174- ChannelPhase::UnfundedV2(chan) => &mut chan.context,
1178+ ChannelPhase::Funded(chan) => (&mut chan.funding, &mut chan.context),
1179+ ChannelPhase::UnfundedOutboundV1(chan) => (&mut chan.funding, &mut chan.context),
1180+ ChannelPhase::UnfundedInboundV1(chan) => (&mut chan.funding, &mut chan.context),
1181+ ChannelPhase::UnfundedV2(chan) => (&mut chan.funding, &mut chan.context),
1182+ };
1183+
1184+ ScopedChannelContext {
1185+ funding,
1186+ context,
11751187 }
11761188 }
11771189
@@ -1549,6 +1561,52 @@ impl UnfundedChannelContext {
15491561pub(super) struct FundingScope {
15501562}
15511563
1564+ ///
1565+ pub(super) struct ScopedChannelContext<C, F, SP: Deref>
1566+ where
1567+ C: Deref<Target = ChannelContext<SP>>,
1568+ F: Deref<Target = FundingScope>,
1569+ SP::Target: SignerProvider,
1570+ {
1571+ funding: F,
1572+ context: C,
1573+ }
1574+
1575+ impl<C, F, SP: Deref> Deref for ScopedChannelContext<C, F, SP>
1576+ where
1577+ C: Deref<Target = ChannelContext<SP>>,
1578+ F: Deref<Target = FundingScope>,
1579+ SP::Target: SignerProvider,
1580+ {
1581+ type Target = ChannelContext<SP>;
1582+
1583+ fn deref(&self) -> &Self::Target {
1584+ &self.context
1585+ }
1586+ }
1587+
1588+ impl<C, F, SP: Deref> DerefMut for ScopedChannelContext<C, F, SP>
1589+ where
1590+ C: DerefMut<Target = ChannelContext<SP>>,
1591+ F: DerefMut<Target = FundingScope>,
1592+ SP::Target: SignerProvider,
1593+ {
1594+ fn deref_mut(&mut self) -> &mut Self::Target {
1595+ &mut self.context
1596+ }
1597+ }
1598+
1599+ impl<C, F, SP: Deref> ScopedChannelContext<C, F, SP>
1600+ where
1601+ C: Deref<Target = ChannelContext<SP>>,
1602+ F: Deref<Target = FundingScope>,
1603+ SP::Target: SignerProvider,
1604+ {
1605+ fn test(&self) -> u64 {
1606+ self.channel_value_satoshis
1607+ }
1608+ }
1609+
15521610/// Contains everything about the channel including state, and various flags.
15531611pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
15541612 config: LegacyChannelConfig,
@@ -1891,9 +1949,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
18911949/// A channel struct implementing this trait can receive an initial counterparty commitment
18921950/// transaction signature.
18931951trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvider {
1894- fn context(&self) -> &ChannelContext<SP>;
1952+ fn context(&self) -> ScopedChannelContext< &ChannelContext<SP>, &FundingScope, SP>;
18951953
1896- fn context_mut(&mut self) -> &mut ChannelContext<SP>;
1954+ fn context_mut(&mut self) -> ScopedChannelContext< &mut ChannelContext<SP>, &mut FundingScope, SP>;
18971955
18981956 fn received_msg(&self) -> &'static str;
18991957
@@ -1939,7 +1997,7 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
19391997 panic!("unexpected error type from check_counterparty_commitment_signature {:?}", e);
19401998 }
19411999 };
1942- let context = self.context_mut();
2000+ let mut context = self.context_mut();
19432001 let counterparty_keys = context.build_remote_transaction_keys();
19442002 let counterparty_initial_commitment_tx = context.build_commitment_transaction(context.cur_counterparty_commitment_transaction_number, &counterparty_keys, false, false, logger).tx;
19452003 let counterparty_trusted_tx = counterparty_initial_commitment_tx.trust();
@@ -2011,12 +2069,19 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
20112069}
20122070
20132071impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for OutboundV1Channel<SP> where SP::Target: SignerProvider {
2014- fn context(&self) -> &ChannelContext<SP> {
2015- &self.context
2072+
2073+ fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
2074+ ScopedChannelContext {
2075+ funding: &self.funding,
2076+ context: &self.context,
2077+ }
20162078 }
20172079
2018- fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2019- &mut self.context
2080+ fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
2081+ ScopedChannelContext {
2082+ funding: &mut self.funding,
2083+ context: &mut self.context,
2084+ }
20202085 }
20212086
20222087 fn received_msg(&self) -> &'static str {
@@ -2025,12 +2090,18 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for OutboundV1Channel<SP> wh
20252090}
20262091
20272092impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for InboundV1Channel<SP> where SP::Target: SignerProvider {
2028- fn context(&self) -> &ChannelContext<SP> {
2029- &self.context
2093+ fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
2094+ ScopedChannelContext {
2095+ funding: &self.funding,
2096+ context: &self.context,
2097+ }
20302098 }
20312099
2032- fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2033- &mut self.context
2100+ fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
2101+ ScopedChannelContext {
2102+ funding: &mut self.funding,
2103+ context: &mut self.context,
2104+ }
20342105 }
20352106
20362107 fn received_msg(&self) -> &'static str {
@@ -2039,12 +2110,18 @@ impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for InboundV1Channel<SP> whe
20392110}
20402111
20412112impl<SP: Deref> InitialRemoteCommitmentReceiver<SP> for FundedChannel<SP> where SP::Target: SignerProvider {
2042- fn context(&self) -> &ChannelContext<SP> {
2043- &self.context
2113+ fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
2114+ ScopedChannelContext {
2115+ funding: &self.funding,
2116+ context: &self.context,
2117+ }
20442118 }
20452119
2046- fn context_mut(&mut self) -> &mut ChannelContext<SP> {
2047- &mut self.context
2120+ fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
2121+ ScopedChannelContext {
2122+ funding: &mut self.funding,
2123+ context: &mut self.context,
2124+ }
20482125 }
20492126
20502127 fn received_msg(&self) -> &'static str {
@@ -4646,6 +4723,20 @@ impl<SP: Deref> FundedChannel<SP> where
46464723 SP::Target: SignerProvider,
46474724 <SP::Target as SignerProvider>::EcdsaSigner: EcdsaChannelSigner
46484725{
4726+ pub fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
4727+ ScopedChannelContext {
4728+ funding: &self.funding,
4729+ context: &self.context,
4730+ }
4731+ }
4732+
4733+ pub fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
4734+ ScopedChannelContext {
4735+ funding: &mut self.funding,
4736+ context: &mut self.context,
4737+ }
4738+ }
4739+
46494740 fn check_remote_fee<F: Deref, L: Deref>(
46504741 channel_type: &ChannelTypeFeatures, fee_estimator: &LowerBoundedFeeEstimator<F>,
46514742 feerate_per_kw: u32, cur_feerate_per_kw: Option<u32>, logger: &L
@@ -8598,6 +8689,20 @@ pub(super) struct OutboundV1Channel<SP: Deref> where SP::Target: SignerProvider
85988689}
85998690
86008691impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
8692+ pub fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
8693+ ScopedChannelContext {
8694+ funding: &self.funding,
8695+ context: &self.context,
8696+ }
8697+ }
8698+
8699+ pub fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
8700+ ScopedChannelContext {
8701+ funding: &mut self.funding,
8702+ context: &mut self.context,
8703+ }
8704+ }
8705+
86018706 #[allow(dead_code)] // TODO(dual_funding): Remove once opending V2 channels is enabled.
86028707 pub fn new<ES: Deref, F: Deref, L: Deref>(
86038708 fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP, counterparty_node_id: PublicKey, their_features: &InitFeatures,
@@ -8929,6 +9034,20 @@ pub(super) fn channel_type_from_open_channel(
89299034}
89309035
89319036impl<SP: Deref> InboundV1Channel<SP> where SP::Target: SignerProvider {
9037+ pub fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
9038+ ScopedChannelContext {
9039+ funding: &self.funding,
9040+ context: &self.context,
9041+ }
9042+ }
9043+
9044+ pub fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
9045+ ScopedChannelContext {
9046+ funding: &mut self.funding,
9047+ context: &mut self.context,
9048+ }
9049+ }
9050+
89329051 /// Creates a new channel from a remote sides' request for one.
89339052 /// Assumes chain_hash has already been checked and corresponds with what we expect!
89349053 pub fn new<ES: Deref, F: Deref, L: Deref>(
@@ -9156,6 +9275,20 @@ pub(super) struct PendingV2Channel<SP: Deref> where SP::Target: SignerProvider {
91569275}
91579276
91589277impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
9278+ pub fn context(&self) -> ScopedChannelContext<&ChannelContext<SP>, &FundingScope, SP> {
9279+ ScopedChannelContext {
9280+ funding: &self.funding,
9281+ context: &self.context,
9282+ }
9283+ }
9284+
9285+ pub fn context_mut(&mut self) -> ScopedChannelContext<&mut ChannelContext<SP>, &mut FundingScope, SP> {
9286+ ScopedChannelContext {
9287+ funding: &mut self.funding,
9288+ context: &mut self.context,
9289+ }
9290+ }
9291+
91599292 #[allow(dead_code)] // TODO(dual_funding): Remove once creating V2 channels is enabled.
91609293 pub fn new_outbound<ES: Deref, F: Deref, L: Deref>(
91619294 fee_estimator: &LowerBoundedFeeEstimator<F>, entropy_source: &ES, signer_provider: &SP,
0 commit comments