Skip to content

Drop unnecessary int reference in SCID conversion utilities #2908

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 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lightning/src/ln/monitor_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ fn do_chanmon_claim_value_coop_close(anchors: bool) {
assert_eq!(shutdown_tx, nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0));
assert_eq!(shutdown_tx.len(), 1);

let shutdown_tx_conf_height_a = block_from_scid(&mine_transaction(&nodes[0], &shutdown_tx[0]));
let shutdown_tx_conf_height_b = block_from_scid(&mine_transaction(&nodes[1], &shutdown_tx[0]));
let shutdown_tx_conf_height_a = block_from_scid(mine_transaction(&nodes[0], &shutdown_tx[0]));
let shutdown_tx_conf_height_b = block_from_scid(mine_transaction(&nodes[1], &shutdown_tx[0]));

assert!(nodes[0].node.list_channels().is_empty());
assert!(nodes[1].node.list_channels().is_empty());
Expand Down Expand Up @@ -736,7 +736,7 @@ fn do_test_balances_on_local_commitment_htlcs(anchors: bool) {
check_spends!(commitment_tx, funding_tx);
commitment_tx
};
let commitment_tx_conf_height_a = block_from_scid(&mine_transaction(&nodes[0], &commitment_tx));
let commitment_tx_conf_height_a = block_from_scid(mine_transaction(&nodes[0], &commitment_tx));
if nodes[0].connect_style.borrow().updates_best_block_first() {
let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
assert_eq!(txn.len(), 1);
Expand Down Expand Up @@ -2674,14 +2674,14 @@ fn do_test_anchors_monitor_fixes_counterparty_payment_script_on_reload(confirm_c
// We should expect our round trip serialization check to fail as we're writing the monitor
// with the incorrect P2WPKH script but reading it with the correct P2WSH script.
*nodes[1].chain_monitor.expect_monitor_round_trip_fail.lock().unwrap() = Some(chan_id);
let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx));
let commitment_tx_conf_height = block_from_scid(mine_transaction(&nodes[1], &commitment_tx));
let serialized_monitor = get_monitor!(nodes[1], chan_id).encode();
reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized);
commitment_tx_conf_height
} else {
let serialized_monitor = get_monitor!(nodes[1], chan_id).encode();
reload_node!(nodes[1], user_config, &nodes[1].node.encode(), &[&serialized_monitor], persister, chain_monitor, node_deserialized);
let commitment_tx_conf_height = block_from_scid(&mine_transaction(&nodes[1], &commitment_tx));
let commitment_tx_conf_height = block_from_scid(mine_transaction(&nodes[1], &commitment_tx));
check_added_monitors(&nodes[1], 1);
check_closed_broadcast(&nodes[1], 1, true);
commitment_tx_conf_height
Expand Down
2 changes: 1 addition & 1 deletion lightning/src/routing/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ where U::Target: UtxoLookup, L::Target: Logger
// Prior replies should use the number of blocks that fit into the reply. Overflow
// safe since first_blocknum is always <= last SCID's block.
else {
(false, block_from_scid(batch.last().unwrap()) - first_blocknum)
(false, block_from_scid(*batch.last().unwrap()) - first_blocknum)
};

prev_batch_endblock = first_blocknum + number_of_blocks;
Expand Down
54 changes: 27 additions & 27 deletions lightning/src/util/scid_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ pub enum ShortChannelIdError {
}

/// Extracts the block height (most significant 3-bytes) from the `short_channel_id`
pub fn block_from_scid(short_channel_id: &u64) -> u32 {
pub fn block_from_scid(short_channel_id: u64) -> u32 {
return (short_channel_id >> 40) as u32;
}

/// Extracts the tx index (bytes [2..4]) from the `short_channel_id`
pub fn tx_index_from_scid(short_channel_id: &u64) -> u32 {
pub fn tx_index_from_scid(short_channel_id: u64) -> u32 {
return ((short_channel_id >> 16) & MAX_SCID_TX_INDEX) as u32;
}

/// Extracts the vout (bytes [0..2]) from the `short_channel_id`
pub fn vout_from_scid(short_channel_id: &u64) -> u16 {
pub fn vout_from_scid(short_channel_id: u64) -> u16 {
return ((short_channel_id) & MAX_SCID_VOUT_INDEX) as u16;
}

Expand Down Expand Up @@ -162,22 +162,22 @@ pub(crate) mod fake_scid {

/// Returns whether the given fake scid falls into the phantom namespace.
pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64, chain_hash: &ChainHash) -> bool {
let block_height = scid_utils::block_from_scid(&scid);
let tx_index = scid_utils::tx_index_from_scid(&scid);
let block_height = scid_utils::block_from_scid(scid);
let tx_index = scid_utils::tx_index_from_scid(scid);
let namespace = Namespace::Phantom;
let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
block_height >= segwit_activation_height(chain_hash)
&& valid_vout == scid_utils::vout_from_scid(&scid) as u8
&& valid_vout == scid_utils::vout_from_scid(scid) as u8
}

/// Returns whether the given fake scid falls into the intercept namespace.
pub fn is_valid_intercept(fake_scid_rand_bytes: &[u8; 32], scid: u64, chain_hash: &ChainHash) -> bool {
let block_height = scid_utils::block_from_scid(&scid);
let tx_index = scid_utils::tx_index_from_scid(&scid);
let block_height = scid_utils::block_from_scid(scid);
let tx_index = scid_utils::tx_index_from_scid(scid);
let namespace = Namespace::Intercept;
let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
block_height >= segwit_activation_height(chain_hash)
&& valid_vout == scid_utils::vout_from_scid(&scid) as u8
&& valid_vout == scid_utils::vout_from_scid(scid) as u8
}

#[cfg(test)]
Expand Down Expand Up @@ -248,14 +248,14 @@ pub(crate) mod fake_scid {
let namespace = Namespace::Phantom;
let fake_scid = namespace.get_fake_scid(500_000, &mainnet_genesis, &fake_scid_rand_bytes, &keys_manager);

let fake_height = scid_utils::block_from_scid(&fake_scid);
let fake_height = scid_utils::block_from_scid(fake_scid);
assert!(fake_height >= MAINNET_SEGWIT_ACTIVATION_HEIGHT);
assert!(fake_height <= 500_000);

let fake_tx_index = scid_utils::tx_index_from_scid(&fake_scid);
let fake_tx_index = scid_utils::tx_index_from_scid(fake_scid);
assert!(fake_tx_index <= MAX_TX_INDEX);

let fake_vout = scid_utils::vout_from_scid(&fake_scid);
let fake_vout = scid_utils::vout_from_scid(fake_scid);
assert!(fake_vout < MAX_NAMESPACES as u16);
}
}
Expand All @@ -267,29 +267,29 @@ mod tests {

#[test]
fn test_block_from_scid() {
assert_eq!(block_from_scid(&0x000000_000000_0000), 0);
assert_eq!(block_from_scid(&0x000001_000000_0000), 1);
assert_eq!(block_from_scid(&0x000001_ffffff_ffff), 1);
assert_eq!(block_from_scid(&0x800000_ffffff_ffff), 0x800000);
assert_eq!(block_from_scid(&0xffffff_ffffff_ffff), 0xffffff);
assert_eq!(block_from_scid(0x000000_000000_0000), 0);
assert_eq!(block_from_scid(0x000001_000000_0000), 1);
assert_eq!(block_from_scid(0x000001_ffffff_ffff), 1);
assert_eq!(block_from_scid(0x800000_ffffff_ffff), 0x800000);
assert_eq!(block_from_scid(0xffffff_ffffff_ffff), 0xffffff);
}

#[test]
fn test_tx_index_from_scid() {
assert_eq!(tx_index_from_scid(&0x000000_000000_0000), 0);
assert_eq!(tx_index_from_scid(&0x000000_000001_0000), 1);
assert_eq!(tx_index_from_scid(&0xffffff_000001_ffff), 1);
assert_eq!(tx_index_from_scid(&0xffffff_800000_ffff), 0x800000);
assert_eq!(tx_index_from_scid(&0xffffff_ffffff_ffff), 0xffffff);
assert_eq!(tx_index_from_scid(0x000000_000000_0000), 0);
assert_eq!(tx_index_from_scid(0x000000_000001_0000), 1);
assert_eq!(tx_index_from_scid(0xffffff_000001_ffff), 1);
assert_eq!(tx_index_from_scid(0xffffff_800000_ffff), 0x800000);
assert_eq!(tx_index_from_scid(0xffffff_ffffff_ffff), 0xffffff);
}

#[test]
fn test_vout_from_scid() {
assert_eq!(vout_from_scid(&0x000000_000000_0000), 0);
assert_eq!(vout_from_scid(&0x000000_000000_0001), 1);
assert_eq!(vout_from_scid(&0xffffff_ffffff_0001), 1);
assert_eq!(vout_from_scid(&0xffffff_ffffff_8000), 0x8000);
assert_eq!(vout_from_scid(&0xffffff_ffffff_ffff), 0xffff);
assert_eq!(vout_from_scid(0x000000_000000_0000), 0);
assert_eq!(vout_from_scid(0x000000_000000_0001), 1);
assert_eq!(vout_from_scid(0xffffff_ffffff_0001), 1);
assert_eq!(vout_from_scid(0xffffff_ffffff_8000), 0x8000);
assert_eq!(vout_from_scid(0xffffff_ffffff_ffff), 0xffff);
}

#[test]
Expand Down