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