From: bmancini55 Date: Tue, 2 Mar 2021 16:56:00 +0000 (-0500) Subject: Add end_blocknum method to query_channel_range X-Git-Tag: v0.0.14~48^2~9 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=8ec5429f0f9405634726c4388e34ef4acfbbf7eb;p=rust-lightning Add end_blocknum method to query_channel_range Overflow safe calculation of the ending block number for a query. Can be used when processing the query. --- diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 3042b4d32..9fb3a0695 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -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: &mut R) -> Result { 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 {