Skip to content

Commit b7eb101

Browse files
scid_utils: add utils for retrieving txindex and vout
1 parent 75352c6 commit b7eb101

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lightning/src/util/scid_utils.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ pub fn block_from_scid(short_channel_id: &u64) -> u32 {
3232
return (short_channel_id >> 40) as u32;
3333
}
3434

35+
/// Extracts the tx index (bytes [2..4]) from the `short_channel_id`
36+
pub fn tx_index_from_scid(short_channel_id: &u64) -> u32 {
37+
return ((short_channel_id >> 16) & MAX_SCID_TX_INDEX) as u32;
38+
}
39+
40+
/// Extracts the vout (bytes [0..2]) from the `short_channel_id`
41+
pub fn vout_from_scid(short_channel_id: &u64) -> u16 {
42+
return ((short_channel_id) & MAX_SCID_VOUT_INDEX) as u16;
43+
}
44+
3545
/// Constructs a `short_channel_id` using the components pieces. Results in an error
3646
/// if the block height, tx index, or vout index overflow the maximum sizes.
3747
pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result<u64, ShortChannelIdError> {
@@ -63,6 +73,24 @@ mod tests {
6373
assert_eq!(block_from_scid(&0xffffff_ffffff_ffff), 0xffffff);
6474
}
6575

76+
#[test]
77+
fn test_tx_index_from_scid() {
78+
assert_eq!(tx_index_from_scid(&0x000000_000000_0000), 0);
79+
assert_eq!(tx_index_from_scid(&0x000000_000001_0000), 1);
80+
assert_eq!(tx_index_from_scid(&0xffffff_000001_ffff), 1);
81+
assert_eq!(tx_index_from_scid(&0xffffff_800000_ffff), 0x800000);
82+
assert_eq!(tx_index_from_scid(&0xffffff_ffffff_ffff), 0xffffff);
83+
}
84+
85+
#[test]
86+
fn test_vout_from_scid() {
87+
assert_eq!(vout_from_scid(&0x000000_000000_0000), 0);
88+
assert_eq!(vout_from_scid(&0x000000_000000_0001), 1);
89+
assert_eq!(vout_from_scid(&0xffffff_ffffff_0001), 1);
90+
assert_eq!(vout_from_scid(&0xffffff_ffffff_8000), 0x8000);
91+
assert_eq!(vout_from_scid(&0xffffff_ffffff_ffff), 0xffff);
92+
}
93+
6694
#[test]
6795
fn test_scid_from_parts() {
6896
assert_eq!(scid_from_parts(0x00000000, 0x00000000, 0x0000).unwrap(), 0x000000_000000_0000);

0 commit comments

Comments
 (0)