Merge pull request #2704 from jkczyz/2023-11-channelmanager-docs
[rust-lightning] / lightning / src / util / scid_utils.rs
index 3db98553af547039ecc24c8ed2fa236aadbc7d18..c9485b60b70be78fd88b52eb45703c74f4da5af1 100644 (file)
@@ -7,6 +7,8 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
+//! Utilities for creating and parsing short channel ids.
+
 /// 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;
@@ -22,23 +24,26 @@ pub const MAX_SCID_VOUT_INDEX: u64 = 0xffff;
 /// A `short_channel_id` construction error
 #[derive(Debug, PartialEq, Eq)]
 pub enum ShortChannelIdError {
+       /// Block height too high
        BlockOverflow,
+       /// Tx index too high
        TxIndexOverflow,
+       /// Vout index too high
        VoutIndexOverflow,
 }
 
 /// 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;
 }
 
@@ -66,13 +71,13 @@ pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result<u64
 /// 3) payments intended to be intercepted will route using a fake scid (this is typically used so
 ///    the forwarding node can open a JIT channel to the next hop)
 pub(crate) mod fake_scid {
-       use bitcoin::hash_types::BlockHash;
-       use bitcoin::hashes::hex::FromHex;
-       use crate::chain::keysinterface::{KeysInterface, EntropySource};
-       use crate::util::chacha20::ChaCha20;
+       use bitcoin::blockdata::constants::ChainHash;
+       use bitcoin::network::constants::Network;
+       use crate::sign::EntropySource;
+       use crate::crypto::chacha20::ChaCha20;
        use crate::util::scid_utils;
+       use crate::prelude::*;
 
-       use core::convert::TryInto;
        use core::ops::Deref;
 
        const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 1;
@@ -91,8 +96,11 @@ pub(crate) mod fake_scid {
        /// into the fake scid.
        #[derive(Copy, Clone)]
        pub(crate) enum Namespace {
+               /// Phantom nodes namespace
                Phantom,
+               /// SCID aliases for outbound private channels
                OutboundAlias,
+               /// Payment interception namespace
                Intercept
        }
 
@@ -101,15 +109,15 @@ pub(crate) mod fake_scid {
                /// between segwit activation and the current best known height, and the tx index and output
                /// index are also selected from a "reasonable" range. We add this logic because it makes it
                /// non-obvious at a glance that the scid is fake, e.g. if it appears in invoice route hints.
-               pub(crate) fn get_fake_scid<K: Deref>(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, fake_scid_rand_bytes: &[u8; 32], keys_manager: &K) -> u64
-                       where K::Target: KeysInterface,
+               pub(crate) fn get_fake_scid<ES: Deref>(&self, highest_seen_blockheight: u32, chain_hash: &ChainHash, fake_scid_rand_bytes: &[u8; 32], entropy_source: &ES) -> u64
+                       where ES::Target: EntropySource,
                {
                        // Ensure we haven't created a namespace that doesn't fit into the 3 bits we've allocated for
                        // namespaces.
                        assert!((*self as u8) < MAX_NAMESPACES);
-                       let rand_bytes = keys_manager.get_secure_random_bytes();
+                       let rand_bytes = entropy_source.get_secure_random_bytes();
 
-                       let segwit_activation_height = segwit_activation_height(genesis_hash);
+                       let segwit_activation_height = segwit_activation_height(chain_hash);
                        let mut blocks_since_segwit_activation = highest_seen_blockheight.saturating_sub(segwit_activation_height);
 
                        // We want to ensure that this fake channel won't conflict with any transactions we haven't
@@ -144,9 +152,8 @@ pub(crate) mod fake_scid {
                }
        }
 
-       fn segwit_activation_height(genesis: &BlockHash) -> u32 {
-               const MAINNET_GENESIS_STR: &'static str = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f";
-               if BlockHash::from_hex(MAINNET_GENESIS_STR).unwrap() == *genesis {
+       fn segwit_activation_height(chain_hash: &ChainHash) -> u32 {
+               if *chain_hash == ChainHash::using_genesis_block(Network::Bitcoin) {
                        MAINNET_SEGWIT_ACTIVATION_HEIGHT
                } else {
                        TEST_SEGWIT_ACTIVATION_HEIGHT
@@ -154,28 +161,28 @@ 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, genesis_hash: &BlockHash) -> bool {
-               let block_height = scid_utils::block_from_scid(&scid);
-               let tx_index = scid_utils::tx_index_from_scid(&scid);
+       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 namespace = Namespace::Phantom;
                let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
-               block_height >= segwit_activation_height(genesis_hash)
-                       && valid_vout == scid_utils::vout_from_scid(&scid) as u8
+               block_height >= segwit_activation_height(chain_hash)
+                       && 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, genesis_hash: &BlockHash) -> bool {
-               let block_height = scid_utils::block_from_scid(&scid);
-               let tx_index = scid_utils::tx_index_from_scid(&scid);
+       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 namespace = Namespace::Intercept;
                let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
-               block_height >= segwit_activation_height(genesis_hash)
-                       && valid_vout == scid_utils::vout_from_scid(&scid) as u8
+               block_height >= segwit_activation_height(chain_hash)
+                       && valid_vout == scid_utils::vout_from_scid(scid) as u8
        }
 
        #[cfg(test)]
        mod tests {
-               use bitcoin::blockdata::constants::genesis_block;
+               use bitcoin::blockdata::constants::ChainHash;
                use bitcoin::network::constants::Network;
                use crate::util::scid_utils::fake_scid::{is_valid_intercept, is_valid_phantom, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_NAMESPACES, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT};
                use crate::util::scid_utils;
@@ -195,16 +202,16 @@ pub(crate) mod fake_scid {
 
                #[test]
                fn test_segwit_activation_height() {
-                       let mainnet_genesis = genesis_block(Network::Bitcoin).header.block_hash();
+                       let mainnet_genesis = ChainHash::using_genesis_block(Network::Bitcoin);
                        assert_eq!(segwit_activation_height(&mainnet_genesis), MAINNET_SEGWIT_ACTIVATION_HEIGHT);
 
-                       let testnet_genesis = genesis_block(Network::Testnet).header.block_hash();
+                       let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet);
                        assert_eq!(segwit_activation_height(&testnet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
 
-                       let signet_genesis = genesis_block(Network::Signet).header.block_hash();
+                       let signet_genesis = ChainHash::using_genesis_block(Network::Signet);
                        assert_eq!(segwit_activation_height(&signet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
 
-                       let regtest_genesis = genesis_block(Network::Regtest).header.block_hash();
+                       let regtest_genesis = ChainHash::using_genesis_block(Network::Regtest);
                        assert_eq!(segwit_activation_height(&regtest_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
                }
 
@@ -212,7 +219,7 @@ pub(crate) mod fake_scid {
                fn test_is_valid_phantom() {
                        let namespace = Namespace::Phantom;
                        let fake_scid_rand_bytes = [0; 32];
-                       let testnet_genesis = genesis_block(Network::Testnet).header.block_hash();
+                       let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet);
                        let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes);
                        let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap();
                        assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis));
@@ -224,7 +231,7 @@ pub(crate) mod fake_scid {
                fn test_is_valid_intercept() {
                        let namespace = Namespace::Intercept;
                        let fake_scid_rand_bytes = [0; 32];
-                       let testnet_genesis = genesis_block(Network::Testnet).header.block_hash();
+                       let testnet_genesis = ChainHash::using_genesis_block(Network::Testnet);
                        let valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes);
                        let valid_fake_scid = scid_utils::scid_from_parts(1, 0, valid_encrypted_vout as u64).unwrap();
                        assert!(is_valid_intercept(&fake_scid_rand_bytes, valid_fake_scid, &testnet_genesis));
@@ -234,21 +241,21 @@ pub(crate) mod fake_scid {
 
                #[test]
                fn test_get_fake_scid() {
-                       let mainnet_genesis = genesis_block(Network::Bitcoin).header.block_hash();
+                       let mainnet_genesis = ChainHash::using_genesis_block(Network::Bitcoin);
                        let seed = [0; 32];
                        let fake_scid_rand_bytes = [1; 32];
                        let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
                        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);
                }
        }
@@ -260,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]