Merge pull request #1843 from TheBlueMatt/2022-11-fix-bp-exit-docs
[rust-lightning] / lightning / src / util / scid_utils.rs
index 11b8d0c95e098761ad6cfdd86e9ad717a48fcd27..c3529a6cbd229aa0a35eddd65a0187faf8a10ac6 100644 (file)
@@ -20,7 +20,7 @@ pub const MAX_SCID_TX_INDEX: u64 = 0x00ffffff;
 pub const MAX_SCID_VOUT_INDEX: u64 = 0xffff;
 
 /// A `short_channel_id` construction error
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, Eq)]
 pub enum ShortChannelIdError {
        BlockOverflow,
        TxIndexOverflow,
@@ -61,32 +61,36 @@ pub fn scid_from_parts(block: u64, tx_index: u64, vout_index: u64) -> Result<u64
 }
 
 /// LDK has multiple reasons to generate fake short channel ids:
-/// 1) zero-conf channels that don't have a confirmed channel id yet
+/// 1) outbound SCID aliases we use for private channels
 /// 2) phantom node payments, to get an scid for the phantom node's phantom channel
 pub(crate) mod fake_scid {
        use bitcoin::hash_types::BlockHash;
        use bitcoin::hashes::hex::FromHex;
-       use chain::keysinterface::{Sign, KeysInterface};
-       use util::chacha20::ChaCha20;
-       use util::scid_utils;
+       use crate::chain::keysinterface::{Sign, KeysInterface};
+       use crate::util::chacha20::ChaCha20;
+       use crate::util::scid_utils;
 
        use core::convert::TryInto;
        use core::ops::Deref;
 
-       const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 0;
+       const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 1;
        const MAINNET_SEGWIT_ACTIVATION_HEIGHT: u32 = 481_824;
        const MAX_TX_INDEX: u32 = 2_500;
        const MAX_NAMESPACES: u8 = 8; // We allocate 3 bits for the namespace identifier.
        const NAMESPACE_ID_BITMASK: u8 = 0b111;
 
+       const BLOCKS_PER_MONTH: u32 = 144 /* blocks per day */ * 30 /* days per month */;
+       pub(crate) const MAX_SCID_BLOCKS_FROM_NOW: u32 = BLOCKS_PER_MONTH;
+
+
        /// Fake scids are divided into namespaces, with each namespace having its own identifier between
        /// [0..7]. This allows us to identify what namespace a fake scid corresponds to upon HTLC
        /// receipt, and handle the HTLC accordingly. The namespace identifier is encrypted when encoded
        /// into the fake scid.
        #[derive(Copy, Clone)]
-       pub(super) enum Namespace {
+       pub(crate) enum Namespace {
                Phantom,
-               // Coming soon: a variant for the zero-conf scid namespace
+               OutboundAlias,
        }
 
        impl Namespace {
@@ -94,13 +98,12 @@ 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(super) fn get_fake_scid<Signer: Sign, K: Deref>(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, fake_scid_rand_bytes: &[u8; 32], keys_manager: &K) -> u64
+               pub(crate) fn get_fake_scid<Signer: Sign, K: Deref>(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, fake_scid_rand_bytes: &[u8; 32], keys_manager: &K) -> u64
                        where K::Target: KeysInterface<Signer = Signer>,
                {
                        // 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);
-                       const BLOCKS_PER_MONTH: u32 = 144 /* blocks per day */ * 30 /* days per month */;
                        let rand_bytes = keys_manager.get_secure_random_bytes();
 
                        let segwit_activation_height = segwit_activation_height(genesis_hash);
@@ -109,7 +112,7 @@ pub(crate) mod fake_scid {
                        // We want to ensure that this fake channel won't conflict with any transactions we haven't
                        // seen yet, in case `highest_seen_blockheight` is updated before we get full information
                        // about transactions confirmed in the given block.
-                       blocks_since_segwit_activation = blocks_since_segwit_activation.saturating_sub(BLOCKS_PER_MONTH);
+                       blocks_since_segwit_activation = blocks_since_segwit_activation.saturating_sub(MAX_SCID_BLOCKS_FROM_NOW);
 
                        let rand_for_height = u32::from_be_bytes(rand_bytes[..4].try_into().unwrap());
                        let fake_scid_height = segwit_activation_height + rand_for_height % (blocks_since_segwit_activation + 1);
@@ -138,13 +141,6 @@ pub(crate) mod fake_scid {
                }
        }
 
-       pub fn get_phantom_scid<Signer: Sign, K: Deref>(fake_scid_rand_bytes: &[u8; 32], highest_seen_blockheight: u32, genesis_hash: &BlockHash, keys_manager: &K) -> u64
-               where K::Target: KeysInterface<Signer = Signer>,
-       {
-               let namespace = Namespace::Phantom;
-               namespace.get_fake_scid(highest_seen_blockheight, genesis_hash, fake_scid_rand_bytes, keys_manager)
-       }
-
        fn segwit_activation_height(genesis: &BlockHash) -> u32 {
                const MAINNET_GENESIS_STR: &'static str = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f";
                if BlockHash::from_hex(MAINNET_GENESIS_STR).unwrap() == *genesis {
@@ -155,22 +151,23 @@ pub(crate) mod fake_scid {
        }
 
        /// Returns whether the given fake scid falls into the given namespace.
-       pub fn is_valid_phantom(fake_scid_rand_bytes: &[u8; 32], scid: u64) -> bool {
+       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);
                let namespace = Namespace::Phantom;
                let valid_vout = namespace.get_encrypted_vout(block_height, tx_index, fake_scid_rand_bytes);
-               valid_vout == scid_utils::vout_from_scid(&scid) as u8
+               block_height >= segwit_activation_height(genesis_hash)
+                       && valid_vout == scid_utils::vout_from_scid(&scid) as u8
        }
 
        #[cfg(test)]
        mod tests {
                use bitcoin::blockdata::constants::genesis_block;
                use bitcoin::network::constants::Network;
-               use util::scid_utils::fake_scid::{is_valid_phantom, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_NAMESPACES, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT};
-               use util::scid_utils;
-               use util::test_utils;
-               use sync::Arc;
+               use crate::util::scid_utils::fake_scid::{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;
+               use crate::util::test_utils;
+               use crate::sync::Arc;
 
                #[test]
                fn namespace_identifier_is_within_range() {
@@ -198,11 +195,12 @@ 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 valid_encrypted_vout = namespace.get_encrypted_vout(0, 0, &fake_scid_rand_bytes);
-                       let valid_fake_scid = scid_utils::scid_from_parts(0, 0, valid_encrypted_vout as u64).unwrap();
-                       assert!(is_valid_phantom(&fake_scid_rand_bytes, valid_fake_scid));
-                       let invalid_fake_scid = scid_utils::scid_from_parts(0, 0, 12).unwrap();
-                       assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid));
+                       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));
+                       let invalid_fake_scid = scid_utils::scid_from_parts(1, 0, 12).unwrap();
+                       assert!(!is_valid_phantom(&fake_scid_rand_bytes, invalid_fake_scid, &testnet_genesis));
                }
 
                #[test]