X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=e51bbb9271983986650ac31fef823473ee498f0a;hb=6aca7e1c4db17f43b79504fd44b942b4bc08db9d;hp=1dae61ab3ef5b1841241b069612f71c7e5cc8f38;hpb=ccf92157620da45032d75f06b5972eaf142c1ce3;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 1dae61ab..e51bbb92 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -16,18 +16,19 @@ use crate::chain::chainmonitor::MonitorUpdateId; use crate::chain::channelmonitor; use crate::chain::channelmonitor::MonitorEvent; use crate::chain::transaction::OutPoint; -use crate::chain::keysinterface; +use crate::sign; +use crate::events; use crate::ln::channelmanager; use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; use crate::ln::{msgs, wire}; use crate::ln::msgs::LightningError; use crate::ln::script::ShutdownScript; -use crate::routing::gossip::NetworkGraph; -use crate::routing::router::{find_route, InFlightHtlcs, Route, RouteHop, RouteParameters, Router, ScorerAccountingForInFlightHtlcs}; -use crate::routing::scoring::FixedPenaltyScorer; +use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId}; +use crate::routing::utxo::{UtxoLookup, UtxoLookupError, UtxoResult}; +use crate::routing::router::{find_route, InFlightHtlcs, Path, Route, RouteParameters, Router, ScorerAccountingForInFlightHtlcs}; +use crate::routing::scoring::{ChannelUsage, Score}; use crate::util::config::UserConfig; use crate::util::enforcing_trait_impls::{EnforcingSigner, EnforcementState}; -use crate::util::events; use crate::util::logger::{Logger, Level, Record}; use crate::util::ser::{Readable, ReadableArgs, Writer, Writeable}; @@ -47,17 +48,27 @@ use regex; use crate::io; use crate::prelude::*; +use core::cell::RefCell; use core::time::Duration; use crate::sync::{Mutex, Arc}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::mem; use bitcoin::bech32::u5; -use crate::chain::keysinterface::{InMemorySigner, Recipient, EntropySource, NodeSigner, SignerProvider}; +use crate::sign::{InMemorySigner, Recipient, EntropySource, NodeSigner, SignerProvider}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; use bitcoin::Sequence; +pub fn pubkey(byte: u8) -> PublicKey { + let secp_ctx = Secp256k1::new(); + PublicKey::from_secret_key(&secp_ctx, &privkey(byte)) +} + +pub fn privkey(byte: u8) -> SecretKey { + SecretKey::from_slice(&[byte; 32]).unwrap() +} + pub struct TestVecWriter(pub Vec); impl Writer for TestVecWriter { fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> { @@ -77,17 +88,18 @@ impl chaininterface::FeeEstimator for TestFeeEstimator { pub struct TestRouter<'a> { pub network_graph: Arc>, - pub next_routes: Mutex>>, + pub next_routes: Mutex)>>, + pub scorer: &'a Mutex, } impl<'a> TestRouter<'a> { - pub fn new(network_graph: Arc>) -> Self { - Self { network_graph, next_routes: Mutex::new(VecDeque::new()), } + pub fn new(network_graph: Arc>, scorer: &'a Mutex) -> Self { + Self { network_graph, next_routes: Mutex::new(VecDeque::new()), scorer } } - pub fn expect_find_route(&self, result: Result) { + pub fn expect_find_route(&self, query: RouteParameters, result: Result) { let mut expected_routes = self.next_routes.lock().unwrap(); - expected_routes.push_back(result); + expected_routes.push_back((query, result)); } } @@ -96,27 +108,50 @@ impl<'a> Router for TestRouter<'a> { &self, payer: &PublicKey, params: &RouteParameters, first_hops: Option<&[&channelmanager::ChannelDetails]>, inflight_htlcs: &InFlightHtlcs ) -> Result { - if let Some(find_route_res) = self.next_routes.lock().unwrap().pop_front() { - return find_route_res + if let Some((find_route_query, find_route_res)) = self.next_routes.lock().unwrap().pop_front() { + assert_eq!(find_route_query, *params); + if let Ok(ref route) = find_route_res { + let locked_scorer = self.scorer.lock().unwrap(); + let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs); + for path in &route.paths { + let mut aggregate_msat = 0u64; + for (idx, hop) in path.hops.iter().rev().enumerate() { + aggregate_msat += hop.fee_msat; + let usage = ChannelUsage { + amount_msat: aggregate_msat, + inflight_htlc_msat: 0, + effective_capacity: EffectiveCapacity::Unknown, + }; + + // Since the path is reversed, the last element in our iteration is the first + // hop. + if idx == path.hops.len() - 1 { + scorer.channel_penalty_msat(hop.short_channel_id, &NodeId::from_pubkey(payer), &NodeId::from_pubkey(&hop.pubkey), usage, &()); + } else { + let curr_hop_path_idx = path.hops.len() - 1 - idx; + scorer.channel_penalty_msat(hop.short_channel_id, &NodeId::from_pubkey(&path.hops[curr_hop_path_idx - 1].pubkey), &NodeId::from_pubkey(&hop.pubkey), usage, &()); + } + } + } + } + return find_route_res; } let logger = TestLogger::new(); + let scorer = self.scorer.lock().unwrap(); find_route( payer, params, &self.network_graph, first_hops, &logger, - &ScorerAccountingForInFlightHtlcs::new(TestScorer::with_penalty(0), &inflight_htlcs), + &ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs), &(), &[42; 32] ) } - fn notify_payment_path_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} - fn notify_payment_path_successful(&self, _path: &[&RouteHop]) {} - fn notify_payment_probe_successful(&self, _path: &[&RouteHop]) {} - fn notify_payment_probe_failed(&self, _path: &[&RouteHop], _short_channel_id: u64) {} } -#[cfg(feature = "std")] // If we put this on the `if`, we get "attributes are not yet allowed on `if` expressions" on 1.41.1 impl<'a> Drop for TestRouter<'a> { fn drop(&mut self) { - if std::thread::panicking() { - return; + #[cfg(feature = "std")] { + if std::thread::panicking() { + return; + } } assert!(self.next_routes.lock().unwrap().is_empty()); } @@ -135,7 +170,7 @@ impl SignerProvider for OnlyReadsKeysInterface { fn derive_channel_signer(&self, _channel_value_satoshis: u64, _channel_keys_id: [u8; 32]) -> Self::Signer { unreachable!(); } fn read_chan_signer(&self, mut reader: &[u8]) -> Result { - let inner: InMemorySigner = Readable::read(&mut reader)?; + let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?; let state = Arc::new(Mutex::new(EnforcementState::new())); Ok(EnforcingSigner::new_with_revoked( @@ -145,8 +180,8 @@ impl SignerProvider for OnlyReadsKeysInterface { )) } - fn get_destination_script(&self) -> Script { unreachable!(); } - fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { unreachable!(); } + fn get_destination_script(&self) -> Result { Err(()) } + fn get_shutdown_scriptpubkey(&self) -> Result { Err(()) } } pub struct TestChainMonitor<'a> { @@ -254,7 +289,7 @@ impl TestPersister { self.update_rets.lock().unwrap().push_back(next_ret); } } -impl chainmonitor::Persist for TestPersister { +impl chainmonitor::Persist for TestPersister { fn persist_new_channel(&self, _funding_txo: OutPoint, _data: &channelmonitor::ChannelMonitor, _id: MonitorUpdateId) -> chain::ChannelMonitorUpdateStatus { if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() { return update_ret @@ -282,8 +317,26 @@ pub struct TestBroadcaster { } impl TestBroadcaster { - pub fn new(blocks: Arc>>) -> TestBroadcaster { - TestBroadcaster { txn_broadcasted: Mutex::new(Vec::new()), blocks } + pub fn new(network: Network) -> Self { + Self { + txn_broadcasted: Mutex::new(Vec::new()), + blocks: Arc::new(Mutex::new(vec![(genesis_block(network), 0)])), + } + } + + pub fn with_blocks(blocks: Arc>>) -> Self { + Self { txn_broadcasted: Mutex::new(Vec::new()), blocks } + } + + pub fn txn_broadcast(&self) -> Vec { + self.txn_broadcasted.lock().unwrap().split_off(0) + } + + pub fn unique_txn_broadcast(&self) -> Vec { + let mut txn = self.txn_broadcasted.lock().unwrap().split_off(0); + let mut seen = HashSet::new(); + txn.retain(|tx| seen.insert(tx.txid())); + txn } } @@ -291,7 +344,7 @@ impl chaininterface::BroadcasterInterface for TestBroadcaster { fn broadcast_transaction(&self, tx: &Transaction) { let lock_time = tx.lock_time.0; assert!(lock_time < 1_500_000_000); - if lock_time > self.blocks.lock().unwrap().len() as u32 + 1 && lock_time < 500_000_000 { + if bitcoin::LockTime::from(tx.lock_time).is_block_height() && lock_time > self.blocks.lock().unwrap().last().unwrap().1 { for inp in tx.input.iter() { if inp.sequence != Sequence::MAX { panic!("We should never broadcast a transaction before its locktime ({})!", tx.lock_time); @@ -305,6 +358,7 @@ impl chaininterface::BroadcasterInterface for TestBroadcaster { pub struct TestChannelMessageHandler { pub pending_events: Mutex>, expected_recv_msgs: Mutex>>>, + connected_peers: Mutex>, } impl TestChannelMessageHandler { @@ -312,6 +366,7 @@ impl TestChannelMessageHandler { TestChannelMessageHandler { pending_events: Mutex::new(Vec::new()), expected_recv_msgs: Mutex::new(None), + connected_peers: Mutex::new(HashSet::new()), } } @@ -396,8 +451,11 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler { fn handle_channel_reestablish(&self, _their_node_id: &PublicKey, msg: &msgs::ChannelReestablish) { self.received_msg(wire::Message::ChannelReestablish(msg.clone())); } - fn peer_disconnected(&self, _their_node_id: &PublicKey, _no_connection_possible: bool) {} - fn peer_connected(&self, _their_node_id: &PublicKey, _msg: &msgs::Init) -> Result<(), ()> { + fn peer_disconnected(&self, their_node_id: &PublicKey) { + assert!(self.connected_peers.lock().unwrap().remove(their_node_id)); + } + fn peer_connected(&self, their_node_id: &PublicKey, _msg: &msgs::Init, _inbound: bool) -> Result<(), ()> { + assert!(self.connected_peers.lock().unwrap().insert(their_node_id.clone())); // Don't bother with `received_msg` for Init as its auto-generated and we don't want to // bother re-generating the expected Init message in all tests. Ok(()) @@ -411,6 +469,50 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler { fn provided_init_features(&self, _their_init_features: &PublicKey) -> InitFeatures { channelmanager::provided_init_features(&UserConfig::default()) } + + fn handle_open_channel_v2(&self, _their_node_id: &PublicKey, msg: &msgs::OpenChannelV2) { + self.received_msg(wire::Message::OpenChannelV2(msg.clone())); + } + + fn handle_accept_channel_v2(&self, _their_node_id: &PublicKey, msg: &msgs::AcceptChannelV2) { + self.received_msg(wire::Message::AcceptChannelV2(msg.clone())); + } + + fn handle_tx_add_input(&self, _their_node_id: &PublicKey, msg: &msgs::TxAddInput) { + self.received_msg(wire::Message::TxAddInput(msg.clone())); + } + + fn handle_tx_add_output(&self, _their_node_id: &PublicKey, msg: &msgs::TxAddOutput) { + self.received_msg(wire::Message::TxAddOutput(msg.clone())); + } + + fn handle_tx_remove_input(&self, _their_node_id: &PublicKey, msg: &msgs::TxRemoveInput) { + self.received_msg(wire::Message::TxRemoveInput(msg.clone())); + } + + fn handle_tx_remove_output(&self, _their_node_id: &PublicKey, msg: &msgs::TxRemoveOutput) { + self.received_msg(wire::Message::TxRemoveOutput(msg.clone())); + } + + fn handle_tx_complete(&self, _their_node_id: &PublicKey, msg: &msgs::TxComplete) { + self.received_msg(wire::Message::TxComplete(msg.clone())); + } + + fn handle_tx_signatures(&self, _their_node_id: &PublicKey, msg: &msgs::TxSignatures) { + self.received_msg(wire::Message::TxSignatures(msg.clone())); + } + + fn handle_tx_init_rbf(&self, _their_node_id: &PublicKey, msg: &msgs::TxInitRbf) { + self.received_msg(wire::Message::TxInitRbf(msg.clone())); + } + + fn handle_tx_ack_rbf(&self, _their_node_id: &PublicKey, msg: &msgs::TxAckRbf) { + self.received_msg(wire::Message::TxAckRbf(msg.clone())); + } + + fn handle_tx_abort(&self, _their_node_id: &PublicKey, msg: &msgs::TxAbort) { + self.received_msg(wire::Message::TxAbort(msg.clone())); + } } impl events::MessageSendEventsProvider for TestChannelMessageHandler { @@ -434,10 +536,10 @@ fn get_dummy_channel_announcement(short_chan_id: u64) -> msgs::ChannelAnnounceme features: ChannelFeatures::empty(), chain_hash: genesis_block(network).header.block_hash(), short_channel_id: short_chan_id, - node_id_1: PublicKey::from_secret_key(&secp_ctx, &node_1_privkey), - node_id_2: PublicKey::from_secret_key(&secp_ctx, &node_2_privkey), - bitcoin_key_1: PublicKey::from_secret_key(&secp_ctx, &node_1_btckey), - bitcoin_key_2: PublicKey::from_secret_key(&secp_ctx, &node_2_btckey), + node_id_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_privkey)), + node_id_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_privkey)), + bitcoin_key_1: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_1_btckey)), + bitcoin_key_2: NodeId::from_pubkey(&PublicKey::from_secret_key(&secp_ctx, &node_2_btckey)), excess_data: Vec::new(), }; @@ -509,11 +611,11 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { Some((chan_ann, Some(chan_upd_1), Some(chan_upd_2))) } - fn get_next_node_announcement(&self, _starting_point: Option<&PublicKey>) -> Option { + fn get_next_node_announcement(&self, _starting_point: Option<&NodeId>) -> Option { None } - fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &msgs::Init) -> Result<(), ()> { + fn peer_connected(&self, their_node_id: &PublicKey, init_msg: &msgs::Init, _inbound: bool) -> Result<(), ()> { if !init_msg.features.supports_gossip_queries() { return Ok(()); } @@ -569,6 +671,8 @@ impl msgs::RoutingMessageHandler for TestRoutingMessageHandler { features.set_gossip_queries_optional(); features } + + fn processing_queue_high(&self) -> bool { false } } impl events::MessageSendEventsProvider for TestRoutingMessageHandler { @@ -609,10 +713,10 @@ impl TestLogger { /// 1. belongs to the specified module and /// 2. contains `line` in it. /// And asserts if the number of occurrences is the same with the given `count` - pub fn assert_log_contains(&self, module: String, line: String, count: usize) { + pub fn assert_log_contains(&self, module: &str, line: &str, count: usize) { let log_entries = self.lines.lock().unwrap(); let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| { - m == &module && l.contains(line.as_str()) + m == module && l.contains(line) }).map(|(_, c) | { c }).sum(); assert_eq!(l, count) } @@ -621,10 +725,10 @@ impl TestLogger { /// 1. belong to the specified module and /// 2. match the given regex pattern. /// Assert that the number of occurrences equals the given `count` - pub fn assert_log_regex(&self, module: String, pattern: regex::Regex, count: usize) { + pub fn assert_log_regex(&self, module: &str, pattern: regex::Regex, count: usize) { let log_entries = self.lines.lock().unwrap(); let l: usize = log_entries.iter().filter(|&(&(ref m, ref l), _c)| { - m == &module && pattern.is_match(&l) + m == module && pattern.is_match(&l) }).map(|(_, c) | { c }).sum(); assert_eq!(l, count) } @@ -651,7 +755,7 @@ impl TestNodeSigner { } impl NodeSigner for TestNodeSigner { - fn get_inbound_payment_key_material(&self) -> crate::chain::keysinterface::KeyMaterial { + fn get_inbound_payment_key_material(&self) -> crate::sign::KeyMaterial { unreachable!() } @@ -684,7 +788,7 @@ impl NodeSigner for TestNodeSigner { } pub struct TestKeysInterface { - pub backing: keysinterface::PhantomKeysManager, + pub backing: sign::PhantomKeysManager, pub override_random_bytes: Mutex>, pub disable_revocation_policy_check: bool, enforcement_states: Mutex>>>, @@ -710,7 +814,7 @@ impl NodeSigner for TestKeysInterface { self.backing.ecdh(recipient, other_key, tweak) } - fn get_inbound_payment_key_material(&self) -> keysinterface::KeyMaterial { + fn get_inbound_payment_key_material(&self) -> sign::KeyMaterial { self.backing.get_inbound_payment_key_material() } @@ -739,7 +843,7 @@ impl SignerProvider for TestKeysInterface { fn read_chan_signer(&self, buffer: &[u8]) -> Result { let mut reader = io::Cursor::new(buffer); - let inner: InMemorySigner = Readable::read(&mut reader)?; + let inner: InMemorySigner = ReadableArgs::read(&mut reader, self)?; let state = self.make_enforcement_state_cell(inner.commitment_seed); Ok(EnforcingSigner::new_with_revoked( @@ -749,14 +853,14 @@ impl SignerProvider for TestKeysInterface { )) } - fn get_destination_script(&self) -> Script { self.backing.get_destination_script() } + fn get_destination_script(&self) -> Result { self.backing.get_destination_script() } - fn get_shutdown_scriptpubkey(&self) -> ShutdownScript { + fn get_shutdown_scriptpubkey(&self) -> Result { match &mut *self.expectations.lock().unwrap() { None => self.backing.get_shutdown_scriptpubkey(), Some(expectations) => match expectations.pop_front() { None => panic!("Unexpected get_shutdown_scriptpubkey"), - Some(expectation) => expectation.returns, + Some(expectation) => Ok(expectation.returns), }, } } @@ -766,7 +870,7 @@ impl TestKeysInterface { pub fn new(seed: &[u8; 32], network: Network) -> Self { let now = Duration::from_secs(genesis_block(network).header.time as u64); Self { - backing: keysinterface::PhantomKeysManager::new(seed, now.as_secs(), now.subsec_nanos(), seed), + backing: sign::PhantomKeysManager::new(seed, now.as_secs(), now.subsec_nanos(), seed), override_random_bytes: Mutex::new(None), disable_revocation_policy_check: false, enforcement_states: Mutex::new(HashMap::new()), @@ -774,7 +878,7 @@ impl TestKeysInterface { } } - /// Sets an expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] is + /// Sets an expectation that [`sign::SignerProvider::get_shutdown_scriptpubkey`] is /// called. pub fn expect(&self, expectation: OnGetShutdownScriptpubkey) -> &Self { self.expectations.lock().unwrap() @@ -822,7 +926,7 @@ impl Drop for TestKeysInterface { } } -/// An expectation that [`keysinterface::SignerProvider::get_shutdown_scriptpubkey`] was called and +/// An expectation that [`sign::SignerProvider::get_shutdown_scriptpubkey`] was called and /// returns a [`ShutdownScript`]. pub struct OnGetShutdownScriptpubkey { /// A shutdown script used to close a channel. @@ -837,7 +941,8 @@ impl core::fmt::Debug for OnGetShutdownScriptpubkey { pub struct TestChainSource { pub genesis_hash: BlockHash, - pub utxo_ret: Mutex>, + pub utxo_ret: Mutex, + pub get_utxo_call_count: AtomicUsize, pub watched_txn: Mutex>, pub watched_outputs: Mutex>, } @@ -847,17 +952,19 @@ impl TestChainSource { let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script(); Self { genesis_hash: genesis_block(network).block_hash(), - utxo_ret: Mutex::new(Ok(TxOut { value: u64::max_value(), script_pubkey })), + utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: u64::max_value(), script_pubkey }))), + get_utxo_call_count: AtomicUsize::new(0), watched_txn: Mutex::new(HashSet::new()), watched_outputs: Mutex::new(HashSet::new()), } } } -impl chain::Access for TestChainSource { - fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> Result { +impl UtxoLookup for TestChainSource { + fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> UtxoResult { + self.get_utxo_call_count.fetch_add(1, Ordering::Relaxed); if self.genesis_hash != *genesis_hash { - return Err(chain::AccessError::UnknownChain); + return UtxoResult::Sync(Err(UtxoLookupError::UnknownChain)); } self.utxo_ret.lock().unwrap().clone() @@ -882,5 +989,66 @@ impl Drop for TestChainSource { } } -/// A scorer useful in testing, when the passage of time isn't a concern. -pub type TestScorer = FixedPenaltyScorer; +pub struct TestScorer { + /// Stores a tuple of (scid, ChannelUsage) + scorer_expectations: RefCell>>, +} + +impl TestScorer { + pub fn new() -> Self { + Self { + scorer_expectations: RefCell::new(None), + } + } + + pub fn expect_usage(&self, scid: u64, expectation: ChannelUsage) { + self.scorer_expectations.borrow_mut().get_or_insert_with(|| VecDeque::new()).push_back((scid, expectation)); + } +} + +#[cfg(c_bindings)] +impl crate::util::ser::Writeable for TestScorer { + fn write(&self, _: &mut W) -> Result<(), crate::io::Error> { unreachable!(); } +} + +impl Score for TestScorer { + type ScoreParams = (); + fn channel_penalty_msat( + &self, short_channel_id: u64, _source: &NodeId, _target: &NodeId, usage: ChannelUsage, _score_params: &Self::ScoreParams + ) -> u64 { + if let Some(scorer_expectations) = self.scorer_expectations.borrow_mut().as_mut() { + match scorer_expectations.pop_front() { + Some((scid, expectation)) => { + assert_eq!(expectation, usage); + assert_eq!(scid, short_channel_id); + }, + None => {}, + } + } + 0 + } + + fn payment_path_failed(&mut self, _actual_path: &Path, _actual_short_channel_id: u64) {} + + fn payment_path_successful(&mut self, _actual_path: &Path) {} + + fn probe_failed(&mut self, _actual_path: &Path, _: u64) {} + + fn probe_successful(&mut self, _actual_path: &Path) {} +} + +impl Drop for TestScorer { + fn drop(&mut self) { + #[cfg(feature = "std")] { + if std::thread::panicking() { + return; + } + } + + if let Some(scorer_expectations) = self.scorer_expectations.borrow().as_ref() { + if !scorer_expectations.is_empty() { + panic!("Unsatisfied scorer expectations: {:?}", scorer_expectations) + } + } + } +}