@@ -2608,6 +2608,7 @@ impl FundingScope {
26082608 self.channel_transaction_parameters.channel_value_satoshis
26092609 }
26102610
2611+ #[cfg(not(feature = "safe_channels"))]
26112612 pub(crate) fn get_value_to_self_msat(&self) -> u64 {
26122613 self.value_to_self_msat
26132614 }
@@ -7413,6 +7414,161 @@ where
74137414 SP::Target: SignerProvider,
74147415 <SP::Target as SignerProvider>::EcdsaSigner: EcdsaChannelSigner,
74157416{
7417+ #[cfg(feature = "safe_channels")]
7418+ pub(crate) fn new_from_state<ES: Deref>(
7419+ channel: FundedChannelState, entropy_source: &ES, signer_provider: &SP,
7420+ _our_supported_features: &ChannelTypeFeatures,
7421+ ) -> Result<Self, DecodeError>
7422+ where
7423+ ES::Target: EntropySource,
7424+ {
7425+ let holder_signer = signer_provider.derive_channel_signer(channel.channel_keys_id);
7426+
7427+ let mut secp_ctx = Secp256k1::new();
7428+ secp_ctx.seeded_randomize(&entropy_source.get_secure_random_bytes());
7429+
7430+ if let Some(funding_negotiation) = channel
7431+ .pending_splice
7432+ .as_ref()
7433+ .and_then(|pending_splice| pending_splice.funding_negotiation.as_ref())
7434+ {
7435+ if !matches!(funding_negotiation, FundingNegotiation::AwaitingSignatures { .. }) {
7436+ return Err(DecodeError::InvalidValue);
7437+ }
7438+ }
7439+
7440+ let announcement_sigs_state = if channel.announcement_sigs_state_sent {
7441+ AnnouncementSigsState::PeerReceived
7442+ } else {
7443+ AnnouncementSigsState::NotSent
7444+ };
7445+
7446+ let blocked_monitor_updates = channel
7447+ .blocked_monitor_updates
7448+ .iter()
7449+ .map(|update| {
7450+ let mut reader = &update[..];
7451+ PendingChannelMonitorUpdate::read(&mut reader)
7452+ })
7453+ .collect::<Result<Vec<_>, _>>()
7454+ .map_err(|_| DecodeError::InvalidValue)?;
7455+
7456+ let channel_update_status = if channel.channel_update_status_enabled {
7457+ ChannelUpdateStatus::Enabled
7458+ } else {
7459+ ChannelUpdateStatus::Disabled
7460+ };
7461+
7462+ Ok(FundedChannel {
7463+ funding: channel.funding,
7464+ context: ChannelContext {
7465+ user_id: channel.user_id,
7466+ config: channel.config,
7467+ prev_config: None,
7468+ inbound_handshake_limits_override: None,
7469+ channel_id: channel.channel_id,
7470+ temporary_channel_id: channel.temporary_channel_id,
7471+ channel_state: channel.channel_state,
7472+ announcement_sigs_state,
7473+ secp_ctx,
7474+ latest_monitor_update_id: channel.latest_monitor_update_id,
7475+ holder_signer: ChannelSignerType::Ecdsa(holder_signer),
7476+ shutdown_scriptpubkey: channel.shutdown_scriptpubkey,
7477+ destination_script: channel.destination_script,
7478+ counterparty_next_commitment_transaction_number: channel
7479+ .counterparty_next_commitment_transaction_number,
7480+ holder_max_accepted_htlcs: channel.holder_max_accepted_htlcs,
7481+ pending_inbound_htlcs: channel.pending_inbound_htlcs,
7482+ pending_outbound_htlcs: channel.pending_outbound_htlcs,
7483+ holding_cell_htlc_updates: channel.holding_cell_htlc_updates,
7484+ resend_order: channel.resend_order,
7485+
7486+ monitor_pending_channel_ready: channel.monitor_pending_channel_ready,
7487+ monitor_pending_revoke_and_ack: channel.monitor_pending_revoke_and_ack,
7488+ monitor_pending_commitment_signed: channel.monitor_pending_commitment_signed,
7489+ monitor_pending_forwards: channel.monitor_pending_forwards,
7490+ monitor_pending_failures: channel.monitor_pending_failures,
7491+ monitor_pending_finalized_fulfills: channel.monitor_pending_finalized_fulfills,
7492+ monitor_pending_update_adds: channel.monitor_pending_update_adds,
7493+
7494+ signer_pending_revoke_and_ack: false,
7495+ signer_pending_commitment_update: false,
7496+ signer_pending_funding: false,
7497+ signer_pending_closing: false,
7498+ signer_pending_channel_ready: false,
7499+ signer_pending_stale_state_verification: None,
7500+
7501+ pending_update_fee: channel.pending_update_fee,
7502+ holding_cell_update_fee: channel.holding_cell_update_fee,
7503+ next_holder_htlc_id: channel.next_holder_htlc_id,
7504+ next_counterparty_htlc_id: channel.next_counterparty_htlc_id,
7505+ update_time_counter: channel.update_time_counter,
7506+ feerate_per_kw: channel.feerate_per_kw,
7507+
7508+ last_sent_closing_fee: None,
7509+ last_received_closing_sig: None,
7510+ pending_counterparty_closing_signed: None,
7511+ expecting_peer_commitment_signed: false,
7512+ closing_fee_limits: None,
7513+ target_closing_feerate_sats_per_kw: channel.target_closing_feerate_sats_per_kw,
7514+
7515+ channel_creation_height: channel.channel_creation_height,
7516+
7517+ counterparty_dust_limit_satoshis: channel.counterparty_dust_limit_satoshis,
7518+ holder_dust_limit_satoshis: channel.holder_dust_limit_satoshis,
7519+ counterparty_max_htlc_value_in_flight_msat: channel
7520+ .counterparty_max_htlc_value_in_flight_msat,
7521+ holder_max_htlc_value_in_flight_msat: channel.holder_max_htlc_value_in_flight_msat,
7522+ counterparty_htlc_minimum_msat: channel.counterparty_htlc_minimum_msat,
7523+ holder_htlc_minimum_msat: channel.holder_htlc_minimum_msat,
7524+ counterparty_max_accepted_htlcs: channel.counterparty_max_accepted_htlcs,
7525+ minimum_depth: channel.minimum_depth,
7526+
7527+ counterparty_forwarding_info: channel.counterparty_forwarding_info,
7528+
7529+ is_batch_funding: channel.is_batch_funding,
7530+
7531+ counterparty_next_commitment_point: channel.counterparty_next_commitment_point,
7532+ counterparty_current_commitment_point: channel
7533+ .counterparty_current_commitment_point,
7534+ counterparty_node_id: channel.counterparty_node_id,
7535+
7536+ counterparty_shutdown_scriptpubkey: channel.counterparty_shutdown_scriptpubkey,
7537+
7538+ commitment_secrets: channel.commitment_secrets,
7539+
7540+ channel_update_status,
7541+ closing_signed_in_flight: false,
7542+
7543+ announcement_sigs: channel.announcement_sigs,
7544+
7545+ workaround_lnd_bug_4006: None,
7546+ sent_message_awaiting_response: None,
7547+
7548+ latest_inbound_scid_alias: channel.latest_inbound_scid_alias,
7549+ outbound_scid_alias: channel.outbound_scid_alias,
7550+ historical_scids: channel.historical_scids,
7551+
7552+ funding_tx_broadcast_safe_event_emitted: channel
7553+ .funding_tx_broadcast_safe_event_emitted,
7554+ channel_pending_event_emitted: channel.channel_pending_event_emitted,
7555+ initial_channel_ready_event_emitted: channel.initial_channel_ready_event_emitted,
7556+
7557+ channel_keys_id: channel.channel_keys_id,
7558+
7559+ local_initiated_shutdown: channel.local_initiated_shutdown,
7560+
7561+ blocked_monitor_updates,
7562+ is_manual_broadcast: channel.is_manual_broadcast,
7563+
7564+ interactive_tx_signing_session: channel.interactive_tx_signing_session,
7565+ },
7566+ holder_commitment_point: channel.holder_commitment_point,
7567+ pending_splice: channel.pending_splice,
7568+ quiescent_action: channel.quiescent_action,
7569+ })
7570+ }
7571+
74167572 pub fn context(&self) -> &ChannelContext<SP> {
74177573 &self.context
74187574 }
@@ -11406,10 +11562,12 @@ where
1140611562 .try_for_each(|funding| self.context.can_accept_incoming_htlc(funding, dust_exposure_limiting_feerate, &logger))
1140711563 }
1140811564
11565+ #[cfg(not(feature = "safe_channels"))]
1140911566 pub fn get_cur_holder_commitment_transaction_number(&self) -> u64 {
1141011567 self.holder_commitment_point.current_transaction_number()
1141111568 }
1141211569
11570+ #[cfg(not(feature = "safe_channels"))]
1141311571 pub fn get_cur_counterparty_commitment_transaction_number(&self) -> u64 {
1141411572 self.context.counterparty_next_commitment_transaction_number + 1
1141511573 - if self.context.channel_state.is_awaiting_remote_revoke() { 1 } else { 0 }
@@ -11523,6 +11681,7 @@ where
1152311681 /// transaction. If the channel is inbound, this implies simply that the channel has not
1152411682 /// advanced state.
1152511683 #[rustfmt::skip]
11684+ #[cfg(not(feature = "safe_channels"))]
1152611685 pub fn is_awaiting_initial_mon_persist(&self) -> bool {
1152711686 if !self.is_awaiting_monitor_update() { return false; }
1152811687 if matches!(
@@ -14961,6 +15120,7 @@ impl Readable for AnnouncementSigsState {
1496115120 }
1496215121}
1496315122
15123+ #[cfg(not(feature = "safe_channels"))]
1496415124impl<SP: Deref> Writeable for FundedChannel<SP>
1496515125where
1496615126 SP::Target: SignerProvider,
@@ -15438,6 +15598,7 @@ where
1543815598 }
1543915599}
1544015600
15601+ #[cfg(not(feature = "safe_channels"))]
1544115602impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c ChannelTypeFeatures)>
1544215603 for FundedChannel<SP>
1544315604where
@@ -16327,6 +16488,8 @@ mod tests {
1632716488 use crate::chain::transaction::OutPoint;
1632816489 use crate::chain::BestBlock;
1632916490 use crate::ln::chan_utils::{self, commit_tx_fee_sat, ChannelTransactionParameters};
16491+ #[cfg(feature = "safe_channels")]
16492+ use crate::ln::channel::FundedChannelState;
1633016493 use crate::ln::channel::{
1633116494 AwaitingChannelReadyFlags, ChannelState, FundedChannel, HTLCCandidate, HTLCInitiator,
1633216495 HTLCUpdateAwaitingACK, InboundHTLCOutput, InboundHTLCState, InboundV1Channel,
@@ -16354,6 +16517,8 @@ mod tests {
1635416517 use crate::types::payment::{PaymentHash, PaymentPreimage};
1635516518 use crate::util::config::UserConfig;
1635616519 use crate::util::errors::APIError;
16520+ #[cfg(feature = "safe_channels")]
16521+ use crate::util::ser::Readable;
1635716522 use crate::util::ser::{ReadableArgs, Writeable};
1635816523 use crate::util::test_utils::{
1635916524 self, OnGetShutdownScriptpubkey, TestFeeEstimator, TestKeysInterface, TestLogger,
@@ -17138,14 +17303,34 @@ mod tests {
1713817303 chan.context.holding_cell_htlc_updates = holding_cell_htlc_updates.clone();
1713917304
1714017305 // Encode and decode the channel and ensure that the HTLCs within are the same.
17141- let encoded_chan = chan.encode();
17142- let mut s = crate::io::Cursor::new(&encoded_chan);
17143- let mut reader =
17144- crate::util::ser::FixedLengthReader::new(&mut s, encoded_chan.len() as u64);
1714517306 let features = channelmanager::provided_channel_type_features(&config);
17146- let decoded_chan =
17147- FundedChannel::read(&mut reader, (&&keys_provider, &&keys_provider, &features))
17148- .unwrap();
17307+ let decoded_chan;
17308+ #[cfg(not(feature = "safe_channels"))]
17309+ {
17310+ let encoded_chan = chan.encode();
17311+ let mut s = crate::io::Cursor::new(&encoded_chan);
17312+ let mut reader =
17313+ crate::util::ser::FixedLengthReader::new(&mut s, encoded_chan.len() as u64);
17314+ decoded_chan =
17315+ FundedChannel::read(&mut reader, (&&keys_provider, &&keys_provider, &features))
17316+ .unwrap();
17317+ }
17318+ #[cfg(feature = "safe_channels")]
17319+ {
17320+ let channel_state: FundedChannelState = (&mut chan).into();
17321+ let encoded_chan = channel_state.encode();
17322+ let mut s = crate::io::Cursor::new(&encoded_chan);
17323+ let mut reader =
17324+ crate::util::ser::FixedLengthReader::new(&mut s, encoded_chan.len() as u64);
17325+ let decoded_state = FundedChannelState::read(&mut reader).unwrap();
17326+ decoded_chan = FundedChannel::new_from_state(
17327+ decoded_state,
17328+ &&keys_provider,
17329+ &&keys_provider,
17330+ &features,
17331+ )
17332+ .unwrap();
17333+ }
1714917334 assert_eq!(decoded_chan.context.pending_outbound_htlcs, pending_outbound_htlcs);
1715017335 assert_eq!(decoded_chan.context.holding_cell_htlc_updates, holding_cell_htlc_updates);
1715117336 }
0 commit comments