]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add end_blocknum method to query_channel_range
authorbmancini55 <bmancini@gmail.com>
Tue, 2 Mar 2021 16:56:00 +0000 (11:56 -0500)
committerbmancini55 <bmancini@gmail.com>
Fri, 5 Mar 2021 20:09:51 +0000 (15:09 -0500)
Overflow safe calculation of the ending block number for a query. Can
be used when processing the query.

lightning/src/ln/msgs.rs

index 3042b4d32e0a344e185ef9756654aefa759606aa..9fb3a069570424cad1c5fcf6ed2db4cba19ef1b4 100644 (file)
@@ -1682,6 +1682,19 @@ impl Writeable for ReplyShortChannelIdsEnd {
        }
 }
 
+impl QueryChannelRange {
+       /**
+        * Calculates the overflow safe ending block height for the query.
+        * Overflow returns `0xffffffff`, otherwise returns `first_blocknum + number_of_blocks`
+        */
+       pub fn end_blocknum(&self) -> u32 {
+               match self.first_blocknum.checked_add(self.number_of_blocks) {
+                       Some(block) => block,
+                       None => 0xffffffff,
+               }
+       }
+}
+
 impl Readable for QueryChannelRange {
        fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
                let chain_hash: BlockHash = Readable::read(r)?;
@@ -2523,6 +2536,24 @@ mod tests {
                assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
        }
 
+       #[test]
+       fn query_channel_range_end_blocknum() {
+               let tests: Vec<(u32, u32, u32)> = vec![
+                       (10000, 1500, 11500),
+                       (0, 0xffffffff, 0xffffffff),
+                       (1, 0xffffffff, 0xffffffff),
+               ];
+
+               for (first_blocknum, number_of_blocks, expected) in tests.into_iter() {
+                       let sut = msgs::QueryChannelRange {
+                               chain_hash: BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap(),
+                               first_blocknum,
+                               number_of_blocks,
+                       };
+                       assert_eq!(sut.end_blocknum(), expected);
+               }
+       }
+
        #[test]
        fn encoding_query_channel_range() {
                let mut query_channel_range = msgs::QueryChannelRange {