Skip to content

Commit 8ec5429

Browse files
committed
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.
1 parent feca062 commit 8ec5429

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lightning/src/ln/msgs.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,6 +1682,19 @@ impl Writeable for ReplyShortChannelIdsEnd {
16821682
}
16831683
}
16841684

1685+
impl QueryChannelRange {
1686+
/**
1687+
* Calculates the overflow safe ending block height for the query.
1688+
* Overflow returns `0xffffffff`, otherwise returns `first_blocknum + number_of_blocks`
1689+
*/
1690+
pub fn end_blocknum(&self) -> u32 {
1691+
match self.first_blocknum.checked_add(self.number_of_blocks) {
1692+
Some(block) => block,
1693+
None => 0xffffffff,
1694+
}
1695+
}
1696+
}
1697+
16851698
impl Readable for QueryChannelRange {
16861699
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
16871700
let chain_hash: BlockHash = Readable::read(r)?;
@@ -2523,6 +2536,24 @@ mod tests {
25232536
assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
25242537
}
25252538

2539+
#[test]
2540+
fn query_channel_range_end_blocknum() {
2541+
let tests: Vec<(u32, u32, u32)> = vec![
2542+
(10000, 1500, 11500),
2543+
(0, 0xffffffff, 0xffffffff),
2544+
(1, 0xffffffff, 0xffffffff),
2545+
];
2546+
2547+
for (first_blocknum, number_of_blocks, expected) in tests.into_iter() {
2548+
let sut = msgs::QueryChannelRange {
2549+
chain_hash: BlockHash::from_hex("06226e46111a0b59caaf126043eb5bbf28c34f3a5e332a1fc7b2b73cf188910f").unwrap(),
2550+
first_blocknum,
2551+
number_of_blocks,
2552+
};
2553+
assert_eq!(sut.end_blocknum(), expected);
2554+
}
2555+
}
2556+
25262557
#[test]
25272558
fn encoding_query_channel_range() {
25282559
let mut query_channel_range = msgs::QueryChannelRange {

0 commit comments

Comments
 (0)