+ (0, features, required),
+ (1, announcement_received_time, (default_value, 0)),
+ (2, node_one, required),
+ (4, one_to_two, required),
+ (6, node_two, required),
+ (8, two_to_one, required),
+ (10, capacity_sats, required),
+ (12, announcement_message, required),
+});
+
+/// A wrapper around [`ChannelInfo`] representing information about the channel as directed from a
+/// source node to a target node.
+#[derive(Clone)]
+pub struct DirectedChannelInfo<'a> {
+ channel: &'a ChannelInfo,
+ direction: Option<&'a ChannelUpdateInfo>,
+ htlc_maximum_msat: u64,
+ effective_capacity: EffectiveCapacity,
+}
+
+impl<'a> DirectedChannelInfo<'a> {
+ #[inline]
+ fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
+ let htlc_maximum_msat = direction.and_then(|direction| direction.htlc_maximum_msat);
+ let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);
+
+ let (htlc_maximum_msat, effective_capacity) = match (htlc_maximum_msat, capacity_msat) {
+ (Some(amount_msat), Some(capacity_msat)) => {
+ let htlc_maximum_msat = cmp::min(amount_msat, capacity_msat);
+ (htlc_maximum_msat, EffectiveCapacity::Total { capacity_msat })
+ },
+ (Some(amount_msat), None) => {
+ (amount_msat, EffectiveCapacity::MaximumHTLC { amount_msat })
+ },
+ (None, Some(capacity_msat)) => {
+ (capacity_msat, EffectiveCapacity::Total { capacity_msat })
+ },
+ (None, None) => (EffectiveCapacity::Unknown.as_msat(), EffectiveCapacity::Unknown),
+ };
+
+ Self {
+ channel, direction, htlc_maximum_msat, effective_capacity
+ }
+ }
+
+ /// Returns information for the channel.
+ pub fn channel(&self) -> &'a ChannelInfo { self.channel }
+
+ /// Returns information for the direction.
+ pub fn direction(&self) -> Option<&'a ChannelUpdateInfo> { self.direction }
+
+ /// Returns the maximum HTLC amount allowed over the channel in the direction.
+ pub fn htlc_maximum_msat(&self) -> u64 {
+ self.htlc_maximum_msat
+ }
+
+ /// Returns the [`EffectiveCapacity`] of the channel in the direction.
+ ///
+ /// This is either the total capacity from the funding transaction, if known, or the
+ /// `htlc_maximum_msat` for the direction as advertised by the gossip network, if known,
+ /// otherwise.
+ pub fn effective_capacity(&self) -> EffectiveCapacity {
+ self.effective_capacity
+ }
+
+ /// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
+ pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
+ match self.direction {
+ Some(_) => Some(DirectedChannelInfoWithUpdate { inner: self }),
+ None => None,
+ }
+ }
+}
+
+impl<'a> fmt::Debug for DirectedChannelInfo<'a> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ f.debug_struct("DirectedChannelInfo")
+ .field("channel", &self.channel)
+ .finish()
+ }
+}
+
+/// A [`DirectedChannelInfo`] with [`ChannelUpdateInfo`] available in its direction.
+#[derive(Clone)]
+pub(super) struct DirectedChannelInfoWithUpdate<'a> {
+ inner: DirectedChannelInfo<'a>,
+}
+
+impl<'a> DirectedChannelInfoWithUpdate<'a> {
+ /// Returns information for the channel.
+ #[inline]
+ pub(super) fn channel(&self) -> &'a ChannelInfo { &self.inner.channel }
+
+ /// Returns information for the direction.
+ #[inline]
+ pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.inner.direction.unwrap() }