-/// Safely calculates the last_blocknum given a first_blocknum and
-/// number_of_blocks by returning the u32::MAX-1 if there is an overflow
-fn last_blocknum(first_blocknum: u32, number_of_blocks: u32) -> u32 {
- match first_blocknum.checked_add(number_of_blocks) {
- Some(val) => val - 1,
- None => 0xffff_ffff - 1,
- }
-}
-
-/// Maintains state for a channel range query that we initiated.
-/// The query may result in one or more reply_channel_range messages
-/// being received. This struct helps determine the status of the query
-/// when there are multiple replies. It also collects results for initiating
-/// SCID queries.
-///
-/// The task is complete and can be cleaned up when a reply meets or
-/// exceeds the last block in the query. The collected SCIDs in the task
-/// can be used to generate an ScidQueryTask.
-///
-/// A query may fail if the recipient does not maintain up-to-date
-/// information for the chain or if the recipient fails to reply within
-/// a reasonable amount of time. In either event, the query can be
-/// re-initiated with a different peer.
-pub struct ChanRangeQueryTask {
- /// The public key of the node we will be sending queries to
- pub node_id: PublicKey,
- /// The genesis hash of the blockchain being queried
- pub chain_hash: BlockHash,
- /// The height of the first block for the channel UTXOs being queried
- pub first_blocknum: u32,
- /// The number of blocks to include in the query results
- pub number_of_blocks: u32,
- /// Tracks the number of reply messages we have received
- pub number_of_replies: usize,
- /// The height of the first block received in a reply. This value
- /// should be less than or equal to the first_blocknum requested in
- /// the query_channel_range. This allows the range of the replies to
- /// contain, but not necessarily strictly, the queried range.
- pub received_first_block: Option<u32>,
- /// The height of the last block received in a reply. This value
- /// will get incrementally closer to the target of
- /// first_blocknum plus number_of_blocks from the query_channel_range.
- pub received_last_block: Option<u32>,
- /// Contains short_channel_ids received in one or more reply messages.
- /// These will be sent in one ore more query_short_channel_ids messages
- /// when the task is complete.
- pub short_channel_ids: Vec<u64>,
-}
-
-impl ChanRangeQueryTask {
- /// Constructs a new GossipQueryRangeTask
- pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, first_blocknum: u32, number_of_blocks: u32) -> Self {
- ChanRangeQueryTask {
- node_id: their_node_id.clone(),
- chain_hash,
- first_blocknum,
- number_of_blocks,
- number_of_replies: 0,
- received_first_block: None,
- received_last_block: None,
- short_channel_ids: vec![],
- }
- }
-}
-
-/// Maintains state when sending one or more short_channel_ids messages
-/// to a peer. Only a single SCID query can be in-flight with a peer. The
-/// number of SCIDs per query is limited by the size of a Lightning message
-/// payload. When querying a large number of SCIDs (results of a large
-/// channel range query for instance), multiple query_short_channel_ids
-/// messages need to be sent. This task maintains the list of awaiting
-/// SCIDs to be queried.
-///
-/// When a successful reply_short_channel_ids_end message is received, the
-/// next batch of SCIDs can be sent. When no remaining SCIDs exist in the
-/// task, the task is complete and can be cleaned up.
-///
-/// The recipient may reply indicating that up-to-date information for the
-/// chain is not maintained. A query may also fail to complete within a
-/// reasonable amount of time. In either event, the short_channel_ids
-/// can be queried from a different peer after validating the set of
-/// SCIDs that still need to be queried.
-pub struct ScidQueryTask {
- /// The public key of the node we will be sending queries to
- pub node_id: PublicKey,
- /// The genesis hash of the blockchain being queried
- pub chain_hash: BlockHash,
- /// A vector of short_channel_ids that we would like routing gossip
- /// information for. This list will be chunked and sent to the peer
- /// in one or more query_short_channel_ids messages.
- pub short_channel_ids: Vec<u64>,
-}
-
-impl ScidQueryTask {
- /// Constructs a new GossipQueryShortChannelIdsTask
- pub fn new(their_node_id: &PublicKey, chain_hash: BlockHash, short_channel_ids: Vec<u64>) -> Self {
- ScidQueryTask {
- node_id: their_node_id.clone(),
- chain_hash,
- short_channel_ids,
- }
- }
-
- /// Adds short_channel_ids to the pending list of short_channel_ids
- /// to be sent in the next request. You can add additional values
- /// while a query is in-flight. These new values will be sent once
- /// the active query has completed.
- pub fn add(&mut self, mut short_channel_ids: Vec<u64>) {
- self.short_channel_ids.append(&mut short_channel_ids);
- }
-}
-