Skip to content

Commit 0c110dd

Browse files
channelmanager: add fake_scid module
See module docs for more details
1 parent 32b39c4 commit 0c110dd

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,157 @@ mod inbound_payment {
338338
}
339339
}
340340

341+
/// LDK has multiple reasons to generate fake short channel ids:
342+
/// 1) zero-conf channels that don't have a confirmed channel id yet
343+
/// 2) phantom node payments, to get an scid for the phantom node's phantom channel
344+
mod fake_scid {
345+
use bitcoin::blockdata::constants::genesis_block;
346+
use bitcoin::hash_types::BlockHash;
347+
use bitcoin::network::constants::Network;
348+
use chain::keysinterface::{Sign, KeysInterface};
349+
use ln::channelmanager::DecodeError;
350+
use util::scid_utils;
351+
use util::ser::Readable;
352+
353+
use core::convert::TryInto;
354+
use core::ops::Deref;
355+
356+
const TEST_SEGWIT_ACTIVATION_HEIGHT: u32 = 0;
357+
const MAINNET_SEGWIT_ACTIVATION_HEIGHT: u32 = 481_824;
358+
const MAX_TX_INDEX: u32 = 2_500;
359+
const MAX_VOUT: u16 = 10_000;
360+
const MAX_NAMESPACES: u8 = 8;
361+
const NAMESPACE_ID_BITMASK: u8 = 0b111;
362+
363+
/// Fake scids are divided into namespaces, with each namespace having its own randomly-selected
364+
/// identifier between [0..7], such that `scid_tx_index % MAX_NAMESPACES == namespace_identifier`.
365+
/// This allows us to identify what namespace a fake scid corresponds to upon HTLC receipt, and
366+
/// handle the HTLC accordingly.
367+
pub(super) enum Namespace {
368+
Phantom(u8),
369+
// Coming soon: a variant for the zero-conf scid namespace
370+
}
371+
372+
impl Namespace {
373+
/// We generate "realistic-looking" random scids here, meaning the scid's block height is
374+
/// between segwit activation and the current best known height, and the tx index and output
375+
/// index are also selected from a "reasonable" range. We add this logic because it makes it
376+
/// non-obvious at a glance that the scid is fake, e.g. if it appears in invoice route hints.
377+
pub(super) fn get_fake_scid<Signer: Sign, K: Deref>(&self, highest_seen_blockheight: u32, genesis_hash: &BlockHash, keys_manager: &K) -> u64
378+
where K::Target: KeysInterface<Signer = Signer>,
379+
{
380+
let scid_namespace = self.as_u8();
381+
let rand_bytes = keys_manager.get_secure_random_bytes();
382+
383+
let valid_block_range = highest_seen_blockheight - segwit_activation_height(genesis_hash);
384+
let rand_value_for_height = u32::from_be_bytes(rand_bytes[..4].try_into().unwrap());
385+
let fake_scid_height = segwit_activation_height(genesis_hash) + rand_value_for_height % valid_block_range;
386+
387+
let rand_i32 = i32::from_be_bytes(rand_bytes[4..8].try_into().unwrap()).abs();
388+
let rand_tx_index = rand_i32 % (MAX_TX_INDEX as i32);
389+
let offset = (scid_namespace as i32 - rand_tx_index) % (MAX_NAMESPACES as i32);
390+
let fake_scid_tx_index: u64 = (rand_tx_index + offset) as u64;
391+
392+
let rand_value_for_vout = u16::from_be_bytes(rand_bytes[8..10].try_into().unwrap());
393+
let fake_scid_vout_index = rand_value_for_vout % MAX_VOUT;
394+
scid_utils::scid_from_parts(fake_scid_height as u64, fake_scid_tx_index, fake_scid_vout_index as u64).unwrap()
395+
}
396+
397+
fn as_u8(&self) -> u8 {
398+
match self {
399+
Namespace::Phantom(namespace) => *namespace,
400+
}
401+
}
402+
403+
pub(super) fn phantom<Signer: Sign, K: Deref>(keys_manager: &K) -> Namespace
404+
where K::Target: KeysInterface<Signer = Signer>,
405+
{
406+
let phantom_scid_namespace_rand_bytes = keys_manager.get_secure_random_bytes();
407+
let phantom_scid_namespace = phantom_scid_namespace_rand_bytes[0] & NAMESPACE_ID_BITMASK;
408+
Namespace::Phantom(phantom_scid_namespace)
409+
}
410+
}
411+
412+
fn segwit_activation_height(genesis: &BlockHash) -> u32 {
413+
if genesis_block(Network::Bitcoin).header.block_hash() == *genesis {
414+
MAINNET_SEGWIT_ACTIVATION_HEIGHT
415+
} else {
416+
TEST_SEGWIT_ACTIVATION_HEIGHT
417+
}
418+
}
419+
420+
/// Returns whether the given fake scid falls into the given namespace.
421+
pub(super) fn is_valid(namespace: &Namespace, scid: u64) -> bool {
422+
scid_utils::tx_index_from_scid(&scid) % MAX_NAMESPACES as u32 == namespace.as_u8() as u32
423+
}
424+
425+
impl_writeable_tlv_based_enum!(Namespace, ;
426+
(0, Phantom),
427+
);
428+
429+
#[cfg(test)]
430+
mod tests {
431+
use bitcoin::blockdata::constants::genesis_block;
432+
use bitcoin::network::constants::Network;
433+
use ln::channelmanager::fake_scid::{is_valid, MAINNET_SEGWIT_ACTIVATION_HEIGHT, MAX_TX_INDEX, MAX_VOUT, Namespace, NAMESPACE_ID_BITMASK, segwit_activation_height, TEST_SEGWIT_ACTIVATION_HEIGHT};
434+
use util::scid_utils;
435+
use util::test_utils;
436+
use sync::Arc;
437+
438+
#[test]
439+
fn namespace_identifier_is_within_range() {
440+
let seed = [0 as u8; 32];
441+
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
442+
let Namespace::Phantom(ns) = Namespace::phantom(&keys_manager);
443+
assert!(ns <= NAMESPACE_ID_BITMASK);
444+
}
445+
446+
#[test]
447+
fn test_segwit_activation_height() {
448+
let mainnet_genesis = genesis_block(Network::Bitcoin).header.block_hash();
449+
assert_eq!(segwit_activation_height(&mainnet_genesis), MAINNET_SEGWIT_ACTIVATION_HEIGHT);
450+
451+
let testnet_genesis = genesis_block(Network::Testnet).header.block_hash();
452+
assert_eq!(segwit_activation_height(&testnet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
453+
454+
let signet_genesis = genesis_block(Network::Signet).header.block_hash();
455+
assert_eq!(segwit_activation_height(&signet_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
456+
457+
let regtest_genesis = genesis_block(Network::Regtest).header.block_hash();
458+
assert_eq!(segwit_activation_height(&regtest_genesis), TEST_SEGWIT_ACTIVATION_HEIGHT);
459+
}
460+
461+
#[test]
462+
fn test_is_valid() {
463+
let namespace = Namespace::Phantom(3);
464+
let valid_fake_scid = scid_utils::scid_from_parts(0, 11, 0).unwrap();
465+
assert!(is_valid(&namespace, valid_fake_scid));
466+
let invalid_fake_scid = scid_utils::scid_from_parts(0, 12, 0).unwrap();
467+
assert!(!is_valid(&namespace, invalid_fake_scid));
468+
}
469+
470+
#[test]
471+
fn test_get_fake_scid() {
472+
let mainnet_genesis = genesis_block(Network::Bitcoin).header.block_hash();
473+
let seed = [0 as u8; 32];
474+
let keys_manager = Arc::new(test_utils::TestKeysInterface::new(&seed, Network::Testnet));
475+
let namespace = Namespace::phantom(&keys_manager);
476+
let fake_scid = namespace.get_fake_scid(500_000, &mainnet_genesis, &keys_manager);
477+
478+
let fake_height = scid_utils::block_from_scid(&fake_scid);
479+
assert!(fake_height >= MAINNET_SEGWIT_ACTIVATION_HEIGHT);
480+
assert!(fake_height <= 500_000);
481+
482+
let fake_tx_index = scid_utils::tx_index_from_scid(&fake_scid);
483+
assert!(fake_tx_index <= MAX_TX_INDEX);
484+
485+
let fake_vout = fake_scid & scid_utils::MAX_SCID_VOUT_INDEX;
486+
assert!(fake_vout <= MAX_VOUT.into());
487+
}
488+
}
489+
}
490+
491+
341492
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
342493
//
343494
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
@@ -960,6 +1111,8 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
9601111

9611112
inbound_payment_key: inbound_payment::ExpandedKey,
9621113

1114+
phantom_scid_namespace: fake_scid::Namespace,
1115+
9631116
/// Used to track the last value sent in a node_announcement "timestamp" field. We ensure this
9641117
/// value increases strictly since we don't assume access to a time source.
9651118
last_node_announcement_serial: AtomicUsize,
@@ -1655,6 +1808,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
16551808
secp_ctx.seeded_randomize(&keys_manager.get_secure_random_bytes());
16561809
let inbound_pmt_key_material = keys_manager.get_inbound_payment_key_material();
16571810
let expanded_inbound_key = inbound_payment::ExpandedKey::new(&inbound_pmt_key_material);
1811+
let phantom_scid_namespace = fake_scid::Namespace::phantom(&keys_manager);
16581812
ChannelManager {
16591813
default_configuration: config.clone(),
16601814
genesis_hash: genesis_block(params.network).header.block_hash(),
@@ -1680,6 +1834,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
16801834

16811835
inbound_payment_key: expanded_inbound_key,
16821836

1837+
phantom_scid_namespace,
1838+
16831839
last_node_announcement_serial: AtomicUsize::new(0),
16841840
highest_seen_timestamp: AtomicUsize::new(0),
16851841

@@ -6145,6 +6301,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> Writeable f
61456301
write_tlv_fields!(writer, {
61466302
(1, pending_outbound_payments_no_retry, required),
61476303
(3, pending_outbound_payments, required),
6304+
(5, self.phantom_scid_namespace, required),
61486305
});
61496306

61506307
Ok(())
@@ -6439,10 +6596,16 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
64396596
// pending_outbound_payments_no_retry is for compatibility with 0.0.101 clients.
64406597
let mut pending_outbound_payments_no_retry: Option<HashMap<PaymentId, HashSet<[u8; 32]>>> = None;
64416598
let mut pending_outbound_payments = None;
6599+
let mut phantom_scid_namespace: Option<fake_scid::Namespace> = None;
64426600
read_tlv_fields!(reader, {
64436601
(1, pending_outbound_payments_no_retry, option),
64446602
(3, pending_outbound_payments, option),
6603+
(5, phantom_scid_namespace, option),
64456604
});
6605+
if phantom_scid_namespace.is_none() {
6606+
phantom_scid_namespace = Some(fake_scid::Namespace::phantom(&args.keys_manager));
6607+
}
6608+
64466609
if pending_outbound_payments.is_none() && pending_outbound_payments_no_retry.is_none() {
64476610
pending_outbound_payments = Some(pending_outbound_payments_compat);
64486611
} else if pending_outbound_payments.is_none() {
@@ -6525,6 +6688,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
65256688
inbound_payment_key: expanded_inbound_key,
65266689
pending_inbound_payments: Mutex::new(pending_inbound_payments),
65276690
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
6691+
phantom_scid_namespace: phantom_scid_namespace.unwrap(),
65286692

65296693
our_network_key: args.keys_manager.get_node_secret(),
65306694
our_network_pubkey: PublicKey::from_secret_key(&secp_ctx, &args.keys_manager.get_node_secret()),

0 commit comments

Comments
 (0)