]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add a ChannelValueStat getter to simplify tests
authorYuntai Kyong <yuntai.kyong@gmail.com>
Mon, 1 Oct 2018 09:11:33 +0000 (18:11 +0900)
committerMatt Corallo <git@bluematt.me>
Sat, 6 Oct 2018 03:03:24 +0000 (12:03 +0900)
src/ln/channel.rs

index 2dc7750e472be7d02a1a85fc2d109cb3a7031a67..d1cfd8b8967bb1e2cc7917abb94c1f1049675def 100644 (file)
@@ -44,6 +44,17 @@ pub struct ChannelKeys {
        pub commitment_seed: [u8; 32],
 }
 
+#[cfg(test)]
+pub struct ChannelValueStat {
+       pub value_to_self_msat: u64,
+       pub channel_value_msat: u64,
+       pub channel_reserve_msat: u64,
+       pub pending_outbound_htlcs_amount_msat: u64,
+       pub pending_inbound_htlcs_amount_msat: u64,
+       pub holding_cell_outbound_amount_msat: u64,
+       pub their_max_htlc_value_in_flight_msat: u64, // outgoing
+}
+
 impl ChannelKeys {
        pub fn new_from_seed(seed: &[u8; 32]) -> Result<ChannelKeys, secp256k1::Error> {
                let mut prk = [0; 32];
@@ -2352,6 +2363,30 @@ impl Channel {
                &self.local_keys
        }
 
+       #[cfg(test)]
+       pub fn get_value_stat(&self) -> ChannelValueStat {
+               ChannelValueStat {
+                       value_to_self_msat: self.value_to_self_msat,
+                       channel_value_msat: self.channel_value_satoshis * 1000,
+                       channel_reserve_msat: self.their_channel_reserve_satoshis * 1000,
+                       pending_outbound_htlcs_amount_msat: self.pending_outbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
+                       pending_inbound_htlcs_amount_msat: self.pending_inbound_htlcs.iter().map(|ref h| h.amount_msat).sum::<u64>(),
+                       holding_cell_outbound_amount_msat: {
+                               let mut res = 0;
+                               for h in self.holding_cell_htlc_updates.iter() {
+                                       match &h {
+                                               &HTLCUpdateAwaitingACK::AddHTLC{amount_msat, .. } => {
+                                                       res += amount_msat;
+                                               }
+                                               _ => {}
+                                       }
+                               }
+                               res
+                       },
+                       their_max_htlc_value_in_flight_msat: self.their_max_htlc_value_in_flight_msat,
+               }
+       }
+
        /// Allowed in any state (including after shutdown)
        pub fn get_channel_update_count(&self) -> u32 {
                self.channel_update_count