use crate::chain::channelmonitor;
use crate::chain::channelmonitor::MonitorEvent;
use crate::chain::transaction::OutPoint;
+use crate::routing::router::CandidateRouteHop;
use crate::sign;
use crate::events;
use crate::events::bump_transaction::{WalletSource, Utxo};
// 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, &Default::default());
+ let first_hops = match first_hops {
+ Some(hops) => hops,
+ None => continue,
+ };
+ if first_hops.len() == 0 {
+ continue;
+ }
+ let idx = if first_hops.len() > 1 { route.paths.iter().position(|p| p == path).unwrap_or(0) } else { 0 };
+ let candidate = CandidateRouteHop::FirstHop {
+ details: first_hops[idx],
+ node_id: NodeId::from_pubkey(payer)
+ };
+ scorer.channel_penalty_msat(&candidate, 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, &Default::default());
+ let network_graph = self.network_graph.read_only();
+ let channel = match network_graph.channel(hop.short_channel_id) {
+ Some(channel) => channel,
+ None => continue,
+ };
+ let channel = match channel.as_directed_to(&NodeId::from_pubkey(&hop.pubkey)) {
+ Some(channel) => channel,
+ None => panic!("Channel directed to {} was not found", hop.pubkey),
+ };
+ let candidate = CandidateRouteHop::PublicHop {
+ info: channel.0,
+ short_channel_id: hop.short_channel_id,
+ };
+ scorer.channel_penalty_msat(&candidate, usage, &());
}
}
}
pub struct TestLogger {
level: Level,
pub(crate) id: String,
- pub lines: Mutex<HashMap<(String, String), usize>>,
- pub context: Mutex<HashMap<(String, Option<PublicKey>, Option<ChannelId>), usize>>,
+ pub lines: Mutex<HashMap<(&'static str, String), usize>>,
+ pub context: Mutex<HashMap<(&'static str, Option<PublicKey>, Option<ChannelId>), usize>>,
}
impl TestLogger {
pub fn enable(&mut self, level: Level) {
self.level = level;
}
- pub fn assert_log(&self, module: String, line: String, count: usize) {
+ pub fn assert_log(&self, module: &str, line: String, count: usize) {
let log_entries = self.lines.lock().unwrap();
assert_eq!(log_entries.get(&(module, line)), Some(&count));
}
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)
+ *m == module && l.contains(line)
}).map(|(_, c) | { c }).sum();
assert_eq!(l, count)
}
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)
}
&self, module: &str, peer_id: Option<PublicKey>, channel_id: Option<ChannelId>, count: usize
) {
let context_entries = self.context.lock().unwrap();
- let l: usize = context_entries.iter()
- .filter(|&(&(ref m, ref p, ref c), _)| m == module && *p == peer_id && *c == channel_id)
- .map(|(_, c) | c)
- .sum();
- assert_eq!(l, count)
+ let l = context_entries.get(&(module, peer_id, channel_id)).unwrap();
+ assert_eq!(*l, count)
}
}
impl Logger for TestLogger {
fn log(&self, record: Record) {
- *self.lines.lock().unwrap().entry((record.module_path.to_string(), format!("{}", record.args))).or_insert(0) += 1;
- *self.context.lock().unwrap().entry((record.module_path.to_string(), record.peer_id, record.channel_id)).or_insert(0) += 1;
+ *self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
+ *self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
if record.level >= self.level {
#[cfg(all(not(ldk_bench), feature = "std"))] {
let pfx = format!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line);
impl ScoreLookUp for TestScorer {
type ScoreParams = ();
fn channel_penalty_msat(
- &self, short_channel_id: u64, _source: &NodeId, _target: &NodeId, usage: ChannelUsage, _score_params: &Self::ScoreParams
+ &self, candidate: &CandidateRouteHop, usage: ChannelUsage, _score_params: &Self::ScoreParams
) -> u64 {
+ let short_channel_id = match candidate.short_channel_id() {
+ Some(scid) => scid,
+ None => return 0,
+ };
if let Some(scorer_expectations) = self.scorer_expectations.borrow_mut().as_mut() {
match scorer_expectations.pop_front() {
Some((scid, expectation)) => {