- // Public utilities:
-
- pub fn channel_id(&self) -> [u8; 32] {
- self.context.channel_id
- }
-
- // Return the `temporary_channel_id` used during channel establishment.
- //
- // Will return `None` for channels created prior to LDK version 0.0.115.
- pub fn temporary_channel_id(&self) -> Option<[u8; 32]> {
- self.context.temporary_channel_id
- }
-
- pub fn minimum_depth(&self) -> Option<u32> {
- self.context.minimum_depth
- }
-
- /// Gets the "user_id" value passed into the construction of this channel. It has no special
- /// meaning and exists only to allow users to have a persistent identifier of a channel.
- pub fn get_user_id(&self) -> u128 {
- self.context.user_id
- }
-
- /// Gets the channel's type
- pub fn get_channel_type(&self) -> &ChannelTypeFeatures {
- &self.context.channel_type
- }
-
- /// Guaranteed to be Some after both ChannelReady messages have been exchanged (and, thus,
- /// is_usable() returns true).
- /// Allowed in any state (including after shutdown)
- pub fn get_short_channel_id(&self) -> Option<u64> {
- self.context.short_channel_id
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn latest_inbound_scid_alias(&self) -> Option<u64> {
- self.context.latest_inbound_scid_alias
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn outbound_scid_alias(&self) -> u64 {
- self.context.outbound_scid_alias
- }
- /// Only allowed immediately after deserialization if get_outbound_scid_alias returns 0,
- /// indicating we were written by LDK prior to 0.0.106 which did not set outbound SCID aliases.
- pub fn set_outbound_scid_alias(&mut self, outbound_scid_alias: u64) {
- assert_eq!(self.context.outbound_scid_alias, 0);
- self.context.outbound_scid_alias = outbound_scid_alias;
- }
-
- /// Returns the funding_txo we either got from our peer, or were given by
- /// get_outbound_funding_created.
- pub fn get_funding_txo(&self) -> Option<OutPoint> {
- self.context.channel_transaction_parameters.funding_outpoint
- }
-
- /// Returns the block hash in which our funding transaction was confirmed.
- pub fn get_funding_tx_confirmed_in(&self) -> Option<BlockHash> {
- self.context.funding_tx_confirmed_in
- }
-
- /// Returns the current number of confirmations on the funding transaction.
- pub fn get_funding_tx_confirmations(&self, height: u32) -> u32 {
- if self.context.funding_tx_confirmation_height == 0 {
- // We either haven't seen any confirmation yet, or observed a reorg.
- return 0;
- }
-
- height.checked_sub(self.context.funding_tx_confirmation_height).map_or(0, |c| c + 1)
- }
-
- fn get_holder_selected_contest_delay(&self) -> u16 {
- self.context.channel_transaction_parameters.holder_selected_contest_delay
- }
-
- fn get_holder_pubkeys(&self) -> &ChannelPublicKeys {
- &self.context.channel_transaction_parameters.holder_pubkeys
- }
-
- pub fn get_counterparty_selected_contest_delay(&self) -> Option<u16> {
- self.context.channel_transaction_parameters.counterparty_parameters
- .as_ref().map(|params| params.selected_contest_delay)
- }
-
- fn get_counterparty_pubkeys(&self) -> &ChannelPublicKeys {
- &self.context.channel_transaction_parameters.counterparty_parameters.as_ref().unwrap().pubkeys
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn get_counterparty_node_id(&self) -> PublicKey {
- self.context.counterparty_node_id
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn get_holder_htlc_minimum_msat(&self) -> u64 {
- self.context.holder_htlc_minimum_msat
- }
-
- /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
- pub fn get_holder_htlc_maximum_msat(&self) -> Option<u64> {
- self.get_htlc_maximum_msat(self.context.holder_max_htlc_value_in_flight_msat)
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn get_announced_htlc_max_msat(&self) -> u64 {
- return cmp::min(
- // Upper bound by capacity. We make it a bit less than full capacity to prevent attempts
- // to use full capacity. This is an effort to reduce routing failures, because in many cases
- // channel might have been used to route very small values (either by honest users or as DoS).
- self.context.channel_value_satoshis * 1000 * 9 / 10,
-
- self.context.counterparty_max_htlc_value_in_flight_msat
- );
- }
-
- /// Allowed in any state (including after shutdown)
- pub fn get_counterparty_htlc_minimum_msat(&self) -> u64 {
- self.context.counterparty_htlc_minimum_msat
- }
-
- /// Allowed in any state (including after shutdown), but will return none before TheirInitSent
- pub fn get_counterparty_htlc_maximum_msat(&self) -> Option<u64> {
- self.get_htlc_maximum_msat(self.context.counterparty_max_htlc_value_in_flight_msat)
- }
-
- fn get_htlc_maximum_msat(&self, party_max_htlc_value_in_flight_msat: u64) -> Option<u64> {
- self.context.counterparty_selected_channel_reserve_satoshis.map(|counterparty_reserve| {
- let holder_reserve = self.context.holder_selected_channel_reserve_satoshis;
- cmp::min(
- (self.context.channel_value_satoshis - counterparty_reserve - holder_reserve) * 1000,
- party_max_htlc_value_in_flight_msat
- )
- })
- }
-
- pub fn get_value_satoshis(&self) -> u64 {
- self.context.channel_value_satoshis
- }
-
- pub fn get_fee_proportional_millionths(&self) -> u32 {
- self.context.config.options.forwarding_fee_proportional_millionths
- }
-
- pub fn get_cltv_expiry_delta(&self) -> u16 {
- cmp::max(self.context.config.options.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
- }
-
- pub fn get_max_dust_htlc_exposure_msat(&self) -> u64 {
- self.context.config.options.max_dust_htlc_exposure_msat
- }
-
- /// Returns the previous [`ChannelConfig`] applied to this channel, if any.
- pub fn prev_config(&self) -> Option<ChannelConfig> {
- self.context.prev_config.map(|prev_config| prev_config.0)
- }
-
- // Checks whether we should emit a `ChannelPending` event.
- pub(crate) fn should_emit_channel_pending_event(&mut self) -> bool {
- self.is_funding_initiated() && !self.context.channel_pending_event_emitted
- }
-
- // Returns whether we already emitted a `ChannelPending` event.
- pub(crate) fn channel_pending_event_emitted(&self) -> bool {
- self.context.channel_pending_event_emitted
- }
-
- // Remembers that we already emitted a `ChannelPending` event.
- pub(crate) fn set_channel_pending_event_emitted(&mut self) {
- self.context.channel_pending_event_emitted = true;
- }
-
- // Checks whether we should emit a `ChannelReady` event.
- pub(crate) fn should_emit_channel_ready_event(&mut self) -> bool {
- self.is_usable() && !self.context.channel_ready_event_emitted
- }
-
- // Remembers that we already emitted a `ChannelReady` event.
- pub(crate) fn set_channel_ready_event_emitted(&mut self) {
- self.context.channel_ready_event_emitted = true;
- }
-
- /// Tracks the number of ticks elapsed since the previous [`ChannelConfig`] was updated. Once
- /// [`EXPIRE_PREV_CONFIG_TICKS`] is reached, the previous config is considered expired and will
- /// no longer be considered when forwarding HTLCs.
- pub fn maybe_expire_prev_config(&mut self) {
- if self.context.prev_config.is_none() {
- return;
- }
- let prev_config = self.context.prev_config.as_mut().unwrap();
- prev_config.1 += 1;
- if prev_config.1 == EXPIRE_PREV_CONFIG_TICKS {
- self.context.prev_config = None;
- }
- }
-
- /// Returns the current [`ChannelConfig`] applied to the channel.
- pub fn config(&self) -> ChannelConfig {
- self.context.config.options
- }
-
- /// Updates the channel's config. A bool is returned indicating whether the config update
- /// applied resulted in a new ChannelUpdate message.
- pub fn update_config(&mut self, config: &ChannelConfig) -> bool {
- let did_channel_update =
- self.context.config.options.forwarding_fee_proportional_millionths != config.forwarding_fee_proportional_millionths ||
- self.context.config.options.forwarding_fee_base_msat != config.forwarding_fee_base_msat ||
- self.context.config.options.cltv_expiry_delta != config.cltv_expiry_delta;
- if did_channel_update {
- self.context.prev_config = Some((self.context.config.options, 0));
- // Update the counter, which backs the ChannelUpdate timestamp, to allow the relay
- // policy change to propagate throughout the network.
- self.context.update_time_counter += 1;
- }
- self.context.config.options = *config;
- did_channel_update
- }
-