lightning_signer/util/
mod.rs

1/// Byte to integer conversion
2pub mod byte_utils;
3/// Clock provider
4pub mod clock;
5/// Cryptographic utilities
6pub mod crypto_utils;
7/// Invoice utilities
8pub mod invoice_utils;
9/// Logging macros
10#[macro_use]
11#[allow(unused_macros)]
12pub mod macro_logger;
13#[macro_use]
14/// Debugging
15pub mod debug_utils;
16/// Logging
17pub mod log_utils;
18/// An implementation of the LDK Sign trait for integration with LDK based nodes
19pub mod loopback;
20#[allow(missing_docs)]
21pub mod test_logger;
22#[allow(missing_docs)]
23#[cfg(any(test, feature = "test_utils"))]
24#[macro_use]
25pub mod test_utils;
26#[cfg(test)]
27pub(crate) mod mocks;
28/// serde for foreign types
29#[allow(missing_docs)]
30pub mod ser_util;
31/// Status error results
32pub mod status;
33/// Transaction utilities
34pub mod transaction_utils;
35/// Velocity control
36pub mod velocity;
37
38/// Reexport the hex crate
39pub use hex;
40
41/// The initial commitment number when counting backwards
42pub const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
43
44use crate::prelude::*;
45use core::slice::Iter;
46use itertools::{put_back, PutBack};
47
48/// Iterator over elements in `to` that are not in `from`
49pub struct AddedItemsIter<'a, T: Ord + Eq> {
50    from: PutBack<Iter<'a, T>>,
51    to: PutBack<Iter<'a, T>>,
52}
53
54impl<'a, T: Ord + Eq> AddedItemsIter<'a, T> {
55    /// Both vectors must be sorted
56    pub fn new(from: &'a Vec<T>, to: &'a Vec<T>) -> Self {
57        AddedItemsIter { from: put_back(from.iter()), to: put_back(to.iter()) }
58    }
59}
60
61impl<'a, T: Ord + Eq> Iterator for AddedItemsIter<'a, T> {
62    type Item = &'a T;
63
64    fn next(&mut self) -> Option<Self::Item> {
65        loop {
66            match self.from.next() {
67                // Nothing in `from` - yield remaining elements in `to`
68                None => return self.to.next(),
69                Some(next_from) => {
70                    match self.to.next() {
71                        // Nothing in `to` - done
72                        None => return None,
73                        Some(next_to) => {
74                            if next_from < next_to {
75                                // `from` is behind - consume `from` but not `to`
76                                self.to.put_back(next_to);
77                                continue;
78                            } else if next_from == next_to {
79                                // consume both
80                                continue;
81                            } else {
82                                // `to` is behind - consume `to` but not `from`
83                                self.from.put_back(next_from);
84                                return Some(next_to);
85                            }
86                        }
87                    }
88                }
89            }
90        }
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use crate::util::AddedItemsIter;
97
98    #[test]
99    fn delta_test() {
100        fn check(from: Vec<u8>, to: Vec<u8>, expect: Vec<u8>) {
101            assert_eq!(AddedItemsIter::new(&from, &to).cloned().collect::<Vec<u8>>(), expect);
102        }
103
104        check(vec![], vec![1, 2, 4], vec![1, 2, 4]);
105        check(vec![3], vec![1, 2, 4], vec![1, 2, 4]);
106        check(vec![2, 3], vec![1, 2, 4], vec![1, 4]);
107        check(vec![1, 2, 3], vec![1, 2, 4], vec![4]);
108        check(vec![0, 1, 2, 3], vec![1, 2, 4], vec![4]);
109        check(vec![0, 1, 3], vec![1, 2, 4], vec![2, 4]);
110        check(vec![0, 1, 3], vec![1, 2, 4, 5], vec![2, 4, 5]);
111    }
112}