Skip to content

Handle query_channel_range gossip queries #828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 17, 2021
12 changes: 5 additions & 7 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
use util::logger::Logger;
use util::errors::APIError;
use util::config::{UserConfig,ChannelConfig};
use util::scid_utils::scid_from_parts;

use std;
use std::default::Default;
Expand Down Expand Up @@ -3556,14 +3557,11 @@ impl<Signer: Sign> Channel<Signer> {
}
}
}
if height > 0xff_ff_ff || (index_in_block) > 0xff_ff_ff {
panic!("Block was bogus - either height 16 million or had > 16 million transactions");
}
assert!(txo_idx <= 0xffff); // txo_idx is a (u16 as usize), so this is just listed here for completeness
self.funding_tx_confirmations = 1;
self.short_channel_id = Some(((height as u64) << (5*8)) |
((index_in_block as u64) << (2*8)) |
((txo_idx as u64) << (0*8)));
self.short_channel_id = match scid_from_parts(height as u64, index_in_block as u64, txo_idx as u64) {
Ok(scid) => Some(scid),
Err(_) => panic!("Block was bogus - either height was > 16 million, had > 16 million transactions, or had > 65k outputs"),
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3499,6 +3499,7 @@ impl<Signer: Sign, M: Deref + Sync + Send, T: Deref + Sync + Send, K: Deref + Sy
&events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => true,
&events::MessageSendEvent::SendChannelRangeQuery { .. } => false,
&events::MessageSendEvent::SendShortIdsQuery { .. } => false,
&events::MessageSendEvent::SendReplyChannelRange { .. } => false,
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions lightning/src/ln/msgs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => u32::max_value(),
}
}
}

impl Readable for QueryChannelRange {
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
let chain_hash: BlockHash = Readable::read(r)?;
Expand Down Expand Up @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions lightning/src/ln/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,17 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, L: Deref> PeerManager<D
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
}
MessageSendEvent::SendReplyChannelRange { ref node_id, ref msg } => {
log_trace!(self.logger, "Handling SendReplyChannelRange event in peer_handler for node {} with num_scids={} first_blocknum={} number_of_blocks={}, sync_complete={}",
log_pubkey!(node_id),
msg.short_channel_ids.len(),
msg.first_blocknum,
msg.number_of_blocks,
msg.sync_complete);
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {});
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
}
}
}

Expand Down
Loading