X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Ftest_utils.rs;h=4d17f8fd17ddcd9a919acb4a4abddd9aac673c17;hb=07d991c82fadf10f59ae01b2b5aef6bc8797fd9a;hp=43518a3fd86cf2c9e8984e953d45b657c8b3bb68;hpb=74c9f9b1832fdb2f6e85af12d27cfaab9fe14154;p=rust-lightning diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 43518a3f..4d17f8fd 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -8,6 +8,7 @@ // licenses. use crate::blinded_path::BlindedPath; +use crate::blinded_path::message::ForwardNode; use crate::blinded_path::payment::ReceiveTlvs; use crate::chain; use crate::chain::WatchedOutput; @@ -24,7 +25,8 @@ use crate::sign; use crate::events; use crate::events::bump_transaction::{WalletSource, Utxo}; use crate::ln::types::ChannelId; -use crate::ln::channelmanager::{ChannelDetails, self}; +use crate::ln::channel_state::ChannelDetails; +use crate::ln::channelmanager; #[cfg(test)] use crate::ln::chan_utils::CommitmentTransaction; use crate::ln::features::{ChannelFeatures, InitFeatures, NodeFeatures}; @@ -45,14 +47,16 @@ use crate::util::logger::{Logger, Level, Record}; use crate::util::ser::{Readable, ReadableArgs, Writer, Writeable}; use crate::util::persist::KVStore; +use bitcoin::amount::Amount; use bitcoin::blockdata::constants::ChainHash; use bitcoin::blockdata::constants::genesis_block; use bitcoin::blockdata::transaction::{Transaction, TxOut}; use bitcoin::blockdata::script::{Builder, Script, ScriptBuf}; use bitcoin::blockdata::opcodes; use bitcoin::blockdata::block::Block; -use bitcoin::network::constants::Network; +use bitcoin::network::Network; use bitcoin::hash_types::{BlockHash, Txid}; +use bitcoin::hashes::Hash; use bitcoin::sighash::{SighashCache, EcdsaSighashType}; use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, self}; @@ -67,14 +71,16 @@ use core::time::Duration; use crate::sync::{Mutex, Arc}; use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering}; use core::mem; -use bitcoin::bech32::u5; +use bech32::u5; use crate::sign::{InMemorySigner, RandomBytes, Recipient, EntropySource, NodeSigner, SignerProvider}; #[cfg(feature = "std")] use std::time::{SystemTime, UNIX_EPOCH}; -use bitcoin::psbt::PartiallySignedTransaction; +use bitcoin::psbt::Psbt; use bitcoin::Sequence; +use super::test_channel_signer::SignerOp; + pub fn pubkey(byte: u8) -> PublicKey { let secp_ctx = Secp256k1::new(); PublicKey::from_secret_key(&secp_ctx, &privkey(byte)) @@ -112,7 +118,7 @@ pub struct TestRouter<'a> { >, //pub entropy_source: &'a RandomBytes, pub network_graph: Arc>, - pub next_routes: Mutex)>>, + pub next_routes: Mutex>)>>, pub scorer: &'a RwLock, } @@ -132,7 +138,12 @@ impl<'a> TestRouter<'a> { pub fn expect_find_route(&self, query: RouteParameters, result: Result) { let mut expected_routes = self.next_routes.lock().unwrap(); - expected_routes.push_back((query, result)); + expected_routes.push_back((query, Some(result))); + } + + pub fn expect_find_route_query(&self, query: RouteParameters) { + let mut expected_routes = self.next_routes.lock().unwrap(); + expected_routes.push_back((query, None)); } } @@ -145,63 +156,67 @@ impl<'a> Router for TestRouter<'a> { let next_route_opt = self.next_routes.lock().unwrap().pop_front(); if let Some((find_route_query, find_route_res)) = next_route_opt { assert_eq!(find_route_query, *params); - if let Ok(ref route) = find_route_res { - assert_eq!(route.route_params, Some(find_route_query)); - let scorer = self.scorer.read().unwrap(); - let scorer = ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs); - for path in &route.paths { - let mut aggregate_msat = 0u64; - let mut prev_hop_node = payer; - 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, - }; - - if idx == path.hops.len() - 1 { - if let Some(first_hops) = first_hops { - if let Some(idx) = first_hops.iter().position(|h| h.get_outbound_payment_scid() == Some(hop.short_channel_id)) { - let node_id = NodeId::from_pubkey(payer); - let candidate = CandidateRouteHop::FirstHop(FirstHopCandidate { - details: first_hops[idx], - payer_node_id: &node_id, - }); - scorer.channel_penalty_msat(&candidate, usage, &Default::default()); - continue; + if let Some(res) = find_route_res { + if let Ok(ref route) = res { + assert_eq!(route.route_params, Some(find_route_query)); + let scorer = self.scorer.read().unwrap(); + let scorer = ScorerAccountingForInFlightHtlcs::new(scorer, &inflight_htlcs); + for path in &route.paths { + let mut aggregate_msat = 0u64; + let mut prev_hop_node = payer; + 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, + }; + + if idx == path.hops.len() - 1 { + if let Some(first_hops) = first_hops { + if let Some(idx) = first_hops.iter().position(|h| h.get_outbound_payment_scid() == Some(hop.short_channel_id)) { + let node_id = NodeId::from_pubkey(payer); + let candidate = CandidateRouteHop::FirstHop(FirstHopCandidate { + details: first_hops[idx], + payer_node_id: &node_id, + }); + scorer.channel_penalty_msat(&candidate, usage, &Default::default()); + continue; + } } } + let network_graph = self.network_graph.read_only(); + if let Some(channel) = network_graph.channel(hop.short_channel_id) { + let (directed, _) = channel.as_directed_to(&NodeId::from_pubkey(&hop.pubkey)).unwrap(); + let candidate = CandidateRouteHop::PublicHop(PublicHopCandidate { + info: directed, + short_channel_id: hop.short_channel_id, + }); + scorer.channel_penalty_msat(&candidate, usage, &Default::default()); + } else { + let target_node_id = NodeId::from_pubkey(&hop.pubkey); + let route_hint = RouteHintHop { + src_node_id: *prev_hop_node, + short_channel_id: hop.short_channel_id, + fees: RoutingFees { base_msat: 0, proportional_millionths: 0 }, + cltv_expiry_delta: 0, + htlc_minimum_msat: None, + htlc_maximum_msat: None, + }; + let candidate = CandidateRouteHop::PrivateHop(PrivateHopCandidate { + hint: &route_hint, + target_node_id: &target_node_id, + }); + scorer.channel_penalty_msat(&candidate, usage, &Default::default()); + } + prev_hop_node = &hop.pubkey; } - let network_graph = self.network_graph.read_only(); - if let Some(channel) = network_graph.channel(hop.short_channel_id) { - let (directed, _) = channel.as_directed_to(&NodeId::from_pubkey(&hop.pubkey)).unwrap(); - let candidate = CandidateRouteHop::PublicHop(PublicHopCandidate { - info: directed, - short_channel_id: hop.short_channel_id, - }); - scorer.channel_penalty_msat(&candidate, usage, &Default::default()); - } else { - let target_node_id = NodeId::from_pubkey(&hop.pubkey); - let route_hint = RouteHintHop { - src_node_id: *prev_hop_node, - short_channel_id: hop.short_channel_id, - fees: RoutingFees { base_msat: 0, proportional_millionths: 0 }, - cltv_expiry_delta: 0, - htlc_minimum_msat: None, - htlc_maximum_msat: None, - }; - let candidate = CandidateRouteHop::PrivateHop(PrivateHopCandidate { - hint: &route_hint, - target_node_id: &target_node_id, - }); - scorer.channel_penalty_msat(&candidate, usage, &Default::default()); - } - prev_hop_node = &hop.pubkey; } } + route_res = res; + } else { + route_res = self.router.find_route(payer, params, first_hops, inflight_htlcs); } - route_res = find_route_res; } else { route_res = self.router.find_route(payer, params, first_hops, inflight_htlcs); }; @@ -241,6 +256,14 @@ impl<'a> MessageRouter for TestRouter<'a> { ) -> Result, ()> { self.router.create_blinded_paths(recipient, peers, secp_ctx) } + + fn create_compact_blinded_paths< + T: secp256k1::Signing + secp256k1::Verification + >( + &self, recipient: PublicKey, peers: Vec, secp_ctx: &Secp256k1, + ) -> Result, ()> { + self.router.create_compact_blinded_paths(recipient, peers, secp_ctx) + } } impl<'a> Drop for TestRouter<'a> { @@ -276,6 +299,12 @@ impl<'a> MessageRouter for TestMessageRouter<'a> { ) -> Result, ()> { self.inner.create_blinded_paths(recipient, peers, secp_ctx) } + + fn create_compact_blinded_paths( + &self, recipient: PublicKey, peers: Vec, secp_ctx: &Secp256k1, + ) -> Result, ()> { + self.inner.create_compact_blinded_paths(recipient, peers, secp_ctx) + } } pub struct OnlyReadsKeysInterface {} @@ -400,7 +429,7 @@ impl<'a> chain::Watch for TestChainMonitor<'a> { #[cfg(test)] struct JusticeTxData { justice_tx: Transaction, - value: u64, + value: Amount, commitment_number: u64, } @@ -450,7 +479,7 @@ impl WatchtowerPersister { } #[cfg(test)] -impl chainmonitor::Persist for WatchtowerPersister { +impl chainmonitor::Persist for WatchtowerPersister { fn persist_new_channel(&self, funding_txo: OutPoint, data: &channelmonitor::ChannelMonitor ) -> chain::ChannelMonitorUpdateStatus { @@ -489,7 +518,7 @@ impl chainmonitor::Persist { let dup = self.watchtower_state.lock().unwrap() .get_mut(&funding_txo).unwrap() @@ -513,21 +542,21 @@ pub struct TestPersister { /// The queue of update statuses we'll return. If none are queued, ::Completed will always be /// returned. pub update_rets: Mutex>, - /// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the - /// MonitorId here. - pub chain_sync_monitor_persistences: Mutex>, /// When we get an update_persisted_channel call *with* a ChannelMonitorUpdate, we insert the /// [`ChannelMonitor::get_latest_update_id`] here. /// /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor pub offchain_monitor_updates: Mutex>>, + /// When we get an update_persisted_channel call with no ChannelMonitorUpdate, we insert the + /// monitor's funding outpoint here. + pub chain_sync_monitor_persistences: Mutex> } impl TestPersister { pub fn new() -> Self { Self { update_rets: Mutex::new(VecDeque::new()), - chain_sync_monitor_persistences: Mutex::new(VecDeque::new()), offchain_monitor_updates: Mutex::new(new_hash_map()), + chain_sync_monitor_persistences: Mutex::new(VecDeque::new()) } } @@ -536,7 +565,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) -> chain::ChannelMonitorUpdateStatus { if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() { return update_ret @@ -550,7 +579,7 @@ impl chainmonitor::Persist chainmonitor::Persist {}, - None => { - // If the channel was not in the offchain_monitor_updates map, it should be in the - // chain_sync_monitor_persistences map. - self.chain_sync_monitor_persistences.lock().unwrap().retain(|x| x != &funding_txo); - } - }; + fn archive_persisted_channel(&self, funding_txo: OutPoint) { + // remove the channel from the offchain_monitor_updates and chain_sync_monitor_persistences. + self.offchain_monitor_updates.lock().unwrap().remove(&funding_txo); + self.chain_sync_monitor_persistences.lock().unwrap().retain(|x| x != &funding_txo); } } @@ -1173,7 +1196,7 @@ impl NodeSigner for TestNodeSigner { Ok(SharedSecret::new(other_key, &node_secret)) } - fn sign_invoice(&self, _: &[u8], _: &[bitcoin::bech32::u5], _: Recipient) -> Result { + fn sign_invoice(&self, _: &[u8], _: &[bech32::u5], _: Recipient) -> Result { unreachable!() } @@ -1201,6 +1224,7 @@ pub struct TestKeysInterface { enforcement_states: Mutex>>>, expectations: Mutex>>, pub unavailable_signers: Mutex>, + pub unavailable_signers_ops: Mutex>>, } impl EntropySource for TestKeysInterface { @@ -1259,9 +1283,11 @@ impl SignerProvider for TestKeysInterface { fn derive_channel_signer(&self, channel_value_satoshis: u64, channel_keys_id: [u8; 32]) -> TestChannelSigner { let keys = self.backing.derive_channel_signer(channel_value_satoshis, channel_keys_id); let state = self.make_enforcement_state_cell(keys.commitment_seed); - let signer = TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check); - if self.unavailable_signers.lock().unwrap().contains(&channel_keys_id) { - signer.set_available(false); + let mut signer = TestChannelSigner::new_with_revoked(keys, state, self.disable_revocation_policy_check); + if let Some(ops) = self.unavailable_signers_ops.lock().unwrap().get(&channel_keys_id) { + for &op in ops { + signer.disable_op(op); + } } signer } @@ -1302,6 +1328,7 @@ impl TestKeysInterface { enforcement_states: Mutex::new(new_hash_map()), expectations: Mutex::new(None), unavailable_signers: Mutex::new(new_hash_set()), + unavailable_signers_ops: Mutex::new(new_hash_map()), } } @@ -1377,14 +1404,14 @@ impl TestChainSource { let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script(); Self { chain_hash: ChainHash::using_genesis_block(network), - utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: u64::max_value(), script_pubkey }))), + utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: Amount::MAX, script_pubkey }))), get_utxo_call_count: AtomicUsize::new(0), watched_txn: Mutex::new(new_hash_set()), watched_outputs: Mutex::new(new_hash_set()), } } pub fn remove_watched_txn_and_outputs(&self, outpoint: OutPoint, script_pubkey: ScriptBuf) { - self.watched_outputs.lock().unwrap().remove(&(outpoint, script_pubkey.clone())); + self.watched_outputs.lock().unwrap().remove(&(outpoint, script_pubkey.clone())); self.watched_txn.lock().unwrap().remove(&(outpoint.txid, script_pubkey)); } } @@ -1508,7 +1535,7 @@ impl TestWalletSource { } } - pub fn add_utxo(&self, outpoint: bitcoin::OutPoint, value: u64) -> TxOut { + pub fn add_utxo(&self, outpoint: bitcoin::OutPoint, value: Amount) -> TxOut { let public_key = bitcoin::PublicKey::new(self.secret_key.public_key(&self.secp)); let utxo = Utxo::new_p2pkh(outpoint, value, &public_key.pubkey_hash()); self.utxos.borrow_mut().push(utxo.clone()); @@ -1536,15 +1563,15 @@ impl WalletSource for TestWalletSource { Ok(ScriptBuf::new_p2pkh(&public_key.pubkey_hash())) } - fn sign_psbt(&self, psbt: PartiallySignedTransaction) -> Result { - let mut tx = psbt.extract_tx(); + fn sign_psbt(&self, psbt: Psbt) -> Result { + let mut tx = psbt.extract_tx_unchecked_fee_rate(); let utxos = self.utxos.borrow(); for i in 0..tx.input.len() { if let Some(utxo) = utxos.iter().find(|utxo| utxo.outpoint == tx.input[i].previous_output) { let sighash = SighashCache::new(&tx) .legacy_signature_hash(i, &utxo.output.script_pubkey, EcdsaSighashType::All as u32) .map_err(|_| ())?; - let sig = self.secp.sign_ecdsa(&(*sighash.as_raw_hash()).into(), &self.secret_key); + let sig = self.secp.sign_ecdsa(&secp256k1::Message::from_digest(sighash.to_byte_array()), &self.secret_key); let bitcoin_sig = bitcoin::ecdsa::Signature { sig, hash_ty: EcdsaSighashType::All }; tx.input[i].script_sig = Builder::new() .push_slice(&bitcoin_sig.serialize())