X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fscid_utils.rs;h=7902a5271a6bcab604627fcd454634fd479ec2a9;hb=a146ef2be28d9f4e4daeb8f88797115931824578;hp=2b9e6fed2be8db8b21554ba64e9541d75c076c10;hpb=0b14e97c499df77c2610026d0cc73ab9086e4fe2;p=rust-lightning diff --git a/lightning/src/util/scid_utils.rs b/lightning/src/util/scid_utils.rs index 2b9e6fed..7902a527 100644 --- a/lightning/src/util/scid_utils.rs +++ b/lightning/src/util/scid_utils.rs @@ -7,32 +7,47 @@ // You may not use this file except in accordance with one or both of these // licenses. +/// Maximum block height that can be used in a `short_channel_id`. This +/// value is based on the 3-bytes available for block height. +pub const MAX_SCID_BLOCK: u64 = 0x00ffffff; + +/// Maximum transaction index that can be used in a `short_channel_id`. +/// This value is based on the 3-bytes available for tx index. +pub const MAX_SCID_TX_INDEX: u64 = 0x00ffffff; + +/// Maximum vout index that can be used in a `short_channel_id`. This +/// value is based on the 2-bytes available for the vout index. +pub const MAX_SCID_VOUT_INDEX: u64 = 0xffff; + /// A `short_channel_id` construction error #[derive(Debug, PartialEq)] pub enum ShortChannelIdError { BlockOverflow, TxIndexOverflow, + VoutIndexOverflow, } /// Extracts the block height (most significant 3-bytes) from the `short_channel_id` -#[allow(dead_code)] pub fn block_from_scid(short_channel_id: &u64) -> u32 { return (short_channel_id >> 40) as u32; } /// Constructs a `short_channel_id` using the components pieces. Results in an error -/// if the block height or tx index overflow the 3-bytes for each component. -#[allow(dead_code)] -pub fn scid_from_parts(block: u32, tx_index: u32, vout_index: u16) -> Result { - if block > 0x00ffffff { +/// if the block height, tx index, or vout index overflow the maximum sizes. +pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result { + if block > MAX_SCID_BLOCK { return Err(ShortChannelIdError::BlockOverflow); } - if tx_index > 0x00ffffff { + if tx_index > MAX_SCID_TX_INDEX { return Err(ShortChannelIdError::TxIndexOverflow); } - Ok(((block as u64) << 40) | ((tx_index as u64) << 16) | (vout_index as u64)) + if vout_index > MAX_SCID_VOUT_INDEX { + return Err(ShortChannelIdError::VoutIndexOverflow); + } + + Ok((block << 40) | (tx_index << 16) | vout_index) } #[cfg(test)] @@ -56,5 +71,6 @@ mod tests { assert_eq!(scid_from_parts(0x00ffffff, 0x00ffffff, 0xffff).unwrap(), 0xffffff_ffffff_ffff); assert_eq!(scid_from_parts(0x01ffffff, 0x00000000, 0x0000).err().unwrap(), ShortChannelIdError::BlockOverflow); assert_eq!(scid_from_parts(0x00000000, 0x01ffffff, 0x0000).err().unwrap(), ShortChannelIdError::TxIndexOverflow); + assert_eq!(scid_from_parts(0x00000000, 0x00000000, 0x010000).err().unwrap(), ShortChannelIdError::VoutIndexOverflow); } }