+# 0.0.115 - Apr 24, 2023 - "Rebroadcast the Bugfixes"
+
+## API Updates
+ * The MSRV of the main LDK crates has been increased to 1.48 (#2107).
+ * Attempting to claim an un-expired payment on a channel which has closed no
+ longer fails. The expiry time of payments is exposed via
+ `PaymentClaimable::claim_deadline` (#2148).
+ * `payment_metadata` is now supported in `Invoice` deserialization, sending,
+ and receiving (via a new `RecipientOnionFields` struct) (#2139, #2127).
+ * `Event::PaymentFailed` now exposes a failure reason (#2142).
+ * BOLT12 messages now support stateless generation and validation (#1989).
+ * The `NetworkGraph` is now pruned of stale data after RGS processing (#2161).
+ * Max inbound HTLCs in-flight can be changed in the handshake config (#2138).
+ * `lightning-transaction-sync` feature `esplora-async-https` was added (#2085).
+ * A `ChannelPending` event is now emitted after the initial handshake (#2098).
+ * `PaymentForwarded::outbound_amount_forwarded_msat` was added (#2136).
+ * `ChannelManager::list_channels_by_counterparty` was added (#2079).
+ * `ChannelDetails::feerate_sat_per_1000_weight` was added (#2094).
+ * `Invoice::fallback_addresses` was added to fetch `bitcoin` types (#2023).
+ * The offer/refund description is now exposed in `Invoice{,Request}` (#2206).
+
+## Backwards Compatibility
+ * Payments sent with the legacy `*_with_route` methods on LDK 0.0.115+ will no
+ longer be retryable via the LDK 0.0.114- `retry_payment` method (#2139).
+ * `Event::PaymentPathFailed::retry` was removed and will always be `None` for
+ payments initiated on 0.0.115 which fail on an earlier version (#2063).
+ * `Route`s and `PaymentParameters` with blinded path information will not be
+ readable on prior versions of LDK. Such objects are not currently constructed
+ by LDK, but may be when processing BOLT12 data in a coming release (#2146).
+ * Providing `ChannelMonitorUpdate`s generated by LDK 0.0.115 to a
+ `ChannelMonitor` on 0.0.114 or before may panic (#2059). Note that this is
+ in general unsupported, and included here only for completeness.
+
+## Bug Fixes
+ * Fixed a case where `process_events_async` may `poll` a `Future` which has
+ already completed (#2081).
+ * Fixed deserialization of `u16` arrays. This bug may have previously corrupted
+ the historical buckets in a `ProbabilisticScorer`. Users relying on the
+ historical buckets may wish to wipe their scorer on upgrade to remove corrupt
+ data rather than waiting on it to decay (#2191).
+ * The `process_events_async` task is now `Send` and can thus be polled on a
+ multi-threaded runtime (#2199).
+ * Fixed a missing macro export causing
+ `impl_writeable_tlv_based_enum{,_upgradable}` calls to not compile (#2091).
+ * Fixed compilation of `lightning-invoice` with both `no-std` and serde (#2187)
+ * Fix an issue where the `background-processor` would not wake when a
+ `ChannelMonitorUpdate` completed asynchronously, causing delays (#2090).
+ * Fix an issue where `process_events_async` would exit immediately (#2145).
+ * `Router` calls from the `ChannelManager` now call `find_route_with_id` rather
+ than `find_route`, as was intended and described in the API (#2092).
+ * Ensure `process_events_async` always exits if any sleep future returns true,
+ not just if all sleep futures repeatedly return true (#2145).
+ * `channel_update` messages no longer set the disable bit unless the peer has
+ been disconnected for some time. This should resolve cases where channels are
+ disabled for extended periods of time (#2198).
+ * We no longer remove CLN nodes from the network graph for violating the BOLT
+ spec in some cases after failing to pay through them (#2220).
+ * Fixed a debug assertion which may panic under heavy load (#2172).
+ * `CounterpartyForceClosed::peer_msg` is now wrapped in UntrustedString (#2114)
+ * Fixed a potential deadlock in `funding_transaction_generated` (#2158).
+
+## Security
+ * Transaction re-broadcasting is now substantially more aggressive, including a
+ new regular rebroadcast feature called on a timer from the
+ `background-processor` or from `ChainMonitor::rebroadcast_pending_claims`.
+ This should substantially increase transaction confirmation reliability
+ without relying on downstream `TransactionBroadcaster` implementations for
+ rebroadcasting (#2203, #2205, #2208).
+ * Implemented the changes from BOLT PRs #1031, #1032, and #1040 which resolve a
+ privacy vulnerability which allows an intermediate node on the path to
+ discover the final destination for a payment (#2062).
+
+In total, this release features 110 files changed, 11928 insertions, 6368
+deletions in 215 commits from 21 authors, in alphabetical order:
+ * Advait
+ * Alan Cohen
+ * Alec Chen
+ * Allan Douglas R. de Oliveira
+ * Arik Sosman
+ * Elias Rohrer
+ * Evan Feenstra
+ * Jeffrey Czyz
+ * John Cantrell
+ * Lucas Soriano del Pino
+ * Marc Tyndel
+ * Matt Corallo
+ * Paul Miller
+ * Steven
+ * Steven Williamson
+ * Steven Zhao
+ * Tony Giorgio
+ * Valentine Wallace
+ * Wilmer Paulino
+ * benthecarman
+ * munjesi
+
+
# 0.0.114 - Mar 3, 2023 - "Faster Async BOLT12 Retries"
## API Updates
use lightning::util::logger::Logger;
use lightning::util::config::UserConfig;
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
-use lightning::routing::router::{InFlightHtlcs, Route, RouteHop, RouteParameters, Router};
+use lightning::routing::router::{InFlightHtlcs, Path, Route, RouteHop, RouteParameters, Router};
use crate::utils::test_logger::{self, Output};
use crate::utils::test_persister::TestPersister;
payment_id[0..8].copy_from_slice(&payment_idx.to_ne_bytes());
*payment_idx += 1;
if let Err(err) = source.send_payment_with_route(&Route {
- paths: vec![vec![RouteHop {
+ paths: vec![Path { hops: vec![RouteHop {
pubkey: dest.get_our_node_id(),
node_features: dest.node_features(),
short_channel_id: dest_chan_id,
channel_features: dest.channel_features(),
fee_msat: amt,
cltv_expiry_delta: 200,
- }]],
+ }], blinded_tail: None }],
payment_params: None,
}, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
check_payment_err(err);
payment_id[0..8].copy_from_slice(&payment_idx.to_ne_bytes());
*payment_idx += 1;
if let Err(err) = source.send_payment_with_route(&Route {
- paths: vec![vec![RouteHop {
+ paths: vec![Path { hops: vec![RouteHop {
pubkey: middle.get_our_node_id(),
node_features: middle.node_features(),
short_channel_id: middle_chan_id,
channel_features: dest.channel_features(),
fee_msat: amt,
cltv_expiry_delta: 200,
- }]],
+ }], blinded_tail: None }],
payment_params: None,
}, payment_hash, RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_id)) {
check_payment_err(err);
use bitcoin::secp256k1::{KeyPair, Parity, PublicKey, Secp256k1, SecretKey, self};
use crate::utils::test_logger;
use core::convert::{Infallible, TryFrom};
+use lightning::blinded_path::BlindedPath;
use lightning::chain::keysinterface::EntropySource;
use lightning::ln::PaymentHash;
use lightning::ln::features::BlindedHopFeatures;
use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
use lightning::offers::invoice_request::InvoiceRequest;
use lightning::offers::parse::SemanticError;
-use lightning::onion_message::BlindedPath;
use lightning::util::ser::Writeable;
#[inline]
) -> Result<UnsignedInvoice<'a>, SemanticError> {
let entropy_source = Randomness {};
let paths = vec![
- BlindedPath::new(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
- BlindedPath::new(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
+ BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
+ BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
];
let payinfo = vec![
use bitcoin::secp256k1::{KeyPair, PublicKey, Secp256k1, SecretKey, self};
use crate::utils::test_logger;
use core::convert::{Infallible, TryFrom};
+use lightning::blinded_path::BlindedPath;
use lightning::chain::keysinterface::EntropySource;
use lightning::ln::PaymentHash;
use lightning::ln::features::BlindedHopFeatures;
use lightning::offers::invoice::{BlindedPayInfo, UnsignedInvoice};
use lightning::offers::parse::SemanticError;
use lightning::offers::refund::Refund;
-use lightning::onion_message::BlindedPath;
use lightning::util::ser::Writeable;
#[inline]
) -> Result<UnsignedInvoice<'a>, SemanticError> {
let entropy_source = Randomness {};
let paths = vec![
- BlindedPath::new(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
- BlindedPath::new(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
+ BlindedPath::new_for_message(&[pubkey(43), pubkey(44), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
+ BlindedPath::new_for_message(&[pubkey(45), pubkey(46), pubkey(42)], &entropy_source, secp_ctx).unwrap(),
];
let payinfo = vec![
},
4 => {
let short_channel_id = slice_to_be64(get_slice!(8));
- net_graph.channel_failed(short_channel_id, false);
+ net_graph.channel_failed_permanent(short_channel_id);
},
_ if node_pks.is_empty() => {},
_ => {
[package]
name = "lightning-background-processor"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Valentine Wallace <vwallace@protonmail.com>"]
license = "MIT OR Apache-2.0"
repository = "http://github.com/lightningdevkit/rust-lightning"
[dependencies]
bitcoin = { version = "0.29.0", default-features = false }
-lightning = { version = "0.0.114", path = "../lightning", default-features = false }
-lightning-rapid-gossip-sync = { version = "0.0.114", path = "../lightning-rapid-gossip-sync", default-features = false }
+lightning = { version = "0.0.115", path = "../lightning", default-features = false }
+lightning-rapid-gossip-sync = { version = "0.0.115", path = "../lightning-rapid-gossip-sync", default-features = false }
[dev-dependencies]
tokio = { version = "1.14", features = [ "macros", "rt", "rt-multi-thread", "sync", "time" ] }
-lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
-lightning-invoice = { version = "0.22.0", path = "../lightning-invoice" }
-lightning-persister = { version = "0.0.114", path = "../lightning-persister" }
+lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
+lightning-invoice = { version = "0.23.0", path = "../lightning-invoice" }
+lightning-persister = { version = "0.0.115", path = "../lightning-persister" }
let mut score = scorer.lock();
match event {
Event::PaymentPathFailed { ref path, short_channel_id: Some(scid), .. } => {
- let path = path.iter().collect::<Vec<_>>();
- score.payment_path_failed(&path, *scid);
+ score.payment_path_failed(path, *scid);
},
Event::PaymentPathFailed { ref path, payment_failed_permanently: true, .. } => {
// Reached if the destination explicitly failed it back. We treat this as a successful probe
// because the payment made it all the way to the destination with sufficient liquidity.
- let path = path.iter().collect::<Vec<_>>();
- score.probe_successful(&path);
+ score.probe_successful(path);
},
Event::PaymentPathSuccessful { path, .. } => {
- let path = path.iter().collect::<Vec<_>>();
- score.payment_path_successful(&path);
+ score.payment_path_successful(path);
},
Event::ProbeSuccessful { path, .. } => {
- let path = path.iter().collect::<Vec<_>>();
- score.probe_successful(&path);
+ score.probe_successful(path);
},
Event::ProbeFailed { path, short_channel_id: Some(scid), .. } => {
- let path = path.iter().collect::<Vec<_>>();
- score.probe_failed(&path, *scid);
+ score.probe_failed(path, *scid);
},
_ => {},
}
// persistence.
$peer_manager.process_events();
+ // Exit the loop if the background processor was requested to stop.
+ if $loop_exit_check {
+ log_trace!($logger, "Terminating background processor.");
+ break;
+ }
+
// We wait up to 100ms, but track how long it takes to detect being put to sleep,
// see `await_start`'s use below.
let mut await_start = None;
let updates_available = $await;
let await_slow = if $check_slow_await { $timer_elapsed(&mut await_start.unwrap(), 1) } else { false };
- if updates_available {
- log_trace!($logger, "Persisting ChannelManager...");
- $persister.persist_manager(&*$channel_manager)?;
- log_trace!($logger, "Done persisting ChannelManager.");
- }
// Exit the loop if the background processor was requested to stop.
if $loop_exit_check {
log_trace!($logger, "Terminating background processor.");
break;
}
+
+ if updates_available {
+ log_trace!($logger, "Persisting ChannelManager...");
+ $persister.persist_manager(&*$channel_manager)?;
+ log_trace!($logger, "Done persisting ChannelManager.");
+ }
if $timer_elapsed(&mut last_freshness_call, FRESHNESS_TIMER) {
log_trace!($logger, "Calling ChannelManager's timer_tick_occurred");
$channel_manager.timer_tick_occurred();
///
/// `sleeper` should return a future which completes in the given amount of time and returns a
/// boolean indicating whether the background processing should exit. Once `sleeper` returns a
-/// future which outputs true, the loop will exit and this function's future will complete.
+/// future which outputs `true`, the loop will exit and this function's future will complete.
+/// The `sleeper` future is free to return early after it has triggered the exit condition.
///
/// See [`BackgroundProcessor::start`] for information on which actions this handles.
///
/// mobile device, where we may need to check for interruption of the application regularly. If you
/// are unsure, you should set the flag, as the performance impact of it is minimal unless there
/// are hundreds or thousands of simultaneous process calls running.
+///
+/// For example, in order to process background events in a [Tokio](https://tokio.rs/) task, you
+/// could setup `process_events_async` like this:
+/// ```
+/// # struct MyPersister {}
+/// # impl lightning::util::persist::KVStorePersister for MyPersister {
+/// # fn persist<W: lightning::util::ser::Writeable>(&self, key: &str, object: &W) -> lightning::io::Result<()> { Ok(()) }
+/// # }
+/// # struct MyEventHandler {}
+/// # impl MyEventHandler {
+/// # async fn handle_event(&self, _: lightning::events::Event) {}
+/// # }
+/// # #[derive(Eq, PartialEq, Clone, Hash)]
+/// # struct MySocketDescriptor {}
+/// # impl lightning::ln::peer_handler::SocketDescriptor for MySocketDescriptor {
+/// # fn send_data(&mut self, _data: &[u8], _resume_read: bool) -> usize { 0 }
+/// # fn disconnect_socket(&mut self) {}
+/// # }
+/// # use std::sync::{Arc, Mutex};
+/// # use std::sync::atomic::{AtomicBool, Ordering};
+/// # use lightning_background_processor::{process_events_async, GossipSync};
+/// # type MyBroadcaster = dyn lightning::chain::chaininterface::BroadcasterInterface + Send + Sync;
+/// # type MyFeeEstimator = dyn lightning::chain::chaininterface::FeeEstimator + Send + Sync;
+/// # type MyNodeSigner = dyn lightning::chain::keysinterface::NodeSigner + Send + Sync;
+/// # type MyUtxoLookup = dyn lightning::routing::utxo::UtxoLookup + Send + Sync;
+/// # type MyFilter = dyn lightning::chain::Filter + Send + Sync;
+/// # type MyLogger = dyn lightning::util::logger::Logger + Send + Sync;
+/// # type MyChainMonitor = lightning::chain::chainmonitor::ChainMonitor<lightning::chain::keysinterface::InMemorySigner, Arc<MyFilter>, Arc<MyBroadcaster>, Arc<MyFeeEstimator>, Arc<MyLogger>, Arc<MyPersister>>;
+/// # type MyPeerManager = lightning::ln::peer_handler::SimpleArcPeerManager<MySocketDescriptor, MyChainMonitor, MyBroadcaster, MyFeeEstimator, MyUtxoLookup, MyLogger>;
+/// # type MyNetworkGraph = lightning::routing::gossip::NetworkGraph<Arc<MyLogger>>;
+/// # type MyGossipSync = lightning::routing::gossip::P2PGossipSync<Arc<MyNetworkGraph>, Arc<MyUtxoLookup>, Arc<MyLogger>>;
+/// # type MyChannelManager = lightning::ln::channelmanager::SimpleArcChannelManager<MyChainMonitor, MyBroadcaster, MyFeeEstimator, MyLogger>;
+/// # type MyScorer = Mutex<lightning::routing::scoring::ProbabilisticScorer<Arc<MyNetworkGraph>, Arc<MyLogger>>>;
+///
+/// # async fn setup_background_processing(my_persister: Arc<MyPersister>, my_event_handler: Arc<MyEventHandler>, my_chain_monitor: Arc<MyChainMonitor>, my_channel_manager: Arc<MyChannelManager>, my_gossip_sync: Arc<MyGossipSync>, my_logger: Arc<MyLogger>, my_scorer: Arc<MyScorer>, my_peer_manager: Arc<MyPeerManager>) {
+/// let background_persister = Arc::clone(&my_persister);
+/// let background_event_handler = Arc::clone(&my_event_handler);
+/// let background_chain_mon = Arc::clone(&my_chain_monitor);
+/// let background_chan_man = Arc::clone(&my_channel_manager);
+/// let background_gossip_sync = GossipSync::p2p(Arc::clone(&my_gossip_sync));
+/// let background_peer_man = Arc::clone(&my_peer_manager);
+/// let background_logger = Arc::clone(&my_logger);
+/// let background_scorer = Arc::clone(&my_scorer);
+///
+/// // Setup the sleeper.
+/// let (stop_sender, stop_receiver) = tokio::sync::watch::channel(());
+///
+/// let sleeper = move |d| {
+/// let mut receiver = stop_receiver.clone();
+/// Box::pin(async move {
+/// tokio::select!{
+/// _ = tokio::time::sleep(d) => false,
+/// _ = receiver.changed() => true,
+/// }
+/// })
+/// };
+///
+/// let mobile_interruptable_platform = false;
+///
+/// let handle = tokio::spawn(async move {
+/// process_events_async(
+/// background_persister,
+/// |e| background_event_handler.handle_event(e),
+/// background_chain_mon,
+/// background_chan_man,
+/// background_gossip_sync,
+/// background_peer_man,
+/// background_logger,
+/// Some(background_scorer),
+/// sleeper,
+/// mobile_interruptable_platform,
+/// )
+/// .await
+/// .expect("Failed to process events");
+/// });
+///
+/// // Stop the background processing.
+/// stop_sender.send(()).unwrap();
+/// handle.await.unwrap();
+/// # }
+///```
#[cfg(feature = "futures")]
pub async fn process_events_async<
'a,
use lightning::ln::msgs::{ChannelMessageHandler, Init};
use lightning::ln::peer_handler::{PeerManager, MessageHandler, SocketDescriptor, IgnoringMessageHandler};
use lightning::routing::gossip::{NetworkGraph, NodeId, P2PGossipSync};
- use lightning::routing::router::{DefaultRouter, RouteHop};
+ use lightning::routing::router::{DefaultRouter, Path, RouteHop};
use lightning::routing::scoring::{ChannelUsage, Score};
use lightning::util::config::UserConfig;
use lightning::util::ser::Writeable;
use lightning::util::persist::KVStorePersister;
use lightning_persister::FilesystemPersister;
use std::collections::VecDeque;
- use std::fs;
+ use std::{fs, env};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::SyncSender;
#[derive(Debug)]
enum TestResult {
- PaymentFailure { path: Vec<RouteHop>, short_channel_id: u64 },
- PaymentSuccess { path: Vec<RouteHop> },
- ProbeFailure { path: Vec<RouteHop> },
- ProbeSuccess { path: Vec<RouteHop> },
+ PaymentFailure { path: Path, short_channel_id: u64 },
+ PaymentSuccess { path: Path },
+ ProbeFailure { path: Path },
+ ProbeSuccess { path: Path },
}
impl TestScorer {
&self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId, _usage: ChannelUsage
) -> u64 { unimplemented!(); }
- fn payment_path_failed(&mut self, actual_path: &[&RouteHop], actual_short_channel_id: u64) {
+ fn payment_path_failed(&mut self, actual_path: &Path, actual_short_channel_id: u64) {
if let Some(expectations) = &mut self.event_expectations {
match expectations.pop_front().unwrap() {
TestResult::PaymentFailure { path, short_channel_id } => {
- assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ assert_eq!(actual_path, &path);
assert_eq!(actual_short_channel_id, short_channel_id);
},
TestResult::PaymentSuccess { path } => {
}
}
- fn payment_path_successful(&mut self, actual_path: &[&RouteHop]) {
+ fn payment_path_successful(&mut self, actual_path: &Path) {
if let Some(expectations) = &mut self.event_expectations {
match expectations.pop_front().unwrap() {
TestResult::PaymentFailure { path, .. } => {
panic!("Unexpected payment path failure: {:?}", path)
},
TestResult::PaymentSuccess { path } => {
- assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ assert_eq!(actual_path, &path);
},
TestResult::ProbeFailure { path } => {
panic!("Unexpected probe failure: {:?}", path)
}
}
- fn probe_failed(&mut self, actual_path: &[&RouteHop], _: u64) {
+ fn probe_failed(&mut self, actual_path: &Path, _: u64) {
if let Some(expectations) = &mut self.event_expectations {
match expectations.pop_front().unwrap() {
TestResult::PaymentFailure { path, .. } => {
panic!("Unexpected payment path success: {:?}", path)
},
TestResult::ProbeFailure { path } => {
- assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ assert_eq!(actual_path, &path);
},
TestResult::ProbeSuccess { path } => {
panic!("Unexpected probe success: {:?}", path)
}
}
}
- fn probe_successful(&mut self, actual_path: &[&RouteHop]) {
+ fn probe_successful(&mut self, actual_path: &Path) {
if let Some(expectations) = &mut self.event_expectations {
match expectations.pop_front().unwrap() {
TestResult::PaymentFailure { path, .. } => {
panic!("Unexpected probe failure: {:?}", path)
},
TestResult::ProbeSuccess { path } => {
- assert_eq!(actual_path, &path.iter().collect::<Vec<_>>()[..]);
+ assert_eq!(actual_path, &path);
}
}
}
path.to_str().unwrap().to_string()
}
- fn create_nodes(num_nodes: usize, persist_dir: String) -> Vec<Node> {
+ fn create_nodes(num_nodes: usize, persist_dir: &str) -> (String, Vec<Node>) {
+ let persist_temp_path = env::temp_dir().join(persist_dir);
+ let persist_dir = persist_temp_path.to_string_lossy().to_string();
+ let network = Network::Testnet;
let mut nodes = Vec::new();
for i in 0..num_nodes {
- let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))});
+ let tx_broadcaster = Arc::new(test_utils::TestBroadcaster::new(network));
let fee_estimator = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) });
let logger = Arc::new(test_utils::TestLogger::with_id(format!("node {}", i)));
- let network = Network::Testnet;
let genesis_block = genesis_block(network);
let network_graph = Arc::new(NetworkGraph::new(network, logger.clone()));
let scorer = Arc::new(Mutex::new(TestScorer::new()));
let seed = [i as u8; 32];
let router = Arc::new(DefaultRouter::new(network_graph.clone(), logger.clone(), seed, scorer.clone()));
let chain_source = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
- let persister = Arc::new(FilesystemPersister::new(format!("{}_persister_{}", persist_dir, i)));
+ let persister = Arc::new(FilesystemPersister::new(format!("{}_persister_{}", &persist_dir, i)));
let now = Duration::from_secs(genesis_block.header.time as u64);
let keys_manager = Arc::new(KeysManager::new(&seed, now.as_secs(), now.subsec_nanos()));
let chain_monitor = Arc::new(chainmonitor::ChainMonitor::new(Some(chain_source.clone()), tx_broadcaster.clone(), logger.clone(), fee_estimator.clone(), persister.clone()));
}
}
- nodes
+ (persist_dir, nodes)
}
macro_rules! open_channel {
// Test that when a new channel is created, the ChannelManager needs to be re-persisted with
// updates. Also test that when new updates are available, the manager signals that it needs
// re-persistence and is successfully re-persisted.
- let nodes = create_nodes(2, "test_background_processor".to_string());
+ let (persist_dir, nodes) = create_nodes(2, "test_background_processor");
// Go through the channel creation process so that each node has something to persist. Since
// open_channel consumes events, it must complete before starting BackgroundProcessor to
}
// Check that the initial channel manager data is persisted as expected.
- let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "manager".to_string());
+ let filepath = get_full_filepath(format!("{}_persister_0", &persist_dir), "manager".to_string());
check_persisted_data!(nodes[0].node, filepath.clone());
loop {
}
// Check network graph is persisted
- let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "network_graph".to_string());
+ let filepath = get_full_filepath(format!("{}_persister_0", &persist_dir), "network_graph".to_string());
check_persisted_data!(nodes[0].network_graph, filepath.clone());
// Check scorer is persisted
- let filepath = get_full_filepath("test_background_processor_persister_0".to_string(), "scorer".to_string());
+ let filepath = get_full_filepath(format!("{}_persister_0", &persist_dir), "scorer".to_string());
check_persisted_data!(nodes[0].scorer, filepath.clone());
if !std::thread::panicking() {
// Test that `ChannelManager::timer_tick_occurred` is called every `FRESHNESS_TIMER`,
// `ChainMonitor::rebroadcast_pending_claims` is called every `REBROADCAST_TIMER`, and
// `PeerManager::timer_tick_occurred` every `PING_TIMER`.
- let nodes = create_nodes(1, "test_timer_tick_called".to_string());
+ let (_, nodes) = create_nodes(1, "test_timer_tick_called");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir));
let event_handler = |_: _| {};
#[test]
fn test_channel_manager_persist_error() {
// Test that if we encounter an error during manager persistence, the thread panics.
- let nodes = create_nodes(2, "test_persist_error".to_string());
+ let (_, nodes) = create_nodes(2, "test_persist_error");
open_channel!(nodes[0], nodes[1], 100000);
let data_dir = nodes[0].persister.get_data_dir();
#[cfg(feature = "futures")]
async fn test_channel_manager_persist_error_async() {
// Test that if we encounter an error during manager persistence, the thread panics.
- let nodes = create_nodes(2, "test_persist_error_sync".to_string());
+ let (_, nodes) = create_nodes(2, "test_persist_error_sync");
open_channel!(nodes[0], nodes[1], 100000);
let data_dir = nodes[0].persister.get_data_dir();
#[test]
fn test_network_graph_persist_error() {
// Test that if we encounter an error during network graph persistence, an error gets returned.
- let nodes = create_nodes(2, "test_persist_network_graph_error".to_string());
+ let (_, nodes) = create_nodes(2, "test_persist_network_graph_error");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir).with_graph_error(std::io::ErrorKind::Other, "test"));
let event_handler = |_: _| {};
#[test]
fn test_scorer_persist_error() {
// Test that if we encounter an error during scorer persistence, an error gets returned.
- let nodes = create_nodes(2, "test_persist_scorer_error".to_string());
+ let (_, nodes) = create_nodes(2, "test_persist_scorer_error");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir).with_scorer_error(std::io::ErrorKind::Other, "test"));
let event_handler = |_: _| {};
#[test]
fn test_background_event_handling() {
- let mut nodes = create_nodes(2, "test_background_event_handling".to_string());
+ let (_, mut nodes) = create_nodes(2, "test_background_event_handling");
let channel_value = 100000;
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir.clone()));
#[test]
fn test_scorer_persistence() {
- let nodes = create_nodes(2, "test_scorer_persistence".to_string());
+ let (_, nodes) = create_nodes(2, "test_scorer_persistence");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir));
let event_handler = |_: _| {};
fn test_not_pruning_network_graph_until_graph_sync_completion() {
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
- let nodes = create_nodes(2, "test_not_pruning_network_graph_until_graph_sync_completion".to_string());
+ let (_, nodes) = create_nodes(2, "test_not_pruning_network_graph_until_graph_sync_completion");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir).with_graph_persistence_notifier(sender));
async fn test_not_pruning_network_graph_until_graph_sync_completion_async() {
let (sender, receiver) = std::sync::mpsc::sync_channel(1);
- let nodes = create_nodes(2, "test_not_pruning_network_graph_until_graph_sync_completion_async".to_string());
+ let (_, nodes) = create_nodes(2, "test_not_pruning_network_graph_until_graph_sync_completion_async");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir).with_graph_persistence_notifier(sender));
let node_1_privkey = SecretKey::from_slice(&[42; 32]).unwrap();
let node_1_id = PublicKey::from_secret_key(&secp_ctx, &node_1_privkey);
- let path = vec![RouteHop {
+ let path = Path { hops: vec![RouteHop {
pubkey: node_1_id,
node_features: NodeFeatures::empty(),
short_channel_id: scored_scid,
channel_features: ChannelFeatures::empty(),
fee_msat: 0,
cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA as u32,
- }];
+ }], blinded_tail: None };
$nodes[0].scorer.lock().unwrap().expect(TestResult::PaymentFailure { path: path.clone(), short_channel_id: scored_scid });
$nodes[0].node.push_pending_event(Event::PaymentPathFailed {
_ => panic!("Unexpected event: {:?}", event),
};
- let nodes = create_nodes(1, "test_payment_path_scoring".to_string());
+ let (_, nodes) = create_nodes(1, "test_payment_path_scoring");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir));
let bg_processor = BackgroundProcessor::start(persister, event_handler, nodes[0].chain_monitor.clone(), nodes[0].node.clone(), nodes[0].no_gossip_sync(), nodes[0].peer_manager.clone(), nodes[0].logger.clone(), Some(nodes[0].scorer.clone()));
}
};
- let nodes = create_nodes(1, "test_payment_path_scoring_async".to_string());
+ let (_, nodes) = create_nodes(1, "test_payment_path_scoring_async");
let data_dir = nodes[0].persister.get_data_dir();
let persister = Arc::new(Persister::new(data_dir));
[package]
name = "lightning-block-sync"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Jeffrey Czyz", "Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "http://github.com/lightningdevkit/rust-lightning"
[dependencies]
bitcoin = "0.29.0"
-lightning = { version = "0.0.114", path = "../lightning" }
+lightning = { version = "0.0.115", path = "../lightning" }
tokio = { version = "1.0", features = [ "io-util", "net", "time" ], optional = true }
serde_json = { version = "1.0", optional = true }
chunked_transfer = { version = "1.4", optional = true }
[dev-dependencies]
-lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
tokio = { version = "1.14", features = [ "macros", "rt" ] }
[package]
name = "lightning-custom-message"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Jeffrey Czyz"]
license = "MIT OR Apache-2.0"
repository = "http://github.com/lightningdevkit/rust-lightning"
[dependencies]
bitcoin = "0.29.0"
-lightning = { version = "0.0.114", path = "../lightning" }
+lightning = { version = "0.0.115", path = "../lightning" }
[package]
name = "lightning-invoice"
description = "Data structures to parse and serialize BOLT11 lightning invoices"
-version = "0.22.0"
+version = "0.23.0"
authors = ["Sebastian Geisler <sgeisler@wh2.tu-dresden.de>"]
documentation = "https://docs.rs/lightning-invoice/"
license = "MIT OR Apache-2.0"
[dependencies]
bech32 = { version = "0.9.0", default-features = false }
-lightning = { version = "0.0.114", path = "../lightning", default-features = false }
+lightning = { version = "0.0.115", path = "../lightning", default-features = false }
secp256k1 = { version = "0.24.0", default-features = false, features = ["recovery", "alloc"] }
num-traits = { version = "0.2.8", default-features = false }
bitcoin_hashes = { version = "0.11", default-features = false }
bitcoin = { version = "0.29.0", default-features = false }
[dev-dependencies]
-lightning = { version = "0.0.114", path = "../lightning", default-features = false, features = ["_test_utils"] }
+lightning = { version = "0.0.115", path = "../lightning", default-features = false, features = ["_test_utils"] }
hex = "0.4"
serde_json = { version = "1"}
pub struct Sha256(/// This is not exported to bindings users as the native hash types are not currently mapped
pub sha256::Hash);
+impl Sha256 {
+ /// Constructs a new [`Sha256`] from the given bytes, which are assumed to be the output of a
+ /// single sha256 hash.
+ #[cfg(c_bindings)]
+ pub fn from_bytes(bytes: &[u8; 32]) -> Self {
+ Self(sha256::Hash::from_slice(bytes).expect("from_slice only fails if len is not 32"))
+ }
+}
+
/// Description string
///
/// # Invariants
/// participating node
/// * It is fine to cache `phantom_route_hints` and reuse it across invoices, as long as the data is
/// updated when a channel becomes disabled or closes
-/// * Note that if too many channels are included in [`PhantomRouteHints::channels`], the invoice
-/// may be too long for QR code scanning. To fix this, `PhantomRouteHints::channels` may be pared
-/// down
+/// * Note that the route hints generated from `phantom_route_hints` will be limited to a maximum
+/// of 3 hints to ensure that the invoice can be scanned in a QR code. These hints are selected
+/// in the order that the nodes in `PhantomRouteHints` are specified, selecting one hint per node
+/// until the maximum is hit. Callers may provide as many `PhantomRouteHints::channels` as
+/// desired, but note that some nodes will be trimmed if more than 3 nodes are provided.
///
/// `description_hash` is a SHA-256 hash of the description text
///
invoice = invoice.amount_milli_satoshis(amt);
}
+ for route_hint in select_phantom_hints(amt_msat, phantom_route_hints, logger) {
+ invoice = invoice.private_route(route_hint);
+ }
+
+ let raw_invoice = match invoice.build_raw() {
+ Ok(inv) => inv,
+ Err(e) => return Err(SignOrCreationError::CreationError(e))
+ };
+ let hrp_str = raw_invoice.hrp.to_string();
+ let hrp_bytes = hrp_str.as_bytes();
+ let data_without_signature = raw_invoice.data.to_base32();
+ let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
+ match signed_raw_invoice {
+ Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
+ Err(e) => Err(SignOrCreationError::SignError(e))
+ }
+}
+
+/// Utility to select route hints for phantom invoices.
+/// See [`PhantomKeysManager`] for more information on phantom node payments.
+///
+/// To ensure that the phantom invoice is still readable by QR code, we limit to 3 hints per invoice:
+/// * Select up to three channels per node.
+/// * Select one hint from each node, up to three hints or until we run out of hints.
+///
+/// [`PhantomKeysManager`]: lightning::chain::keysinterface::PhantomKeysManager
+fn select_phantom_hints<L: Deref>(amt_msat: Option<u64>, phantom_route_hints: Vec<PhantomRouteHints>,
+ logger: L) -> Vec<RouteHint>
+where
+ L::Target: Logger,
+{
+ let mut phantom_hints: Vec<Vec<RouteHint>> = Vec::new();
+
for PhantomRouteHints { channels, phantom_scid, real_node_pubkey } in phantom_route_hints {
log_trace!(logger, "Generating phantom route hints for node {}",
log_pubkey!(real_node_pubkey));
- let mut route_hints = filter_channels(channels, amt_msat, &logger);
+ let mut route_hints = sort_and_filter_channels(channels, amt_msat, &logger);
- // If we have any public channel, the route hints from `filter_channels` will be empty.
- // In that case we create a RouteHint on which we will push a single hop with the phantom
- // route into the invoice, and let the sender find the path to the `real_node_pubkey`
+ // If we have any public channel, the route hints from `sort_and_filter_channels` will be
+ // empty. In that case we create a RouteHint on which we will push a single hop with the
+ // phantom route into the invoice, and let the sender find the path to the `real_node_pubkey`
// node by looking at our public channels.
if route_hints.is_empty() {
route_hints.push(RouteHint(vec![]))
}
- for mut route_hint in route_hints {
+ for route_hint in &mut route_hints {
route_hint.0.push(RouteHintHop {
src_node_id: real_node_pubkey,
short_channel_id: phantom_scid,
cltv_expiry_delta: MIN_CLTV_EXPIRY_DELTA,
htlc_minimum_msat: None,
htlc_maximum_msat: None,});
- invoice = invoice.private_route(route_hint.clone());
}
+
+ phantom_hints.push(route_hints);
}
- let raw_invoice = match invoice.build_raw() {
- Ok(inv) => inv,
- Err(e) => return Err(SignOrCreationError::CreationError(e))
- };
- let hrp_str = raw_invoice.hrp.to_string();
- let hrp_bytes = hrp_str.as_bytes();
- let data_without_signature = raw_invoice.data.to_base32();
- let signed_raw_invoice = raw_invoice.sign(|_| node_signer.sign_invoice(hrp_bytes, &data_without_signature, Recipient::PhantomNode));
- match signed_raw_invoice {
- Ok(inv) => Ok(Invoice::from_signed(inv).unwrap()),
- Err(e) => Err(SignOrCreationError::SignError(e))
+ // We have one vector per real node involved in creating the phantom invoice. To distribute
+ // the hints across our real nodes we add one hint from each in turn until no node has any hints
+ // left (if one node has more hints than any other, these will accumulate at the end of the
+ // vector).
+ let mut invoice_hints: Vec<RouteHint> = Vec::new();
+ let mut hint_idx = 0;
+
+ loop {
+ let mut remaining_hints = false;
+
+ for hints in phantom_hints.iter() {
+ if invoice_hints.len() == 3 {
+ return invoice_hints
+ }
+
+ if hint_idx < hints.len() {
+ invoice_hints.push(hints[hint_idx].clone());
+ remaining_hints = true
+ }
+ }
+
+ if !remaining_hints {
+ return invoice_hints
+ }
+
+ hint_idx +=1;
}
}
invoice = invoice.amount_milli_satoshis(amt);
}
- let route_hints = filter_channels(channels, amt_msat, &logger);
+ let route_hints = sort_and_filter_channels(channels, amt_msat, &logger);
for hint in route_hints {
invoice = invoice.private_route(hint);
}
}
}
-/// Filters the `channels` for an invoice, and returns the corresponding `RouteHint`s to include
+/// Sorts and filters the `channels` for an invoice, and returns the corresponding `RouteHint`s to include
/// in the invoice.
///
/// The filtering is based on the following criteria:
/// * Only one channel per counterparty node
-/// * Always select the channel with the highest inbound capacity per counterparty node
+/// * If the counterparty has a channel that is above the `min_inbound_capacity_msat` + 10% scaling
+/// factor (to allow some margin for change in inbound), select the channel with the lowest
+/// inbound capacity that is above this threshold.
+/// * If no `min_inbound_capacity_msat` is specified, or the counterparty has no channels above the
+/// minimum + 10% scaling factor, select the channel with the highest inbound capacity per counterparty.
/// * Prefer channels with capacity at least `min_inbound_capacity_msat` and where the channel
/// `is_usable` (i.e. the peer is connected).
/// * If any public channel exists, only public [`RouteHint`]s will be returned.
/// * If any public, announced, channel exists (i.e. a channel with 7+ confs, to ensure the
/// announcement has had a chance to propagate), no [`RouteHint`]s will be returned, as the
/// sender is expected to find the path by looking at the public channels instead.
-fn filter_channels<L: Deref>(
+/// * Limited to a total of 3 channels.
+/// * Sorted by lowest inbound capacity if an online channel with the minimum amount requested exists,
+/// otherwise sort by highest inbound capacity to give the payment the best chance of succeeding.
+fn sort_and_filter_channels<L: Deref>(
channels: Vec<ChannelDetails>, min_inbound_capacity_msat: Option<u64>, logger: &L
) -> Vec<RouteHint> where L::Target: Logger {
let mut filtered_channels: HashMap<PublicKey, ChannelDetails> = HashMap::new();
// If this channel is public and the previous channel is not, ensure we replace the
// previous channel to avoid announcing non-public channels.
let new_now_public = channel.is_public && !entry.get().is_public;
+ // Decide whether we prefer the currently selected channel with the node to the new one,
+ // based on their inbound capacity.
+ let prefer_current = prefer_current_channel(min_inbound_capacity_msat, current_max_capacity,
+ channel.inbound_capacity_msat);
// If the public-ness of the channel has not changed (in which case simply defer to
- // `new_now_public), and this channel has a greater capacity, prefer to announce
- // this channel.
- let new_higher_capacity = channel.is_public == entry.get().is_public &&
- channel.inbound_capacity_msat > current_max_capacity;
- if new_now_public || new_higher_capacity {
+ // `new_now_public), and this channel has more desirable inbound than the incumbent,
+ // prefer to include this channel.
+ let new_channel_preferable = channel.is_public == entry.get().is_public && !prefer_current;
+
+ if new_now_public || new_channel_preferable {
log_trace!(logger,
"Preferring counterparty {} channel {} (SCID {:?}, {} msats) over {} (SCID {:?}, {} msats) for invoice route hints",
log_pubkey!(channel.counterparty.node_id),
// the payment value and where we're currently connected to the channel counterparty.
// Even if we cannot satisfy both goals, always ensure we include *some* hints, preferring
// those which meet at least one criteria.
- filtered_channels
+ let mut eligible_channels = filtered_channels
.into_iter()
.map(|(_, channel)| channel)
.filter(|channel| {
include_channel
})
- .map(route_hint_from_channel)
- .collect::<Vec<RouteHint>>()
+ .collect::<Vec<ChannelDetails>>();
+
+ eligible_channels.sort_unstable_by(|a, b| {
+ if online_min_capacity_channel_exists {
+ a.inbound_capacity_msat.cmp(&b.inbound_capacity_msat)
+ } else {
+ b.inbound_capacity_msat.cmp(&a.inbound_capacity_msat)
+ }});
+ eligible_channels.into_iter().take(3).map(route_hint_from_channel).collect::<Vec<RouteHint>>()
+}
+
+/// prefer_current_channel chooses a channel to use for route hints between a currently selected and candidate
+/// channel based on the inbound capacity of each channel and the minimum inbound capacity requested for the hints,
+/// returning true if the current channel should be preferred over the candidate channel.
+/// * If no minimum amount is requested, the channel with the most inbound is chosen to maximize the chances that a
+/// payment of any size will succeed.
+/// * If we have channels with inbound above our minimum requested inbound (plus a 10% scaling factor, expressed as a
+/// percentage) then we choose the lowest inbound channel with above this amount. If we have sufficient inbound
+/// channels, we don't want to deplete our larger channels with small payments (the off-chain version of "grinding
+/// our change").
+/// * If no channel above our minimum amount exists, then we just prefer the channel with the most inbound to give
+/// payments the best chance of succeeding in multiple parts.
+fn prefer_current_channel(min_inbound_capacity_msat: Option<u64>, current_channel: u64,
+ candidate_channel: u64) -> bool {
+
+ // If no min amount is given for the hints, err of the side of caution and choose the largest channel inbound to
+ // maximize chances of any payment succeeding.
+ if min_inbound_capacity_msat.is_none() {
+ return current_channel > candidate_channel
+ }
+
+ let scaled_min_inbound = min_inbound_capacity_msat.unwrap() * 110;
+ let current_sufficient = current_channel * 100 >= scaled_min_inbound;
+ let candidate_sufficient = candidate_channel * 100 >= scaled_min_inbound;
+
+ if current_sufficient && candidate_sufficient {
+ return current_channel < candidate_channel
+ } else if current_sufficient {
+ return true
+ } else if candidate_sufficient {
+ return false
+ }
+
+ current_channel > candidate_channel
}
#[cfg(test)]
use crate::utils::create_invoice_from_channelmanager_and_duration_since_epoch;
use std::collections::HashSet;
+ #[test]
+ fn test_prefer_current_channel() {
+ // No minimum, prefer larger candidate channel.
+ assert_eq!(crate::utils::prefer_current_channel(None, 100, 200), false);
+
+ // No minimum, prefer larger current channel.
+ assert_eq!(crate::utils::prefer_current_channel(None, 200, 100), true);
+
+ // Minimum set, prefer current channel over minimum + buffer.
+ assert_eq!(crate::utils::prefer_current_channel(Some(100), 115, 100), true);
+
+ // Minimum set, prefer candidate channel over minimum + buffer.
+ assert_eq!(crate::utils::prefer_current_channel(Some(100), 105, 125), false);
+
+ // Minimum set, both channels sufficient, prefer smaller current channel.
+ assert_eq!(crate::utils::prefer_current_channel(Some(100), 115, 125), true);
+
+ // Minimum set, both channels sufficient, prefer smaller candidate channel.
+ assert_eq!(crate::utils::prefer_current_channel(Some(100), 200, 160), false);
+
+ // Minimum set, neither sufficient, prefer larger current channel.
+ assert_eq!(crate::utils::prefer_current_channel(Some(200), 100, 50), true);
+
+ // Minimum set, neither sufficient, prefer larger candidate channel.
+ assert_eq!(crate::utils::prefer_current_channel(Some(200), 100, 150), false);
+ }
+
+
#[test]
fn test_from_channelmanager() {
let chanmon_cfgs = create_chanmon_cfgs(2);
}
#[test]
- fn test_hints_has_only_highest_inbound_capacity_channel() {
+ fn test_hints_has_only_lowest_inbound_capacity_channel_above_minimum() {
let chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
- let _chan_1_0_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0);
- let chan_1_0_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000_000, 0);
- let _chan_1_0_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 1_000_000, 0);
+
+ let _chan_1_0_inbound_below_amt = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 10_000, 0);
+ let _chan_1_0_large_inbound_above_amt = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 500_000, 0);
+ let chan_1_0_low_inbound_above_amt = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 200_000, 0);
+
let mut scid_aliases = HashSet::new();
- scid_aliases.insert(chan_1_0_high_inbound_capacity.0.short_channel_id_alias.unwrap());
- match_invoice_routes(Some(5000), &nodes[0], scid_aliases);
+ scid_aliases.insert(chan_1_0_low_inbound_above_amt.0.short_channel_id_alias.unwrap());
+ match_invoice_routes(Some(100_000_000), &nodes[0], scid_aliases);
}
#[test]
match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases);
}
+ #[test]
+ fn test_insufficient_inbound_sort_by_highest_capacity() {
+ let chanmon_cfgs = create_chanmon_cfgs(5);
+ let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
+ let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
+ let _chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0);
+ let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 200_000, 0);
+ let chan_3_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 3, 0, 300_000, 0);
+ let chan_4_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 4, 0, 400_000, 0);
+
+ // When no single channel has enough inbound capacity for the payment, we expect the three
+ // highest inbound channels to be chosen.
+ let mut scid_aliases = HashSet::new();
+ scid_aliases.insert(chan_2_0.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_3_0.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_4_0.0.short_channel_id_alias.unwrap());
+
+ match_invoice_routes(Some(1_000_000_000), &nodes[0], scid_aliases.clone());
+ }
+
+ #[test]
+ fn test_sufficient_inbound_sort_by_lowest_capacity() {
+ let chanmon_cfgs = create_chanmon_cfgs(5);
+ let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
+ let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
+ let chan_1_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 0, 100_000, 0);
+ let chan_2_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 0, 200_000, 0);
+ let chan_3_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 3, 0, 300_000, 0);
+ let _chan_4_0 = create_unannounced_chan_between_nodes_with_value(&nodes, 4, 0, 400_000, 0);
+
+ // When we have channels that have sufficient inbound for the payment, test that we sort
+ // by lowest inbound capacity.
+ let mut scid_aliases = HashSet::new();
+ scid_aliases.insert(chan_1_0.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_2_0.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_3_0.0.short_channel_id_alias.unwrap());
+
+ match_invoice_routes(Some(50_000_000), &nodes[0], scid_aliases.clone());
+ }
+
#[test]
fn test_forwarding_info_not_assigned_channel_excluded_from_hints() {
let chanmon_cfgs = create_chanmon_cfgs(3);
#[test]
#[cfg(feature = "std")]
- fn test_multi_node_hints_has_only_highest_inbound_capacity_channel() {
+ fn test_multi_node_hints_has_only_lowest_inbound_channel_above_minimum() {
let mut chanmon_cfgs = create_chanmon_cfgs(3);
let seed_1 = [42u8; 32];
let seed_2 = [43u8; 32];
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, None, None]);
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
- let _chan_0_1_low_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
- let chan_0_1_high_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0);
- let _chan_0_1_medium_inbound_capacity = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
+ let _chan_0_1_below_amt = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 100_000, 0);
+ let _chan_0_1_above_amt_high_inbound = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 500_000, 0);
+ let chan_0_1_above_amt_low_inbound = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 180_000, 0);
let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 100000, 10001);
let mut scid_aliases = HashSet::new();
- scid_aliases.insert(chan_0_1_high_inbound_capacity.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_1_above_amt_low_inbound.0.short_channel_id_alias.unwrap());
scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
match_multi_node_invoice_routes(
- Some(10_000),
+ Some(100_000_000),
&nodes[1],
vec![&nodes[1], &nodes[2],],
scid_aliases,
);
}
- #[cfg(feature = "std")]
+ #[test]
+ fn test_multi_node_hints_limited_to_3() {
+ let mut chanmon_cfgs = create_chanmon_cfgs(6);
+ let seed_1 = [42 as u8; 32];
+ let seed_2 = [43 as u8; 32];
+ let seed_3 = [44 as u8; 32];
+ let seed_4 = [45 as u8; 32];
+ let cross_node_seed = [44 as u8; 32];
+ chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
+ chanmon_cfgs[3].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
+ chanmon_cfgs[4].keys_manager.backing = PhantomKeysManager::new(&seed_3, 43, 44, &cross_node_seed);
+ chanmon_cfgs[5].keys_manager.backing = PhantomKeysManager::new(&seed_4, 43, 44, &cross_node_seed);
+ let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
+ let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
+
+ // Setup each phantom node with two channels from distinct peers.
+ let chan_0_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 2, 10_000, 0);
+ let chan_1_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 20_000, 0);
+ let chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 20_000, 0);
+ let _chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 10_000, 0);
+ let chan_0_4 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 4, 20_000, 0);
+ let _chan_1_4 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000, 0);
+ let _chan_0_5 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 5, 20_000, 0);
+ let _chan_1_5 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000, 0);
+
+ // Set invoice amount > all channels inbound so that every one is eligible for inclusion
+ // and hints will be sorted by largest inbound capacity.
+ let invoice_amt = Some(100_000_000);
+
+ // With 4 phantom nodes, assert that we include 1 hint per node, up to 3 nodes.
+ let mut scid_aliases = HashSet::new();
+ scid_aliases.insert(chan_1_2.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_4.0.short_channel_id_alias.unwrap());
+
+ match_multi_node_invoice_routes(
+ invoice_amt,
+ &nodes[3],
+ vec![&nodes[2], &nodes[3], &nodes[4], &nodes[5]],
+ scid_aliases,
+ false,
+ );
+
+ // With 2 phantom nodes, assert that we include no more than 3 hints.
+ let mut scid_aliases = HashSet::new();
+ scid_aliases.insert(chan_1_2.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_3.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_2.0.short_channel_id_alias.unwrap());
+
+ match_multi_node_invoice_routes(
+ invoice_amt,
+ &nodes[3],
+ vec![&nodes[2], &nodes[3]],
+ scid_aliases,
+ false,
+ );
+ }
+
+ #[test]
+ fn test_multi_node_hints_at_least_3() {
+ let mut chanmon_cfgs = create_chanmon_cfgs(5);
+ let seed_1 = [42 as u8; 32];
+ let seed_2 = [43 as u8; 32];
+ let cross_node_seed = [44 as u8; 32];
+ chanmon_cfgs[1].keys_manager.backing = PhantomKeysManager::new(&seed_1, 43, 44, &cross_node_seed);
+ chanmon_cfgs[2].keys_manager.backing = PhantomKeysManager::new(&seed_2, 43, 44, &cross_node_seed);
+ let node_cfgs = create_node_cfgs(5, &chanmon_cfgs);
+ let node_chanmgrs = create_node_chanmgrs(5, &node_cfgs, &[None, None, None, None, None]);
+ let nodes = create_network(5, &node_cfgs, &node_chanmgrs);
+
+ let _chan_0_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 3, 10_000, 0);
+ let chan_1_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 3, 20_000, 0);
+ let chan_2_3 = create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 30_000, 0);
+ let chan_0_4 = create_unannounced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000, 0);
+
+ // Since the invoice amount is above all channels inbound, all four are eligible. Test that
+ // we still include 3 hints from 2 distinct nodes sorted by inbound.
+ let mut scid_aliases = HashSet::new();
+ scid_aliases.insert(chan_1_3.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_2_3.0.short_channel_id_alias.unwrap());
+ scid_aliases.insert(chan_0_4.0.short_channel_id_alias.unwrap());
+
+ match_multi_node_invoice_routes(
+ Some(100_000_000),
+ &nodes[3],
+ vec![&nodes[3], &nodes[4],],
+ scid_aliases,
+ false,
+ );
+ }
+
fn match_multi_node_invoice_routes<'a, 'b: 'a, 'c: 'b>(
invoice_amt: Option<u64>,
invoice_node: &Node<'a, 'b, 'c>,
[package]
name = "lightning-net-tokio"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning/"
[dependencies]
bitcoin = "0.29.0"
-lightning = { version = "0.0.114", path = "../lightning" }
+lightning = { version = "0.0.115", path = "../lightning" }
tokio = { version = "1.0", features = [ "io-util", "macros", "rt", "sync", "net", "time" ] }
[dev-dependencies]
tokio = { version = "1.14", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
-lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
[package]
name = "lightning-persister"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Valentine Wallace", "Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning/"
[dependencies]
bitcoin = "0.29.0"
-lightning = { version = "0.0.114", path = "../lightning" }
+lightning = { version = "0.0.115", path = "../lightning" }
libc = "0.2"
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winbase"] }
[dev-dependencies]
-lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
[package]
name = "lightning-rapid-gossip-sync"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Arik Sosman <git@arik.io>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning"
_bench_unstable = []
[dependencies]
-lightning = { version = "0.0.114", path = "../lightning", default-features = false }
+lightning = { version = "0.0.115", path = "../lightning", default-features = false }
bitcoin = { version = "0.29.0", default-features = false }
[dev-dependencies]
-lightning = { version = "0.0.114", path = "../lightning", features = ["_test_utils"] }
+lightning = { version = "0.0.115", path = "../lightning", features = ["_test_utils"] }
[package]
name = "lightning-transaction-sync"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Elias Rohrer"]
license = "MIT OR Apache-2.0"
repository = "http://github.com/lightningdevkit/rust-lightning"
async-interface = []
[dependencies]
-lightning = { version = "0.0.114", path = "../lightning", default-features = false }
+lightning = { version = "0.0.115", path = "../lightning", default-features = false }
bitcoin = { version = "0.29.0", default-features = false }
bdk-macros = "0.6"
futures = { version = "0.3", optional = true }
reqwest = { version = "0.11", optional = true, default-features = false, features = ["json"] }
[dev-dependencies]
-lightning = { version = "0.0.114", path = "../lightning", features = ["std"] }
+lightning = { version = "0.0.115", path = "../lightning", features = ["std"] }
electrsd = { version = "0.22.0", features = ["legacy", "esplora_a33e97e1", "bitcoind_23_0"] }
electrum-client = "0.12.0"
tokio = { version = "1.14.0", features = ["full"] }
[package]
name = "lightning"
-version = "0.0.114"
+version = "0.0.115"
authors = ["Matt Corallo"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/lightningdevkit/rust-lightning/"
--- /dev/null
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Creating blinded paths and related utilities live here.
+
+pub(crate) mod utils;
+
+use bitcoin::hashes::{Hash, HashEngine};
+use bitcoin::hashes::sha256::Hash as Sha256;
+use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
+
+use crate::chain::keysinterface::{EntropySource, NodeSigner, Recipient};
+use crate::onion_message::ControlTlvs;
+use crate::ln::msgs::DecodeError;
+use crate::ln::onion_utils;
+use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
+use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, VecWriter, Writeable, Writer};
+
+use core::mem;
+use core::ops::Deref;
+use crate::io::{self, Cursor};
+use crate::prelude::*;
+
+/// Onion messages and payments can be sent and received to blinded paths, which serve to hide the
+/// identity of the recipient.
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+pub struct BlindedPath {
+ /// To send to a blinded path, the sender first finds a route to the unblinded
+ /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
+ /// message or payment's next hop and forward it along.
+ ///
+ /// [`encrypted_payload`]: BlindedHop::encrypted_payload
+ pub(crate) introduction_node_id: PublicKey,
+ /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
+ /// message or payment.
+ ///
+ /// [`encrypted_payload`]: BlindedHop::encrypted_payload
+ pub(crate) blinding_point: PublicKey,
+ /// The hops composing the blinded path.
+ pub(crate) blinded_hops: Vec<BlindedHop>,
+}
+
+/// Used to construct the blinded hops portion of a blinded path. These hops cannot be identified
+/// by outside observers and thus can be used to hide the identity of the recipient.
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+pub struct BlindedHop {
+ /// The blinded node id of this hop in a blinded path.
+ pub(crate) blinded_node_id: PublicKey,
+ /// The encrypted payload intended for this hop in a blinded path.
+ // The node sending to this blinded path will later encode this payload into the onion packet for
+ // this hop.
+ pub(crate) encrypted_payload: Vec<u8>,
+}
+
+impl BlindedPath {
+ /// Create a blinded path for an onion message, to be forwarded along `node_pks`. The last node
+ /// pubkey in `node_pks` will be the destination node.
+ ///
+ /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
+ // TODO: make all payloads the same size with padding + add dummy hops
+ pub fn new_for_message<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
+ (node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
+ {
+ if node_pks.len() < 2 { return Err(()) }
+ let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
+ let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
+ let introduction_node_id = node_pks[0];
+
+ Ok(BlindedPath {
+ introduction_node_id,
+ blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
+ blinded_hops: blinded_message_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
+ })
+ }
+
+ // Advance the blinded onion message path by one hop, so make the second hop into the new
+ // introduction node.
+ pub(super) fn advance_message_path_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>
+ (&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()>
+ where NS::Target: NodeSigner
+ {
+ let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.blinding_point, None)?;
+ let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
+ let encrypted_control_tlvs = self.blinded_hops.remove(0).encrypted_payload;
+ let mut s = Cursor::new(&encrypted_control_tlvs);
+ let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
+ match ChaChaPolyReadAdapter::read(&mut reader, rho) {
+ Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
+ mut next_node_id, next_blinding_override,
+ })}) => {
+ let mut new_blinding_point = match next_blinding_override {
+ Some(blinding_point) => blinding_point,
+ None => {
+ let blinding_factor = {
+ let mut sha = Sha256::engine();
+ sha.input(&self.blinding_point.serialize()[..]);
+ sha.input(control_tlvs_ss.as_ref());
+ Sha256::from_engine(sha).into_inner()
+ };
+ self.blinding_point.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
+ .map_err(|_| ())?
+ }
+ };
+ mem::swap(&mut self.blinding_point, &mut new_blinding_point);
+ mem::swap(&mut self.introduction_node_id, &mut next_node_id);
+ Ok(())
+ },
+ _ => Err(())
+ }
+ }
+}
+
+/// Construct blinded onion message hops for the given `unblinded_path`.
+fn blinded_message_hops<T: secp256k1::Signing + secp256k1::Verification>(
+ secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
+) -> Result<Vec<BlindedHop>, secp256k1::Error> {
+ let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
+
+ let mut prev_ss_and_blinded_node_id = None;
+ utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
+ if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
+ if let Some(pk) = unblinded_pk {
+ let payload = ForwardTlvs {
+ next_node_id: pk,
+ next_blinding_override: None,
+ };
+ blinded_hops.push(BlindedHop {
+ blinded_node_id: prev_blinded_node_id,
+ encrypted_payload: encrypt_payload(payload, prev_ss),
+ });
+ } else { debug_assert!(false); }
+ }
+ prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
+ })?;
+
+ if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
+ let final_payload = ReceiveTlvs { path_id: None };
+ blinded_hops.push(BlindedHop {
+ blinded_node_id: final_blinded_node_id,
+ encrypted_payload: encrypt_payload(final_payload, final_ss),
+ });
+ } else { debug_assert!(false) }
+
+ Ok(blinded_hops)
+}
+
+/// Encrypt TLV payload to be used as a [`BlindedHop::encrypted_payload`].
+fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
+ let mut writer = VecWriter(Vec::new());
+ let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload);
+ write_adapter.write(&mut writer).expect("In-memory writes cannot fail");
+ writer.0
+}
+
+impl Writeable for BlindedPath {
+ fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+ self.introduction_node_id.write(w)?;
+ self.blinding_point.write(w)?;
+ (self.blinded_hops.len() as u8).write(w)?;
+ for hop in &self.blinded_hops {
+ hop.write(w)?;
+ }
+ Ok(())
+ }
+}
+
+impl Readable for BlindedPath {
+ fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
+ let introduction_node_id = Readable::read(r)?;
+ let blinding_point = Readable::read(r)?;
+ let num_hops: u8 = Readable::read(r)?;
+ if num_hops == 0 { return Err(DecodeError::InvalidValue) }
+ let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
+ for _ in 0..num_hops {
+ blinded_hops.push(Readable::read(r)?);
+ }
+ Ok(BlindedPath {
+ introduction_node_id,
+ blinding_point,
+ blinded_hops,
+ })
+ }
+}
+
+impl_writeable!(BlindedHop, {
+ blinded_node_id,
+ encrypted_payload
+});
+
+/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
+/// route, they are encoded into [`BlindedHop::encrypted_payload`].
+pub(crate) struct ForwardTlvs {
+ /// The node id of the next hop in the onion message's path.
+ pub(super) next_node_id: PublicKey,
+ /// Senders to a blinded path use this value to concatenate the route they find to the
+ /// introduction node with the blinded path.
+ pub(super) next_blinding_override: Option<PublicKey>,
+}
+
+/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
+pub(crate) struct ReceiveTlvs {
+ /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
+ /// sending to. This is useful for receivers to check that said blinded path is being used in
+ /// the right context.
+ pub(super) path_id: Option<[u8; 32]>,
+}
+
+impl Writeable for ForwardTlvs {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ // TODO: write padding
+ encode_tlv_stream!(writer, {
+ (4, self.next_node_id, required),
+ (8, self.next_blinding_override, option)
+ });
+ Ok(())
+ }
+}
+
+impl Writeable for ReceiveTlvs {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ // TODO: write padding
+ encode_tlv_stream!(writer, {
+ (6, self.path_id, option),
+ });
+ Ok(())
+ }
+}
--- /dev/null
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
+//! Onion message utility methods live here.
+
+use bitcoin::hashes::{Hash, HashEngine};
+use bitcoin::hashes::hmac::{Hmac, HmacEngine};
+use bitcoin::hashes::sha256::Hash as Sha256;
+use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar};
+use bitcoin::secp256k1::ecdh::SharedSecret;
+
+use super::BlindedPath;
+use crate::ln::onion_utils;
+use crate::onion_message::Destination;
+
+use crate::prelude::*;
+
+// TODO: DRY with onion_utils::construct_onion_keys_callback
+#[inline]
+pub(crate) fn construct_keys_callback<T: secp256k1::Signing + secp256k1::Verification,
+ FType: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>)>(
+ secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], destination: Option<Destination>,
+ session_priv: &SecretKey, mut callback: FType
+) -> Result<(), secp256k1::Error> {
+ let mut msg_blinding_point_priv = session_priv.clone();
+ let mut msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
+ let mut onion_packet_pubkey_priv = msg_blinding_point_priv.clone();
+ let mut onion_packet_pubkey = msg_blinding_point.clone();
+
+ macro_rules! build_keys {
+ ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {{
+ let encrypted_data_ss = SharedSecret::new(&$pk, &msg_blinding_point_priv);
+
+ let blinded_hop_pk = if $blinded { $pk } else {
+ let hop_pk_blinding_factor = {
+ let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
+ hmac.input(encrypted_data_ss.as_ref());
+ Hmac::from_engine(hmac).into_inner()
+ };
+ $pk.mul_tweak(secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
+ };
+ let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);
+
+ let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref());
+ let unblinded_pk_opt = if $blinded { None } else { Some($pk) };
+ callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
+ (encrypted_data_ss, onion_packet_ss)
+ }}
+ }
+
+ macro_rules! build_keys_in_loop {
+ ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {
+ let (encrypted_data_ss, onion_packet_ss) = build_keys!($pk, $blinded, $encrypted_payload);
+
+ let msg_blinding_point_blinding_factor = {
+ let mut sha = Sha256::engine();
+ sha.input(&msg_blinding_point.serialize()[..]);
+ sha.input(encrypted_data_ss.as_ref());
+ Sha256::from_engine(sha).into_inner()
+ };
+
+ msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())?;
+ msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
+
+ let onion_packet_pubkey_blinding_factor = {
+ let mut sha = Sha256::engine();
+ sha.input(&onion_packet_pubkey.serialize()[..]);
+ sha.input(onion_packet_ss.as_ref());
+ Sha256::from_engine(sha).into_inner()
+ };
+ onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())?;
+ onion_packet_pubkey = PublicKey::from_secret_key(secp_ctx, &onion_packet_pubkey_priv);
+ };
+ }
+
+ for pk in unblinded_path {
+ build_keys_in_loop!(*pk, false, None);
+ }
+ if let Some(dest) = destination {
+ match dest {
+ Destination::Node(pk) => {
+ build_keys!(pk, false, None);
+ },
+ Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => {
+ for hop in blinded_hops {
+ build_keys_in_loop!(hop.blinded_node_id, true, Some(hop.encrypted_payload));
+ }
+ },
+ }
+ }
+ Ok(())
+}
/// The height at which the counterparty may be able to claim the balance if we have not
/// done so.
timeout_height: u32,
+ /// The payment hash that locks this HTLC.
+ payment_hash: PaymentHash,
+ /// The preimage that can be used to claim this HTLC.
+ payment_preimage: PaymentPreimage,
},
/// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
/// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
/// The height at which we will be able to claim the balance if our counterparty has not
/// done so.
claimable_height: u32,
+ /// The payment hash whose preimage our counterparty needs to claim this HTLC.
+ payment_hash: PaymentHash,
},
/// HTLCs which we received from our counterparty which are claimable with a preimage which we
/// do not currently have. This will only be claimable if we receive the preimage from the node
/// The height at which our counterparty will be able to claim the balance if we have not
/// yet received the preimage and claimed it ourselves.
expiry_height: u32,
+ /// The payment hash whose preimage we need to claim this HTLC.
+ payment_hash: PaymentHash,
},
/// The channel has been closed, and our counterparty broadcasted a revoked commitment
/// transaction.
return Some(Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: htlc.amount_msat / 1000,
claimable_height: htlc.cltv_expiry,
+ payment_hash: htlc.payment_hash,
});
}
- } else if self.payment_preimages.get(&htlc.payment_hash).is_some() {
+ } else if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
// Otherwise (the payment was inbound), only expose it as claimable if
// we know the preimage.
// Note that if there is a pending claim, but it did not use the
return Some(Balance::ContentiousClaimable {
claimable_amount_satoshis: htlc.amount_msat / 1000,
timeout_height: htlc.cltv_expiry,
+ payment_hash: htlc.payment_hash,
+ payment_preimage: *payment_preimage,
});
}
} else if htlc_resolved.is_none() {
return Some(Balance::MaybePreimageClaimableHTLC {
claimable_amount_satoshis: htlc.amount_msat / 1000,
expiry_height: htlc.cltv_expiry,
+ payment_hash: htlc.payment_hash,
});
}
None
res.push(Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: htlc.amount_msat / 1000,
claimable_height: htlc.cltv_expiry,
+ payment_hash: htlc.payment_hash,
});
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
res.push(Balance::MaybePreimageClaimableHTLC {
claimable_amount_satoshis: htlc.amount_msat / 1000,
expiry_height: htlc.cltv_expiry,
+ payment_hash: htlc.payment_hash,
});
}
}
replay_update.updates.push(ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage: payment_preimage_1 });
replay_update.updates.push(ChannelMonitorUpdateStep::PaymentPreimage { payment_preimage: payment_preimage_2 });
- let broadcaster = TestBroadcaster::new(Arc::clone(&nodes[1].blocks));
+ let broadcaster = TestBroadcaster::with_blocks(Arc::clone(&nodes[1].blocks));
assert!(
pre_update_monitor.update_monitor(&replay_update, &&broadcaster, &chanmon_cfgs[1].fee_estimator, &nodes[1].logger)
.is_err());
fn test_prune_preimages() {
let secp_ctx = Secp256k1::new();
let logger = Arc::new(TestLogger::new());
- let broadcaster = Arc::new(TestBroadcaster {
- txn_broadcasted: Mutex::new(Vec::new()),
- blocks: Arc::new(Mutex::new(Vec::new()))
- });
+ let broadcaster = Arc::new(TestBroadcaster::new(Network::Testnet));
let fee_estimator = TestFeeEstimator { sat_per_kw: Mutex::new(253) };
let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
preprocessed_requests.push(req);
}
- // Claim everything up to and including cur_height + 1
- let remaining_locked_packages = self.locktimed_packages.split_off(&(cur_height + 2));
+ // Claim everything up to and including `cur_height`
+ let remaining_locked_packages = self.locktimed_packages.split_off(&(cur_height + 1));
for (pop_height, mut entry) in self.locktimed_packages.iter_mut() {
log_trace!(logger, "Restoring delayed claim of package(s) at their timelock at {}.", pop_height);
preprocessed_requests.append(&mut entry);
}
}
for ((_package_id, _), ref mut request) in bump_candidates.iter_mut() {
+ // `height` is the height being disconnected, so our `current_height` is 1 lower.
+ let current_height = height - 1;
if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(
- height, &request, true /* force_feerate_bump */, fee_estimator, &&*logger
+ current_height, &request, true /* force_feerate_bump */, fee_estimator, &&*logger
) {
request.set_timer(new_timer);
request.set_feerate(new_feerate);
}
}
fn absolute_tx_timelock(&self, current_height: u32) -> u32 {
- // We use `current_height + 1` as our default locktime to discourage fee sniping and because
+ // We use `current_height` as our default locktime to discourage fee sniping and because
// transactions with it always propagate.
let absolute_timelock = match self {
- PackageSolvingData::RevokedOutput(_) => current_height + 1,
- PackageSolvingData::RevokedHTLCOutput(_) => current_height + 1,
- PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => current_height + 1,
- PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => cmp::max(outp.htlc.cltv_expiry, current_height + 1),
+ PackageSolvingData::RevokedOutput(_) => current_height,
+ PackageSolvingData::RevokedHTLCOutput(_) => current_height,
+ PackageSolvingData::CounterpartyOfferedHTLCOutput(_) => current_height,
+ PackageSolvingData::CounterpartyReceivedHTLCOutput(ref outp) => cmp::max(outp.htlc.cltv_expiry, current_height),
// HTLC timeout/success transactions rely on a fixed timelock due to the counterparty's
// signature.
PackageSolvingData::HolderHTLCOutput(ref outp) => {
}
outp.cltv_expiry
},
- PackageSolvingData::HolderFundingOutput(_) => current_height + 1,
+ PackageSolvingData::HolderFundingOutput(_) => current_height,
};
absolute_timelock
}
use crate::util::errors::APIError;
use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, RequiredWrapper, UpgradableRequired, WithoutLength};
use crate::util::string::UntrustedString;
-use crate::routing::router::{RouteHop, RouteParameters};
+use crate::routing::router::{BlindedTail, Path, RouteHop, RouteParameters};
use bitcoin::{PackedLockTime, Transaction, OutPoint};
#[cfg(anchors)]
/// The payment path that was successful.
///
/// May contain a closed channel if the HTLC sent along the path was fulfilled on chain.
- path: Vec<RouteHop>,
+ path: Path,
},
/// Indicates an outbound HTLC we sent failed, likely due to an intermediary node being unable to
/// handle the HTLC.
/// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
failure: PathFailure,
/// The payment path that failed.
- path: Vec<RouteHop>,
+ path: Path,
/// The channel responsible for the failed payment path.
///
/// Note that for route hints or for the first hop in a path this may be an SCID alias and
/// [`ChannelManager::send_probe`]: crate::ln::channelmanager::ChannelManager::send_probe
payment_hash: PaymentHash,
/// The payment path that was successful.
- path: Vec<RouteHop>,
+ path: Path,
},
/// Indicates that a probe payment we sent failed at an intermediary node on the path.
ProbeFailed {
/// [`ChannelManager::send_probe`]: crate::ln::channelmanager::ChannelManager::send_probe
payment_hash: PaymentHash,
/// The payment path that failed.
- path: Vec<RouteHop>,
+ path: Path,
/// The channel responsible for the failed probe.
///
/// Note that for route hints or for the first hop in a path this may be an SCID alias and
(1, None::<NetworkUpdate>, option), // network_update in LDK versions prior to 0.0.114
(2, payment_failed_permanently, required),
(3, false, required), // all_paths_failed in LDK versions prior to 0.0.114
- (5, *path, vec_type),
+ (4, path.blinded_tail, option),
+ (5, path.hops, vec_type),
(7, short_channel_id, option),
(9, None::<RouteParameters>, option), // retry in LDK versions prior to 0.0.115
(11, payment_id, option),
write_tlv_fields!(writer, {
(0, payment_id, required),
(2, payment_hash, option),
- (4, *path, vec_type)
+ (4, path.hops, vec_type),
+ (6, path.blinded_tail, option),
})
},
&Event::PaymentFailed { ref payment_id, ref payment_hash, ref reason } => {
write_tlv_fields!(writer, {
(0, payment_id, required),
(2, payment_hash, required),
- (4, *path, vec_type)
+ (4, path.hops, vec_type),
+ (6, path.blinded_tail, option),
})
},
&Event::ProbeFailed { ref payment_id, ref payment_hash, ref path, ref short_channel_id } => {
write_tlv_fields!(writer, {
(0, payment_id, required),
(2, payment_hash, required),
- (4, *path, vec_type),
+ (4, path.hops, vec_type),
(6, short_channel_id, option),
+ (8, path.blinded_tail, option),
})
},
&Event::HTLCHandlingFailed { ref prev_channel_id, ref failed_next_destination } => {
let mut payment_hash = PaymentHash([0; 32]);
let mut payment_failed_permanently = false;
let mut network_update = None;
+ let mut blinded_tail: Option<BlindedTail> = None;
let mut path: Option<Vec<RouteHop>> = Some(vec![]);
let mut short_channel_id = None;
let mut payment_id = None;
(0, payment_hash, required),
(1, network_update, upgradable_option),
(2, payment_failed_permanently, required),
+ (4, blinded_tail, option),
(5, path, vec_type),
(7, short_channel_id, option),
(11, payment_id, option),
payment_hash,
payment_failed_permanently,
failure,
- path: path.unwrap(),
+ path: Path { hops: path.unwrap(), blinded_tail },
short_channel_id,
#[cfg(test)]
error_code,
},
13u8 => {
let f = || {
- let mut payment_id = PaymentId([0; 32]);
- let mut payment_hash = None;
- let mut path: Option<Vec<RouteHop>> = Some(vec![]);
- read_tlv_fields!(reader, {
+ _init_and_read_tlv_fields!(reader, {
(0, payment_id, required),
(2, payment_hash, option),
(4, path, vec_type),
+ (6, blinded_tail, option),
});
Ok(Some(Event::PaymentPathSuccessful {
- payment_id,
+ payment_id: payment_id.0.unwrap(),
payment_hash,
- path: path.unwrap(),
+ path: Path { hops: path.unwrap(), blinded_tail },
}))
};
f()
},
21u8 => {
let f = || {
- let mut payment_id = PaymentId([0; 32]);
- let mut payment_hash = PaymentHash([0; 32]);
- let mut path: Option<Vec<RouteHop>> = Some(vec![]);
- read_tlv_fields!(reader, {
+ _init_and_read_tlv_fields!(reader, {
(0, payment_id, required),
(2, payment_hash, required),
(4, path, vec_type),
+ (6, blinded_tail, option),
});
Ok(Some(Event::ProbeSuccessful {
- payment_id,
- payment_hash,
- path: path.unwrap(),
+ payment_id: payment_id.0.unwrap(),
+ payment_hash: payment_hash.0.unwrap(),
+ path: Path { hops: path.unwrap(), blinded_tail },
}))
};
f()
},
23u8 => {
let f = || {
- let mut payment_id = PaymentId([0; 32]);
- let mut payment_hash = PaymentHash([0; 32]);
- let mut path: Option<Vec<RouteHop>> = Some(vec![]);
- let mut short_channel_id = None;
- read_tlv_fields!(reader, {
+ _init_and_read_tlv_fields!(reader, {
(0, payment_id, required),
(2, payment_hash, required),
(4, path, vec_type),
(6, short_channel_id, option),
+ (8, blinded_tail, option),
});
Ok(Some(Event::ProbeFailed {
- payment_id,
- payment_hash,
- path: path.unwrap(),
+ payment_id: payment_id.0.unwrap(),
+ payment_hash: payment_hash.0.unwrap(),
+ path: Path { hops: path.unwrap(), blinded_tail },
short_channel_id,
}))
};
pub mod offers;
pub mod routing;
pub mod onion_message;
+pub mod blinded_path;
pub mod events;
#[cfg(feature = "std")]
// Set us up to take multiple routes, one 0 -> 1 -> 3 and one 0 -> 2 -> 3:
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_id;
- route.paths[0][1].short_channel_id = chan_3_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_ann.contents.short_channel_id;
- route.paths[1][1].short_channel_id = chan_4_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_ann.contents.short_channel_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_id;
// Set it so that the first monitor update (for the path 0 -> 1 -> 3) succeeds, but the second
// (for the path 0 -> 2 -> 3) fails.
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator, ConfirmationTarget};
use crate::chain::keysinterface::{ChannelSigner, InMemorySigner, EntropySource, SignerProvider};
use crate::chain::transaction::OutPoint;
+ use crate::routing::router::Path;
use crate::util::config::UserConfig;
use crate::util::enforcing_trait_impls::EnforcingSigner;
use crate::util::errors::APIError;
cltv_expiry: 200000000,
state: OutboundHTLCState::Committed,
source: HTLCSource::OutboundRoute {
- path: Vec::new(),
+ path: Path { hops: Vec::new(), blinded_tail: None },
session_priv: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
first_hop_htlc_msat: 548,
payment_id: PaymentId([42; 32]),
#[cfg(any(feature = "_test_utils", test))]
use crate::ln::features::InvoiceFeatures;
use crate::routing::gossip::NetworkGraph;
-use crate::routing::router::{DefaultRouter, InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, RoutePath, Router};
+use crate::routing::router::{BlindedTail, DefaultRouter, InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters, Router};
use crate::routing::scoring::ProbabilisticScorer;
use crate::ln::msgs;
use crate::ln::onion_utils;
pub(crate) enum HTLCSource {
PreviousHopData(HTLCPreviousHopData),
OutboundRoute {
- path: Vec<RouteHop>,
+ path: Path,
session_priv: SecretKey,
/// Technically we can recalculate this from the route, but we cache it here to avoid
/// doing a double-pass on route when we get a failure back
#[cfg(test)]
pub fn dummy() -> Self {
HTLCSource::OutboundRoute {
- path: Vec::new(),
+ path: Path { hops: Vec::new(), blinded_tail: None },
session_priv: SecretKey::from_slice(&[1; 32]).unwrap(),
first_hop_htlc_msat: 0,
payment_id: PaymentId([2; 32]),
}
#[cfg(test)]
- pub(crate) fn test_send_payment_along_path(&self, path: &Vec<RouteHop>, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
+ pub(crate) fn test_send_payment_along_path(&self, path: &Path, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
let _lck = self.total_consistency_lock.read().unwrap();
self.send_payment_along_path(path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv_bytes)
}
- fn send_payment_along_path(&self, path: &Vec<RouteHop>, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
+ fn send_payment_along_path(&self, path: &Path, payment_hash: &PaymentHash, recipient_onion: RecipientOnionFields, total_value: u64, cur_height: u32, payment_id: PaymentId, keysend_preimage: &Option<PaymentPreimage>, session_priv_bytes: [u8; 32]) -> Result<(), APIError> {
// The top-level caller should hold the total_consistency_lock read lock.
debug_assert!(self.total_consistency_lock.try_write().is_err());
- log_trace!(self.logger, "Attempting to send payment for path with next hop {}", path.first().unwrap().short_channel_id);
+ log_trace!(self.logger, "Attempting to send payment for path with next hop {}", path.hops.first().unwrap().short_channel_id);
let prng_seed = self.entropy_source.get_secure_random_bytes();
let session_priv = SecretKey::from_slice(&session_priv_bytes[..]).expect("RNG is busted");
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, payment_hash);
let err: Result<(), _> = loop {
- let (counterparty_node_id, id) = match self.short_to_chan_info.read().unwrap().get(&path.first().unwrap().short_channel_id) {
+ let (counterparty_node_id, id) = match self.short_to_chan_info.read().unwrap().get(&path.hops.first().unwrap().short_channel_id) {
None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
Some((cp_id, chan_id)) => (cp_id.clone(), chan_id.clone()),
};
return Ok(());
};
- match handle_error!(self, err, path.first().unwrap().pubkey) {
+ match handle_error!(self, err, path.hops.first().unwrap().pubkey) {
Ok(_) => unreachable!(),
Err(e) => {
Err(APIError::ChannelUnavailable { err: e.err })
/// Send a payment that is probing the given route for liquidity. We calculate the
/// [`PaymentHash`] of probes based on a static secret and a random [`PaymentId`], which allows
/// us to easily discern them from real payments.
- pub fn send_probe(&self, hops: Vec<RouteHop>) -> Result<(PaymentHash, PaymentId), PaymentSendFailure> {
+ pub fn send_probe(&self, path: Path) -> Result<(PaymentHash, PaymentId), PaymentSendFailure> {
let best_block_height = self.best_block.read().unwrap().height();
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(&self.total_consistency_lock, &self.persistence_notifier);
- self.pending_outbound_payments.send_probe(hops, self.probing_cookie_secret, &self.entropy_source, &self.node_signer, best_block_height,
+ self.pending_outbound_payments.send_probe(path, self.probing_cookie_secret, &self.entropy_source, &self.node_signer, best_block_height,
|path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv|
self.send_payment_along_path(path, payment_hash, recipient_onion, total_value, cur_height, payment_id, keysend_preimage, session_priv))
}
}
{
let height = self.best_block.read().unwrap().height();
- // Transactions are evaluated as final by network mempools at the next block. However, the modules
- // constituting our Lightning node might not have perfect sync about their blockchain views. Thus, if
- // the wallet module is in advance on the LDK view, allow one more block of headroom.
- if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX) && LockTime::from(funding_transaction.lock_time).is_block_height() && funding_transaction.lock_time.0 > height + 2 {
+ // Transactions are evaluated as final by network mempools if their locktime is strictly
+ // lower than the next block height. However, the modules constituting our Lightning
+ // node might not have perfect sync about their blockchain views. Thus, if the wallet
+ // module is ahead of LDK, only allow one more block of headroom.
+ if !funding_transaction.input.iter().all(|input| input.sequence == Sequence::MAX) && LockTime::from(funding_transaction.lock_time).is_block_height() && funding_transaction.lock_time.0 > height + 1 {
return Err(APIError::APIMisuseError {
err: "Funding transaction absolute timelock is non-final".to_owned()
});
0 => {
let mut session_priv: crate::util::ser::RequiredWrapper<SecretKey> = crate::util::ser::RequiredWrapper(None);
let mut first_hop_htlc_msat: u64 = 0;
- let mut path: Option<Vec<RouteHop>> = Some(Vec::new());
+ let mut path_hops: Option<Vec<RouteHop>> = Some(Vec::new());
let mut payment_id = None;
let mut payment_params: Option<PaymentParameters> = None;
+ let mut blinded_tail: Option<BlindedTail> = None;
read_tlv_fields!(reader, {
(0, session_priv, required),
(1, payment_id, option),
(2, first_hop_htlc_msat, required),
- (4, path, vec_type),
+ (4, path_hops, vec_type),
(5, payment_params, (option: ReadableArgs, 0)),
+ (6, blinded_tail, option),
});
if payment_id.is_none() {
// For backwards compat, if there was no payment_id written, use the session_priv bytes
// instead.
payment_id = Some(PaymentId(*session_priv.0.unwrap().as_ref()));
}
- if path.is_none() || path.as_ref().unwrap().is_empty() {
+ let path = Path { hops: path_hops.ok_or(DecodeError::InvalidValue)?, blinded_tail };
+ if path.hops.len() == 0 {
return Err(DecodeError::InvalidValue);
}
- let path = path.unwrap();
if let Some(params) = payment_params.as_mut() {
if params.final_cltv_expiry_delta == 0 {
- params.final_cltv_expiry_delta = path.last().unwrap().cltv_expiry_delta;
+ params.final_cltv_expiry_delta = path.final_cltv_expiry_delta().ok_or(DecodeError::InvalidValue)?;
}
}
Ok(HTLCSource::OutboundRoute {
(1, payment_id_opt, option),
(2, first_hop_htlc_msat, required),
// 3 was previously used to write a PaymentSecret for the payment.
- (4, *path, vec_type),
+ (4, path.hops, vec_type),
(5, None::<PaymentParameters>, option), // payment_params in LDK versions prior to 0.0.115
+ (6, path.blinded_tail, option),
});
}
HTLCSource::PreviousHopData(ref field) => {
if id_to_peer.get(&monitor.get_funding_txo().0.to_channel_id()).is_none() {
for (htlc_source, (htlc, _)) in monitor.get_pending_or_resolved_outbound_htlcs() {
if let HTLCSource::OutboundRoute { payment_id, session_priv, path, .. } = htlc_source {
- if path.is_empty() {
+ if path.hops.is_empty() {
log_error!(args.logger, "Got an empty path for a pending payment");
return Err(DecodeError::InvalidValue);
}
- let path_amt = path.last().unwrap().fee_msat;
+ let path_amt = path.final_value_msat();
let mut session_priv_bytes = [0; 32];
session_priv_bytes[..].copy_from_slice(&session_priv[..]);
match pending_outbounds.pending_outbound_payments.lock().unwrap().entry(payment_id) {
if newly_added { "Added" } else { "Had" }, path_amt, log_bytes!(session_priv_bytes), log_bytes!(htlc.payment_hash.0));
},
hash_map::Entry::Vacant(entry) => {
- let path_fee = path.get_path_fees();
+ let path_fee = path.fee_msat();
entry.insert(PendingOutboundPayment::Retryable {
retry_strategy: None,
attempts: PaymentAttempts::new(),
let (mut route, payment_hash, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_id;
- route.paths[0][1].short_channel_id = chan_3_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_id;
- route.paths[1][1].short_channel_id = chan_4_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_id;
match nodes[0].node.send_payment_with_route(&route, payment_hash,
RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0))
// calls per node.
let network = bitcoin::Network::Testnet;
- let tx_broadcaster = test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), blocks: Arc::new(Mutex::new(Vec::new()))};
+ let tx_broadcaster = test_utils::TestBroadcaster::new(network);
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
let logger_a = test_utils::TestLogger::with_id("node a".to_owned());
let scorer = Mutex::new(test_utils::TestScorer::new());
use crate::util::ser::{ReadableArgs, Writeable};
use bitcoin::blockdata::block::{Block, BlockHeader};
-use bitcoin::blockdata::constants::genesis_block;
use bitcoin::blockdata::transaction::{Transaction, TxOut};
use bitcoin::network::constants::Network;
#[cfg(feature = "std")] {
eprintln!("Connecting block using Block Connection Style: {:?}", *node.connect_style.borrow());
}
+ // Update the block internally before handing it over to LDK, to ensure our assertions regarding
+ // transaction broadcast are correct.
+ node.blocks.lock().unwrap().push((block.clone(), height));
if !skip_intermediaries {
let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
match *node.connect_style.borrow() {
}
call_claimable_balances(node);
node.node.test_process_background_events();
- node.blocks.lock().unwrap().push((block, height));
}
pub fn disconnect_blocks<'a, 'b, 'c, 'd>(node: &'a Node<'b, 'c, 'd>, count: u32) {
}
/// Gets a route from the given sender to the node described in `payment_params`.
-pub fn get_route(send_node: &Node, payment_params: &PaymentParameters, recv_value: u64, final_cltv_expiry_delta: u32) -> Result<Route, msgs::LightningError> {
+pub fn get_route(send_node: &Node, payment_params: &PaymentParameters, recv_value: u64) -> Result<Route, msgs::LightningError> {
let scorer = TestScorer::new();
let keys_manager = TestKeysInterface::new(&[0u8; 32], bitcoin::network::constants::Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
router::get_route(
&send_node.node.get_our_node_id(), payment_params, &send_node.network_graph.read_only(),
Some(&send_node.node.list_usable_channels().iter().collect::<Vec<_>>()),
- recv_value, final_cltv_expiry_delta, send_node.logger, &scorer, &random_seed_bytes
+ recv_value, send_node.logger, &scorer, &random_seed_bytes
)
}
/// Don't use this, use the identically-named function instead.
#[macro_export]
macro_rules! get_route {
- ($send_node: expr, $payment_params: expr, $recv_value: expr, $cltv: expr) => {
- $crate::ln::functional_test_utils::get_route(&$send_node, &$payment_params, $recv_value, $cltv)
+ ($send_node: expr, $payment_params: expr, $recv_value: expr) => {
+ $crate::ln::functional_test_utils::get_route(&$send_node, &$payment_params, $recv_value)
}
}
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
let payment_params = $crate::routing::router::PaymentParameters::from_node_id($recv_node.node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features($recv_node.node.invoice_features());
- $crate::get_route_and_payment_hash!($send_node, $recv_node, payment_params, $recv_value, TEST_FINAL_CLTV)
+ $crate::get_route_and_payment_hash!($send_node, $recv_node, payment_params, $recv_value)
}};
- ($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr, $cltv: expr) => {{
+ ($send_node: expr, $recv_node: expr, $payment_params: expr, $recv_value: expr) => {{
let (payment_preimage, payment_hash, payment_secret) =
$crate::ln::functional_test_utils::get_payment_preimage_hash(&$recv_node, Some($recv_value), None);
- let route = $crate::ln::functional_test_utils::get_route(&$send_node, &$payment_params, $recv_value, $cltv);
+ let route = $crate::ln::functional_test_utils::get_route(&$send_node, &$payment_params, $recv_value);
(route.unwrap(), payment_hash, payment_preimage, payment_secret)
}}
}
pub fn route_payment<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>, expected_route: &[&Node<'a, 'b, 'c>], recv_value: u64) -> (PaymentPreimage, PaymentHash, PaymentSecret) {
let payment_params = PaymentParameters::from_node_id(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(expected_route.last().unwrap().node.invoice_features());
- let route = get_route(origin_node, &payment_params, recv_value, TEST_FINAL_CLTV).unwrap();
+ let route = get_route(origin_node, &payment_params, recv_value).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), expected_route.len());
- for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
+ assert_eq!(route.paths[0].hops.len(), expected_route.len());
+ for (node, hop) in expected_route.iter().zip(route.paths[0].hops.iter()) {
assert_eq!(hop.pubkey, node.node.get_our_node_id());
}
let random_seed_bytes = keys_manager.get_secure_random_bytes();
let route = router::get_route(
&origin_node.node.get_our_node_id(), &payment_params, &network_graph,
- None, recv_value, TEST_FINAL_CLTV, origin_node.logger, &scorer, &random_seed_bytes).unwrap();
+ None, recv_value, origin_node.logger, &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), expected_route.len());
- for (node, hop) in expected_route.iter().zip(route.paths[0].iter()) {
+ assert_eq!(route.paths[0].hops.len(), expected_route.len());
+ for (node, hop) in expected_route.iter().zip(route.paths[0].hops.iter()) {
assert_eq!(hop.pubkey, node.node.get_our_node_id());
}
assert_eq!(payment_hash, our_payment_hash);
assert!(payment_failed_permanently);
for (idx, hop) in expected_route.iter().enumerate() {
- assert_eq!(hop.node.get_our_node_id(), path[idx].pubkey);
+ assert_eq!(hop.node.get_our_node_id(), path.hops[idx].pubkey);
}
payment_id.unwrap()
},
pub fn create_chanmon_cfgs(node_count: usize) -> Vec<TestChanMonCfg> {
let mut chan_mon_cfgs = Vec::new();
for i in 0..node_count {
- let tx_broadcaster = test_utils::TestBroadcaster {
- txn_broadcasted: Mutex::new(Vec::new()),
- blocks: Arc::new(Mutex::new(vec![(genesis_block(Network::Testnet), 0)])),
- };
+ let tx_broadcaster = test_utils::TestBroadcaster::new(Network::Testnet);
let fee_estimator = test_utils::TestFeeEstimator { sat_per_kw: Mutex::new(253) };
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
let logger = test_utils::TestLogger::with_id(format!("node {}", i));
use crate::ln::{chan_utils, onion_utils};
use crate::ln::chan_utils::{OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
-use crate::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
+use crate::routing::router::{Path, PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
use crate::ln::features::{ChannelFeatures, NodeFeatures};
use crate::ln::msgs;
use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ErrorAction};
});
hops[1].fee_msat = chan_4.1.contents.fee_base_msat as u64 + chan_4.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
hops[0].fee_msat = chan_3.0.contents.fee_base_msat as u64 + chan_3.0.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
- let payment_preimage_1 = send_along_route(&nodes[1], Route { paths: vec![hops], payment_params: None }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
+ let payment_preimage_1 = send_along_route(&nodes[1], Route { paths: vec![Path { hops, blinded_tail: None }], payment_params: None }, &vec!(&nodes[2], &nodes[3], &nodes[1])[..], 1000000).0;
let mut hops = Vec::with_capacity(3);
hops.push(RouteHop {
});
hops[1].fee_msat = chan_2.1.contents.fee_base_msat as u64 + chan_2.1.contents.fee_proportional_millionths as u64 * hops[2].fee_msat as u64 / 1000000;
hops[0].fee_msat = chan_3.1.contents.fee_base_msat as u64 + chan_3.1.contents.fee_proportional_millionths as u64 * hops[1].fee_msat as u64 / 1000000;
- let payment_hash_2 = send_along_route(&nodes[1], Route { paths: vec![hops], payment_params: None }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
+ let payment_hash_2 = send_along_route(&nodes[1], Route { paths: vec![Path { hops, blinded_tail: None }], payment_params: None }, &vec!(&nodes[3], &nodes[2], &nodes[1])[..], 1000000).1;
// Claim the rebalances...
fail_payment(&nodes[1], &vec!(&nodes[3], &nodes[2], &nodes[1])[..], payment_hash_2);
mine_transaction(&nodes[0], &remote_txn[0]);
check_added_monitors!(nodes[0], 1);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
let claim_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
assert_eq!(claim_txn.len(), 3);
{
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(nodes[2].node.invoice_features()).with_max_channel_saturation_power_of_half(0);
- let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0, TEST_FINAL_CLTV);
- route.paths[0].last_mut().unwrap().fee_msat += 1;
- assert!(route.paths[0].iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
+ let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, recv_value_0);
+ route.paths[0].hops.last_mut().unwrap().fee_msat += 1;
+ assert!(route.paths[0].hops.iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(nodes[2].node.invoice_features()).with_max_channel_saturation_power_of_half(0);
- let route = get_route!(nodes[0], payment_params, recv_value_0, TEST_FINAL_CLTV).unwrap();
+ let route = get_route!(nodes[0], payment_params, recv_value_0).unwrap();
let (payment_preimage, ..) = send_along_route(&nodes[0], route, &[&nodes[1], &nodes[2]], recv_value_0);
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage);
test_txn_broadcast(&nodes[1], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::NONE);
mine_transaction(&nodes[0], &revoked_local_txn[0]);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
// Verify broadcast of revoked HTLC-timeout
let node_txn = test_txn_broadcast(&nodes[0], &chan_5, Some(revoked_local_txn[0].clone()), HTLCType::TIMEOUT);
check_added_monitors!(nodes[0], 1);
// Verify that B's ChannelManager is able to extract preimage from HTLC Success tx and pass it backward
let header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[1].best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42};
connect_block(&nodes[1], &Block { header, txdata: vec![commitment_tx[0].clone(), node_txn[0].clone(), node_txn[1].clone()]});
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
{
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
assert_eq!(added_monitors.len(), 1);
assert_eq!(commitment_spend.input.len(), 2);
assert_eq!(commitment_spend.input[0].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
assert_eq!(commitment_spend.input[1].witness.last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
- assert_eq!(commitment_spend.lock_time.0, nodes[1].best_block_info().1 + 1);
+ assert_eq!(commitment_spend.lock_time.0, nodes[1].best_block_info().1);
assert!(commitment_spend.output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
// We don't bother to check that B can claim the HTLC output on its commitment tx here as
// we already checked the same situation with A.
// Verify that A's ChannelManager is able to extract preimage from preimage tx and generate PaymentSent
let mut header = BlockHeader { version: 0x20000000, prev_blockhash: nodes[0].best_block_hash(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42};
connect_block(&nodes[0], &Block { header, txdata: vec![node_a_commitment_tx[0].clone(), commitment_spend.clone()] });
- connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
let events = nodes[0].node.get_and_clear_pending_events();
check_spends!(commitment_tx[0], chan_1.3);
mine_transaction(&nodes[0], &commitment_tx[0]);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
_ => panic!("Unexpected event"),
}
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
// Check B's monitor was able to send back output descriptor event for timeout tx on A's commitment tx
let node_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
let revoked_htlc_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
assert_eq!(revoked_htlc_txn.len(), 1);
check_spends!(b_txn[0], commitment_tx[0]);
assert_eq!(b_txn[0].input[0].witness.clone().last().unwrap().len(), OFFERED_HTLC_SCRIPT_WEIGHT);
assert!(b_txn[0].output[0].script_pubkey.is_v0_p2wpkh()); // direct payment
- assert_eq!(b_txn[0].lock_time.0, nodes[1].best_block_info().1 + 1); // Success tx
+ assert_eq!(b_txn[0].lock_time.0, nodes[1].best_block_info().1); // Success tx
check_closed_broadcast!(nodes[1], true);
check_added_monitors!(nodes[1], 1);
// ACCEPTED_HTLC_SCRIPT_WEIGHT.
let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV - 40)
.with_features(nodes[3].node.invoice_features());
- let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000, TEST_FINAL_CLTV - 40);
+ let (route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[3], payment_params, 800_000);
send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[2], &nodes[3]]], 800_000, duplicate_payment_hash, payment_secret);
let commitment_txn = get_local_commitment_txn!(nodes[2], chan_2.2);
check_closed_broadcast!(nodes[1], true);
check_added_monitors!(nodes[1], 1);
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32 - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV - 40 + MIN_CLTV_EXPIRY_DELTA as u32); // Confirm blocks until the HTLC expires
let htlc_timeout_tx;
{ // Extract one of the two HTLC-Timeout transaction
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
let htlc_timeout = {
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
// Timeout HTLC on A's chain and so it can generate a HTLC-Timeout tx
mine_transaction(&nodes[0], &local_txn_1[0]);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
assert_eq!(PaymentId(our_payment_hash.0), *payment_id.as_ref().unwrap());
assert_eq!(our_payment_hash.clone(), *payment_hash);
assert_eq!(*payment_failed_permanently, false);
- assert_eq!(*short_channel_id, Some(route.paths[0][0].short_channel_id));
+ assert_eq!(*short_channel_id, Some(route.paths[0].hops[0].short_channel_id));
},
_ => panic!("Unexpected event"),
}
assert_eq!(payment_id_2, *payment_id.as_ref().unwrap());
assert_eq!(payment_hash_2.clone(), *payment_hash);
assert_eq!(*payment_failed_permanently, false);
- assert_eq!(*short_channel_id, Some(route_2.paths[0][0].short_channel_id));
+ assert_eq!(*short_channel_id, Some(route_2.paths[0].hops[0].short_channel_id));
},
_ => panic!("Unexpected event"),
}
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
- route.paths[0][0].fee_msat = 100;
+ route.paths[0].hops[0].fee_msat = 100;
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
let _chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000);
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000);
- route.paths[0][0].fee_msat = 0;
+ route.paths[0].hops[0].fee_msat = 0;
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)),
true, APIError::ChannelUnavailable { ref err },
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), 0)
.with_features(nodes[1].node.invoice_features());
- let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000, 0);
- route.paths[0].last_mut().unwrap().cltv_expiry_delta = 500000001;
+ let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], payment_params, 100000000);
+ route.paths[0].hops.last_mut().unwrap().cltv_expiry_delta = 500000001;
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
), true, APIError::InvalidRoute { ref err },
let (mut route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], max_in_flight);
// Manually create a route over our max in flight (which our router normally automatically
// limits us to.
- route.paths[0][0].fee_msat = max_in_flight + 1;
+ route.paths[0].hops[0].fee_msat = max_in_flight + 1;
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)
), true, APIError::ChannelUnavailable { ref err },
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
assert_eq!(nodes[0].node.get_and_clear_pending_events().len(), 0);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
timeout_tx = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().drain(..)
.filter(|tx| tx.input[0].previous_output.txid == bs_commitment_tx[0].txid()).collect();
check_spends!(timeout_tx[0], bs_commitment_tx[0]);
if !revoked {
assert_eq!(timeout_tx[0].input[0].witness.last().unwrap().len(), ACCEPTED_HTLC_SCRIPT_WEIGHT);
} else {
- assert_eq!(timeout_tx[0].lock_time.0, 12);
+ assert_eq!(timeout_tx[0].lock_time.0, 11);
}
// We fail non-dust-HTLC 2 by broadcast of local timeout/revocation-claim tx
mine_transaction(&nodes[0], &timeout_tx[0]);
let scorer = test_utils::TestScorer::new();
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[1].node.invoice_features());
- let route = get_route(&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(), None, 10_000, TEST_FINAL_CLTV, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(), None, 10_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
let (_, our_payment_hash, _) = get_payment_preimage_hash!(nodes[0]);
let our_payment_secret = nodes[1].node.create_inbound_payment_for_hash(our_payment_hash, Some(100_000), 7200, None).unwrap();
nodes[0].node.send_payment_with_route(&route, our_payment_hash,
let payment_preimage = route_payment(&nodes[0], &vec!(&nodes[1])[..], 3000000).0;
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 30)
.with_features(nodes[0].node.invoice_features());
- let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000, 30);
+ let (route,_, _, _) = get_route_and_payment_hash!(nodes[1], nodes[0], payment_params, 3000000);
send_along_route(&nodes[1], route, &vec!(&nodes[0])[..], 3000000);
let revoked_txn = get_local_commitment_txn!(nodes[0], chan.2);
let scorer = test_utils::TestScorer::new();
let random_seed_bytes = chanmon_cfgs[1].keys_manager.get_secure_random_bytes();
let route = get_route(&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(), None,
- 3_000_000, 50, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
+ 3_000_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
let payment_preimage = send_along_route(&nodes[0], route, &[&nodes[1]], 3_000_000).0;
let payment_params = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), 50).with_features(nodes[0].node.invoice_features());
let route = get_route(&nodes[1].node.get_our_node_id(), &payment_params, &nodes[1].network_graph.read_only(), None,
- 3_000_000, 50, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
+ 3_000_000, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
send_along_route(&nodes[1], route, &[&nodes[0]], 3_000_000);
let revoked_local_txn = get_local_commitment_txn!(nodes[1], chan.2);
check_closed_broadcast!(nodes[1], true);
check_added_monitors!(nodes[1], 1);
check_closed_event!(nodes[1], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[1], 49); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
+ connect_blocks(&nodes[1], 50); // Confirm blocks until the HTLC expires (note CLTV was explicitly 50 above)
let revoked_htlc_txn = {
let txn = nodes[1].tx_broadcaster.unique_txn_broadcast();
expect_payment_claimed!(nodes[1], payment_hash, 3_000_000);
mine_transaction(&nodes[1], &remote_txn[0]);
check_added_monitors!(nodes[1], 2);
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
// One or more claim tx should have been broadcast, check it
let timeout;
// almost-claimed HTLC as available balance.
let (mut route, _, _, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
route.payment_params = None; // This is all wrong, but unnecessary
- route.paths[0][0].pubkey = nodes[0].node.get_our_node_id();
+ route.paths[0].hops[0].pubkey = nodes[0].node.get_our_node_id();
let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[0]);
nodes[1].node.send_payment_with_route(&route, payment_hash_2,
RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
let sample_path = route.paths.pop().unwrap();
let mut path_1 = sample_path.clone();
- path_1[0].pubkey = nodes[1].node.get_our_node_id();
- path_1[0].short_channel_id = chan_1_id;
- path_1[1].pubkey = nodes[3].node.get_our_node_id();
- path_1[1].short_channel_id = chan_3_id;
- path_1[1].fee_msat = 100_000;
+ path_1.hops[0].pubkey = nodes[1].node.get_our_node_id();
+ path_1.hops[0].short_channel_id = chan_1_id;
+ path_1.hops[1].pubkey = nodes[3].node.get_our_node_id();
+ path_1.hops[1].short_channel_id = chan_3_id;
+ path_1.hops[1].fee_msat = 100_000;
route.paths.push(path_1);
let mut path_2 = sample_path.clone();
- path_2[0].pubkey = nodes[2].node.get_our_node_id();
- path_2[0].short_channel_id = chan_2_id;
- path_2[1].pubkey = nodes[3].node.get_our_node_id();
- path_2[1].short_channel_id = chan_4_id;
- path_2[1].fee_msat = 1_000;
+ path_2.hops[0].pubkey = nodes[2].node.get_our_node_id();
+ path_2.hops[0].short_channel_id = chan_2_id;
+ path_2.hops[1].pubkey = nodes[3].node.get_our_node_id();
+ path_2.hops[1].short_channel_id = chan_4_id;
+ path_2.hops[1].fee_msat = 1_000;
route.paths.push(path_2);
// Send payment
for i in 0..routing_node_count {
let routing_node = 2 + i;
let mut path = sample_path.clone();
- path[0].pubkey = nodes[routing_node].node.get_our_node_id();
- path[0].short_channel_id = src_chan_ids[i];
- path[1].pubkey = nodes[dst_idx].node.get_our_node_id();
- path[1].short_channel_id = dst_chan_ids[i];
- path[1].fee_msat = msat_amounts[i];
+ path.hops[0].pubkey = nodes[routing_node].node.get_our_node_id();
+ path.hops[0].short_channel_id = src_chan_ids[i];
+ path.hops[1].pubkey = nodes[dst_idx].node.get_our_node_id();
+ path.hops[1].short_channel_id = dst_chan_ids[i];
+ path.hops[1].fee_msat = msat_amounts[i];
route.paths.push(path);
}
let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_id;
- route.paths[0][1].short_channel_id = chan_3_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_id;
- route.paths[1][1].short_channel_id = chan_4_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_id;
send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
}
let block = Block { header, txdata: vec![] };
// Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
// transaction lock time requirements here.
- chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 0));
+ chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize(200, (block.clone(), 200));
watchtower.chain_monitor.block_connected(&block, 200);
// Try to update ChannelMonitor
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
let logger = test_utils::TestLogger::with_id(format!("node {}", "Alice"));
let persister = test_utils::TestPersister::new();
+ let alice_broadcaster = test_utils::TestBroadcaster::with_blocks(
+ Arc::new(Mutex::new(nodes[0].blocks.lock().unwrap().clone())),
+ );
let watchtower_alice = {
let new_monitor = {
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
assert!(new_monitor == *monitor);
new_monitor
};
- let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
+ let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &alice_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
watchtower
};
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
let block = Block { header, txdata: vec![] };
- // Make the tx_broadcaster aware of enough blocks that it doesn't think we're violating
- // transaction lock time requirements here.
- chanmon_cfgs[0].tx_broadcaster.blocks.lock().unwrap().resize((CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS) as usize, (block.clone(), 0));
- watchtower_alice.chain_monitor.block_connected(&block, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
+ // Make Alice aware of enough blocks that it doesn't think we're violating transaction lock time
+ // requirements here.
+ const HTLC_TIMEOUT_BROADCAST: u32 = CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS;
+ alice_broadcaster.blocks.lock().unwrap().resize((HTLC_TIMEOUT_BROADCAST) as usize, (block.clone(), HTLC_TIMEOUT_BROADCAST));
+ watchtower_alice.chain_monitor.block_connected(&block, HTLC_TIMEOUT_BROADCAST);
// Watchtower Alice should have broadcast a commitment/HTLC-timeout
let alice_state = {
- let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
+ let mut txn = alice_broadcaster.txn_broadcast();
assert_eq!(txn.len(), 2);
txn.remove(0)
};
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
let logger = test_utils::TestLogger::with_id(format!("node {}", "Bob"));
let persister = test_utils::TestPersister::new();
+ let bob_broadcaster = test_utils::TestBroadcaster::with_blocks(Arc::clone(&alice_broadcaster.blocks));
let watchtower_bob = {
let new_monitor = {
let monitor = nodes[0].chain_monitor.chain_monitor.get_monitor(outpoint).unwrap();
assert!(new_monitor == *monitor);
new_monitor
};
- let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &chanmon_cfgs[0].tx_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
+ let watchtower = test_utils::TestChainMonitor::new(Some(&chain_source), &bob_broadcaster, &logger, &chanmon_cfgs[0].fee_estimator, &persister, &node_cfgs[0].keys_manager);
assert_eq!(watchtower.watch_channel(outpoint, new_monitor), ChannelMonitorUpdateStatus::Completed);
watchtower
};
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
- watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
+ watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, HTLC_TIMEOUT_BROADCAST - 1);
// Route another payment to generate another update with still previous HTLC pending
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 3000000);
//// Provide one more block to watchtower Bob, expect broadcast of commitment and HTLC-Timeout
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
- watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, CHAN_CONFIRM_DEPTH + 1 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
+ watchtower_bob.chain_monitor.block_connected(&Block { header, txdata: vec![] }, HTLC_TIMEOUT_BROADCAST);
// Watchtower Bob should have broadcast a commitment/HTLC-timeout
let bob_state_y;
{
- let mut txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
+ let mut txn = bob_broadcaster.txn_broadcast();
assert_eq!(txn.len(), 2);
bob_state_y = txn.remove(0);
};
// We confirm Bob's state Y on Alice, she should broadcast a HTLC-timeout
let header = BlockHeader { version: 0x20000000, prev_blockhash: BlockHash::all_zeros(), merkle_root: TxMerkleNode::all_zeros(), time: 42, bits: 42, nonce: 42 };
- watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, CHAN_CONFIRM_DEPTH + 2 + TEST_FINAL_CLTV + LATENCY_GRACE_PERIOD_BLOCKS);
+ let height = HTLC_TIMEOUT_BROADCAST + 1;
+ connect_blocks(&nodes[0], height - nodes[0].best_block_info().1);
+ check_closed_broadcast(&nodes[0], 1, true);
+ check_closed_event(&nodes[0], 1, ClosureReason::CommitmentTxConfirmed, false);
+ watchtower_alice.chain_monitor.block_connected(&Block { header, txdata: vec![bob_state_y.clone()] }, height);
+ check_added_monitors(&nodes[0], 1);
{
- let htlc_txn = chanmon_cfgs[0].tx_broadcaster.txn_broadcast();
+ let htlc_txn = alice_broadcaster.txn_broadcast();
assert_eq!(htlc_txn.len(), 2);
check_spends!(htlc_txn[0], bob_state_y);
// Alice doesn't clean up the old HTLC claim since it hasn't seen a conflicting spend for
check_closed_broadcast!(nodes[0], true);
check_added_monitors!(nodes[0], 1);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV);
let htlc_timeout = {
let node_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap();
let payment_params = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(nodes[1].node.invoice_features());
- let route = get_route!(nodes[0], payment_params, 10_000, TEST_FINAL_CLTV).unwrap();
+ let route = get_route!(nodes[0], payment_params, 10_000).unwrap();
let (our_payment_preimage, our_payment_hash, our_payment_secret) = get_payment_preimage_hash!(&nodes[1]);
let payment_params = PaymentParameters::from_node_id(nodes[3].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(nodes[3].node.invoice_features());
- let mut route = get_route!(nodes[0], payment_params, 15_000_000, TEST_FINAL_CLTV).unwrap();
+ let mut route = get_route!(nodes[0], payment_params, 15_000_000).unwrap();
assert_eq!(route.paths.len(), 2);
route.paths.sort_by(|path_a, _| {
// Sort the path so that the path through nodes[1] comes first
- if path_a[0].pubkey == nodes[1].node.get_our_node_id() {
+ if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
});
assert_eq!(route.paths.len(), 2);
route.paths.sort_by(|path_a, _| {
// Sort the path so that the path through nodes[1] comes first
- if path_a[0].pubkey == nodes[1].node.get_our_node_id() {
+ if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
});
assert_eq!(events.len(), 1);
let mut tx = match events[0] {
Event::FundingGenerationReady { ref channel_value_satoshis, ref output_script, .. } => {
- // Timelock the transaction _beyond_ the best client height + 2.
- Transaction { version: chan_id as i32, lock_time: PackedLockTime(best_height + 3), input: vec![input], output: vec![TxOut {
+ // Timelock the transaction _beyond_ the best client height + 1.
+ Transaction { version: chan_id as i32, lock_time: PackedLockTime(best_height + 2), input: vec![input], output: vec![TxOut {
value: *channel_value_satoshis, script_pubkey: output_script.clone(),
}]}
},
_ => panic!()
}
- // However, transaction should be accepted if it's in a +2 headroom from best block.
+ // However, transaction should be accepted if it's in a +1 headroom from best block.
tx.lock_time = PackedLockTime(tx.lock_time.0 - 1);
assert!(nodes[0].node.funding_transaction_generated(&temp_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).is_ok());
get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id());
let (payment_hash, payment_secret) = nodes[1].node.create_inbound_payment(Some(recv_value), 7200, Some(min_final_cltv_expiry_delta)).unwrap();
(payment_hash, nodes[1].node.get_payment_preimage(payment_hash, payment_secret).unwrap(), payment_secret)
};
- let route = get_route!(nodes[0], payment_parameters, recv_value, final_cltv_expiry_delta as u32).unwrap();
+ let route = get_route!(nodes[0], payment_parameters, recv_value).unwrap();
nodes[0].node.send_payment_with_route(&route, payment_hash,
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
check_added_monitors!(nodes[0], 1);
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
+#[cfg_attr(test, derive(PartialOrd, Ord))]
pub struct PaymentHash(pub [u8; 32]);
/// payment_preimage type, use to route payment between hop
///
/// This is not exported to bindings users as we just use [u8; 32] directly
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
+#[cfg_attr(test, derive(PartialOrd, Ord))]
pub struct PaymentPreimage(pub [u8; 32]);
/// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
///
let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
let remote_txn = get_local_commitment_txn!(nodes[1], chan_id);
+ let sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 3_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash,
+ };
+ let sent_htlc_timeout_balance = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 4_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash: timeout_payment_hash,
+ };
+ let received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
+ claimable_amount_satoshis: 3_000,
+ expiry_height: htlc_cltv_timeout,
+ payment_hash,
+ };
+ let received_htlc_timeout_balance = Balance::MaybePreimageClaimableHTLC {
+ claimable_amount_satoshis: 4_000,
+ expiry_height: htlc_cltv_timeout,
+ payment_hash: timeout_payment_hash,
+ };
+ let received_htlc_claiming_balance = Balance::ContentiousClaimable {
+ claimable_amount_satoshis: 3_000,
+ timeout_height: htlc_cltv_timeout,
+ payment_hash,
+ payment_preimage,
+ };
+ let received_htlc_timeout_claiming_balance = Balance::ContentiousClaimable {
+ claimable_amount_satoshis: 4_000,
+ timeout_height: htlc_cltv_timeout,
+ payment_hash: timeout_payment_hash,
+ payment_preimage: timeout_payment_preimage,
+ };
+
// Before B receives the payment preimage, it only suggests the push_msat value of 1_000 sats
// as claimable. A lists both its to-self balance and the (possibly-claimable) HTLCs.
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
claimable_amount_satoshis: 1_000,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- expiry_height: htlc_cltv_timeout,
- }]),
+ }, received_htlc_balance.clone(), received_htlc_timeout_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
nodes[1].node.claim_funds(payment_preimage);
chan_feerate * (channel::commitment_tx_base_weight(opt_anchors) +
if prev_commitment_tx { 1 } else { 2 } *
channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }];
+ }, sent_htlc_timeout_balance.clone()];
if !prev_commitment_tx {
- a_expected_balances.push(Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- claimable_height: htlc_cltv_timeout,
- });
+ a_expected_balances.push(sent_htlc_balance.clone());
}
assert_eq!(sorted_vec(a_expected_balances),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
claimable_amount_satoshis: 1_000_000 - 3_000 - 4_000 - 1_000 - 3 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: nodes[0].best_block_info().1 + ANTI_REORG_DELAY - 1,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// The main non-HTLC balance is just awaiting confirmations, but the claimable height is the
// CSV delay, not ANTI_REORG_DELAY.
},
// Both HTLC balances are "contentious" as our counterparty could claim them if we wait too
// long.
- Balance::ContentiousClaimable {
- claimable_amount_satoshis: 3_000,
- timeout_height: htlc_cltv_timeout,
- }, Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }]),
+ received_htlc_claiming_balance.clone(), received_htlc_timeout_claiming_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
// After ANTI_REORG_DELAY, A will consider its balance fully spendable and generate a
// `SpendableOutputs` event. However, B still has to wait for the CSV delay.
- assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ assert_eq!(sorted_vec(vec![sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 1_000,
confirmation_height: node_b_commitment_claimable,
- }, Balance::ContentiousClaimable {
- claimable_amount_satoshis: 3_000,
- timeout_height: htlc_cltv_timeout,
- }, Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }]),
+ }, received_htlc_claiming_balance.clone(), received_htlc_timeout_claiming_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
test_spendable_output(&nodes[0], &remote_txn[0]);
} else {
expect_payment_sent!(nodes[0], payment_preimage);
}
- assert_eq!(sorted_vec(vec![Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 3_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ assert_eq!(sorted_vec(vec![sent_htlc_balance.clone(), sent_htlc_timeout_balance.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
connect_blocks(&nodes[0], ANTI_REORG_DELAY - 1);
- assert_eq!(vec![Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 4_000,
- claimable_height: htlc_cltv_timeout,
- }],
+ assert_eq!(vec![sent_htlc_timeout_balance.clone()],
nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
// When the HTLC timeout output is spendable in the next block, A should broadcast it
- connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1 - 1);
+ connect_blocks(&nodes[0], htlc_cltv_timeout - nodes[0].best_block_info().1);
let a_broadcast_txn = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
assert_eq!(a_broadcast_txn.len(), 2);
assert_eq!(a_broadcast_txn[0].input.len(), 1);
}, Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 3_000,
confirmation_height: node_b_htlc_claimable,
- }, Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }]),
+ }, received_htlc_timeout_claiming_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// After reaching the commitment output CSV, we'll get a SpendableOutputs event for it and have
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 3_000,
confirmation_height: node_b_htlc_claimable,
- }, Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }]),
+ }, received_htlc_timeout_claiming_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// After reaching the claimed HTLC output CSV, we'll get a SpendableOutptus event for it and
connect_blocks(&nodes[1], node_b_htlc_claimable - nodes[1].best_block_info().1);
test_spendable_output(&nodes[1], &b_broadcast_txn[0]);
- assert_eq!(vec![Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }],
+ assert_eq!(vec![received_htlc_timeout_claiming_balance.clone()],
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
// Finally, mine the HTLC timeout transaction that A broadcasted (even though B should be able
// to claim this HTLC with the preimage it knows!). It will remain listed as a claimable HTLC
// until ANTI_REORG_DELAY confirmations on the spend.
mine_transaction(&nodes[1], &a_broadcast_txn[1]);
- assert_eq!(vec![Balance::ContentiousClaimable {
- claimable_amount_satoshis: 4_000,
- timeout_height: htlc_cltv_timeout,
- }],
+ assert_eq!(vec![received_htlc_timeout_claiming_balance.clone()],
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 1);
assert_eq!(Vec::<Balance>::new(),
check_closed_broadcast!(nodes[0], true);
check_closed_event!(nodes[0], 1, ClosureReason::CommitmentTxConfirmed);
+ let htlc_balance_known_preimage = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 10_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash,
+ };
+ let htlc_balance_unknown_preimage = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 20_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash: payment_hash_2,
+ };
+
assert_eq!(sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: node_a_commitment_claimable,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// Get nodes[1]'s HTLC claim tx for the second HTLC
claimable_amount_satoshis: 1_000_000 - 10_000 - 20_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: node_a_commitment_claimable,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- claimable_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, htlc_balance_known_preimage.clone(), htlc_balance_unknown_preimage.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
assert_eq!(as_txn[1].lock_time.0, nodes[0].best_block_info().1 + 1); // as_txn[1] can be included in the next block
}, Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 10_000,
confirmation_height: node_a_htlc_claimable,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, htlc_balance_unknown_preimage.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// Now confirm nodes[1]'s HTLC claim, giving nodes[0] the preimage. Note that the "maybe
}, Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 10_000,
confirmation_height: node_a_htlc_claimable,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, htlc_balance_unknown_preimage.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// Finally make the HTLC transactions have ANTI_REORG_DELAY blocks. This call previously
let chan_feerate = get_feerate!(nodes[0], nodes[1], chan_id) as u64;
let opt_anchors = get_opt_anchors!(nodes[0], nodes[1], chan_id);
+ let a_sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 10_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash: to_b_failed_payment_hash,
+ };
+ let a_received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
+ claimable_amount_satoshis: 20_000,
+ expiry_height: htlc_cltv_timeout,
+ payment_hash: to_a_failed_payment_hash,
+ };
+ let b_received_htlc_balance = Balance::MaybePreimageClaimableHTLC {
+ claimable_amount_satoshis: 10_000,
+ expiry_height: htlc_cltv_timeout,
+ payment_hash: to_b_failed_payment_hash,
+ };
+ let b_sent_htlc_balance = Balance::MaybeTimeoutClaimableHTLC {
+ claimable_amount_satoshis: 20_000,
+ claimable_height: htlc_cltv_timeout,
+ payment_hash: to_a_failed_payment_hash,
+ };
+
// Both A and B will have an HTLC that's claimable on timeout and one that's claimable if they
// receive the preimage. These will remain the same through the channel closure and until the
// HTLC output is spent.
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]),
sorted_vec(nodes[0].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
assert_eq!(sorted_vec(vec![Balance::ClaimableOnChannelClose {
claimable_amount_satoshis: 500_000 - 20_000,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]),
+ }, b_received_htlc_balance.clone(), b_sent_htlc_balance.clone()]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// Get nodes[0]'s commitment transaction and HTLC-Timeout transaction
claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: node_a_commitment_claimable,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- claimable_height: htlc_cltv_timeout,
- }]);
+ }, a_received_htlc_balance.clone(), a_sent_htlc_balance.clone()]);
mine_transaction(&nodes[0], &as_txn[0]);
nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
let mut bs_pre_spend_claims = sorted_vec(vec![Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 500_000 - 20_000,
confirmation_height: node_b_commitment_claimable,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::MaybeTimeoutClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- claimable_height: htlc_cltv_timeout,
- }]);
+ }, b_received_htlc_balance.clone(), b_sent_htlc_balance.clone()]);
assert_eq!(bs_pre_spend_claims,
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
// HTLC has been spent, even after the HTLC expires. We'll also fail the inbound HTLC, but it
// won't do anything as the channel is already closed.
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV);
let as_htlc_timeout_claim = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
assert_eq!(as_htlc_timeout_claim.len(), 1);
check_spends!(as_htlc_timeout_claim[0], as_txn[0]);
// The next few blocks for B look the same as for A, though for the opposite HTLC
nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clear();
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1) - 1);
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV - (ANTI_REORG_DELAY - 1));
expect_pending_htlcs_forwardable_conditions!(nodes[1],
[HTLCDestination::FailedPayment { payment_hash: to_b_failed_payment_hash }]);
let bs_htlc_timeout_claim = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: node_a_commitment_claimable,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::ClaimableAwaitingConfirmations {
+ }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 10_000,
confirmation_height: as_timeout_claimable_height,
}]),
claimable_amount_satoshis: 1_000_000 - 500_000 - 10_000 - chan_feerate *
(channel::commitment_tx_base_weight(opt_anchors) + 2 * channel::COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000,
confirmation_height: node_a_commitment_claimable,
- }, Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 20_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::ClaimableAwaitingConfirmations {
+ }, a_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 10_000,
confirmation_height: as_timeout_claimable_height,
}]),
// was already claimed.
mine_transaction(&nodes[1], &bs_htlc_timeout_claim[0]);
let bs_timeout_claimable_height = nodes[1].best_block_info().1 + ANTI_REORG_DELAY - 1;
- assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::ClaimableAwaitingConfirmations {
+ assert_eq!(sorted_vec(vec![b_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 20_000,
confirmation_height: bs_timeout_claimable_height,
}]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
mine_transaction(&nodes[1], &as_htlc_timeout_claim[0]);
- assert_eq!(sorted_vec(vec![Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- expiry_height: htlc_cltv_timeout,
- }, Balance::ClaimableAwaitingConfirmations {
+ assert_eq!(sorted_vec(vec![b_received_htlc_balance.clone(), Balance::ClaimableAwaitingConfirmations {
claimable_amount_satoshis: 20_000,
confirmation_height: bs_timeout_claimable_height,
}]),
connect_blocks(&nodes[1], ANTI_REORG_DELAY - 2);
expect_payment_failed!(nodes[1], to_a_failed_payment_hash, false);
- assert_eq!(vec![Balance::MaybePreimageClaimableHTLC {
- claimable_amount_satoshis: 10_000,
- expiry_height: htlc_cltv_timeout,
- }],
+ assert_eq!(vec![b_received_htlc_balance.clone()],
nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances());
test_spendable_output(&nodes[1], &bs_htlc_timeout_claim[0]);
}, Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: 2_000,
claimable_height: missing_htlc_cltv_timeout,
+ payment_hash: missing_htlc_payment_hash,
}, Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: 4_000,
claimable_height: htlc_cltv_timeout,
+ payment_hash: timeout_payment_hash,
}, Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: 5_000,
claimable_height: live_htlc_cltv_timeout,
+ payment_hash: live_payment_hash,
}]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
}, Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: 4_000,
claimable_height: htlc_cltv_timeout,
+ payment_hash: revoked_payment_hash,
}, Balance::MaybeTimeoutClaimableHTLC {
claimable_amount_satoshis: 3_000,
claimable_height: htlc_cltv_timeout,
+ payment_hash: claimed_payment_hash,
}]),
sorted_vec(nodes[1].chain_monitor.chain_monitor.get_monitor(funding_outpoint).unwrap().get_claimable_balances()));
mine_transaction(&nodes[0], &commitment_tx);
// Connect blocks until the HTLC's expiration is met, expecting a transaction broadcast.
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1);
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV);
let htlc_timeout_tx = {
let mut txn = nodes[0].tx_broadcaster.txn_broadcast();
assert_eq!(txn.len(), 1);
if anchors {
assert!(cfg!(anchors));
}
- let secp = Secp256k1::new();
let mut chanmon_cfgs = create_chanmon_cfgs(2);
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
let mut config = test_default_channel_config();
feerate = if let Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
target_feerate_sat_per_1000_weight, mut htlc_descriptors, tx_lock_time,
}) = events.pop().unwrap() {
+ let secp = Secp256k1::new();
assert_eq!(htlc_descriptors.len(), 1);
let descriptor = htlc_descriptors.pop().unwrap();
assert_eq!(descriptor.commitment_txid, commitment_txn[0].txid());
};
// Connect blocks up to one before the HTLC expires. This should not result in a claim/retry.
- connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 2);
+ connect_blocks(&nodes[0], htlc_expiry - nodes[0].best_block_info().1 - 1);
check_htlc_retry(false, false);
// Connect one more block, producing our first claim.
let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[0].shared_secret.as_ref(), NODE|2, &[0;0]);
- }, ||{}, true, Some(NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][0].pubkey, is_permanent: false}), Some(route.paths[0][0].short_channel_id));
+ }, ||{}, true, Some(NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[0].pubkey, is_permanent: false}), Some(route.paths[0].hops[0].short_channel_id));
// final node failure
run_onion_failure_test_with_fail_intercept("temporary_node_failure", 200, &nodes, &route, &payment_hash, &payment_secret, |_msg| {}, |msg| {
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[1].shared_secret.as_ref(), NODE|2, &[0;0]);
}, ||{
nodes[2].node.fail_htlc_backwards(&payment_hash);
- }, true, Some(NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][1].pubkey, is_permanent: false}), Some(route.paths[0][1].short_channel_id));
+ }, true, Some(NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[1].pubkey, is_permanent: false}), Some(route.paths[0].hops[1].short_channel_id));
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
// intermediate node failure
let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[0].shared_secret.as_ref(), PERM|NODE|2, &[0;0]);
- }, ||{}, true, Some(PERM|NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][0].pubkey, is_permanent: true}), Some(route.paths[0][0].short_channel_id));
+ }, ||{}, true, Some(PERM|NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[0].pubkey, is_permanent: true}), Some(route.paths[0].hops[0].short_channel_id));
// final node failure
run_onion_failure_test_with_fail_intercept("permanent_node_failure", 200, &nodes, &route, &payment_hash, &payment_secret, |_msg| {}, |msg| {
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[1].shared_secret.as_ref(), PERM|NODE|2, &[0;0]);
}, ||{
nodes[2].node.fail_htlc_backwards(&payment_hash);
- }, false, Some(PERM|NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][1].pubkey, is_permanent: true}), Some(route.paths[0][1].short_channel_id));
+ }, false, Some(PERM|NODE|2), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[1].pubkey, is_permanent: true}), Some(route.paths[0].hops[1].short_channel_id));
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
// intermediate node failure
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[0].shared_secret.as_ref(), PERM|NODE|3, &[0;0]);
}, ||{
nodes[2].node.fail_htlc_backwards(&payment_hash);
- }, true, Some(PERM|NODE|3), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][0].pubkey, is_permanent: true}), Some(route.paths[0][0].short_channel_id));
+ }, true, Some(PERM|NODE|3), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[0].pubkey, is_permanent: true}), Some(route.paths[0].hops[0].short_channel_id));
// final node failure
run_onion_failure_test_with_fail_intercept("required_node_feature_missing", 200, &nodes, &route, &payment_hash, &payment_secret, |_msg| {}, |msg| {
msg.reason = onion_utils::build_first_hop_failure_packet(onion_keys[1].shared_secret.as_ref(), PERM|NODE|3, &[0;0]);
}, ||{
nodes[2].node.fail_htlc_backwards(&payment_hash);
- }, false, Some(PERM|NODE|3), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][1].pubkey, is_permanent: true}), Some(route.paths[0][1].short_channel_id));
+ }, false, Some(PERM|NODE|3), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[1].pubkey, is_permanent: true}), Some(route.paths[0].hops[1].short_channel_id));
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[2]);
// Our immediate peer sent UpdateFailMalformedHTLC because it couldn't understand the onion in
}, ||{}, true, Some(PERM|9), Some(NetworkUpdate::ChannelFailure{short_channel_id, is_permanent: true}), Some(short_channel_id));
let mut bogus_route = route.clone();
- bogus_route.paths[0][1].short_channel_id -= 1;
- let short_channel_id = bogus_route.paths[0][1].short_channel_id;
+ bogus_route.paths[0].hops[1].short_channel_id -= 1;
+ let short_channel_id = bogus_route.paths[0].hops[1].short_channel_id;
run_onion_failure_test("unknown_next_peer", 0, &nodes, &bogus_route, &payment_hash, &payment_secret, |_| {}, ||{}, true, Some(PERM|10),
Some(NetworkUpdate::ChannelFailure{short_channel_id, is_permanent:true}), Some(short_channel_id));
.unwrap().lock().unwrap().channel_by_id.get(&channels[1].2).unwrap()
.get_counterparty_htlc_minimum_msat() - 1;
let mut bogus_route = route.clone();
- let route_len = bogus_route.paths[0].len();
- bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward;
+ let route_len = bogus_route.paths[0].hops.len();
+ bogus_route.paths[0].hops[route_len-1].fee_msat = amt_to_forward;
run_onion_failure_test("amount_below_minimum", 0, &nodes, &bogus_route, &payment_hash, &payment_secret, |_| {}, ||{}, true, Some(UPDATE|11), Some(NetworkUpdate::ChannelUpdateMessage{msg: ChannelUpdate::dummy(short_channel_id)}), Some(short_channel_id));
// Clear pending payments so that the following positive test has the correct payment hash.
}
// Test a positive test-case with one extra msat, meeting the minimum.
- bogus_route.paths[0][route_len-1].fee_msat = amt_to_forward + 1;
+ bogus_route.paths[0].hops[route_len-1].fee_msat = amt_to_forward + 1;
let preimage = send_along_route(&nodes[0], bogus_route, &[&nodes[1], &nodes[2]], amt_to_forward+1).0;
claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], preimage);
let session_priv = SecretKey::from_slice(&[3; 32]).unwrap();
let mut route = route.clone();
let height = nodes[2].best_block_info().1;
- route.paths[0][1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.paths[0][0].cltv_expiry_delta + 1;
+ route.paths[0].hops[1].cltv_expiry_delta += CLTV_FAR_FAR_AWAY + route.paths[0].hops[0].cltv_expiry_delta + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route.paths[0], &session_priv).unwrap();
let (onion_payloads, _, htlc_cltv) = onion_utils::build_onion_payloads(
&route.paths[0], 40000, RecipientOnionFields::spontaneous_empty(), height, &None).unwrap();
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
msg.cltv_expiry = htlc_cltv;
msg.onion_routing_packet = onion_packet;
- }, ||{}, true, Some(21), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0][0].pubkey, is_permanent: true}), Some(route.paths[0][0].short_channel_id));
+ }, ||{}, true, Some(21), Some(NetworkUpdate::NodeFailure{node_id: route.paths[0].hops[0].pubkey, is_permanent: true}), Some(route.paths[0].hops[0].short_channel_id));
run_onion_failure_test_with_fail_intercept("mpp_timeout", 200, &nodes, &route, &payment_hash, &payment_secret, |_msg| {}, |msg| {
// Tamper returning error message
let payment_params = PaymentParameters::from_node_id(*channel_to_update_counterparty, TEST_FINAL_CLTV)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(hop_hints);
- get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, PAYMENT_AMT, TEST_FINAL_CLTV)
+ get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, PAYMENT_AMT)
};
send_along_route_with_secret(&nodes[0], route.clone(), &[&[&nodes[1], &nodes[2]]], PAYMENT_AMT,
payment_hash, payment_secret);
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(InvoiceFeatures::empty());
- let (route, _payment_hash, _payment_preimage, _payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 40000, TEST_FINAL_CLTV);
+ let (route, _payment_hash, _payment_preimage, _payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 40000);
- let hops = &route.paths[0];
+ let hops = &route.paths[0].hops;
// Asserts that the first hop to `node[1]` signals no support for variable length onions.
assert!(!hops[0].node_features.supports_variable_length_onion());
// Asserts that the first hop to `node[1]` signals no support for variable length onions.
(get_route(
&$nodes[0].node.get_our_node_id(), &payment_params, &network_graph,
Some(&$nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
- $amt, TEST_FINAL_CLTV, $nodes[0].logger, &scorer, &[0u8; 32]
+ $amt, $nodes[0].logger, &scorer, &[0u8; 32]
).unwrap(), phantom_route_hint.phantom_scid)
}
}}
let (mut route, phantom_scid) = get_phantom_route!(nodes, recv_value_msat, channel);
// Modify the route to have a too-low cltv.
- route.paths[0][1].cltv_expiry_delta = 5;
+ route.paths[0].hops[1].cltv_expiry_delta = 5;
// Route the HTLC through to the destination.
nodes[0].node.send_payment_with_route(&route, payment_hash,
nodes[1].node.process_pending_htlc_forwards();
expect_pending_htlcs_forwardable_ignore!(nodes[1]);
nodes[1].node.process_pending_htlc_forwards();
- expect_payment_claimable!(nodes[1], payment_hash, payment_secret, recv_amt_msat, None, route.paths[0].last().unwrap().pubkey);
+ expect_payment_claimable!(nodes[1], payment_hash, payment_secret, recv_amt_msat, None, route.paths[0].hops.last().unwrap().pubkey);
nodes[1].node.fail_htlc_backwards(&payment_hash);
expect_pending_htlcs_forwardable_and_htlc_handling_failed_ignore!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
nodes[1].node.process_pending_htlc_forwards();
use crate::ln::msgs;
use crate::ln::wire::Encode;
use crate::routing::gossip::NetworkUpdate;
-use crate::routing::router::RouteHop;
+use crate::routing::router::{Path, RouteHop};
use crate::util::chacha20::{ChaCha20, ChaChaReader};
use crate::util::errors::{self, APIError};
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, LengthCalculatingWriter};
}
// can only fail if an intermediary hop has an invalid public key or session_priv is invalid
-pub(super) fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, path: &Vec<RouteHop>, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, secp256k1::Error> {
- let mut res = Vec::with_capacity(path.len());
+pub(super) fn construct_onion_keys<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, secp256k1::Error> {
+ let mut res = Vec::with_capacity(path.hops.len());
- construct_onion_keys_callback(secp_ctx, path, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _, _| {
+ construct_onion_keys_callback(secp_ctx, &path.hops, session_priv, |shared_secret, _blinding_factor, ephemeral_pubkey, _, _| {
let (rho, mu) = gen_rho_mu_from_shared_secret(shared_secret.as_ref());
res.push(OnionKeys {
}
/// returns the hop data, as well as the first-hop value_msat and CLTV value we should send.
-pub(super) fn build_onion_payloads(path: &Vec<RouteHop>, total_msat: u64, mut recipient_onion: RecipientOnionFields, starting_htlc_offset: u32, keysend_preimage: &Option<PaymentPreimage>) -> Result<(Vec<msgs::OnionHopData>, u64, u32), APIError> {
+pub(super) fn build_onion_payloads(path: &Path, total_msat: u64, mut recipient_onion: RecipientOnionFields, starting_htlc_offset: u32, keysend_preimage: &Option<PaymentPreimage>) -> Result<(Vec<msgs::OnionHopData>, u64, u32), APIError> {
let mut cur_value_msat = 0u64;
let mut cur_cltv = starting_htlc_offset;
let mut last_short_channel_id = 0;
- let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(path.len());
+ let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(path.hops.len());
- for (idx, hop) in path.iter().rev().enumerate() {
+ for (idx, hop) in path.hops.iter().rev().enumerate() {
// First hop gets special values so that it can check, on receipt, that everything is
// exactly as it should be (and the next hop isn't trying to probe to find out if we're
// the intended recipient).
let mut is_from_final_node = false;
// Handle packed channel/node updates for passing back for the route handler
- construct_onion_keys_callback(secp_ctx, path, session_priv, |shared_secret, _, _, route_hop, route_hop_idx| {
+ construct_onion_keys_callback(secp_ctx, &path.hops, session_priv, |shared_secret, _, _, route_hop, route_hop_idx| {
if res.is_some() { return; }
let amt_to_forward = htlc_msat - route_hop.fee_msat;
// The failing hop includes either the inbound channel to the recipient or the outbound
// channel from the current hop (i.e., the next hop's inbound channel).
- is_from_final_node = route_hop_idx + 1 == path.len();
- let failing_route_hop = if is_from_final_node { route_hop } else { &path[route_hop_idx + 1] };
+ is_from_final_node = route_hop_idx + 1 == path.hops.len();
+ let failing_route_hop = if is_from_final_node { route_hop } else { &path.hops[route_hop_idx + 1] };
if let Ok(err_packet) = msgs::DecodedOnionErrorPacket::read(&mut Cursor::new(&packet_decrypted)) {
let um = gen_um_from_shared_secret(shared_secret.as_ref());
} else {
log_trace!(logger, "Failure provided features a channel update without type prefix. Deprecated, but allowing for now.");
}
- if let Ok(chan_update) = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice)) {
+ let update_opt = msgs::ChannelUpdate::read(&mut Cursor::new(&update_slice));
+ if update_opt.is_ok() || update_slice.is_empty() {
// if channel_update should NOT have caused the failure:
// MAY treat the channel_update as invalid.
let is_chan_update_invalid = match error_code & 0xff {
7 => false,
- 11 => amt_to_forward > chan_update.contents.htlc_minimum_msat,
- 12 => amt_to_forward
- .checked_mul(chan_update.contents.fee_proportional_millionths as u64)
+ 11 => update_opt.is_ok() &&
+ amt_to_forward >
+ update_opt.as_ref().unwrap().contents.htlc_minimum_msat,
+ 12 => update_opt.is_ok() && amt_to_forward
+ .checked_mul(update_opt.as_ref().unwrap()
+ .contents.fee_proportional_millionths as u64)
.map(|prop_fee| prop_fee / 1_000_000)
- .and_then(|prop_fee| prop_fee.checked_add(chan_update.contents.fee_base_msat as u64))
+ .and_then(|prop_fee| prop_fee.checked_add(
+ update_opt.as_ref().unwrap().contents.fee_base_msat as u64))
.map(|fee_msats| route_hop.fee_msat >= fee_msats)
.unwrap_or(false),
- 13 => route_hop.cltv_expiry_delta as u16 >= chan_update.contents.cltv_expiry_delta,
+ 13 => update_opt.is_ok() &&
+ route_hop.cltv_expiry_delta as u16 >=
+ update_opt.as_ref().unwrap().contents.cltv_expiry_delta,
14 => false, // expiry_too_soon; always valid?
- 20 => chan_update.contents.flags & 2 == 0,
+ 20 => update_opt.as_ref().unwrap().contents.flags & 2 == 0,
_ => false, // unknown error code; take channel_update as valid
};
if is_chan_update_invalid {
is_permanent: true,
});
} else {
- // Make sure the ChannelUpdate contains the expected
- // short channel id.
- if failing_route_hop.short_channel_id == chan_update.contents.short_channel_id {
- short_channel_id = Some(failing_route_hop.short_channel_id);
+ if let Ok(chan_update) = update_opt {
+ // Make sure the ChannelUpdate contains the expected
+ // short channel id.
+ if failing_route_hop.short_channel_id == chan_update.contents.short_channel_id {
+ short_channel_id = Some(failing_route_hop.short_channel_id);
+ } else {
+ log_info!(logger, "Node provided a channel_update for which it was not authoritative, ignoring.");
+ }
+ network_update = Some(NetworkUpdate::ChannelUpdateMessage {
+ msg: chan_update,
+ })
} else {
- log_info!(logger, "Node provided a channel_update for which it was not authoritative, ignoring.");
+ network_update = Some(NetworkUpdate::ChannelFailure {
+ short_channel_id: route_hop.short_channel_id,
+ is_permanent: false,
+ });
}
- network_update = Some(NetworkUpdate::ChannelUpdateMessage {
- msg: chan_update,
- })
};
+ } else {
+ // If the channel_update had a non-zero length (i.e. was
+ // present) but we couldn't read it, treat it as a total
+ // node failure.
+ log_info!(logger,
+ "Failed to read a channel_update of len {} in an onion",
+ update_slice.len());
}
}
}
// generally ignores its view of our own channels as we provide them via
// ChannelDetails.
if let &HTLCSource::OutboundRoute { ref path, .. } = htlc_source {
- (None, Some(path.first().unwrap().short_channel_id), true, Some(*failure_code), Some(data.clone()))
+ (None, Some(path.hops[0].short_channel_id), true, Some(*failure_code), Some(data.clone()))
} else { unreachable!(); }
}
}
use crate::prelude::*;
use crate::ln::PaymentHash;
use crate::ln::features::{ChannelFeatures, NodeFeatures};
- use crate::routing::router::{Route, RouteHop};
+ use crate::routing::router::{Path, Route, RouteHop};
use crate::ln::msgs;
use crate::util::ser::{Writeable, Writer, VecWriter};
let secp_ctx = Secp256k1::new();
let route = Route {
- paths: vec![vec![
+ paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // We fill in the payloads manually instead of generating them from RouteHops.
},
- ]],
+ ], blinded_tail: None }],
payment_params: None,
};
let onion_keys = super::construct_onion_keys(&secp_ctx, &route.paths[0], &get_test_session_key()).unwrap();
- assert_eq!(onion_keys.len(), route.paths[0].len());
+ assert_eq!(onion_keys.len(), route.paths[0].hops.len());
onion_keys
}
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
use crate::ln::channelmanager::{ChannelDetails, HTLCSource, IDEMPOTENCY_TIMEOUT_TICKS, PaymentId};
use crate::ln::onion_utils::HTLCFailReason;
-use crate::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters, RoutePath, Router};
+use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
use crate::util::errors::APIError;
use crate::util::logger::Logger;
use crate::util::time::Time;
use crate::util::time::tests::SinceEpoch;
use crate::util::ser::ReadableArgs;
-use core::cmp;
use core::fmt::{self, Display, Formatter};
use core::ops::Deref;
}
/// panics if path is None and !self.is_fulfilled
- fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Vec<RouteHop>>) -> bool {
+ fn remove(&mut self, session_priv: &[u8; 32], path: Option<&Path>) -> bool {
let remove_res = match self {
PendingOutboundPayment::Legacy { session_privs } |
PendingOutboundPayment::Retryable { session_privs, .. } |
if remove_res {
if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
let path = path.expect("Fulfilling a payment should always come with a path");
- let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
- *pending_amt_msat -= path_last_hop.fee_msat;
+ *pending_amt_msat -= path.final_value_msat();
if let Some(fee_msat) = pending_fee_msat.as_mut() {
- *fee_msat -= path.get_path_fees();
+ *fee_msat -= path.fee_msat();
}
}
}
remove_res
}
- pub(super) fn insert(&mut self, session_priv: [u8; 32], path: &Vec<RouteHop>) -> bool {
+ pub(super) fn insert(&mut self, session_priv: [u8; 32], path: &Path) -> bool {
let insert_res = match self {
PendingOutboundPayment::Legacy { session_privs } |
PendingOutboundPayment::Retryable { session_privs, .. } => {
};
if insert_res {
if let PendingOutboundPayment::Retryable { ref mut pending_amt_msat, ref mut pending_fee_msat, .. } = self {
- let path_last_hop = path.last().expect("Outbound payments must have had a valid path");
- *pending_amt_msat += path_last_hop.fee_msat;
+ *pending_amt_msat += path.final_value_msat();
if let Some(fee_msat) = pending_fee_msat.as_mut() {
- *fee_msat += path.get_path_fees();
+ *fee_msat += path.fee_msat();
}
}
}
NS::Target: NodeSigner,
L::Target: Logger,
IH: Fn() -> InFlightHtlcs,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>,
{
self.send_payment_internal(payment_id, payment_hash, recipient_onion, None, retry_strategy,
where
ES::Target: EntropySource,
NS::Target: NodeSigner,
- F: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ F: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
let onion_session_privs = self.add_new_pending_payment(payment_hash, recipient_onion.clone(), payment_id, None, route, None, None, entropy_source, best_block_height)?;
NS::Target: NodeSigner,
L::Target: Logger,
IH: Fn() -> InFlightHtlcs,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>,
{
let preimage = payment_preimage
where
ES::Target: EntropySource,
NS::Target: NodeSigner,
- F: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ F: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
let preimage = payment_preimage
R::Target: Router,
ES::Target: EntropySource,
NS::Target: NodeSigner,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>,
IH: Fn() -> InFlightHtlcs,
FH: Fn() -> Vec<ChannelDetails>,
NS::Target: NodeSigner,
L::Target: Logger,
IH: Fn() -> InFlightHtlcs,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
#[cfg(feature = "std")] {
NS::Target: NodeSigner,
L::Target: Logger,
IH: Fn() -> InFlightHtlcs,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
#[cfg(feature = "std")] {
}
};
for path in route.paths.iter() {
- if path.len() == 0 {
- log_error!(logger, "length-0 path in route");
+ if path.hops.len() == 0 {
+ log_error!(logger, "Unusable path in route (path.hops.len() must be at least 1");
self.abandon_payment(payment_id, PaymentFailureReason::UnexpectedError, pending_events);
return
}
PendingOutboundPayment::Retryable {
total_msat, keysend_preimage, payment_secret, payment_metadata, pending_amt_msat, ..
} => {
- let retry_amt_msat: u64 = route.paths.iter().map(|path| path.last().unwrap().fee_msat).sum();
+ let retry_amt_msat = route.get_total_amount();
if retry_amt_msat + *pending_amt_msat > *total_msat * (100 + RETRY_OVERFLOW_PERCENTAGE) / 100 {
log_error!(logger, "retry_amt_msat of {} will put pending_amt_msat (currently: {}) more than 10% over total_payment_amt_msat of {}", retry_amt_msat, pending_amt_msat, total_msat);
abandon_with_entry!(payment, PaymentFailureReason::UnexpectedError);
NS::Target: NodeSigner,
L::Target: Logger,
IH: Fn() -> InFlightHtlcs,
- SP: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ SP: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
match err {
fn push_path_failed_evs_and_scids<I: ExactSizeIterator + Iterator<Item = Result<(), APIError>>, L: Deref>(
payment_id: PaymentId, payment_hash: PaymentHash, route_params: &mut RouteParameters,
- paths: Vec<Vec<RouteHop>>, path_results: I, logger: &L, pending_events: &Mutex<Vec<events::Event>>
+ paths: Vec<Path>, path_results: I, logger: &L, pending_events: &Mutex<Vec<events::Event>>
) where L::Target: Logger {
let mut events = pending_events.lock().unwrap();
debug_assert_eq!(paths.len(), path_results.len());
log_error!(logger, "Failed to send along path due to error: {:?}", e);
let mut failed_scid = None;
if let APIError::ChannelUnavailable { .. } = e {
- let scid = path[0].short_channel_id;
+ let scid = path.hops[0].short_channel_id;
failed_scid = Some(scid);
route_params.payment_params.previously_failed_channels.push(scid);
}
}
pub(super) fn send_probe<ES: Deref, NS: Deref, F>(
- &self, hops: Vec<RouteHop>, probing_cookie_secret: [u8; 32], entropy_source: &ES,
- node_signer: &NS, best_block_height: u32, send_payment_along_path: F
+ &self, path: Path, probing_cookie_secret: [u8; 32], entropy_source: &ES, node_signer: &NS,
+ best_block_height: u32, send_payment_along_path: F
) -> Result<(PaymentHash, PaymentId), PaymentSendFailure>
where
ES::Target: EntropySource,
NS::Target: NodeSigner,
- F: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ F: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
let payment_id = PaymentId(entropy_source.get_secure_random_bytes());
let payment_hash = probing_cookie_from_id(&payment_id, probing_cookie_secret);
- if hops.len() < 2 {
+ if path.hops.len() < 2 && path.blinded_tail.is_none() {
return Err(PaymentSendFailure::ParameterError(APIError::APIMisuseError {
err: "No need probing a path with less than two hops".to_string()
}))
}
- let route = Route { paths: vec![hops], payment_params: None };
+ let route = Route { paths: vec![path], payment_params: None };
let onion_session_privs = self.add_new_pending_payment(payment_hash,
RecipientOnionFields::spontaneous_empty(), payment_id, None, &route, None, None,
entropy_source, best_block_height)?;
) -> Result<(), PaymentSendFailure>
where
NS::Target: NodeSigner,
- F: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ F: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
if route.paths.len() < 1 {
let our_node_id = node_signer.get_node_id(Recipient::Node).unwrap(); // TODO no unwrap
let mut path_errs = Vec::with_capacity(route.paths.len());
'path_check: for path in route.paths.iter() {
- if path.len() < 1 || path.len() > 20 {
+ if path.hops.len() < 1 || path.hops.len() > 20 {
path_errs.push(Err(APIError::InvalidRoute{err: "Path didn't go anywhere/had bogus size".to_owned()}));
continue 'path_check;
}
- for (idx, hop) in path.iter().enumerate() {
- if idx != path.len() - 1 && hop.pubkey == our_node_id {
+ if path.blinded_tail.is_some() {
+ path_errs.push(Err(APIError::InvalidRoute{err: "Sending to blinded paths isn't supported yet".to_owned()}));
+ continue 'path_check;
+ }
+ let dest_hop_idx = if path.blinded_tail.is_some() && path.blinded_tail.as_ref().unwrap().hops.len() > 1 {
+ usize::max_value() } else { path.hops.len() - 1 };
+ for (idx, hop) in path.hops.iter().enumerate() {
+ if idx != dest_hop_idx && hop.pubkey == our_node_id {
path_errs.push(Err(APIError::InvalidRoute{err: "Path went through us but wasn't a simple rebalance loop to us".to_owned()}));
continue 'path_check;
}
}
- total_value += path.last().unwrap().fee_msat;
+ total_value += path.final_value_msat();
path_errs.push(Ok(()));
}
if path_errs.iter().any(|e| e.is_err()) {
let mut has_ok = false;
let mut has_err = false;
let mut pending_amt_unsent = 0;
- let mut max_unsent_cltv_delta = 0;
for (res, path) in results.iter().zip(route.paths.iter()) {
if res.is_ok() { has_ok = true; }
if res.is_err() { has_err = true; }
has_err = true;
has_ok = true;
} else if res.is_err() {
- pending_amt_unsent += path.last().unwrap().fee_msat;
- max_unsent_cltv_delta = cmp::max(max_unsent_cltv_delta, path.last().unwrap().cltv_expiry_delta);
+ pending_amt_unsent += path.final_value_msat();
}
}
if has_err && has_ok {
) -> Result<(), PaymentSendFailure>
where
NS::Target: NodeSigner,
- F: Fn(&Vec<RouteHop>, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
+ F: Fn(&Path, &PaymentHash, RecipientOnionFields, u64, u32, PaymentId,
&Option<PaymentPreimage>, [u8; 32]) -> Result<(), APIError>
{
self.pay_route_internal(route, payment_hash, recipient_onion, keysend_preimage, payment_id,
pub(super) fn claim_htlc<L: Deref>(
&self, payment_id: PaymentId, payment_preimage: PaymentPreimage, session_priv: SecretKey,
- path: Vec<RouteHop>, from_onchain: bool, pending_events: &Mutex<Vec<events::Event>>, logger: &L
+ path: Path, from_onchain: bool, pending_events: &Mutex<Vec<events::Event>>, logger: &L
) where L::Target: Logger {
let mut session_priv_bytes = [0; 32];
session_priv_bytes.copy_from_slice(&session_priv[..]);
// Returns a bool indicating whether a PendingHTLCsForwardable event should be generated.
pub(super) fn fail_htlc<L: Deref>(
&self, source: &HTLCSource, payment_hash: &PaymentHash, onion_error: &HTLCFailReason,
- path: &Vec<RouteHop>, session_priv: &SecretKey, payment_id: &PaymentId,
- probing_cookie_secret: [u8; 32], secp_ctx: &Secp256k1<secp256k1::All>,
- pending_events: &Mutex<Vec<events::Event>>, logger: &L
+ path: &Path, session_priv: &SecretKey, payment_id: &PaymentId, probing_cookie_secret: [u8; 32],
+ secp_ctx: &Secp256k1<secp256k1::All>, pending_events: &Mutex<Vec<events::Event>>, logger: &L
) -> bool where L::Target: Logger {
#[cfg(test)]
let (network_update, short_channel_id, payment_retryable, onion_error_code, onion_error_data) = onion_error.decode_onion_failure(secp_ctx, logger, &source);
use crate::ln::msgs::{ErrorAction, LightningError};
use crate::ln::outbound_payment::{OutboundPayments, Retry, RetryableSendFailure};
use crate::routing::gossip::NetworkGraph;
- use crate::routing::router::{InFlightHtlcs, PaymentParameters, Route, RouteHop, RouteParameters};
+ use crate::routing::router::{InFlightHtlcs, Path, PaymentParameters, Route, RouteHop, RouteParameters};
use crate::sync::{Arc, Mutex};
use crate::util::errors::APIError;
use crate::util::test_utils;
};
let failed_scid = 42;
let route = Route {
- paths: vec![vec![RouteHop {
+ paths: vec![Path { hops: vec![RouteHop {
pubkey: receiver_pk,
node_features: NodeFeatures::empty(),
short_channel_id: failed_scid,
channel_features: ChannelFeatures::empty(),
fee_msat: 0,
cltv_expiry_delta: 0,
- }]],
+ }], blinded_tail: None }],
payment_params: Some(payment_params),
};
router.expect_find_route(route_params.clone(), Ok(route.clone()));
use crate::ln::msgs::ChannelMessageHandler;
use crate::ln::outbound_payment::Retry;
use crate::routing::gossip::{EffectiveCapacity, RoutingFees};
-use crate::routing::router::{get_route, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters};
+use crate::routing::router::{get_route, Path, PaymentParameters, Route, Router, RouteHint, RouteHintHop, RouteHop, RouteParameters};
use crate::routing::scoring::ChannelUsage;
use crate::util::test_utils;
use crate::util::errors::APIError;
let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(&nodes[0], nodes[3], 100000);
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_id;
- route.paths[0][1].short_channel_id = chan_3_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_id;
- route.paths[1][1].short_channel_id = chan_4_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_id;
send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], 200_000, payment_hash, payment_secret);
fail_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_hash);
}
let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], amt_msat);
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_update.contents.short_channel_id;
- route.paths[0][1].short_channel_id = chan_3_update.contents.short_channel_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_update.contents.short_channel_id;
- route.paths[1][1].short_channel_id = chan_4_update.contents.short_channel_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_update.contents.short_channel_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_update.contents.short_channel_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_update.contents.short_channel_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_update.contents.short_channel_id;
// Initiate the MPP payment.
let payment_id = PaymentId(payment_hash.0);
let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[3], 100_000);
let path = route.paths[0].clone();
route.paths.push(path);
- route.paths[0][0].pubkey = nodes[1].node.get_our_node_id();
- route.paths[0][0].short_channel_id = chan_1_update.contents.short_channel_id;
- route.paths[0][1].short_channel_id = chan_3_update.contents.short_channel_id;
- route.paths[1][0].pubkey = nodes[2].node.get_our_node_id();
- route.paths[1][0].short_channel_id = chan_2_update.contents.short_channel_id;
- route.paths[1][1].short_channel_id = chan_4_update.contents.short_channel_id;
+ route.paths[0].hops[0].pubkey = nodes[1].node.get_our_node_id();
+ route.paths[0].hops[0].short_channel_id = chan_1_update.contents.short_channel_id;
+ route.paths[0].hops[1].short_channel_id = chan_3_update.contents.short_channel_id;
+ route.paths[1].hops[0].pubkey = nodes[2].node.get_our_node_id();
+ route.paths[1].hops[0].short_channel_id = chan_2_update.contents.short_channel_id;
+ route.paths[1].hops[1].short_channel_id = chan_4_update.contents.short_channel_id;
// Initiate the MPP payment.
nodes[0].node.send_payment_with_route(&route, payment_hash,
let mut new_config = channel.config();
new_config.forwarding_fee_base_msat += 100_000;
channel.update_config(&new_config);
- new_route.paths[0][0].fee_msat += 100_000;
+ new_route.paths[0].hops[0].fee_msat += 100_000;
}
// Force expiration of the channel's previous config.
assert_eq!(events.len(), 1);
pass_along_path(&nodes[0], &[&nodes[1], &nodes[2]], 1_000_000, payment_hash, Some(payment_secret), events.pop().unwrap(), true, None);
do_claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], false, payment_preimage);
- expect_payment_sent!(nodes[0], payment_preimage, Some(new_route.paths[0][0].fee_msat));
+ expect_payment_sent!(nodes[0], payment_preimage, Some(new_route.paths[0].hops[0].fee_msat));
}
#[test]
mine_transaction(&nodes[0], &bs_commitment_tx[0]);
mine_transaction(&nodes[1], &bs_commitment_tx[0]);
if !use_dust {
- connect_blocks(&nodes[0], TEST_FINAL_CLTV - 1 + (MIN_CLTV_EXPIRY_DELTA as u32));
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1 + (MIN_CLTV_EXPIRY_DELTA as u32));
+ connect_blocks(&nodes[0], TEST_FINAL_CLTV + (MIN_CLTV_EXPIRY_DELTA as u32));
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV + (MIN_CLTV_EXPIRY_DELTA as u32));
let as_htlc_timeout = nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
check_spends!(as_htlc_timeout[0], bs_commitment_tx[0]);
assert_eq!(as_htlc_timeout.len(), 1);
let route = get_route(
&nodes[0].node.get_our_node_id(), &payment_params, &nodes[0].network_graph.read_only(),
Some(&nodes[0].node.list_usable_channels().iter().collect::<Vec<_>>()),
- amt_msat, TEST_FINAL_CLTV, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
+ amt_msat, nodes[0].logger, &scorer, &random_seed_bytes).unwrap();
nodes[0].node.send_payment_with_route(&route, payment_hash,
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
check_added_monitors!(nodes[0], 1);
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
- let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 9_998_000, 42);
+ let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 9_998_000);
let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42);
// Send a dust HTLC, which will be treated as if it timed out once the channel hits the chain.
- let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 1_000, 42);
+ let (route, _, _, _) = get_route_and_payment_hash!(&nodes[0], nodes[2], &payment_params, 1_000);
let (payment_hash, payment_id) = nodes[0].node.send_probe(route.paths[0].clone()).unwrap();
// node[0] -- update_add_htlcs -> node[1]
let route = get_route(
&nodes[0].node.get_our_node_id(), &route_params.payment_params,
&nodes[0].network_graph.read_only(), None, route_params.final_value_msat,
- route_params.payment_params.final_cltv_expiry_delta, nodes[0].logger, &scorer,
- &random_seed_bytes,
+ nodes[0].logger, &scorer, &random_seed_bytes,
).unwrap();
let (payment_hash, payment_secret) = nodes[2].node.create_inbound_payment(Some(amt_msat), 60 * 60, None).unwrap();
// Configure the initial send, retry1 and retry2's paths.
let send_route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_id,
channel_features: nodes[1].node.channel_features(),
fee_msat: amt_msat / 2,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_2_id,
channel_features: nodes[1].node.channel_features(),
fee_msat: amt_msat / 2,
cltv_expiry_delta: 100,
- }],
+ }], blinded_tail: None },
],
payment_params: Some(route_params.payment_params.clone()),
};
let retry_1_route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_id,
channel_features: nodes[1].node.channel_features(),
fee_msat: amt_msat / 4,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_3_id,
channel_features: nodes[1].node.channel_features(),
fee_msat: amt_msat / 4,
cltv_expiry_delta: 100,
- }],
+ }], blinded_tail: None },
],
payment_params: Some(route_params.payment_params.clone()),
};
let retry_2_route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_id,
channel_features: nodes[1].node.channel_features(),
fee_msat: amt_msat / 4,
cltv_expiry_delta: 100,
- }],
+ }], blinded_tail: None },
],
payment_params: Some(route_params.payment_params.clone()),
};
let chans = nodes[0].node.list_usable_channels();
let mut route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chans[0].short_channel_id.unwrap(),
channel_features: nodes[1].node.channel_features(),
fee_msat: 10_000,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chans[1].short_channel_id.unwrap(),
channel_features: nodes[1].node.channel_features(),
fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
cltv_expiry_delta: 100,
- }],
+ }], blinded_tail: None },
],
payment_params: Some(payment_params),
};
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
// On retry, split the payment across both channels.
- route.paths[0][0].fee_msat = 50_000_001;
- route.paths[1][0].fee_msat = 50_000_000;
+ route.paths[0].hops[0].fee_msat = 50_000_001;
+ route.paths[1].hops[0].fee_msat = 50_000_000;
let mut pay_params = route.payment_params.clone().unwrap();
pay_params.previously_failed_channels.push(chans[1].short_channel_id.unwrap());
nodes[0].router.expect_find_route(RouteParameters {
short_channel_id: Some(expected_scid), .. } =>
{
assert_eq!(payment_hash, ev_payment_hash);
- assert_eq!(expected_scid, route.paths[1][0].short_channel_id);
+ assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
assert!(err_msg.contains("max HTLC"));
},
_ => panic!("Unexpected event"),
let chans = nodes[0].node.list_usable_channels();
let mut route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chans[0].short_channel_id.unwrap(),
channel_features: nodes[1].node.channel_features(),
fee_msat: 100_000_001, // Our default max-HTLC-value is 10% of the channel value, which this is one more than
cltv_expiry_delta: 100,
- }],
+ }], blinded_tail: None },
],
payment_params: Some(PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV)),
};
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
// On retry, split the payment across both channels.
route.paths.push(route.paths[0].clone());
- route.paths[0][0].short_channel_id = chans[1].short_channel_id.unwrap();
- route.paths[0][0].fee_msat = 50_000_000;
- route.paths[1][0].fee_msat = 50_000_001;
+ route.paths[0].hops[0].short_channel_id = chans[1].short_channel_id.unwrap();
+ route.paths[0].hops[0].fee_msat = 50_000_000;
+ route.paths[1].hops[0].fee_msat = 50_000_001;
let mut pay_params = route_params.payment_params.clone();
pay_params.previously_failed_channels.push(chans[0].short_channel_id.unwrap());
nodes[0].router.expect_find_route(RouteParameters {
short_channel_id: Some(expected_scid), .. } =>
{
assert_eq!(payment_hash, ev_payment_hash);
- assert_eq!(expected_scid, route.paths[1][0].short_channel_id);
+ assert_eq!(expected_scid, route.paths[1].hops[0].short_channel_id);
assert!(err_msg.contains("max HTLC"));
},
_ => panic!("Unexpected event"),
let mut route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_scid,
channel_features: nodes[2].node.channel_features(),
fee_msat: 100_000_000,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_scid,
channel_features: nodes[2].node.channel_features(),
fee_msat: 100_000_000,
cltv_expiry_delta: 100,
- }]
+ }], blinded_tail: None }
],
payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
};
second_payment_params.previously_failed_channels = vec![chan_2_scid, chan_2_scid];
// On retry, we'll only return one path
route.paths.remove(1);
- route.paths[0][1].fee_msat = amt_msat;
+ route.paths[0].hops[1].fee_msat = amt_msat;
nodes[0].router.expect_find_route(RouteParameters {
payment_params: second_payment_params,
final_value_msat: amt_msat,
let mut route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_scid,
channel_features: nodes[2].node.channel_features(),
fee_msat: 100_000_000,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_scid,
channel_features: nodes[2].node.channel_features(),
fee_msat: 100_000_000,
cltv_expiry_delta: 100,
- }]
+ }], blinded_tail: None }
],
payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
};
let mut route = Route {
paths: vec![
- vec![RouteHop {
+ Path { hops: vec![RouteHop {
pubkey: nodes[1].node.get_our_node_id(),
node_features: nodes[1].node.node_features(),
short_channel_id: chan_1_scid,
channel_features: nodes[2].node.channel_features(),
fee_msat: amt_msat / 1000,
cltv_expiry_delta: 100,
- }],
- vec![RouteHop {
+ }], blinded_tail: None },
+ Path { hops: vec![RouteHop {
pubkey: nodes[2].node.get_our_node_id(),
node_features: nodes[2].node.node_features(),
short_channel_id: chan_3_scid,
channel_features: nodes[3].node.channel_features(),
fee_msat: amt_msat - amt_msat / 1000,
cltv_expiry_delta: 100,
- }]
+ }], blinded_tail: None }
],
payment_params: Some(PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)),
};
// we should still ultimately fail for the same reason - because we're trying to send too
// many HTLCs at once.
let mut new_route_params = route_params.clone();
- previously_failed_channels.push(route.paths[0][1].short_channel_id);
+ previously_failed_channels.push(route.paths[0].hops[1].short_channel_id);
new_route_params.payment_params.previously_failed_channels = previously_failed_channels.clone();
- route.paths[0][1].short_channel_id += 1;
+ route.paths[0].hops[1].short_channel_id += 1;
nodes[0].router.expect_find_route(new_route_params, Ok(route.clone()));
let bs_fail_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
let mut route = nodes[0].router.find_route(&nodes[0].node.get_our_node_id(), &route_params,
None, &nodes[0].node.compute_inflight_htlcs()).unwrap();
// Make sure the route is ordered as the B->D path before C->D
- route.paths.sort_by(|a, _| if a[0].pubkey == nodes[1].node.get_our_node_id() {
+ route.paths.sort_by(|a, _| if a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
std::cmp::Ordering::Less } else { std::cmp::Ordering::Greater });
// Note that we add an extra 1 in the send pipeline to compensate for any blocks found while
// the HTLC is being relayed.
- route.paths[0][1].cltv_expiry_delta = TEST_FINAL_CLTV + 8;
- route.paths[1][1].cltv_expiry_delta = TEST_FINAL_CLTV + 12;
+ route.paths[0].hops[1].cltv_expiry_delta = TEST_FINAL_CLTV + 8;
+ route.paths[1].hops[1].cltv_expiry_delta = TEST_FINAL_CLTV + 12;
let final_cltv = nodes[0].best_block_info().1 + TEST_FINAL_CLTV + 8 + 1;
nodes[0].router.expect_find_route(route_params.clone(), Ok(route.clone()));
sync_status: InitSyncTracker,
msgs_sent_since_pong: usize,
- awaiting_pong_timer_tick_intervals: i8,
+ awaiting_pong_timer_tick_intervals: i64,
received_message_since_timer_tick: bool,
sent_gossip_timestamp_filter: bool,
match self.do_read_event(peer_descriptor, data) {
Ok(res) => Ok(res),
Err(e) => {
- log_trace!(self.logger, "Peer sent invalid data or we decided to disconnect due to a protocol error");
+ log_trace!(self.logger, "Disconnecting peer due to a protocol error (usually a duplicate connection).");
self.disconnect_event_internal(peer_descriptor);
Err(e)
}
if let Some((node_id, _)) = peer.their_node_id {
self.node_id_to_descriptor.lock().unwrap().remove(&node_id);
}
- self.do_disconnect(descriptor, &*peer, "ping timeout");
+ self.do_disconnect(descriptor, &*peer, "ping/handshake timeout");
}
}
}
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(last_hops);
- let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, TEST_FINAL_CLTV);
+ let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000);
nodes[0].node.send_payment_with_route(&route, our_payment_hash,
RecipientOnionFields::secret_only(our_payment_secret), PaymentId(our_payment_hash.0)).unwrap();
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(hop_hints);
- let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
- assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
+ let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
nodes[0].node.send_payment_with_route(&route, payment_hash,
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
check_added_monitors!(nodes[0], 1);
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(hop_hints.clone());
- let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42);
- assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
+ let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
nodes[0].node.send_payment_with_route(&route, payment_hash,
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
check_added_monitors!(nodes[0], 1);
let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(hop_hints);
- let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42);
- assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap());
+ let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000);
+ assert_eq!(route_2.paths[0].hops[1].short_channel_id, last_hop[0].short_channel_id.unwrap());
nodes[0].node.send_payment_with_route(&route_2, payment_hash_2,
RecipientOnionFields::secret_only(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
check_added_monitors!(nodes[0], 1);
let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
.with_features(nodes[2].node.invoice_features())
.with_route_hints(hop_hints);
- let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42);
- assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
+ let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
- route.paths[0][1].fee_msat = 10_000_000; // Overshoot the last channel's value
+ route.paths[0].hops[1].fee_msat = 10_000_000; // Overshoot the last channel's value
// Route the HTLC through to the destination.
nodes[0].node.send_payment_with_route(&route, payment_hash,
PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap())
.blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data));
- route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount
- route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0
+ route.paths[0].hops[1].fee_msat = 10_000; // Reset to the correct payment amount
+ route.paths[0].hops[0].fee_msat = 0; // But set fee paid to the middle hop to 0
// Route the HTLC through to the destination.
nodes[0].node.send_payment_with_route(&route, payment_hash,
assert_eq!(nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(), real_scid);
let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000);
- assert_eq!(route.paths[0][0].short_channel_id, real_scid);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, real_scid);
send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1]]], 10_000, payment_hash, payment_secret);
claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
assert_eq!(route.paths.len(), 2);
route.paths.sort_by(|path_a, _| {
// Sort the path so that the path through nodes[1] comes first
- if path_a[0].pubkey == nodes[1].node.get_our_node_id() {
+ if path_a.hops[0].pubkey == nodes[1].node.get_our_node_id() {
core::cmp::Ordering::Less } else { core::cmp::Ordering::Greater }
});
let (mut route, payment_hash, payment_preimage, payment_secret) =
get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000);
if use_intercept {
- route.paths[0][1].short_channel_id = intercept_scid;
+ route.paths[0].hops[1].short_channel_id = intercept_scid;
}
let payment_id = PaymentId(nodes[0].keys_manager.backing.get_secure_random_bytes());
let htlc_expiry = nodes[0].best_block_info().1 + TEST_FINAL_CLTV;
if claim_htlc {
confirm_transaction(&nodes[1], &cs_commitment_tx[1]);
} else {
- connect_blocks(&nodes[1], htlc_expiry - nodes[1].best_block_info().1);
+ connect_blocks(&nodes[1], htlc_expiry - nodes[1].best_block_info().1 + 1);
let bs_htlc_timeout_tx = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0);
assert_eq!(bs_htlc_timeout_tx.len(), 1);
confirm_transaction(&nodes[1], &bs_htlc_timeout_tx[0]);
// Give node 1 node 2's commitment transaction and get its response (timing the HTLC out)
mine_transaction(&nodes[1], &node_2_commitment_txn[0]);
- connect_blocks(&nodes[1], TEST_FINAL_CLTV - 1); // Confirm blocks until the HTLC expires
+ connect_blocks(&nodes[1], TEST_FINAL_CLTV); // Confirm blocks until the HTLC expires
let node_1_commitment_txn = nodes[1].tx_broadcaster.txn_broadcasted.lock().unwrap().clone();
assert_eq!(node_1_commitment_txn.len(), 1); // ChannelMonitor: 1 offered HTLC-Timeout
check_spends!(node_1_commitment_txn[0], node_2_commitment_txn[0]);
let (_, payment_hash, payment_secret) = get_payment_preimage_hash!(nodes[0]);
let payment_params_1 = PaymentParameters::from_node_id(nodes[1].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[1].node.invoice_features());
- let route_1 = get_route(&nodes[0].node.get_our_node_id(), &payment_params_1, &nodes[0].network_graph.read_only(), None, 100000, TEST_FINAL_CLTV, &logger, &scorer, &random_seed_bytes).unwrap();
+ let route_1 = get_route(&nodes[0].node.get_our_node_id(), &payment_params_1, &nodes[0].network_graph.read_only(), None, 100000, &logger, &scorer, &random_seed_bytes).unwrap();
let payment_params_2 = PaymentParameters::from_node_id(nodes[0].node.get_our_node_id(), TEST_FINAL_CLTV).with_features(nodes[0].node.invoice_features());
- let route_2 = get_route(&nodes[1].node.get_our_node_id(), &payment_params_2, &nodes[1].network_graph.read_only(), None, 100000, TEST_FINAL_CLTV, &logger, &scorer, &random_seed_bytes).unwrap();
+ let route_2 = get_route(&nodes[1].node.get_our_node_id(), &payment_params_2, &nodes[1].network_graph.read_only(), None, 100000, &logger, &scorer, &random_seed_bytes).unwrap();
unwrap_send_err!(nodes[0].node.send_payment_with_route(&route_1, payment_hash,
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)
), true, APIError::ChannelUnavailable {..}, {});
//!
//! # use lightning::ln::PaymentHash;
//! # use lightning::offers::invoice::BlindedPayInfo;
-//! # use lightning::onion_message::BlindedPath;
+//! # use lightning::blinded_path::BlindedPath;
//! #
//! # fn create_payment_paths() -> Vec<(BlindedPath, BlindedPayInfo)> { unimplemented!() }
//! # fn create_payment_hash() -> PaymentHash { unimplemented!() }
use core::convert::{Infallible, TryFrom};
use core::time::Duration;
use crate::io;
+use crate::blinded_path::BlindedPath;
use crate::ln::PaymentHash;
use crate::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
use crate::ln::inbound_payment::ExpandedKey;
use crate::offers::payer::{PAYER_METADATA_TYPE, PayerTlvStream, PayerTlvStreamRef};
use crate::offers::refund::{IV_BYTES as REFUND_IV_BYTES, Refund, RefundContents};
use crate::offers::signer;
-use crate::onion_message::BlindedPath;
use crate::util::ser::{HighZeroBytesDroppedBigSize, Iterable, SeekReadable, WithoutLength, Writeable, Writer};
use crate::util::string::PrintableString;
///
/// See [module-level documentation] for usage.
///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
/// [`Refund`]: crate::offers::refund::Refund
/// [module-level documentation]: self
}
/// Indicates how [`Invoice::signing_pubkey`] was set.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait SigningPubkeyStrategy {}
/// [`Invoice::signing_pubkey`] was explicitly set.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitSigningPubkey {}
/// [`Invoice::signing_pubkey`] was derived.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedSigningPubkey {}
impl SigningPubkeyStrategy for ExplicitSigningPubkey {}
}
/// Signs the invoice using the given function.
+ ///
+ /// This is not exported to bindings users as functions aren't currently mapped.
pub fn sign<F, E>(self, sign: F) -> Result<Invoice, SignError<E>>
where
F: FnOnce(&Message) -> Result<Signature, E>
/// An invoice may be sent in response to an [`InvoiceRequest`] in the case of an offer or sent
/// directly after scanning a refund. It includes all the information needed to pay a recipient.
///
+/// This is not exported to bindings users as its name conflicts with the BOLT 11 Invoice type.
+///
/// [`Offer`]: crate::offers::offer::Offer
/// [`Refund`]: crate::offers::refund::Refund
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
>;
/// Information needed to route a payment across a [`BlindedPath`].
-#[derive(Clone, Debug, PartialEq)]
+#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct BlindedPayInfo {
/// Base fee charged (in millisatoshi) for the entire blinded path.
pub fee_base_msat: u32,
use bitcoin::util::schnorr::TweakedPublicKey;
use core::convert::TryFrom;
use core::time::Duration;
+ use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::chain::keysinterface::KeyMaterial;
use crate::ln::features::Bolt12InvoiceFeatures;
use crate::ln::inbound_payment::ExpandedKey;
use crate::offers::payer::PayerTlvStreamRef;
use crate::offers::refund::RefundBuilder;
use crate::offers::test_utils::*;
- use crate::onion_message::{BlindedHop, BlindedPath};
use crate::util::ser::{BigSize, Iterable, Writeable};
use crate::util::string::PrintableString;
use core::ops::Deref;
use crate::chain::keysinterface::EntropySource;
use crate::io;
+use crate::blinded_path::BlindedPath;
use crate::ln::PaymentHash;
use crate::ln::features::InvoiceRequestFeatures;
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
use crate::offers::parse::{ParseError, ParsedMessage, SemanticError};
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
use crate::offers::signer::{Metadata, MetadataMaterial};
-use crate::onion_message::BlindedPath;
use crate::util::ser::{HighZeroBytesDroppedBigSize, SeekReadable, WithoutLength, Writeable, Writer};
use crate::util::string::PrintableString;
///
/// See [module-level documentation] for usage.
///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+///
/// [module-level documentation]: self
pub struct InvoiceRequestBuilder<'a, 'b, P: PayerIdStrategy, T: secp256k1::Signing> {
offer: &'a Offer,
}
/// Indicates how [`InvoiceRequest::payer_id`] will be set.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait PayerIdStrategy {}
/// [`InvoiceRequest::payer_id`] will be explicitly set.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitPayerId {}
/// [`InvoiceRequest::payer_id`] will be derived.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedPayerId {}
impl PayerIdStrategy for ExplicitPayerId {}
impl<'a> UnsignedInvoiceRequest<'a> {
/// Signs the invoice request using the given function.
+ ///
+ /// This is not exported to bindings users as functions are not yet mapped.
pub fn sign<F, E>(self, sign: F) -> Result<InvoiceRequest, SignError<E>>
where
F: FnOnce(&Message) -> Result<Signature, E>
/// See [`InvoiceRequest::respond_with_no_std`] for further details where the aforementioned
/// creation time is used for the `created_at` parameter.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Duration`]: core::time::Duration
#[cfg(feature = "std")]
pub fn respond_with(
///
/// Errors if the request contains unknown required features.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
pub fn respond_with_no_std(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
///
/// See [`InvoiceRequest::respond_with`] for further details.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice`]: crate::offers::invoice::Invoice
#[cfg(feature = "std")]
pub fn verify_and_respond_using_derived_keys<T: secp256k1::Signing>(
///
/// See [`InvoiceRequest::respond_with_no_std`] for further details.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn verify_and_respond_using_derived_keys_no_std<T: secp256k1::Signing>(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
//! use lightning::offers::parse::ParseError;
//! use lightning::util::ser::{Readable, Writeable};
//!
-//! # use lightning::onion_message::BlindedPath;
+//! # use lightning::blinded_path::BlindedPath;
//! # #[cfg(feature = "std")]
//! # use std::time::SystemTime;
//! #
use core::time::Duration;
use crate::chain::keysinterface::EntropySource;
use crate::io;
+use crate::blinded_path::BlindedPath;
use crate::ln::features::OfferFeatures;
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
use crate::ln::msgs::MAX_VALUE_MSAT;
use crate::offers::merkle::TlvStream;
use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError};
use crate::offers::signer::{Metadata, MetadataMaterial, self};
-use crate::onion_message::BlindedPath;
use crate::util::ser::{HighZeroBytesDroppedBigSize, WithoutLength, Writeable, Writer};
use crate::util::string::PrintableString;
///
/// See [module-level documentation] for usage.
///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+///
/// [module-level documentation]: self
pub struct OfferBuilder<'a, M: MetadataStrategy, T: secp256k1::Signing> {
offer: OfferContents,
}
/// Indicates how [`Offer::metadata`] may be set.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub trait MetadataStrategy {}
/// [`Offer::metadata`] may be explicitly set or left empty.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct ExplicitMetadata {}
/// [`Offer::metadata`] will be derived.
+///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
pub struct DerivedMetadata {}
impl MetadataStrategy for ExplicitMetadata {}
///
/// Useful to protect the sender's privacy.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
/// [`InvoiceRequest::metadata`]: crate::offers::invoice_request::InvoiceRequest::metadata
/// [`Invoice::verify`]: crate::offers::invoice::Invoice::verify
///
/// Useful for recurring payments using the same `payer_id` with different invoices.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`InvoiceRequest::payer_id`]: crate::offers::invoice_request::InvoiceRequest::payer_id
pub fn request_invoice_deriving_metadata<ES: Deref>(
&self, payer_id: PublicKey, expanded_key: &ExpandedKey, entropy_source: ES
///
/// Errors if the offer contains unknown required features.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
pub fn request_invoice(
&self, metadata: Vec<u8>, payer_id: PublicKey
use core::convert::TryFrom;
use core::num::NonZeroU64;
use core::time::Duration;
+ use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::chain::keysinterface::KeyMaterial;
use crate::ln::features::OfferFeatures;
use crate::ln::inbound_payment::ExpandedKey;
use crate::ln::msgs::{DecodeError, MAX_VALUE_MSAT};
use crate::offers::parse::{ParseError, SemanticError};
use crate::offers::test_utils::*;
- use crate::onion_message::{BlindedHop, BlindedPath};
use crate::util::ser::{BigSize, Writeable};
use crate::util::string::PrintableString;
}
/// Error when parsing a bech32 encoded message using [`str::parse`].
+///
+/// This is not exported to bindings users as its name conflicts with the BOLT 11 ParseError type.
#[derive(Debug, PartialEq)]
pub enum ParseError {
/// The bech32 encoding does not conform to the BOLT 12 requirements for continuing messages
}
/// Error when interpreting a TLV stream as a specific type.
+///
+/// This is not exported to bindings users as its name conflicts with the BOLT 11 SemanticError type.
#[derive(Debug, PartialEq)]
pub enum SemanticError {
/// The current [`std::time::SystemTime`] is past the offer or invoice's expiration.
//! use lightning::offers::refund::{Refund, RefundBuilder};
//! use lightning::util::ser::{Readable, Writeable};
//!
-//! # use lightning::onion_message::BlindedPath;
+//! # use lightning::blinded_path::BlindedPath;
//! # #[cfg(feature = "std")]
//! # use std::time::SystemTime;
//! #
use core::time::Duration;
use crate::chain::keysinterface::EntropySource;
use crate::io;
+use crate::blinded_path::BlindedPath;
use crate::ln::PaymentHash;
use crate::ln::features::InvoiceRequestFeatures;
use crate::ln::inbound_payment::{ExpandedKey, IV_LEN, Nonce};
use crate::offers::parse::{Bech32Encode, ParseError, ParsedMessage, SemanticError};
use crate::offers::payer::{PayerContents, PayerTlvStream, PayerTlvStreamRef};
use crate::offers::signer::{Metadata, MetadataMaterial, self};
-use crate::onion_message::BlindedPath;
use crate::util::ser::{SeekReadable, WithoutLength, Writeable, Writer};
use crate::util::string::PrintableString;
///
/// See [module-level documentation] for usage.
///
+/// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+///
/// [module-level documentation]: self
pub struct RefundBuilder<'a, T: secp256k1::Signing> {
refund: RefundContents,
/// See [`Refund::respond_with_no_std`] for further details where the aforementioned creation
/// time is used for the `created_at` parameter.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Duration`]: core::time::Duration
#[cfg(feature = "std")]
pub fn respond_with(
///
/// Errors if the request contains unknown required features.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice::created_at`]: crate::offers::invoice::Invoice::created_at
pub fn respond_with_no_std(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
///
/// See [`Refund::respond_with`] for further details.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice`]: crate::offers::invoice::Invoice
#[cfg(feature = "std")]
pub fn respond_using_derived_keys<ES: Deref>(
///
/// See [`Refund::respond_with_no_std`] for further details.
///
+ /// This is not exported to bindings users as builder patterns don't map outside of move semantics.
+ ///
/// [`Invoice`]: crate::offers::invoice::Invoice
pub fn respond_using_derived_keys_no_std<ES: Deref>(
&self, payment_paths: Vec<(BlindedPath, BlindedPayInfo)>, payment_hash: PaymentHash,
use bitcoin::secp256k1::{KeyPair, Secp256k1, SecretKey};
use core::convert::TryFrom;
use core::time::Duration;
+ use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::chain::keysinterface::KeyMaterial;
use crate::ln::features::{InvoiceRequestFeatures, OfferFeatures};
use crate::ln::inbound_payment::ExpandedKey;
use crate::offers::parse::{ParseError, SemanticError};
use crate::offers::payer::PayerTlvStreamRef;
use crate::offers::test_utils::*;
- use crate::onion_message::{BlindedHop, BlindedPath};
use crate::util::ser::{BigSize, Writeable};
use crate::util::string::PrintableString;
use bitcoin::secp256k1::schnorr::Signature;
use core::convert::Infallible;
use core::time::Duration;
+use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::chain::keysinterface::EntropySource;
use crate::ln::PaymentHash;
use crate::ln::features::BlindedHopFeatures;
use crate::offers::invoice::BlindedPayInfo;
-use crate::onion_message::{BlindedHop, BlindedPath};
pub(super) fn payer_keys() -> KeyPair {
let secp_ctx = Secp256k1::new();
+++ /dev/null
-// This file is Copyright its original authors, visible in version control
-// history.
-//
-// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
-// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
-// You may not use this file except in accordance with one or both of these
-// licenses.
-
-//! Creating blinded paths and related utilities live here.
-
-use bitcoin::hashes::{Hash, HashEngine};
-use bitcoin::hashes::sha256::Hash as Sha256;
-use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
-
-use crate::chain::keysinterface::{EntropySource, NodeSigner, Recipient};
-use super::packet::ControlTlvs;
-use super::utils;
-use crate::ln::msgs::DecodeError;
-use crate::ln::onion_utils;
-use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
-use crate::util::ser::{FixedLengthReader, LengthReadableArgs, Readable, VecWriter, Writeable, Writer};
-
-use core::mem;
-use core::ops::Deref;
-use crate::io::{self, Cursor};
-use crate::prelude::*;
-
-/// Onion messages can be sent and received to blinded paths, which serve to hide the identity of
-/// the recipient.
-#[derive(Clone, Debug, PartialEq)]
-pub struct BlindedPath {
- /// To send to a blinded path, the sender first finds a route to the unblinded
- /// `introduction_node_id`, which can unblind its [`encrypted_payload`] to find out the onion
- /// message's next hop and forward it along.
- ///
- /// [`encrypted_payload`]: BlindedHop::encrypted_payload
- pub(crate) introduction_node_id: PublicKey,
- /// Used by the introduction node to decrypt its [`encrypted_payload`] to forward the onion
- /// message.
- ///
- /// [`encrypted_payload`]: BlindedHop::encrypted_payload
- pub(crate) blinding_point: PublicKey,
- /// The hops composing the blinded path.
- pub(crate) blinded_hops: Vec<BlindedHop>,
-}
-
-/// Used to construct the blinded hops portion of a blinded path. These hops cannot be identified
-/// by outside observers and thus can be used to hide the identity of the recipient.
-#[derive(Clone, Debug, PartialEq)]
-pub struct BlindedHop {
- /// The blinded node id of this hop in a blinded path.
- pub(crate) blinded_node_id: PublicKey,
- /// The encrypted payload intended for this hop in a blinded path.
- // The node sending to this blinded path will later encode this payload into the onion packet for
- // this hop.
- pub(crate) encrypted_payload: Vec<u8>,
-}
-
-impl BlindedPath {
- /// Create a blinded path to be forwarded along `node_pks`. The last node pubkey in `node_pks`
- /// will be the destination node.
- ///
- /// Errors if less than two hops are provided or if `node_pk`(s) are invalid.
- // TODO: make all payloads the same size with padding + add dummy hops
- pub fn new<ES: EntropySource, T: secp256k1::Signing + secp256k1::Verification>
- (node_pks: &[PublicKey], entropy_source: &ES, secp_ctx: &Secp256k1<T>) -> Result<Self, ()>
- {
- if node_pks.len() < 2 { return Err(()) }
- let blinding_secret_bytes = entropy_source.get_secure_random_bytes();
- let blinding_secret = SecretKey::from_slice(&blinding_secret_bytes[..]).expect("RNG is busted");
- let introduction_node_id = node_pks[0];
-
- Ok(BlindedPath {
- introduction_node_id,
- blinding_point: PublicKey::from_secret_key(secp_ctx, &blinding_secret),
- blinded_hops: blinded_hops(secp_ctx, node_pks, &blinding_secret).map_err(|_| ())?,
- })
- }
-
- // Advance the blinded path by one hop, so make the second hop into the new introduction node.
- pub(super) fn advance_by_one<NS: Deref, T: secp256k1::Signing + secp256k1::Verification>
- (&mut self, node_signer: &NS, secp_ctx: &Secp256k1<T>) -> Result<(), ()>
- where NS::Target: NodeSigner
- {
- let control_tlvs_ss = node_signer.ecdh(Recipient::Node, &self.blinding_point, None)?;
- let rho = onion_utils::gen_rho_from_shared_secret(&control_tlvs_ss.secret_bytes());
- let encrypted_control_tlvs = self.blinded_hops.remove(0).encrypted_payload;
- let mut s = Cursor::new(&encrypted_control_tlvs);
- let mut reader = FixedLengthReader::new(&mut s, encrypted_control_tlvs.len() as u64);
- match ChaChaPolyReadAdapter::read(&mut reader, rho) {
- Ok(ChaChaPolyReadAdapter { readable: ControlTlvs::Forward(ForwardTlvs {
- mut next_node_id, next_blinding_override,
- })}) => {
- let mut new_blinding_point = match next_blinding_override {
- Some(blinding_point) => blinding_point,
- None => {
- let blinding_factor = {
- let mut sha = Sha256::engine();
- sha.input(&self.blinding_point.serialize()[..]);
- sha.input(control_tlvs_ss.as_ref());
- Sha256::from_engine(sha).into_inner()
- };
- self.blinding_point.mul_tweak(secp_ctx, &Scalar::from_be_bytes(blinding_factor).unwrap())
- .map_err(|_| ())?
- }
- };
- mem::swap(&mut self.blinding_point, &mut new_blinding_point);
- mem::swap(&mut self.introduction_node_id, &mut next_node_id);
- Ok(())
- },
- _ => Err(())
- }
- }
-}
-
-/// Construct blinded hops for the given `unblinded_path`.
-fn blinded_hops<T: secp256k1::Signing + secp256k1::Verification>(
- secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], session_priv: &SecretKey
-) -> Result<Vec<BlindedHop>, secp256k1::Error> {
- let mut blinded_hops = Vec::with_capacity(unblinded_path.len());
-
- let mut prev_ss_and_blinded_node_id = None;
- utils::construct_keys_callback(secp_ctx, unblinded_path, None, session_priv, |blinded_node_id, _, _, encrypted_payload_ss, unblinded_pk, _| {
- if let Some((prev_ss, prev_blinded_node_id)) = prev_ss_and_blinded_node_id {
- if let Some(pk) = unblinded_pk {
- let payload = ForwardTlvs {
- next_node_id: pk,
- next_blinding_override: None,
- };
- blinded_hops.push(BlindedHop {
- blinded_node_id: prev_blinded_node_id,
- encrypted_payload: encrypt_payload(payload, prev_ss),
- });
- } else { debug_assert!(false); }
- }
- prev_ss_and_blinded_node_id = Some((encrypted_payload_ss, blinded_node_id));
- })?;
-
- if let Some((final_ss, final_blinded_node_id)) = prev_ss_and_blinded_node_id {
- let final_payload = ReceiveTlvs { path_id: None };
- blinded_hops.push(BlindedHop {
- blinded_node_id: final_blinded_node_id,
- encrypted_payload: encrypt_payload(final_payload, final_ss),
- });
- } else { debug_assert!(false) }
-
- Ok(blinded_hops)
-}
-
-/// Encrypt TLV payload to be used as a [`BlindedHop::encrypted_payload`].
-fn encrypt_payload<P: Writeable>(payload: P, encrypted_tlvs_ss: [u8; 32]) -> Vec<u8> {
- let mut writer = VecWriter(Vec::new());
- let write_adapter = ChaChaPolyWriteAdapter::new(encrypted_tlvs_ss, &payload);
- write_adapter.write(&mut writer).expect("In-memory writes cannot fail");
- writer.0
-}
-
-impl Writeable for BlindedPath {
- fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
- self.introduction_node_id.write(w)?;
- self.blinding_point.write(w)?;
- (self.blinded_hops.len() as u8).write(w)?;
- for hop in &self.blinded_hops {
- hop.write(w)?;
- }
- Ok(())
- }
-}
-
-impl Readable for BlindedPath {
- fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
- let introduction_node_id = Readable::read(r)?;
- let blinding_point = Readable::read(r)?;
- let num_hops: u8 = Readable::read(r)?;
- if num_hops == 0 { return Err(DecodeError::InvalidValue) }
- let mut blinded_hops: Vec<BlindedHop> = Vec::with_capacity(num_hops.into());
- for _ in 0..num_hops {
- blinded_hops.push(Readable::read(r)?);
- }
- Ok(BlindedPath {
- introduction_node_id,
- blinding_point,
- blinded_hops,
- })
- }
-}
-
-impl_writeable!(BlindedHop, {
- blinded_node_id,
- encrypted_payload
-});
-
-/// TLVs to encode in an intermediate onion message packet's hop data. When provided in a blinded
-/// route, they are encoded into [`BlindedHop::encrypted_payload`].
-pub(crate) struct ForwardTlvs {
- /// The node id of the next hop in the onion message's path.
- pub(super) next_node_id: PublicKey,
- /// Senders to a blinded path use this value to concatenate the route they find to the
- /// introduction node with the blinded path.
- pub(super) next_blinding_override: Option<PublicKey>,
-}
-
-/// Similar to [`ForwardTlvs`], but these TLVs are for the final node.
-pub(crate) struct ReceiveTlvs {
- /// If `path_id` is `Some`, it is used to identify the blinded path that this onion message is
- /// sending to. This is useful for receivers to check that said blinded path is being used in
- /// the right context.
- pub(super) path_id: Option<[u8; 32]>,
-}
-
-impl Writeable for ForwardTlvs {
- fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
- // TODO: write padding
- encode_tlv_stream!(writer, {
- (4, self.next_node_id, required),
- (8, self.next_blinding_override, option)
- });
- Ok(())
- }
-}
-
-impl Writeable for ReceiveTlvs {
- fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
- // TODO: write padding
- encode_tlv_stream!(writer, {
- (6, self.path_id, option),
- });
- Ok(())
- }
-}
//! Onion message testing and test utilities live here.
+use crate::blinded_path::BlindedPath;
use crate::chain::keysinterface::{NodeSigner, Recipient};
use crate::ln::features::InitFeatures;
use crate::ln::msgs::{self, DecodeError, OnionMessageHandler};
-use super::{BlindedPath, CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError};
+use super::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError};
use crate::util::ser::{Writeable, Writer};
use crate::util::test_utils;
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
let secp_ctx = Secp256k1::new();
- let blinded_path = BlindedPath::new(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
+ let blinded_path = BlindedPath::new_for_message(&[nodes[3].get_node_pk(), nodes[4].get_node_pk()], &*nodes[4].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
pass_along_path(&nodes, None);
let test_msg = OnionMessageContents::Custom(TestCustomMessage {});
let secp_ctx = Secp256k1::new();
- let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
+ let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), test_msg, None).unwrap();
pass_along_path(&nodes, None);
let test_msg = TestCustomMessage {};
let secp_ctx = Secp256k1::new();
- let blinded_path = BlindedPath::new(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
+ let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap();
pass_along_path(&nodes, None);
// Try with a two-hop blinded path where we are the introduction node.
- let blinded_path = BlindedPath::new(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
+ let blinded_path = BlindedPath::new_for_message(&[nodes[0].get_node_pk(), nodes[1].get_node_pk()], &*nodes[1].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap();
nodes.remove(2);
pass_along_path(&nodes, None);
// 0 hops
let secp_ctx = Secp256k1::new();
- let mut blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
+ let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
blinded_path.blinded_hops.clear();
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg.clone()), None).unwrap_err();
assert_eq!(err, SendError::TooFewBlindedHops);
// 1 hop
- let mut blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
+ let mut blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], &*nodes[2].keys_manager, &secp_ctx).unwrap();
blinded_path.blinded_hops.remove(0);
assert_eq!(blinded_path.blinded_hops.len(), 1);
let err = nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), None).unwrap_err();
let secp_ctx = Secp256k1::new();
// Destination::Node
- let reply_path = BlindedPath::new(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
+ let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk()], Destination::Node(nodes[3].get_node_pk()), OnionMessageContents::Custom(test_msg.clone()), Some(reply_path)).unwrap();
pass_along_path(&nodes, None);
// Make sure the last node successfully decoded the reply path.
&format!("Received an onion message with path_id None and a reply_path"), 1);
// Destination::BlindedPath
- let blinded_path = BlindedPath::new(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
- let reply_path = BlindedPath::new(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
+ let blinded_path = BlindedPath::new_for_message(&[nodes[1].get_node_pk(), nodes[2].get_node_pk(), nodes[3].get_node_pk()], &*nodes[3].keys_manager, &secp_ctx).unwrap();
+ let reply_path = BlindedPath::new_for_message(&[nodes[2].get_node_pk(), nodes[1].get_node_pk(), nodes[0].get_node_pk()], &*nodes[0].keys_manager, &secp_ctx).unwrap();
nodes[0].messenger.send_onion_message(&[], Destination::BlindedPath(blinded_path), OnionMessageContents::Custom(test_msg), Some(reply_path)).unwrap();
pass_along_path(&nodes, None);
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::secp256k1::{self, PublicKey, Scalar, Secp256k1, SecretKey};
+use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs, utils};
use crate::chain::keysinterface::{EntropySource, KeysManager, NodeSigner, Recipient};
use crate::events::OnionMessageProvider;
use crate::ln::features::{InitFeatures, NodeFeatures};
use crate::ln::msgs::{self, OnionMessageHandler};
use crate::ln::onion_utils;
use crate::ln::peer_handler::IgnoringMessageHandler;
-use super::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs};
pub use super::packet::{CustomOnionMessageContents, OnionMessageContents};
use super::packet::{BIG_PACKET_HOP_DATA_LEN, ForwardControlTlvs, Packet, Payload, ReceiveControlTlvs, SMALL_PACKET_HOP_DATA_LEN};
-use super::utils;
use crate::util::logger::Logger;
use crate::util::ser::Writeable;
/// # extern crate bitcoin;
/// # use bitcoin::hashes::_export::_core::time::Duration;
/// # use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
+/// # use lightning::blinded_path::BlindedPath;
/// # use lightning::chain::keysinterface::KeysManager;
/// # use lightning::ln::peer_handler::IgnoringMessageHandler;
-/// # use lightning::onion_message::{BlindedPath, CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
+/// # use lightning::onion_message::{CustomOnionMessageContents, Destination, OnionMessageContents, OnionMessenger};
/// # use lightning::util::logger::{Logger, Record};
/// # use lightning::util::ser::{Writeable, Writer};
/// # use lightning::io;
/// // Create a blinded path to yourself, for someone to send an onion message to.
/// # let your_node_id = hop_node_id1;
/// let hops = [hop_node_id3, hop_node_id4, your_node_id];
-/// let blinded_path = BlindedPath::new(&hops, &keys_manager, &secp_ctx).unwrap();
+/// let blinded_path = BlindedPath::new_for_message(&hops, &keys_manager, &secp_ctx).unwrap();
///
/// // Send a custom onion message to a blinded path.
/// # let intermediate_hops = [hop_node_id1, hop_node_id2];
let our_node_id = self.node_signer.get_node_id(Recipient::Node)
.map_err(|()| SendError::GetNodeIdFailed)?;
if blinded_path.introduction_node_id == our_node_id {
- blinded_path.advance_by_one(&self.node_signer, &self.secp_ctx)
+ blinded_path.advance_message_path_by_one(&self.node_signer, &self.secp_ctx)
.map_err(|()| SendError::BlindedPathAdvanceFailed)?;
}
}
//! information on its usage.
//!
//! [offers]: <https://github.com/lightning/bolts/pull/798>
-//! [blinded paths]: crate::onion_message::BlindedPath
+//! [blinded paths]: crate::blinded_path::BlindedPath
-mod blinded_path;
mod messenger;
mod packet;
-mod utils;
#[cfg(test)]
mod functional_tests;
// Re-export structs so they can be imported with just the `onion_message::` module prefix.
-pub use self::blinded_path::{BlindedPath, BlindedHop};
pub use self::messenger::{CustomOnionMessageContents, CustomOnionMessageHandler, Destination, OnionMessageContents, OnionMessenger, SendError, SimpleArcOnionMessenger, SimpleRefOnionMessenger};
-pub(crate) use self::packet::Packet;
+pub(crate) use self::packet::{ControlTlvs, Packet};
use bitcoin::secp256k1::PublicKey;
use bitcoin::secp256k1::ecdh::SharedSecret;
+use crate::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs};
use crate::ln::msgs::DecodeError;
use crate::ln::onion_utils;
-use super::blinded_path::{BlindedPath, ForwardTlvs, ReceiveTlvs};
use super::messenger::CustomOnionMessageHandler;
use crate::util::chacha20poly1305rfc::{ChaChaPolyReadAdapter, ChaChaPolyWriteAdapter};
use crate::util::ser::{BigSize, FixedLengthReader, LengthRead, LengthReadable, LengthReadableArgs, Readable, ReadableArgs, Writeable, Writer};
Blinded(Vec<u8>),
/// If we're constructing an onion message hop through an intermediate unblinded node, we'll need
/// to construct the intermediate hop's control TLVs in their unblinded state to avoid encoding
- /// them into an intermediate Vec. See [`super::blinded_path::ForwardTlvs`] for more info.
+ /// them into an intermediate Vec. See [`crate::blinded_path::ForwardTlvs`] for more info.
Unblinded(ForwardTlvs),
}
pub(super) enum ReceiveControlTlvs {
/// See [`ForwardControlTlvs::Blinded`].
Blinded(Vec<u8>),
- /// See [`ForwardControlTlvs::Unblinded`] and [`super::blinded_path::ReceiveTlvs`].
+ /// See [`ForwardControlTlvs::Unblinded`] and [`crate::blinded_path::ReceiveTlvs`].
Unblinded(ReceiveTlvs),
}
/// When reading a packet off the wire, we don't know a priori whether the packet is to be forwarded
/// or received. Thus we read a ControlTlvs rather than reading a ForwardControlTlvs or
/// ReceiveControlTlvs directly.
-pub(super) enum ControlTlvs {
+pub(crate) enum ControlTlvs {
/// This onion message is intended to be forwarded.
Forward(ForwardTlvs),
/// This onion message is intended to be received.
+++ /dev/null
-// This file is Copyright its original authors, visible in version control
-// history.
-//
-// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
-// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
-// You may not use this file except in accordance with one or both of these
-// licenses.
-
-//! Onion message utility methods live here.
-
-use bitcoin::hashes::{Hash, HashEngine};
-use bitcoin::hashes::hmac::{Hmac, HmacEngine};
-use bitcoin::hashes::sha256::Hash as Sha256;
-use bitcoin::secp256k1::{self, PublicKey, Secp256k1, SecretKey, Scalar};
-use bitcoin::secp256k1::ecdh::SharedSecret;
-
-use crate::ln::onion_utils;
-use super::blinded_path::BlindedPath;
-use super::messenger::Destination;
-
-use crate::prelude::*;
-
-// TODO: DRY with onion_utils::construct_onion_keys_callback
-#[inline]
-pub(super) fn construct_keys_callback<T: secp256k1::Signing + secp256k1::Verification,
- FType: FnMut(PublicKey, SharedSecret, PublicKey, [u8; 32], Option<PublicKey>, Option<Vec<u8>>)>(
- secp_ctx: &Secp256k1<T>, unblinded_path: &[PublicKey], destination: Option<Destination>,
- session_priv: &SecretKey, mut callback: FType
-) -> Result<(), secp256k1::Error> {
- let mut msg_blinding_point_priv = session_priv.clone();
- let mut msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
- let mut onion_packet_pubkey_priv = msg_blinding_point_priv.clone();
- let mut onion_packet_pubkey = msg_blinding_point.clone();
-
- macro_rules! build_keys {
- ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {{
- let encrypted_data_ss = SharedSecret::new(&$pk, &msg_blinding_point_priv);
-
- let blinded_hop_pk = if $blinded { $pk } else {
- let hop_pk_blinding_factor = {
- let mut hmac = HmacEngine::<Sha256>::new(b"blinded_node_id");
- hmac.input(encrypted_data_ss.as_ref());
- Hmac::from_engine(hmac).into_inner()
- };
- $pk.mul_tweak(secp_ctx, &Scalar::from_be_bytes(hop_pk_blinding_factor).unwrap())?
- };
- let onion_packet_ss = SharedSecret::new(&blinded_hop_pk, &onion_packet_pubkey_priv);
-
- let rho = onion_utils::gen_rho_from_shared_secret(encrypted_data_ss.as_ref());
- let unblinded_pk_opt = if $blinded { None } else { Some($pk) };
- callback(blinded_hop_pk, onion_packet_ss, onion_packet_pubkey, rho, unblinded_pk_opt, $encrypted_payload);
- (encrypted_data_ss, onion_packet_ss)
- }}
- }
-
- macro_rules! build_keys_in_loop {
- ($pk: expr, $blinded: expr, $encrypted_payload: expr) => {
- let (encrypted_data_ss, onion_packet_ss) = build_keys!($pk, $blinded, $encrypted_payload);
-
- let msg_blinding_point_blinding_factor = {
- let mut sha = Sha256::engine();
- sha.input(&msg_blinding_point.serialize()[..]);
- sha.input(encrypted_data_ss.as_ref());
- Sha256::from_engine(sha).into_inner()
- };
-
- msg_blinding_point_priv = msg_blinding_point_priv.mul_tweak(&Scalar::from_be_bytes(msg_blinding_point_blinding_factor).unwrap())?;
- msg_blinding_point = PublicKey::from_secret_key(secp_ctx, &msg_blinding_point_priv);
-
- let onion_packet_pubkey_blinding_factor = {
- let mut sha = Sha256::engine();
- sha.input(&onion_packet_pubkey.serialize()[..]);
- sha.input(onion_packet_ss.as_ref());
- Sha256::from_engine(sha).into_inner()
- };
- onion_packet_pubkey_priv = onion_packet_pubkey_priv.mul_tweak(&Scalar::from_be_bytes(onion_packet_pubkey_blinding_factor).unwrap())?;
- onion_packet_pubkey = PublicKey::from_secret_key(secp_ctx, &onion_packet_pubkey_priv);
- };
- }
-
- for pk in unblinded_path {
- build_keys_in_loop!(*pk, false, None);
- }
- if let Some(dest) = destination {
- match dest {
- Destination::Node(pk) => {
- build_keys!(pk, false, None);
- },
- Destination::BlindedPath(BlindedPath { blinded_hops, .. }) => {
- for hop in blinded_hops {
- build_keys_in_loop!(hop.blinded_node_id, true, Some(hop.encrypted_payload));
- }
- },
- }
- }
- Ok(())
-}
use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, NodeAnnouncement, GossipTimestampFilter};
use crate::ln::msgs::{QueryChannelRange, ReplyChannelRange, QueryShortChannelIds, ReplyShortChannelIdsEnd};
use crate::ln::msgs;
-use crate::routing::utxo::{self, UtxoLookup};
+use crate::routing::utxo::{self, UtxoLookup, UtxoResolver};
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer, MaybeReadable};
use crate::util::logger::{Logger, Level};
use crate::util::scid_utils::{block_from_scid, scid_from_parts, MAX_SCID_BLOCK};
msg: ChannelUpdate,
},
/// An error indicating that a channel failed to route a payment, which should be applied via
- /// [`NetworkGraph::channel_failed`].
+ /// [`NetworkGraph::channel_failed_permanent`] if permanent.
ChannelFailure {
/// The short channel id of the closed channel.
short_channel_id: u64,
let _ = self.update_channel(msg);
},
NetworkUpdate::ChannelFailure { short_channel_id, is_permanent } => {
- let action = if is_permanent { "Removing" } else { "Disabling" };
- log_debug!(self.logger, "{} channel graph entry for {} due to a payment failure.", action, short_channel_id);
- self.channel_failed(short_channel_id, is_permanent);
+ if is_permanent {
+ log_debug!(self.logger, "Removing channel graph entry for {} due to a payment failure.", short_channel_id);
+ self.channel_failed_permanent(short_channel_id);
+ }
},
NetworkUpdate::NodeFailure { ref node_id, is_permanent } => {
if is_permanent {
/// Store or update channel info from a channel announcement.
///
- /// You probably don't want to call this directly, instead relying on a P2PGossipSync's
- /// RoutingMessageHandler implementation to call it indirectly. This may be useful to accept
+ /// You probably don't want to call this directly, instead relying on a [`P2PGossipSync`]'s
+ /// [`RoutingMessageHandler`] implementation to call it indirectly. This may be useful to accept
/// routing messages from a source using a protocol other than the lightning P2P protocol.
///
/// If a [`UtxoLookup`] object is provided via `utxo_lookup`, it will be called to verify
self.update_channel_from_unsigned_announcement_intern(&msg.contents, Some(msg), utxo_lookup)
}
+ /// Store or update channel info from a channel announcement.
+ ///
+ /// You probably don't want to call this directly, instead relying on a [`P2PGossipSync`]'s
+ /// [`RoutingMessageHandler`] implementation to call it indirectly. This may be useful to accept
+ /// routing messages from a source using a protocol other than the lightning P2P protocol.
+ ///
+ /// This will skip verification of if the channel is actually on-chain.
+ pub fn update_channel_from_announcement_no_lookup(
+ &self, msg: &ChannelAnnouncement
+ ) -> Result<(), LightningError> {
+ self.update_channel_from_announcement::<&UtxoResolver>(msg, &None)
+ }
+
/// Store or update channel info from a channel announcement without verifying the associated
/// signatures. Because we aren't given the associated signatures here we cannot relay the
/// channel announcement to any of our peers.
return Err(LightningError{err: "Channel announcement node had a channel with itself".to_owned(), action: ErrorAction::IgnoreError});
}
+ if msg.chain_hash != self.genesis_hash {
+ return Err(LightningError {
+ err: "Channel announcement chain hash does not match genesis hash".to_owned(),
+ action: ErrorAction::IgnoreAndLog(Level::Debug),
+ });
+ }
+
{
let channels = self.channels.read().unwrap();
Ok(())
}
- /// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
- /// If permanent, removes a channel from the local storage.
- /// May cause the removal of nodes too, if this was their last channel.
- /// If not permanent, makes channels unavailable for routing.
- pub fn channel_failed(&self, short_channel_id: u64, is_permanent: bool) {
+ /// Marks a channel in the graph as failed permanently.
+ ///
+ /// The channel and any node for which this was their last channel are removed from the graph.
+ pub fn channel_failed_permanent(&self, short_channel_id: u64) {
#[cfg(feature = "std")]
let current_time_unix = Some(SystemTime::now().duration_since(UNIX_EPOCH).expect("Time must be > 1970").as_secs());
#[cfg(not(feature = "std"))]
let current_time_unix = None;
- self.channel_failed_with_time(short_channel_id, is_permanent, current_time_unix)
+ self.channel_failed_permanent_with_time(short_channel_id, current_time_unix)
}
- /// Marks a channel in the graph as failed if a corresponding HTLC fail was sent.
- /// If permanent, removes a channel from the local storage.
- /// May cause the removal of nodes too, if this was their last channel.
- /// If not permanent, makes channels unavailable for routing.
- fn channel_failed_with_time(&self, short_channel_id: u64, is_permanent: bool, current_time_unix: Option<u64>) {
+ /// Marks a channel in the graph as failed permanently.
+ ///
+ /// The channel and any node for which this was their last channel are removed from the graph.
+ fn channel_failed_permanent_with_time(&self, short_channel_id: u64, current_time_unix: Option<u64>) {
let mut channels = self.channels.write().unwrap();
- if is_permanent {
- if let Some(chan) = channels.remove(&short_channel_id) {
- let mut nodes = self.nodes.write().unwrap();
- self.removed_channels.lock().unwrap().insert(short_channel_id, current_time_unix);
- Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
- }
- } else {
- if let Some(chan) = channels.get_mut(&short_channel_id) {
- if let Some(one_to_two) = chan.one_to_two.as_mut() {
- one_to_two.enabled = false;
- }
- if let Some(two_to_one) = chan.two_to_one.as_mut() {
- two_to_one.enabled = false;
- }
- }
+ if let Some(chan) = channels.remove(&short_channel_id) {
+ let mut nodes = self.nodes.write().unwrap();
+ self.removed_channels.lock().unwrap().insert(short_channel_id, current_time_unix);
+ Self::remove_channel_in_nodes(&mut nodes, &chan, short_channel_id);
}
}
fn update_channel_intern(&self, msg: &msgs::UnsignedChannelUpdate, full_msg: Option<&msgs::ChannelUpdate>, sig: Option<&secp256k1::ecdsa::Signature>) -> Result<(), LightningError> {
let chan_enabled = msg.flags & (1 << 1) != (1 << 1);
+ if msg.chain_hash != self.genesis_hash {
+ return Err(LightningError {
+ err: "Channel update chain hash does not match genesis hash".to_owned(),
+ action: ErrorAction::IgnoreAndLog(Level::Debug),
+ });
+ }
+
#[cfg(all(feature = "std", not(test), not(feature = "_test_utils")))]
{
// Note that many tests rely on being able to set arbitrarily old timestamps, thus we
Ok(_) => panic!(),
Err(e) => assert_eq!(e.err, "Channel announcement node had a channel with itself")
};
+
+ // Test that channel announcements with the wrong chain hash are ignored (network graph is testnet,
+ // announcement is mainnet).
+ let incorrect_chain_announcement = get_signed_channel_announcement(|unsigned_announcement| {
+ unsigned_announcement.chain_hash = genesis_block(Network::Bitcoin).header.block_hash();
+ }, node_1_privkey, node_2_privkey, &secp_ctx);
+ match gossip_sync.handle_channel_announcement(&incorrect_chain_announcement) {
+ Ok(_) => panic!(),
+ Err(e) => assert_eq!(e.err, "Channel announcement chain hash does not match genesis hash")
+ };
}
#[test]
Ok(_) => panic!(),
Err(e) => assert_eq!(e.err, "Invalid signature on channel_update message")
};
+
+ // Test that channel updates with the wrong chain hash are ignored (network graph is testnet, channel
+ // update is mainet).
+ let incorrect_chain_update = get_signed_channel_update(|unsigned_channel_update| {
+ unsigned_channel_update.chain_hash = genesis_block(Network::Bitcoin).header.block_hash();
+ }, node_1_privkey, &secp_ctx);
+
+ match gossip_sync.handle_channel_update(&incorrect_chain_update) {
+ Ok(_) => panic!(),
+ Err(e) => assert_eq!(e.err, "Channel update chain hash does not match genesis hash")
+ };
}
#[test]
assert!(network_graph.read_only().channels().get(&short_channel_id).unwrap().one_to_two.is_some());
}
- // Non-permanent closing just disables a channel
+ // Non-permanent failure doesn't touch the channel at all
{
match network_graph.read_only().channels().get(&short_channel_id) {
None => panic!(),
match network_graph.read_only().channels().get(&short_channel_id) {
None => panic!(),
Some(channel_info) => {
- assert!(!channel_info.one_to_two.as_ref().unwrap().enabled);
+ assert!(channel_info.one_to_two.as_ref().unwrap().enabled);
}
};
}
// Mark the channel as permanently failed. This will also remove the two nodes
// and all of the entries will be tracked as removed.
- network_graph.channel_failed_with_time(short_channel_id, true, Some(tracking_time));
+ network_graph.channel_failed_permanent_with_time(short_channel_id, Some(tracking_time));
// Should not remove from tracking if insufficient time has passed
network_graph.remove_stale_channels_and_tracking_with_time(
// Mark the channel as permanently failed. This will also remove the two nodes
// and all of the entries will be tracked as removed.
- network_graph.channel_failed(short_channel_id, true);
+ network_graph.channel_failed_permanent(short_channel_id);
// The first time we call the following, the channel will have a removal time assigned.
network_graph.remove_stale_channels_and_tracking_with_time(removal_time);
use bitcoin::hashes::Hash;
use bitcoin::hashes::sha256::Hash as Sha256;
+use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::ln::PaymentHash;
use crate::ln::channelmanager::{ChannelDetails, PaymentId};
use crate::ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures};
use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
+use crate::offers::invoice::BlindedPayInfo;
use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
use crate::routing::scoring::{ChannelUsage, LockableScore, Score};
use crate::util::ser::{Writeable, Readable, ReadableArgs, Writer};
}
}
- fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
self.scorer.payment_path_failed(path, short_channel_id)
}
- fn payment_path_successful(&mut self, path: &[&RouteHop]) {
+ fn payment_path_successful(&mut self, path: &Path) {
self.scorer.payment_path_successful(path)
}
- fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
self.scorer.probe_failed(path, short_channel_id)
}
- fn probe_successful(&mut self, path: &[&RouteHop]) {
+ fn probe_successful(&mut self, path: &Path) {
self.scorer.probe_successful(path)
}
}
pub fn new() -> Self { InFlightHtlcs(HashMap::new()) }
/// Takes in a path with payer's node id and adds the path's details to `InFlightHtlcs`.
- pub fn process_path(&mut self, path: &[RouteHop], payer_node_id: PublicKey) {
- if path.is_empty() { return };
+ pub fn process_path(&mut self, path: &Path, payer_node_id: PublicKey) {
+ if path.hops.is_empty() { return };
+
+ let mut cumulative_msat = 0;
+ if let Some(tail) = &path.blinded_tail {
+ cumulative_msat += tail.final_value_msat;
+ }
+
// total_inflight_map needs to be direction-sensitive when keeping track of the HTLC value
// that is held up. However, the `hops` array, which is a path returned by `find_route` in
// the router excludes the payer node. In the following lines, the payer's information is
// hardcoded with an inflight value of 0 so that we can correctly represent the first hop
// in our sliding window of two.
- let reversed_hops_with_payer = path.iter().rev().skip(1)
+ let reversed_hops_with_payer = path.hops.iter().rev().skip(1)
.map(|hop| hop.pubkey)
.chain(core::iter::once(payer_node_id));
- let mut cumulative_msat = 0;
// Taking the reversed vector from above, we zip it with just the reversed hops list to
// work "backwards" of the given path, since the last hop's `fee_msat` actually represents
// the total amount sent.
- for (next_hop, prev_hop) in path.iter().rev().zip(reversed_hops_with_payer) {
+ for (next_hop, prev_hop) in path.hops.iter().rev().zip(reversed_hops_with_payer) {
cumulative_msat += next_hop.fee_msat;
self.0
.entry((next_hop.short_channel_id, NodeId::from_pubkey(&prev_hop) < NodeId::from_pubkey(&next_hop.pubkey)))
}
}
-/// A hop in a route
+/// A hop in a route, and additional metadata about it. "Hop" is defined as a node and the channel
+/// that leads to it.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct RouteHop {
/// The node_id of the node at this hop.
/// to reach this node.
pub channel_features: ChannelFeatures,
/// The fee taken on this hop (for paying for the use of the *next* channel in the path).
- /// For the last hop, this should be the full value of the payment (might be more than
- /// requested if we had to match htlc_minimum_msat).
+ /// If this is the last hop in [`Path::hops`]:
+ /// * if we're sending to a [`BlindedPath`], this is the fee paid for use of the entire blinded path
+ /// * otherwise, this is the full value of this [`Path`]'s part of the payment
+ ///
+ /// [`BlindedPath`]: crate::blinded_path::BlindedPath
pub fee_msat: u64,
- /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
- /// expected at the destination, in excess of the current block height.
+ /// The CLTV delta added for this hop.
+ /// If this is the last hop in [`Path::hops`]:
+ /// * if we're sending to a [`BlindedPath`], this is the CLTV delta for the entire blinded path
+ /// * otherwise, this is the CLTV delta expected at the destination
+ ///
+ /// [`BlindedPath`]: crate::blinded_path::BlindedPath
pub cltv_expiry_delta: u32,
}
(10, cltv_expiry_delta, required),
});
+/// The blinded portion of a [`Path`], if we're routing to a recipient who provided blinded paths in
+/// their BOLT12 [`Invoice`].
+///
+/// [`Invoice`]: crate::offers::invoice::Invoice
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+pub struct BlindedTail {
+ /// The hops of the [`BlindedPath`] provided by the recipient.
+ ///
+ /// [`BlindedPath`]: crate::blinded_path::BlindedPath
+ pub hops: Vec<BlindedHop>,
+ /// The blinding point of the [`BlindedPath`] provided by the recipient.
+ ///
+ /// [`BlindedPath`]: crate::blinded_path::BlindedPath
+ pub blinding_point: PublicKey,
+ /// Excess CLTV delta added to the recipient's CLTV expiry to deter intermediate nodes from
+ /// inferring the destination. May be 0.
+ pub excess_final_cltv_expiry_delta: u32,
+ /// The total amount paid on this [`Path`], excluding the fees.
+ pub final_value_msat: u64,
+}
+
+impl_writeable_tlv_based!(BlindedTail, {
+ (0, hops, vec_type),
+ (2, blinding_point, required),
+ (4, excess_final_cltv_expiry_delta, required),
+ (6, final_value_msat, required),
+});
+
+/// A path in a [`Route`] to the payment recipient. Must always be at least length one.
+/// If no [`Path::blinded_tail`] is present, then [`Path::hops`] length may be up to 19.
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+pub struct Path {
+ /// The list of unblinded hops in this [`Path`]. Must be at least length one.
+ pub hops: Vec<RouteHop>,
+ /// The blinded path at which this path terminates, if we're sending to one, and its metadata.
+ pub blinded_tail: Option<BlindedTail>,
+}
+
+impl Path {
+ /// Gets the fees for a given path, excluding any excess paid to the recipient.
+ pub fn fee_msat(&self) -> u64 {
+ match &self.blinded_tail {
+ Some(_) => self.hops.iter().map(|hop| hop.fee_msat).sum::<u64>(),
+ None => {
+ // Do not count last hop of each path since that's the full value of the payment
+ self.hops.split_last().map_or(0,
+ |(_, path_prefix)| path_prefix.iter().map(|hop| hop.fee_msat).sum())
+ }
+ }
+ }
+
+ /// Gets the total amount paid on this [`Path`], excluding the fees.
+ pub fn final_value_msat(&self) -> u64 {
+ match &self.blinded_tail {
+ Some(blinded_tail) => blinded_tail.final_value_msat,
+ None => self.hops.last().map_or(0, |hop| hop.fee_msat)
+ }
+ }
+
+ /// Gets the final hop's CLTV expiry delta.
+ pub fn final_cltv_expiry_delta(&self) -> Option<u32> {
+ match &self.blinded_tail {
+ Some(_) => None,
+ None => self.hops.last().map(|hop| hop.cltv_expiry_delta)
+ }
+ }
+}
+
/// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
/// it can take multiple paths. Each path is composed of one or more hops through the network.
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct Route {
- /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
- /// last RouteHop in each path must be the same. Each entry represents a list of hops, NOT
- /// INCLUDING our own, where the last hop is the destination. Thus, this must always be at
- /// least length one. While the maximum length of any given path is variable, keeping the length
- /// of any path less or equal to 19 should currently ensure it is viable.
- pub paths: Vec<Vec<RouteHop>>,
+ /// The list of [`Path`]s taken for a single (potentially-)multi-part payment. If no
+ /// [`BlindedTail`]s are present, then the pubkey of the last [`RouteHop`] in each path must be
+ /// the same.
+ pub paths: Vec<Path>,
/// The `payment_params` parameter passed to [`find_route`].
/// This is used by `ChannelManager` to track information which may be required for retries,
/// provided back to you via [`Event::PaymentPathFailed`].
pub payment_params: Option<PaymentParameters>,
}
-pub(crate) trait RoutePath {
- /// Gets the fees for a given path, excluding any excess paid to the recipient.
- fn get_path_fees(&self) -> u64;
-}
-impl RoutePath for Vec<RouteHop> {
- fn get_path_fees(&self) -> u64 {
- // Do not count last hop of each path since that's the full value of the payment
- self.split_last().map(|(_, path_prefix)| path_prefix).unwrap_or(&[])
- .iter().map(|hop| &hop.fee_msat)
- .sum()
- }
-}
-
impl Route {
/// Returns the total amount of fees paid on this [`Route`].
///
/// This doesn't include any extra payment made to the recipient, which can happen in excess of
/// the amount passed to [`find_route`]'s `params.final_value_msat`.
pub fn get_total_fees(&self) -> u64 {
- self.paths.iter().map(|path| path.get_path_fees()).sum()
+ self.paths.iter().map(|path| path.fee_msat()).sum()
}
- /// Returns the total amount paid on this [`Route`], excluding the fees.
+ /// Returns the total amount paid on this [`Route`], excluding the fees. Might be more than
+ /// requested if we had to reach htlc_minimum_msat.
pub fn get_total_amount(&self) -> u64 {
- return self.paths.iter()
- .map(|path| path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0))
- .sum();
+ self.paths.iter().map(|path| path.final_value_msat()).sum()
}
}
fn write<W: crate::util::ser::Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
write_ver_prefix!(writer, SERIALIZATION_VERSION, MIN_SERIALIZATION_VERSION);
(self.paths.len() as u64).write(writer)?;
- for hops in self.paths.iter() {
- (hops.len() as u8).write(writer)?;
- for hop in hops.iter() {
+ let mut blinded_tails = Vec::new();
+ for path in self.paths.iter() {
+ (path.hops.len() as u8).write(writer)?;
+ for (idx, hop) in path.hops.iter().enumerate() {
hop.write(writer)?;
+ if let Some(blinded_tail) = &path.blinded_tail {
+ if blinded_tails.is_empty() {
+ blinded_tails = Vec::with_capacity(path.hops.len());
+ for _ in 0..idx {
+ blinded_tails.push(None);
+ }
+ }
+ blinded_tails.push(Some(blinded_tail));
+ } else if !blinded_tails.is_empty() { blinded_tails.push(None); }
}
}
write_tlv_fields!(writer, {
(1, self.payment_params, option),
+ (2, blinded_tails, optional_vec),
});
Ok(())
}
if hops.is_empty() { return Err(DecodeError::InvalidValue); }
min_final_cltv_expiry_delta =
cmp::min(min_final_cltv_expiry_delta, hops.last().unwrap().cltv_expiry_delta);
- paths.push(hops);
+ paths.push(Path { hops, blinded_tail: None });
}
- let mut payment_params = None;
- read_tlv_fields!(reader, {
+ _init_and_read_tlv_fields!(reader, {
(1, payment_params, (option: ReadableArgs, min_final_cltv_expiry_delta)),
+ (2, blinded_tails, optional_vec),
});
+ let blinded_tails = blinded_tails.unwrap_or(Vec::new());
+ if blinded_tails.len() != 0 {
+ if blinded_tails.len() != paths.len() { return Err(DecodeError::InvalidValue) }
+ for (mut path, blinded_tail_opt) in paths.iter_mut().zip(blinded_tails.into_iter()) {
+ path.blinded_tail = blinded_tail_opt;
+ }
+ }
Ok(Route { paths, payment_params })
}
}
pub features: Option<InvoiceFeatures>,
/// Hints for routing to the payee, containing channels connecting the payee to public nodes.
- pub route_hints: Vec<RouteHint>,
+ pub route_hints: Hints,
/// Expiration of a payment to the payee, in seconds relative to the UNIX epoch.
pub expiry_time: Option<u64>,
impl Writeable for PaymentParameters {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ let mut clear_hints = &vec![];
+ let mut blinded_hints = &vec![];
+ match &self.route_hints {
+ Hints::Clear(hints) => clear_hints = hints,
+ Hints::Blinded(hints) => blinded_hints = hints,
+ }
write_tlv_fields!(writer, {
(0, self.payee_pubkey, required),
(1, self.max_total_cltv_expiry_delta, required),
(2, self.features, option),
(3, self.max_path_count, required),
- (4, self.route_hints, vec_type),
+ (4, *clear_hints, vec_type),
(5, self.max_channel_saturation_power_of_half, required),
(6, self.expiry_time, option),
(7, self.previously_failed_channels, vec_type),
+ (8, *blinded_hints, optional_vec),
(9, self.final_cltv_expiry_delta, required),
});
Ok(())
(5, max_channel_saturation_power_of_half, (default_value, 2)),
(6, expiry_time, option),
(7, previously_failed_channels, vec_type),
+ (8, blinded_route_hints, optional_vec),
(9, final_cltv_expiry_delta, (default_value, default_final_cltv_expiry_delta)),
});
+ let clear_route_hints = route_hints.unwrap_or(vec![]);
+ let blinded_route_hints = blinded_route_hints.unwrap_or(vec![]);
+ let route_hints = if blinded_route_hints.len() != 0 {
+ if clear_route_hints.len() != 0 { return Err(DecodeError::InvalidValue) }
+ Hints::Blinded(blinded_route_hints)
+ } else {
+ Hints::Clear(clear_route_hints)
+ };
Ok(Self {
payee_pubkey: _init_tlv_based_struct_field!(payee_pubkey, required),
max_total_cltv_expiry_delta: _init_tlv_based_struct_field!(max_total_cltv_expiry_delta, (default_value, unused)),
features,
max_path_count: _init_tlv_based_struct_field!(max_path_count, (default_value, unused)),
- route_hints: route_hints.unwrap_or(Vec::new()),
+ route_hints,
max_channel_saturation_power_of_half: _init_tlv_based_struct_field!(max_channel_saturation_power_of_half, (default_value, unused)),
expiry_time,
previously_failed_channels: previously_failed_channels.unwrap_or(Vec::new()),
Self {
payee_pubkey,
features: None,
- route_hints: vec![],
+ route_hints: Hints::Clear(vec![]),
expiry_time: None,
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
max_path_count: DEFAULT_MAX_PATH_COUNT,
///
/// This is not exported to bindings users since bindings don't support move semantics
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Self {
- Self { route_hints, ..self }
+ Self { route_hints: Hints::Clear(route_hints), ..self }
}
/// Includes a payment expiration in seconds relative to the UNIX epoch.
}
}
+/// Routing hints for the tail of the route.
+#[derive(Clone, Debug, Hash, PartialEq, Eq)]
+pub enum Hints {
+ /// The recipient provided blinded paths and payinfo to reach them. The blinded paths themselves
+ /// will be included in the final [`Route`].
+ Blinded(Vec<(BlindedPayInfo, BlindedPath)>),
+ /// The recipient included these route hints in their BOLT11 invoice.
+ Clear(Vec<RouteHint>),
+}
+
/// A list of hops along a payment path terminating with a channel to the recipient.
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
pub struct RouteHint(pub Vec<RouteHintHop>);
) -> Result<Route, LightningError>
where L::Target: Logger, GL::Target: Logger {
let graph_lock = network_graph.read_only();
- let final_cltv_expiry_delta = route_params.payment_params.final_cltv_expiry_delta;
let mut route = get_route(our_node_pubkey, &route_params.payment_params, &graph_lock, first_hops,
- route_params.final_value_msat, final_cltv_expiry_delta, logger, scorer,
+ route_params.final_value_msat, logger, scorer,
random_seed_bytes)?;
add_random_cltv_offset(&mut route, &route_params.payment_params, &graph_lock, random_seed_bytes);
Ok(route)
pub(crate) fn get_route<L: Deref, S: Score>(
our_node_pubkey: &PublicKey, payment_params: &PaymentParameters, network_graph: &ReadOnlyNetworkGraph,
- first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, final_cltv_expiry_delta: u32,
- logger: L, scorer: &S, _random_seed_bytes: &[u8; 32]
+ first_hops: Option<&[&ChannelDetails]>, final_value_msat: u64, logger: L, scorer: &S,
+ _random_seed_bytes: &[u8; 32]
) -> Result<Route, LightningError>
where L::Target: Logger {
let payee_node_id = NodeId::from_pubkey(&payment_params.payee_pubkey);
return Err(LightningError{err: "Cannot send a payment of 0 msat".to_owned(), action: ErrorAction::IgnoreError});
}
- for route in payment_params.route_hints.iter() {
- for hop in &route.0 {
- if hop.src_node_id == payment_params.payee_pubkey {
- return Err(LightningError{err: "Route hint cannot have the payee as the source.".to_owned(), action: ErrorAction::IgnoreError});
+ match &payment_params.route_hints {
+ Hints::Clear(hints) => {
+ for route in hints.iter() {
+ for hop in &route.0 {
+ if hop.src_node_id == payment_params.payee_pubkey {
+ return Err(LightningError{err: "Route hint cannot have the payee as the source.".to_owned(), action: ErrorAction::IgnoreError});
+ }
+ }
}
- }
+ },
+ _ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
+
}
- if payment_params.max_total_cltv_expiry_delta <= final_cltv_expiry_delta {
+ if payment_params.max_total_cltv_expiry_delta <= payment_params.final_cltv_expiry_delta {
return Err(LightningError{err: "Can't find a route where the maximum total CLTV expiry delta is below the final CLTV expiry.".to_owned(), action: ErrorAction::IgnoreError});
}
- // TODO: Remove the explicit final_cltv_expiry_delta parameter
- debug_assert_eq!(final_cltv_expiry_delta, payment_params.final_cltv_expiry_delta);
-
// The general routing idea is the following:
// 1. Fill first/last hops communicated by the caller.
// 2. Attempt to construct a path from payer to payee for transferring
// In order to already account for some of the privacy enhancing random CLTV
// expiry delta offset we add on top later, we subtract a rough estimate
// (2*MEDIAN_HOP_CLTV_EXPIRY_DELTA) here.
- let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta)
+ let max_total_cltv_expiry_delta = (payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta)
.checked_sub(2*MEDIAN_HOP_CLTV_EXPIRY_DELTA)
- .unwrap_or(payment_params.max_total_cltv_expiry_delta - final_cltv_expiry_delta);
+ .unwrap_or(payment_params.max_total_cltv_expiry_delta - payment_params.final_cltv_expiry_delta);
let hop_total_cltv_delta = ($next_hops_cltv_delta as u32)
.saturating_add($candidate.cltv_expiry_delta());
let exceeds_cltv_delta_limit = hop_total_cltv_delta > max_total_cltv_expiry_delta;
// If a caller provided us with last hops, add them to routing targets. Since this happens
// earlier than general path finding, they will be somewhat prioritized, although currently
// it matters only if the fees are exactly the same.
- for route in payment_params.route_hints.iter().filter(|route| !route.0.is_empty()) {
+ let route_hints = match &payment_params.route_hints {
+ Hints::Clear(hints) => hints,
+ _ => return Err(LightningError{err: "Routing to blinded paths isn't supported yet".to_owned(), action: ErrorAction::IgnoreError}),
+ };
+ for route in route_hints.iter().filter(|route| !route.0.is_empty()) {
let first_hop_in_route = &(route.0)[0];
let have_hop_src_in_graph =
// Only add the hops in this route to our candidate set if either
}).collect::<Vec<_>>();
// Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
// applicable for the previous hop.
- path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
+ path.iter_mut().rev().fold(payment_params.final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
});
selected_paths.push(path);
}
}
+ let mut paths: Vec<Path> = Vec::new();
+ for results_vec in selected_paths {
+ let mut hops = Vec::with_capacity(results_vec.len());
+ for res in results_vec { hops.push(res?); }
+ paths.push(Path { hops, blinded_tail: None });
+ }
let route = Route {
- paths: selected_paths.into_iter().map(|path| path.into_iter().collect()).collect::<Result<Vec<_>, _>>()?,
+ paths,
payment_params: Some(payment_params.clone()),
};
log_info!(logger, "Got route to {}: {}", payment_params.payee_pubkey, log_route!(route));
// Remember the last three nodes of the random walk and avoid looping back on them.
// Init with the last three nodes from the actual path, if possible.
- let mut nodes_to_avoid: [NodeId; 3] = [NodeId::from_pubkey(&path.last().unwrap().pubkey),
- NodeId::from_pubkey(&path.get(path.len().saturating_sub(2)).unwrap().pubkey),
- NodeId::from_pubkey(&path.get(path.len().saturating_sub(3)).unwrap().pubkey)];
+ let mut nodes_to_avoid: [NodeId; 3] = [NodeId::from_pubkey(&path.hops.last().unwrap().pubkey),
+ NodeId::from_pubkey(&path.hops.get(path.hops.len().saturating_sub(2)).unwrap().pubkey),
+ NodeId::from_pubkey(&path.hops.get(path.hops.len().saturating_sub(3)).unwrap().pubkey)];
// Choose the last publicly known node as the starting point for the random walk.
let mut cur_hop: Option<NodeId> = None;
let mut path_nonce = [0u8; 12];
- if let Some(starting_hop) = path.iter().rev()
+ if let Some(starting_hop) = path.hops.iter().rev()
.find(|h| network_nodes.contains_key(&NodeId::from_pubkey(&h.pubkey))) {
cur_hop = Some(NodeId::from_pubkey(&starting_hop.pubkey));
path_nonce.copy_from_slice(&cur_hop.unwrap().as_slice()[..12]);
// Limit the offset so we never exceed the max_total_cltv_expiry_delta. To improve plausibility,
// we choose the limit to be the largest possible multiple of MEDIAN_HOP_CLTV_EXPIRY_DELTA.
- let path_total_cltv_expiry_delta: u32 = path.iter().map(|h| h.cltv_expiry_delta).sum();
+ let path_total_cltv_expiry_delta: u32 = path.hops.iter().map(|h| h.cltv_expiry_delta).sum();
let mut max_path_offset = payment_params.max_total_cltv_expiry_delta - path_total_cltv_expiry_delta;
max_path_offset = cmp::max(
max_path_offset - (max_path_offset % MEDIAN_HOP_CLTV_EXPIRY_DELTA),
shadow_ctlv_expiry_delta_offset = cmp::min(shadow_ctlv_expiry_delta_offset, max_path_offset);
// Add 'shadow' CLTV offset to the final hop
- if let Some(last_hop) = path.last_mut() {
+ if let Some(tail) = path.blinded_tail.as_mut() {
+ tail.excess_final_cltv_expiry_delta = tail.excess_final_cltv_expiry_delta
+ .checked_add(shadow_ctlv_expiry_delta_offset).unwrap_or(tail.excess_final_cltv_expiry_delta);
+ }
+ if let Some(last_hop) = path.hops.last_mut() {
last_hop.cltv_expiry_delta = last_hop.cltv_expiry_delta
.checked_add(shadow_ctlv_expiry_delta_offset).unwrap_or(last_hop.cltv_expiry_delta);
}
let graph_lock = network_graph.read_only();
let mut route = build_route_from_hops_internal(
our_node_pubkey, hops, &route_params.payment_params, &graph_lock,
- route_params.final_value_msat, route_params.payment_params.final_cltv_expiry_delta,
- logger, random_seed_bytes)?;
+ route_params.final_value_msat, logger, random_seed_bytes)?;
add_random_cltv_offset(&mut route, &route_params.payment_params, &graph_lock, random_seed_bytes);
Ok(route)
}
fn build_route_from_hops_internal<L: Deref>(
our_node_pubkey: &PublicKey, hops: &[PublicKey], payment_params: &PaymentParameters,
- network_graph: &ReadOnlyNetworkGraph, final_value_msat: u64, final_cltv_expiry_delta: u32,
- logger: L, random_seed_bytes: &[u8; 32]
+ network_graph: &ReadOnlyNetworkGraph, final_value_msat: u64, logger: L,
+ random_seed_bytes: &[u8; 32]
) -> Result<Route, LightningError> where L::Target: Logger {
struct HopScorer {
u64::max_value()
}
- fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
+ fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
- fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
+ fn payment_path_successful(&mut self, _path: &Path) {}
- fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
+ fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
- fn probe_successful(&mut self, _path: &[&RouteHop]) {}
+ fn probe_successful(&mut self, _path: &Path) {}
}
impl<'a> Writeable for HopScorer {
let scorer = HopScorer { our_node_id, hop_ids };
get_route(our_node_pubkey, payment_params, network_graph, None, final_value_msat,
- final_cltv_expiry_delta, logger, &scorer, random_seed_bytes)
+ logger, &scorer, random_seed_bytes)
}
#[cfg(test)]
mod tests {
+ use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
use crate::routing::utxo::UtxoResult;
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
- PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
+ BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
use crate::routing::scoring::{ChannelUsage, FixedPenaltyScorer, Score, ProbabilisticScorer, ProbabilisticScoringParameters};
use crate::routing::test_utils::{add_channel, add_or_update_node, build_graph, build_line_graph, id_to_feature_flags, get_nodes, update_channel};
use crate::util::config::UserConfig;
use crate::util::test_utils as ln_test_utils;
use crate::util::chacha20::ChaCha20;
+ use crate::util::ser::{Readable, Writeable};
#[cfg(c_bindings)]
- use crate::util::ser::{Writeable, Writer};
+ use crate::util::ser::Writer;
use bitcoin::hashes::Hash;
use bitcoin::network::constants::Network;
use bitcoin::secp256k1::{PublicKey,SecretKey};
use bitcoin::secp256k1::Secp256k1;
+ use crate::io::Cursor;
use crate::prelude::*;
use crate::sync::Arc;
// Simple route to 2 via 1
- if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 0, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 0, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Cannot send a payment of 0 msat");
} else { panic!(); }
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 100);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
}
#[test]
let our_chans = vec![get_channel_details(Some(2), our_id, InitFeatures::from_le_bytes(vec![0b11]), 100000)];
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) =
- get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "First hop cannot have our_node_pubkey as a destination.");
} else { panic!(); }
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
}
#[test]
});
// Not possible to send 199_999_999, because the minimum on channel=2 is 200_000_000.
- if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 199_999_999, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 199_999_999, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a path to the given destination");
} else { panic!(); }
});
// A payment above the minimum should pass
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 199_999_999, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 199_999_999, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
}
#[test]
excess_data: Vec::new()
});
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 60_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 60_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
// Overpay fees to hit htlc_minimum_msat.
- let overpaid_fees = route.paths[0][0].fee_msat + route.paths[1][0].fee_msat;
+ let overpaid_fees = route.paths[0].hops[0].fee_msat + route.paths[1].hops[0].fee_msat;
// TODO: this could be better balanced to overpay 10k and not 15k.
assert_eq!(overpaid_fees, 15_000);
excess_data: Vec::new()
});
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 60_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 60_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
// Fine to overpay for htlc_minimum_msat if it allows us to save fee.
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0][0].short_channel_id, 12);
- let fees = route.paths[0][0].fee_msat;
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 12);
+ let fees = route.paths[0].hops[0].fee_msat;
assert_eq!(fees, 5_000);
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
// Not fine to overpay for htlc_minimum_msat if it requires paying more than fee on
// the other channel.
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- let fees = route.paths[0][0].fee_msat;
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ let fees = route.paths[0].hops[0].fee_msat;
assert_eq!(fees, 5_000);
}
});
// If all the channels require some features we don't understand, route should fail
- if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a path to the given destination");
} else { panic!(); }
// If we specify a channel to node7, that overrides our local channel view and that gets used
let our_chans = vec![get_channel_details(Some(42), nodes[7].clone(), InitFeatures::from_le_bytes(vec![0b11]), 250_000_000)];
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
- assert_eq!(route.paths[0][0].pubkey, nodes[7]);
- assert_eq!(route.paths[0][0].short_channel_id, 42);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[7]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 42);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (13 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 13);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 13);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(13));
}
#[test]
add_or_update_node(&gossip_sync, &secp_ctx, &privkeys[7], unknown_features.clone(), 1);
// If all nodes require some features we don't understand, route should fail
- if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a path to the given destination");
} else { panic!(); }
// If we specify a channel to node7, that overrides our local channel view and that gets used
let our_chans = vec![get_channel_details(Some(42), nodes[7].clone(), InitFeatures::from_le_bytes(vec![0b11]), 250_000_000)];
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[7]);
- assert_eq!(route.paths[0][0].short_channel_id, 42);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 13);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[7]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 42);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (13 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &vec![0b11]); // it should also override our view of their features
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 13);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(13));
// Note that we don't test disabling node 3 and failing to route to it, as we (somewhat
// naively) assume that the user checked the feature bits on the invoice, which override
// Route to 1 via 2 and 3 because our channel to 1 is disabled
let payment_params = PaymentParameters::from_node_id(nodes[0], 42);
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 3);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (3 << 4) | 2);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[0]);
- assert_eq!(route.paths[0][2].short_channel_id, 3);
- assert_eq!(route.paths[0][2].fee_msat, 100);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(1));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(3));
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 3);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (3 << 4) | 2);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[0]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 3);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(1));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(3));
// If we specify a channel to node7, that overrides our local channel view and that gets used
let payment_params = PaymentParameters::from_node_id(nodes[2], 42);
let our_chans = vec![get_channel_details(Some(42), nodes[7].clone(), InitFeatures::from_le_bytes(vec![0b11]), 250_000_000)];
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
- assert_eq!(route.paths[0][0].pubkey, nodes[7]);
- assert_eq!(route.paths[0][0].short_channel_id, 42);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[7]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 42);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (13 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &vec![0b11]);
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 13);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 13);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(13));
}
fn last_hops(nodes: &Vec<PublicKey>) -> Vec<RouteHint> {
invalid_last_hops.push(invalid_last_hop);
{
let payment_params = PaymentParameters::from_node_id(nodes[6], 42).with_route_hints(invalid_last_hops);
- if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Route hint cannot have the payee as the source.");
} else { panic!(); }
}
let payment_params = PaymentParameters::from_node_id(nodes[6], 42).with_route_hints(last_hops_multi_private_channels(&nodes));
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 5);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 100);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 0);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 4) | 1);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[4]);
- assert_eq!(route.paths[0][2].short_channel_id, 6);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 4) | 1);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
-
- assert_eq!(route.paths[0][3].pubkey, nodes[3]);
- assert_eq!(route.paths[0][3].short_channel_id, 11);
- assert_eq!(route.paths[0][3].fee_msat, 0);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 4) | 1);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 5);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (6 << 4) | 1);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[4]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, (11 << 4) | 1);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(5));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(6));
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, 11);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, (8 << 4) | 1);
// If we have a peer in the node map, we'll use their features here since we don't have
// a way of figuring out their features from the invoice:
- assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &id_to_feature_flags(11));
- assert_eq!(route.paths[0][4].pubkey, nodes[6]);
- assert_eq!(route.paths[0][4].short_channel_id, 8);
- assert_eq!(route.paths[0][4].fee_msat, 100);
- assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[4].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[4].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[4].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[4].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
fn empty_last_hop(nodes: &Vec<PublicKey>) -> Vec<RouteHint> {
// Test handling of an empty RouteHint passed in Invoice.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 5);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 100);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 0);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 4) | 1);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[4]);
- assert_eq!(route.paths[0][2].short_channel_id, 6);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 4) | 1);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
-
- assert_eq!(route.paths[0][3].pubkey, nodes[3]);
- assert_eq!(route.paths[0][3].short_channel_id, 11);
- assert_eq!(route.paths[0][3].fee_msat, 0);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 4) | 1);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 5);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (6 << 4) | 1);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[4]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, (11 << 4) | 1);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(5));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(6));
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, 11);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, (8 << 4) | 1);
// If we have a peer in the node map, we'll use their features here since we don't have
// a way of figuring out their features from the invoice:
- assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &id_to_feature_flags(11));
- assert_eq!(route.paths[0][4].pubkey, nodes[6]);
- assert_eq!(route.paths[0][4].short_channel_id, 8);
- assert_eq!(route.paths[0][4].fee_msat, 100);
- assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[4].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[4].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[4].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[4].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
/// Builds a trivial last-hop hint that passes through the two nodes given, with channel 0xff00
excess_data: Vec::new()
});
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 4);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, 65);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 81);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[3]);
- assert_eq!(route.paths[0][2].short_channel_id, last_hops[0].0[0].short_channel_id);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, 129);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
-
- assert_eq!(route.paths[0][3].pubkey, nodes[6]);
- assert_eq!(route.paths[0][3].short_channel_id, last_hops[0].0[1].short_channel_id);
- assert_eq!(route.paths[0][3].fee_msat, 100);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 4);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, 65);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 81);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, last_hops[0].0[0].short_channel_id);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, 129);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, last_hops[0].0[1].short_channel_id);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
#[test]
excess_data: Vec::new()
});
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &[42u8; 32]).unwrap();
- assert_eq!(route.paths[0].len(), 4);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 200);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, 65);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 81);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, non_announced_pubkey);
- assert_eq!(route.paths[0][2].short_channel_id, last_hops[0].0[0].short_channel_id);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, 129);
- assert_eq!(route.paths[0][2].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
-
- assert_eq!(route.paths[0][3].pubkey, nodes[6]);
- assert_eq!(route.paths[0][3].short_channel_id, last_hops[0].0[1].short_channel_id);
- assert_eq!(route.paths[0][3].fee_msat, 100);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &[42u8; 32]).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 4);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, 65);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 81);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, non_announced_pubkey);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, last_hops[0].0[0].short_channel_id);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, 129);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, last_hops[0].0[1].short_channel_id);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
fn last_hops_with_public_channel(nodes: &Vec<PublicKey>) -> Vec<RouteHint> {
// This test shows that public routes can be present in the invoice
// which would be handled in the same manner.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 5);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 100);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 0);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 4) | 1);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[4]);
- assert_eq!(route.paths[0][2].short_channel_id, 6);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 4) | 1);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
-
- assert_eq!(route.paths[0][3].pubkey, nodes[3]);
- assert_eq!(route.paths[0][3].short_channel_id, 11);
- assert_eq!(route.paths[0][3].fee_msat, 0);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 4) | 1);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 5);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (6 << 4) | 1);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[4]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, (11 << 4) | 1);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(5));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(6));
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, 11);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, (8 << 4) | 1);
// If we have a peer in the node map, we'll use their features here since we don't have
// a way of figuring out their features from the invoice:
- assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &id_to_feature_flags(11));
- assert_eq!(route.paths[0][4].pubkey, nodes[6]);
- assert_eq!(route.paths[0][4].short_channel_id, 8);
- assert_eq!(route.paths[0][4].fee_msat, 100);
- assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[4].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[4].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[4].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[4].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
#[test]
let our_chans = vec![get_channel_details(Some(42), nodes[3].clone(), InitFeatures::from_le_bytes(vec![0b11]), 250_000_000)];
let mut last_hops = last_hops(&nodes);
let payment_params = PaymentParameters::from_node_id(nodes[6], 42).with_route_hints(last_hops.clone());
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 2);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[3]);
- assert_eq!(route.paths[0][0].short_channel_id, 42);
- assert_eq!(route.paths[0][0].fee_msat, 0);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &vec![0b11]);
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
-
- assert_eq!(route.paths[0][1].pubkey, nodes[6]);
- assert_eq!(route.paths[0][1].short_channel_id, 8);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 2);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 42);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (8 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &vec![0b11]);
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &Vec::<u8>::new()); // No feature flags will meet the relevant-to-channel conversion
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
last_hops[0].0[0].fees.base_msat = 1000;
// Revert to via 6 as the fee on 8 goes up
let payment_params = PaymentParameters::from_node_id(nodes[6], 42).with_route_hints(last_hops);
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 4);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 200); // fee increased as its % of value transferred across node
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 100);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (7 << 4) | 1);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[5]);
- assert_eq!(route.paths[0][2].short_channel_id, 7);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, (10 << 4) | 1);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 4);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 200); // fee increased as its % of value transferred across node
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (7 << 4) | 1);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[5]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 7);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, (10 << 4) | 1);
// If we have a peer in the node map, we'll use their features here since we don't have
// a way of figuring out their features from the invoice:
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(6));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(7));
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(6));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(7));
- assert_eq!(route.paths[0][3].pubkey, nodes[6]);
- assert_eq!(route.paths[0][3].short_channel_id, 10);
- assert_eq!(route.paths[0][3].fee_msat, 100);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, 10);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
// ...but still use 8 for larger payments as 6 has a variable feerate
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 2000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- assert_eq!(route.paths[0].len(), 5);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 2);
- assert_eq!(route.paths[0][0].fee_msat, 3000);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (4 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(2));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(2));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 4);
- assert_eq!(route.paths[0][1].fee_msat, 0);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (6 << 4) | 1);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(4));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[4]);
- assert_eq!(route.paths[0][2].short_channel_id, 6);
- assert_eq!(route.paths[0][2].fee_msat, 0);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, (11 << 4) | 1);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(5));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(6));
-
- assert_eq!(route.paths[0][3].pubkey, nodes[3]);
- assert_eq!(route.paths[0][3].short_channel_id, 11);
- assert_eq!(route.paths[0][3].fee_msat, 1000);
- assert_eq!(route.paths[0][3].cltv_expiry_delta, (8 << 4) | 1);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 2000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ assert_eq!(route.paths[0].hops.len(), 5);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 3000);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (4 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(2));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(2));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 4);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (6 << 4) | 1);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(4));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[4]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, (11 << 4) | 1);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(5));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(6));
+
+ assert_eq!(route.paths[0].hops[3].pubkey, nodes[3]);
+ assert_eq!(route.paths[0].hops[3].short_channel_id, 11);
+ assert_eq!(route.paths[0].hops[3].fee_msat, 1000);
+ assert_eq!(route.paths[0].hops[3].cltv_expiry_delta, (8 << 4) | 1);
// If we have a peer in the node map, we'll use their features here since we don't have
// a way of figuring out their features from the invoice:
- assert_eq!(route.paths[0][3].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][3].channel_features.le_flags(), &id_to_feature_flags(11));
+ assert_eq!(route.paths[0].hops[3].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[3].channel_features.le_flags(), &id_to_feature_flags(11));
- assert_eq!(route.paths[0][4].pubkey, nodes[6]);
- assert_eq!(route.paths[0][4].short_channel_id, 8);
- assert_eq!(route.paths[0][4].fee_msat, 2000);
- assert_eq!(route.paths[0][4].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[4].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[4].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[4].fee_msat, 2000);
+ assert_eq!(route.paths[0].hops[4].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[4].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[4].channel_features.le_flags(), &Vec::<u8>::new()); // We can't learn any flags from invoices, sadly
}
fn do_unannounced_path_test(last_hop_htlc_max: Option<u64>, last_hop_fee_prop: u32, outbound_capacity_msat: u64, route_val: u64) -> Result<Route, LightningError> {
let logger = ln_test_utils::TestLogger::new();
let network_graph = NetworkGraph::new(Network::Testnet, &logger);
let route = get_route(&source_node_id, &payment_params, &network_graph.read_only(),
- Some(&our_chans.iter().collect::<Vec<_>>()), route_val, 42, &logger, &scorer, &random_seed_bytes);
+ Some(&our_chans.iter().collect::<Vec<_>>()), route_val, &logger, &scorer, &random_seed_bytes);
route
}
let middle_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 42).repeat(32)).unwrap()[..]).unwrap());
let target_node_id = PublicKey::from_secret_key(&Secp256k1::new(), &SecretKey::from_slice(&hex::decode(format!("{:02}", 43).repeat(32)).unwrap()[..]).unwrap());
- assert_eq!(route.paths[0].len(), 2);
+ assert_eq!(route.paths[0].hops.len(), 2);
- assert_eq!(route.paths[0][0].pubkey, middle_node_id);
- assert_eq!(route.paths[0][0].short_channel_id, 42);
- assert_eq!(route.paths[0][0].fee_msat, 1001);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (8 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &[0b11]);
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[0].pubkey, middle_node_id);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 42);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 1001);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (8 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &[0b11]);
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
- assert_eq!(route.paths[0][1].pubkey, target_node_id);
- assert_eq!(route.paths[0][1].short_channel_id, 8);
- assert_eq!(route.paths[0][1].fee_msat, 1000000);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
+ assert_eq!(route.paths[0].hops[1].pubkey, target_node_id);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 8);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 1000000);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), default_node_features().le_flags()); // We dont pass flags in from invoices yet
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &[0; 0]); // We can't learn any flags from invoices, sadly
}
#[test]
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 250_000_001, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 250_000_001, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route an exact amount we have should be fine.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 250_000_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 250_000_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let path = route.paths.last().unwrap();
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- assert_eq!(path.last().unwrap().fee_msat, 250_000_000);
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ assert_eq!(path.final_value_msat(), 250_000_000);
}
// Check that setting next_outbound_htlc_limit_msat in first_hops limits the channels.
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 200_000_001, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 200_000_001, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route an exact amount we have should be fine.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 200_000_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&our_chans.iter().collect::<Vec<_>>()), 200_000_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let path = route.paths.last().unwrap();
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- assert_eq!(path.last().unwrap().fee_msat, 200_000_000);
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ assert_eq!(path.final_value_msat(), 200_000_000);
}
// Enable channel #1 back.
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 15_001, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 15_001, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route an exact amount we have should be fine.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 15_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 15_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let path = route.paths.last().unwrap();
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- assert_eq!(path.last().unwrap().fee_msat, 15_000);
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ assert_eq!(path.final_value_msat(), 15_000);
}
// Now let's see if routing works if we know only capacity from the UTXO.
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 15_001, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 15_001, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route an exact amount we have should be fine.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 15_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 15_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let path = route.paths.last().unwrap();
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- assert_eq!(path.last().unwrap().fee_msat, 15_000);
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ assert_eq!(path.final_value_msat(), 15_000);
}
// Now let's see if routing chooses htlc_maximum_msat over UTXO capacity.
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 10_001, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 10_001, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route an exact amount we have should be fine.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 10_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 10_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let path = route.paths.last().unwrap();
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- assert_eq!(path.last().unwrap().fee_msat, 10_000);
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ assert_eq!(path.final_value_msat(), 10_000);
}
}
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 60_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 60_000, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route 49 sats (just a bit below the capacity).
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 49_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 49_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 4);
- assert_eq!(path.last().unwrap().pubkey, nodes[3]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 4);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[3]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 49_000);
}
{
// Attempt to route an exact amount is also fine
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 4);
- assert_eq!(path.last().unwrap().pubkey, nodes[3]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 4);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[3]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 50_000);
}
});
{
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 50_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 50_000);
}
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 300_000, 42,
+ &our_id, &payment_params, &network_graph.read_only(), None, 300_000,
Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
// Attempt to route while setting max_path_count to 0 results in a failure.
let zero_payment_params = payment_params.clone().with_max_path_count(0);
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &zero_payment_params, &network_graph.read_only(), None, 100, 42,
+ &our_id, &zero_payment_params, &network_graph.read_only(), None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Can't find a route with no paths allowed.");
} else { panic!(); }
// to account for 1/3 of the total value, which is violated by 2 out of 3 paths.
let fail_payment_params = payment_params.clone().with_max_path_count(3);
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &fail_payment_params, &network_graph.read_only(), None, 250_000, 42,
+ &our_id, &fail_payment_params, &network_graph.read_only(), None, 250_000,
Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
// Now, attempt to route 250 sats (just a bit below the capacity).
// Our algorithm should provide us with these 3 paths.
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None,
- 250_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ 250_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 3);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 250_000);
}
{
// Attempt to route an exact amount is also fine
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None,
- 290_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ 290_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 3);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 290_000);
}
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 350_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 350_000, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route 300 sats (exact amount we can route).
// Our algorithm should provide us with these 3 paths, 100 sats each.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 300_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 300_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 3);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.last().unwrap().pubkey, nodes[3]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[3]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 300_000);
}
{
// Now, attempt to route 180 sats.
// Our algorithm should provide us with these 2 paths.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 180_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 180_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
let mut total_value_transferred_msat = 0;
let mut total_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.last().unwrap().pubkey, nodes[3]);
- total_value_transferred_msat += path.last().unwrap().fee_msat;
- for hop in path {
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[3]);
+ total_value_transferred_msat += path.final_value_msat();
+ for hop in &path.hops {
total_paid_msat += hop.fee_msat;
}
}
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 210_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 210_000, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route 200 sats (exact amount we can route).
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 200_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 200_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.last().unwrap().pubkey, nodes[3]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[3]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 200_000);
assert_eq!(route.get_total_fees(), 150_000);
// Get a route for 100 sats and check that we found the MPP route no problem and didn't
// overpay at all.
- let mut route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let mut route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
- route.paths.sort_by_key(|path| path[0].short_channel_id);
+ route.paths.sort_by_key(|path| path.hops[0].short_channel_id);
// Paths are manually ordered ordered by SCID, so:
// * the first is channel 1 (0 fee, but 99 sat maximum) -> channel 3 -> channel 42
// * the second is channel 2 (1 msat fee) -> channel 4 -> channel 42
- assert_eq!(route.paths[0][0].short_channel_id, 1);
- assert_eq!(route.paths[0][0].fee_msat, 0);
- assert_eq!(route.paths[0][2].fee_msat, 99_000);
- assert_eq!(route.paths[1][0].short_channel_id, 2);
- assert_eq!(route.paths[1][0].fee_msat, 1);
- assert_eq!(route.paths[1][2].fee_msat, 1_000);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 1);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 99_000);
+ assert_eq!(route.paths[1].hops[0].short_channel_id, 2);
+ assert_eq!(route.paths[1].hops[0].fee_msat, 1);
+ assert_eq!(route.paths[1].hops[2].fee_msat, 1_000);
assert_eq!(route.get_total_fees(), 1);
assert_eq!(route.get_total_amount(), 100_000);
}
{
// Attempt to route more than available results in a failure.
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 150_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ &our_id, &payment_params, &network_graph.read_only(), None, 150_000, Arc::clone(&logger), &scorer, &random_seed_bytes) {
assert_eq!(err, "Failed to find a sufficient route to the given destination");
} else { panic!(); }
}
{
// Now, attempt to route 125 sats (just a bit below the capacity of 3 channels).
// Our algorithm should provide us with these 3 paths.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 125_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 125_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 3);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 125_000);
}
{
// Attempt to route without the last small cheap channel
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
let mut total_amount_paid_msat = 0;
for path in &route.paths {
- assert_eq!(path.len(), 2);
- assert_eq!(path.last().unwrap().pubkey, nodes[2]);
- total_amount_paid_msat += path.last().unwrap().fee_msat;
+ assert_eq!(path.hops.len(), 2);
+ assert_eq!(path.hops.last().unwrap().pubkey, nodes[2]);
+ total_amount_paid_msat += path.final_value_msat();
}
assert_eq!(total_amount_paid_msat, 90_000);
}
{
// Now ensure the route flows simply over nodes 1 and 4 to 6.
- let route = get_route(&our_id, &payment_params, &network.read_only(), None, 10_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network.read_only(), None, 10_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), 3);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[1]);
- assert_eq!(route.paths[0][0].short_channel_id, 6);
- assert_eq!(route.paths[0][0].fee_msat, 100);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (5 << 4) | 0);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(1));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(6));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[4]);
- assert_eq!(route.paths[0][1].short_channel_id, 5);
- assert_eq!(route.paths[0][1].fee_msat, 0);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, (1 << 4) | 0);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(4));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(5));
-
- assert_eq!(route.paths[0][2].pubkey, nodes[6]);
- assert_eq!(route.paths[0][2].short_channel_id, 1);
- assert_eq!(route.paths[0][2].fee_msat, 10_000);
- assert_eq!(route.paths[0][2].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][2].node_features.le_flags(), &id_to_feature_flags(6));
- assert_eq!(route.paths[0][2].channel_features.le_flags(), &id_to_feature_flags(1));
+ assert_eq!(route.paths[0].hops.len(), 3);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[1]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (5 << 4) | 0);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(1));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(6));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[4]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 5);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 0);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, (1 << 4) | 0);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(4));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(5));
+
+ assert_eq!(route.paths[0].hops[2].pubkey, nodes[6]);
+ assert_eq!(route.paths[0].hops[2].short_channel_id, 1);
+ assert_eq!(route.paths[0].hops[2].fee_msat, 10_000);
+ assert_eq!(route.paths[0].hops[2].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[2].node_features.le_flags(), &id_to_feature_flags(6));
+ assert_eq!(route.paths[0].hops[2].channel_features.le_flags(), &id_to_feature_flags(1));
}
}
{
// Now, attempt to route 90 sats, which is exactly 90 sats at the last hop, plus the
// 200% fee charged channel 13 in the 1-to-2 direction.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), 2);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[7]);
- assert_eq!(route.paths[0][0].short_channel_id, 12);
- assert_eq!(route.paths[0][0].fee_msat, 90_000*2);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(8));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(12));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 13);
- assert_eq!(route.paths[0][1].fee_msat, 90_000);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
+ assert_eq!(route.paths[0].hops.len(), 2);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[7]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 12);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 90_000*2);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (13 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(8));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(12));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 13);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 90_000);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), &id_to_feature_flags(3));
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(13));
}
}
// Now, attempt to route 90 sats, hitting the htlc_minimum on channel 4, but
// overshooting the htlc_maximum on channel 2. Thus, we should pick the (absurdly
// expensive) channels 12-13 path.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 90_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), 2);
-
- assert_eq!(route.paths[0][0].pubkey, nodes[7]);
- assert_eq!(route.paths[0][0].short_channel_id, 12);
- assert_eq!(route.paths[0][0].fee_msat, 90_000*2);
- assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 4) | 1);
- assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(8));
- assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(12));
-
- assert_eq!(route.paths[0][1].pubkey, nodes[2]);
- assert_eq!(route.paths[0][1].short_channel_id, 13);
- assert_eq!(route.paths[0][1].fee_msat, 90_000);
- assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
- assert_eq!(route.paths[0][1].node_features.le_flags(), channelmanager::provided_invoice_features(&config).le_flags());
- assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
+ assert_eq!(route.paths[0].hops.len(), 2);
+
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[7]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 12);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 90_000*2);
+ assert_eq!(route.paths[0].hops[0].cltv_expiry_delta, (13 << 4) | 1);
+ assert_eq!(route.paths[0].hops[0].node_features.le_flags(), &id_to_feature_flags(8));
+ assert_eq!(route.paths[0].hops[0].channel_features.le_flags(), &id_to_feature_flags(12));
+
+ assert_eq!(route.paths[0].hops[1].pubkey, nodes[2]);
+ assert_eq!(route.paths[0].hops[1].short_channel_id, 13);
+ assert_eq!(route.paths[0].hops[1].fee_msat, 90_000);
+ assert_eq!(route.paths[0].hops[1].cltv_expiry_delta, 42);
+ assert_eq!(route.paths[0].hops[1].node_features.le_flags(), channelmanager::provided_invoice_features(&config).le_flags());
+ assert_eq!(route.paths[0].hops[1].channel_features.le_flags(), &id_to_feature_flags(13));
}
}
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&[
&get_channel_details(Some(3), nodes[0], channelmanager::provided_init_features(&config), 200_000),
&get_channel_details(Some(2), nodes[0], channelmanager::provided_init_features(&config), 10_000),
- ]), 100_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ ]), 100_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), 1);
+ assert_eq!(route.paths[0].hops.len(), 1);
- assert_eq!(route.paths[0][0].pubkey, nodes[0]);
- assert_eq!(route.paths[0][0].short_channel_id, 3);
- assert_eq!(route.paths[0][0].fee_msat, 100_000);
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[0]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 3);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100_000);
}
{
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), Some(&[
&get_channel_details(Some(3), nodes[0], channelmanager::provided_init_features(&config), 50_000),
&get_channel_details(Some(2), nodes[0], channelmanager::provided_init_features(&config), 50_000),
- ]), 100_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ ]), 100_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
- assert_eq!(route.paths[0].len(), 1);
- assert_eq!(route.paths[1].len(), 1);
+ assert_eq!(route.paths[0].hops.len(), 1);
+ assert_eq!(route.paths[1].hops.len(), 1);
- assert!((route.paths[0][0].short_channel_id == 3 && route.paths[1][0].short_channel_id == 2) ||
- (route.paths[0][0].short_channel_id == 2 && route.paths[1][0].short_channel_id == 3));
+ assert!((route.paths[0].hops[0].short_channel_id == 3 && route.paths[1].hops[0].short_channel_id == 2) ||
+ (route.paths[0].hops[0].short_channel_id == 2 && route.paths[1].hops[0].short_channel_id == 3));
- assert_eq!(route.paths[0][0].pubkey, nodes[0]);
- assert_eq!(route.paths[0][0].fee_msat, 50_000);
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[0]);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 50_000);
- assert_eq!(route.paths[1][0].pubkey, nodes[0]);
- assert_eq!(route.paths[1][0].fee_msat, 50_000);
+ assert_eq!(route.paths[1].hops[0].pubkey, nodes[0]);
+ assert_eq!(route.paths[1].hops[0].fee_msat, 50_000);
}
{
&get_channel_details(Some(8), nodes[0], channelmanager::provided_init_features(&config), 50_000),
&get_channel_details(Some(9), nodes[0], channelmanager::provided_init_features(&config), 50_000),
&get_channel_details(Some(4), nodes[0], channelmanager::provided_init_features(&config), 1_000_000),
- ]), 100_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ ]), 100_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- assert_eq!(route.paths[0].len(), 1);
+ assert_eq!(route.paths[0].hops.len(), 1);
- assert_eq!(route.paths[0][0].pubkey, nodes[0]);
- assert_eq!(route.paths[0][0].short_channel_id, 6);
- assert_eq!(route.paths[0][0].fee_msat, 100_000);
+ assert_eq!(route.paths[0].hops[0].pubkey, nodes[0]);
+ assert_eq!(route.paths[0].hops[0].short_channel_id, 6);
+ assert_eq!(route.paths[0].hops[0].fee_msat, 100_000);
}
}
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
let route = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 100, 42,
+ &our_id, &payment_params, &network_graph.read_only(), None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes
).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert_eq!(route.get_total_fees(), 100);
assert_eq!(route.get_total_amount(), 100);
// from nodes[2] rather than channel 6, 11, and 8, even though the longer path is cheaper.
let scorer = FixedPenaltyScorer::with_penalty(100);
let route = get_route(
- &our_id, &payment_params, &network_graph.read_only(), None, 100, 42,
+ &our_id, &payment_params, &network_graph.read_only(), None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes
).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert_eq!(route.get_total_fees(), 300);
assert_eq!(route.get_total_amount(), 100);
if short_channel_id == self.short_channel_id { u64::max_value() } else { 0 }
}
- fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
- fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
- fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
- fn probe_successful(&mut self, _path: &[&RouteHop]) {}
+ fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
+ fn payment_path_successful(&mut self, _path: &Path) {}
+ fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
+ fn probe_successful(&mut self, _path: &Path) {}
}
struct BadNodeScorer {
if *target == self.node_id { u64::max_value() } else { 0 }
}
- fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
- fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
- fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
- fn probe_successful(&mut self, _path: &[&RouteHop]) {}
+ fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
+ fn payment_path_successful(&mut self, _path: &Path) {}
+ fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
+ fn probe_successful(&mut self, _path: &Path) {}
}
#[test]
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
let route = get_route(
- &our_id, &payment_params, &network_graph, None, 100, 42,
+ &our_id, &payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes
).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert_eq!(route.get_total_fees(), 100);
assert_eq!(route.get_total_amount(), 100);
// A different path to nodes[6] exists if channel 6 cannot be routed over.
let scorer = BadChannelScorer { short_channel_id: 6 };
let route = get_route(
- &our_id, &payment_params, &network_graph, None, 100, 42,
+ &our_id, &payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes
).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert_eq!(route.get_total_fees(), 300);
assert_eq!(route.get_total_amount(), 100);
// A path to nodes[6] does not exist if nodes[2] cannot be routed through.
let scorer = BadNodeScorer { node_id: NodeId::from_pubkey(&nodes[2]) };
match get_route(
- &our_id, &payment_params, &network_graph, None, 100, 42,
+ &our_id, &payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes
) {
Err(LightningError { err, .. } ) => {
#[test]
fn total_fees_single_path() {
let route = Route {
- paths: vec![vec![
+ paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 225, cltv_expiry_delta: 0
},
- ]],
+ ], blinded_tail: None }],
payment_params: None,
};
#[test]
fn total_fees_multi_path() {
let route = Route {
- paths: vec![vec![
+ paths: vec![Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 150, cltv_expiry_delta: 0
},
- ],vec![
+ ], blinded_tail: None }, Path { hops: vec![
RouteHop {
pubkey: PublicKey::from_slice(&hex::decode("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
channel_features: ChannelFeatures::empty(), node_features: NodeFeatures::empty(),
short_channel_id: 0, fee_msat: 150, cltv_expiry_delta: 0
},
- ]],
+ ], blinded_tail: None }],
payment_params: None,
};
.with_max_total_cltv_expiry_delta(feasible_max_total_cltv_delta);
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
- let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert_ne!(path.len(), 0);
// But not if we exclude all paths on the basis of their accumulated CLTV delta
let fail_max_total_cltv_delta = 23;
let fail_payment_params = PaymentParameters::from_node_id(nodes[6], 0).with_route_hints(last_hops(&nodes))
.with_max_total_cltv_expiry_delta(fail_max_total_cltv_delta);
- match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes)
+ match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes)
{
Err(LightningError { err, .. } ) => {
assert_eq!(err, "Failed to find a path to the given destination");
// We should be able to find a route initially, and then after we fail a few random
// channels eventually we won't be able to any longer.
- assert!(get_route(&our_id, &payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes).is_ok());
+ assert!(get_route(&our_id, &payment_params, &network_graph, None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).is_ok());
loop {
- if let Ok(route) = get_route(&our_id, &payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes) {
- for chan in route.paths[0].iter() {
+ if let Ok(route) = get_route(&our_id, &payment_params, &network_graph, None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes) {
+ for chan in route.paths[0].hops.iter() {
assert!(!payment_params.previously_failed_channels.contains(&chan.short_channel_id));
}
let victim = (u64::from_ne_bytes(random_seed_bytes[0..8].try_into().unwrap()) as usize)
- % route.paths[0].len();
- payment_params.previously_failed_channels.push(route.paths[0][victim].short_channel_id);
+ % route.paths[0].hops.len();
+ payment_params.previously_failed_channels.push(route.paths[0].hops[victim].short_channel_id);
} else { break; }
}
}
// First check we can actually create a long route on this graph.
let feasible_payment_params = PaymentParameters::from_node_id(nodes[18], 0);
- let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, 0,
+ let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
- let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
+ let path = route.paths[0].hops.iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
assert!(path.len() == MAX_PATH_LENGTH_ESTIMATE.into());
// But we can't create a path surpassing the MAX_PATH_LENGTH_ESTIMATE limit.
let fail_payment_params = PaymentParameters::from_node_id(nodes[19], 0);
- match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, 0,
+ match get_route(&our_id, &fail_payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes)
{
Err(LightningError { err, .. } ) => {
let payment_params = PaymentParameters::from_node_id(nodes[6], 42).with_route_hints(last_hops(&nodes));
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 1);
- let cltv_expiry_deltas_before = route.paths[0].iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
+ let cltv_expiry_deltas_before = route.paths[0].hops.iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
// Check whether the offset added to the last hop by default is in [1 .. DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA]
let mut route_default = route.clone();
add_random_cltv_offset(&mut route_default, &payment_params, &network_graph.read_only(), &random_seed_bytes);
- let cltv_expiry_deltas_default = route_default.paths[0].iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
+ let cltv_expiry_deltas_default = route_default.paths[0].hops.iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
assert_eq!(cltv_expiry_deltas_before.split_last().unwrap().1, cltv_expiry_deltas_default.split_last().unwrap().1);
assert!(cltv_expiry_deltas_default.last() > cltv_expiry_deltas_before.last());
assert!(cltv_expiry_deltas_default.last().unwrap() <= &DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA);
let limited_max_total_cltv_expiry_delta = cltv_expiry_deltas_before.iter().sum();
let limited_payment_params = payment_params.with_max_total_cltv_expiry_delta(limited_max_total_cltv_expiry_delta);
add_random_cltv_offset(&mut route_limited, &limited_payment_params, &network_graph.read_only(), &random_seed_bytes);
- let cltv_expiry_deltas_limited = route_limited.paths[0].iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
+ let cltv_expiry_deltas_limited = route_limited.paths[0].hops.iter().map(|h| h.cltv_expiry_delta).collect::<Vec<u32>>();
assert_eq!(cltv_expiry_deltas_before, cltv_expiry_deltas_limited);
}
let keys_manager = ln_test_utils::TestKeysInterface::new(&[4u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
- let mut route = get_route(&our_id, &payment_params, &network_graph, None, 100, 0,
+ let mut route = get_route(&our_id, &payment_params, &network_graph, None, 100,
Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
add_random_cltv_offset(&mut route, &payment_params, &network_graph, &random_seed_bytes);
let mut random_bytes = [0u8; ::core::mem::size_of::<usize>()];
prng.process_in_place(&mut random_bytes);
- let random_path_index = usize::from_be_bytes(random_bytes).wrapping_rem(p.len());
- let observation_point = NodeId::from_pubkey(&p.get(random_path_index).unwrap().pubkey);
+ let random_path_index = usize::from_be_bytes(random_bytes).wrapping_rem(p.hops.len());
+ let observation_point = NodeId::from_pubkey(&p.hops.get(random_path_index).unwrap().pubkey);
// 2. Calculate what CLTV expiry delta we would observe there
- let observed_cltv_expiry_delta: u32 = p[random_path_index..].iter().map(|h| h.cltv_expiry_delta).sum();
+ let observed_cltv_expiry_delta: u32 = p.hops[random_path_index..].iter().map(|h| h.cltv_expiry_delta).sum();
// 3. Starting from the observation point, find candidate paths
let mut candidates: VecDeque<(NodeId, Vec<u32>)> = VecDeque::new();
let payment_params = PaymentParameters::from_node_id(nodes[3], 0);
let hops = [nodes[1], nodes[2], nodes[4], nodes[3]];
let route = build_route_from_hops_internal(&our_id, &hops, &payment_params,
- &network_graph, 100, 0, Arc::clone(&logger), &random_seed_bytes).unwrap();
- let route_hop_pubkeys = route.paths[0].iter().map(|hop| hop.pubkey).collect::<Vec<_>>();
- assert_eq!(hops.len(), route.paths[0].len());
+ &network_graph, 100, Arc::clone(&logger), &random_seed_bytes).unwrap();
+ let route_hop_pubkeys = route.paths[0].hops.iter().map(|hop| hop.pubkey).collect::<Vec<_>>();
+ assert_eq!(hops.len(), route.paths[0].hops.len());
for (idx, hop_pubkey) in hops.iter().enumerate() {
assert!(*hop_pubkey == route_hop_pubkeys[idx]);
}
let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
let random_seed_bytes = keys_manager.get_secure_random_bytes();
// 100,000 sats is less than the available liquidity on each channel, set above.
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100_000_000, 42, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100_000_000, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
assert_eq!(route.paths.len(), 2);
- assert!((route.paths[0][1].short_channel_id == 4 && route.paths[1][1].short_channel_id == 13) ||
- (route.paths[1][1].short_channel_id == 4 && route.paths[0][1].short_channel_id == 13));
+ assert!((route.paths[0].hops[1].short_channel_id == 4 && route.paths[1].hops[1].short_channel_id == 13) ||
+ (route.paths[1].hops[1].short_channel_id == 4 && route.paths[0].hops[1].short_channel_id == 13));
}
#[cfg(not(feature = "no-std"))]
let amt = seed as u64 % 200_000_000;
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &graph, &logger);
- if get_route(src, &payment_params, &graph.read_only(), None, amt, 42, &logger, &scorer, &random_seed_bytes).is_ok() {
+ if get_route(src, &payment_params, &graph.read_only(), None, amt, &logger, &scorer, &random_seed_bytes).is_ok() {
continue 'load_endpoints;
}
}
let amt = seed as u64 % 200_000_000;
let params = ProbabilisticScoringParameters::default();
let scorer = ProbabilisticScorer::new(params, &graph, &logger);
- if get_route(src, &payment_params, &graph.read_only(), None, amt, 42, &logger, &scorer, &random_seed_bytes).is_ok() {
+ if get_route(src, &payment_params, &graph.read_only(), None, amt, &logger, &scorer, &random_seed_bytes).is_ok() {
continue 'load_endpoints;
}
}
// Then check we can get a normal route
let payment_params = PaymentParameters::from_node_id(nodes[10], 42);
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes);
assert!(route.is_ok());
// Then check that we can't get a route if we ban an intermediate node.
scorer.add_banned(&NodeId::from_pubkey(&nodes[3]));
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes);
assert!(route.is_err());
// Finally make sure we can route again, when we remove the ban.
scorer.remove_banned(&NodeId::from_pubkey(&nodes[3]));
- let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes);
+ let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, Arc::clone(&logger), &scorer, &random_seed_bytes);
assert!(route.is_ok());
}
+
+ #[test]
+ fn blinded_route_ser() {
+ let blinded_path_1 = BlindedPath {
+ introduction_node_id: ln_test_utils::pubkey(42),
+ blinding_point: ln_test_utils::pubkey(43),
+ blinded_hops: vec![
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(44), encrypted_payload: Vec::new() },
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(45), encrypted_payload: Vec::new() }
+ ],
+ };
+ let blinded_path_2 = BlindedPath {
+ introduction_node_id: ln_test_utils::pubkey(46),
+ blinding_point: ln_test_utils::pubkey(47),
+ blinded_hops: vec![
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(48), encrypted_payload: Vec::new() },
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(49), encrypted_payload: Vec::new() }
+ ],
+ };
+ // (De)serialize a Route with 1 blinded path out of two total paths.
+ let mut route = Route { paths: vec![Path {
+ hops: vec![RouteHop {
+ pubkey: ln_test_utils::pubkey(50),
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 42,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 100,
+ cltv_expiry_delta: 0,
+ }],
+ blinded_tail: Some(BlindedTail {
+ hops: blinded_path_1.blinded_hops,
+ blinding_point: blinded_path_1.blinding_point,
+ excess_final_cltv_expiry_delta: 40,
+ final_value_msat: 100,
+ })}, Path {
+ hops: vec![RouteHop {
+ pubkey: ln_test_utils::pubkey(51),
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 43,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 100,
+ cltv_expiry_delta: 0,
+ }], blinded_tail: None }],
+ payment_params: None,
+ };
+ let encoded_route = route.encode();
+ let decoded_route: Route = Readable::read(&mut Cursor::new(&encoded_route[..])).unwrap();
+ assert_eq!(decoded_route.paths[0].blinded_tail, route.paths[0].blinded_tail);
+ assert_eq!(decoded_route.paths[1].blinded_tail, route.paths[1].blinded_tail);
+
+ // (De)serialize a Route with two paths, each containing a blinded tail.
+ route.paths[1].blinded_tail = Some(BlindedTail {
+ hops: blinded_path_2.blinded_hops,
+ blinding_point: blinded_path_2.blinding_point,
+ excess_final_cltv_expiry_delta: 41,
+ final_value_msat: 101,
+ });
+ let encoded_route = route.encode();
+ let decoded_route: Route = Readable::read(&mut Cursor::new(&encoded_route[..])).unwrap();
+ assert_eq!(decoded_route.paths[0].blinded_tail, route.paths[0].blinded_tail);
+ assert_eq!(decoded_route.paths[1].blinded_tail, route.paths[1].blinded_tail);
+ }
+
+ #[test]
+ fn blinded_path_inflight_processing() {
+ // Ensure we'll score the channel that's inbound to a blinded path's introduction node, and
+ // account for the blinded tail's final amount_msat.
+ let mut inflight_htlcs = InFlightHtlcs::new();
+ let blinded_path = BlindedPath {
+ introduction_node_id: ln_test_utils::pubkey(43),
+ blinding_point: ln_test_utils::pubkey(48),
+ blinded_hops: vec![BlindedHop { blinded_node_id: ln_test_utils::pubkey(49), encrypted_payload: Vec::new() }],
+ };
+ let path = Path {
+ hops: vec![RouteHop {
+ pubkey: ln_test_utils::pubkey(42),
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 42,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 100,
+ cltv_expiry_delta: 0,
+ },
+ RouteHop {
+ pubkey: blinded_path.introduction_node_id,
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 43,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 1,
+ cltv_expiry_delta: 0,
+ }],
+ blinded_tail: Some(BlindedTail {
+ hops: blinded_path.blinded_hops,
+ blinding_point: blinded_path.blinding_point,
+ excess_final_cltv_expiry_delta: 0,
+ final_value_msat: 200,
+ }),
+ };
+ inflight_htlcs.process_path(&path, ln_test_utils::pubkey(44));
+ assert_eq!(*inflight_htlcs.0.get(&(42, true)).unwrap(), 301);
+ assert_eq!(*inflight_htlcs.0.get(&(43, false)).unwrap(), 201);
+ }
+
+ #[test]
+ fn blinded_path_cltv_shadow_offset() {
+ // Make sure we add a shadow offset when sending to blinded paths.
+ let blinded_path = BlindedPath {
+ introduction_node_id: ln_test_utils::pubkey(43),
+ blinding_point: ln_test_utils::pubkey(44),
+ blinded_hops: vec![
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(45), encrypted_payload: Vec::new() },
+ BlindedHop { blinded_node_id: ln_test_utils::pubkey(46), encrypted_payload: Vec::new() }
+ ],
+ };
+ let mut route = Route { paths: vec![Path {
+ hops: vec![RouteHop {
+ pubkey: ln_test_utils::pubkey(42),
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 42,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 100,
+ cltv_expiry_delta: 0,
+ },
+ RouteHop {
+ pubkey: blinded_path.introduction_node_id,
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 43,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: 1,
+ cltv_expiry_delta: 0,
+ }
+ ],
+ blinded_tail: Some(BlindedTail {
+ hops: blinded_path.blinded_hops,
+ blinding_point: blinded_path.blinding_point,
+ excess_final_cltv_expiry_delta: 0,
+ final_value_msat: 200,
+ }),
+ }], payment_params: None};
+
+ let payment_params = PaymentParameters::from_node_id(ln_test_utils::pubkey(47), 18);
+ let (_, network_graph, _, _, _) = build_line_graph();
+ add_random_cltv_offset(&mut route, &payment_params, &network_graph.read_only(), &[0; 32]);
+ assert_eq!(route.paths[0].blinded_tail.as_ref().unwrap().excess_final_cltv_expiry_delta, 40);
+ assert_eq!(route.paths[0].hops.last().unwrap().cltv_expiry_delta, 40);
+ }
}
#[cfg(all(test, not(feature = "no-std")))]
let params = PaymentParameters::from_node_id(dst, 42).with_features(features.clone());
let first_hop = first_hop(src);
let amt = seed as u64 % 1_000_000;
- if let Ok(route) = get_route(&payer, ¶ms, &graph.read_only(), Some(&[&first_hop]), amt, 42, &DummyLogger{}, &scorer, &random_seed_bytes) {
+ if let Ok(route) = get_route(&payer, ¶ms, &graph.read_only(), Some(&[&first_hop]), amt, &DummyLogger{}, &scorer, &random_seed_bytes) {
routes.push(route);
route_endpoints.push((first_hop, params, amt));
continue 'load_endpoints;
let amount = route.get_total_amount();
if amount < 250_000 {
for path in route.paths {
- scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
+ scorer.payment_path_successful(&path);
}
} else if amount > 750_000 {
for path in route.paths {
- let short_channel_id = path[path.len() / 2].short_channel_id;
- scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), short_channel_id);
+ let short_channel_id = path.hops[path.hops.len() / 2].short_channel_id;
+ scorer.payment_path_failed(&path, short_channel_id);
}
}
}
// selected destinations, possibly causing us to fail because, eg, the newly-selected path
// requires a too-high CLTV delta.
route_endpoints.retain(|(first_hop, params, amt)| {
- get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, 42, &DummyLogger{}, &scorer, &random_seed_bytes).is_ok()
+ get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, &DummyLogger{}, &scorer, &random_seed_bytes).is_ok()
});
route_endpoints.truncate(100);
assert_eq!(route_endpoints.len(), 100);
let mut idx = 0;
bench.iter(|| {
let (first_hop, params, amt) = &route_endpoints[idx % route_endpoints.len()];
- assert!(get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, 42, &DummyLogger{}, &scorer, &random_seed_bytes).is_ok());
+ assert!(get_route(&payer, params, &graph.read_only(), Some(&[first_hop]), *amt, &DummyLogger{}, &scorer, &random_seed_bytes).is_ok());
idx += 1;
});
}
use crate::ln::msgs::DecodeError;
use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
-use crate::routing::router::RouteHop;
+use crate::routing::router::Path;
use crate::util::ser::{Readable, ReadableArgs, Writeable, Writer};
use crate::util::logger::Logger;
use crate::util::time::Time;
) -> u64;
/// Handles updating channel penalties after failing to route through a channel.
- fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
+ fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64);
/// Handles updating channel penalties after successfully routing along a path.
- fn payment_path_successful(&mut self, path: &[&RouteHop]);
+ fn payment_path_successful(&mut self, path: &Path);
/// Handles updating channel penalties after a probe over the given path failed.
- fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
+ fn probe_failed(&mut self, path: &Path, short_channel_id: u64);
/// Handles updating channel penalties after a probe over the given path succeeded.
- fn probe_successful(&mut self, path: &[&RouteHop]);
+ fn probe_successful(&mut self, path: &Path);
}
impl<S: Score, T: DerefMut<Target=S> $(+ $supertrait)*> Score for T {
self.deref().channel_penalty_msat(short_channel_id, source, target, usage)
}
- fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
self.deref_mut().payment_path_failed(path, short_channel_id)
}
- fn payment_path_successful(&mut self, path: &[&RouteHop]) {
+ fn payment_path_successful(&mut self, path: &Path) {
self.deref_mut().payment_path_successful(path)
}
- fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
self.deref_mut().probe_failed(path, short_channel_id)
}
- fn probe_successful(&mut self, path: &[&RouteHop]) {
+ fn probe_successful(&mut self, path: &Path) {
self.deref_mut().probe_successful(path)
}
}
fn channel_penalty_msat(&self, scid: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage) -> u64 {
self.0.channel_penalty_msat(scid, source, target, usage)
}
- fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
self.0.payment_path_failed(path, short_channel_id)
}
- fn payment_path_successful(&mut self, path: &[&RouteHop]) {
+ fn payment_path_successful(&mut self, path: &Path) {
self.0.payment_path_successful(path)
}
- fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
self.0.probe_failed(path, short_channel_id)
}
- fn probe_successful(&mut self, path: &[&RouteHop]) {
+ fn probe_successful(&mut self, path: &Path) {
self.0.probe_successful(path)
}
}
self.penalty_msat
}
- fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
+ fn payment_path_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
- fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
+ fn payment_path_successful(&mut self, _path: &Path) {}
- fn probe_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
+ fn probe_failed(&mut self, _path: &Path, _short_channel_id: u64) {}
- fn probe_successful(&mut self, _path: &[&RouteHop]) {}
+ fn probe_successful(&mut self, _path: &Path) {}
}
impl Writeable for FixedPenaltyScorer {
.saturating_add(base_penalty_msat)
}
- fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
- let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
+ fn payment_path_failed(&mut self, path: &Path, short_channel_id: u64) {
+ let amount_msat = path.final_value_msat();
log_trace!(self.logger, "Scoring path through to SCID {} as having failed at {} msat", short_channel_id, amount_msat);
let network_graph = self.network_graph.read_only();
- for (hop_idx, hop) in path.iter().enumerate() {
+ for (hop_idx, hop) in path.hops.iter().enumerate() {
let target = NodeId::from_pubkey(&hop.pubkey);
let channel_directed_from_source = network_graph.channels()
.get(&hop.short_channel_id)
}
}
- fn payment_path_successful(&mut self, path: &[&RouteHop]) {
- let amount_msat = path.split_last().map(|(hop, _)| hop.fee_msat).unwrap_or(0);
+ fn payment_path_successful(&mut self, path: &Path) {
+ let amount_msat = path.final_value_msat();
log_trace!(self.logger, "Scoring path through SCID {} as having succeeded at {} msat.",
- path.split_last().map(|(hop, _)| hop.short_channel_id).unwrap_or(0), amount_msat);
+ path.hops.split_last().map(|(hop, _)| hop.short_channel_id).unwrap_or(0), amount_msat);
let network_graph = self.network_graph.read_only();
- for hop in path {
+ for hop in &path.hops {
let target = NodeId::from_pubkey(&hop.pubkey);
let channel_directed_from_source = network_graph.channels()
.get(&hop.short_channel_id)
}
}
- fn probe_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
+ fn probe_failed(&mut self, path: &Path, short_channel_id: u64) {
self.payment_path_failed(path, short_channel_id)
}
- fn probe_successful(&mut self, path: &[&RouteHop]) {
+ fn probe_successful(&mut self, path: &Path) {
self.payment_path_failed(path, u64::max_value())
}
}
#[cfg(test)]
mod tests {
use super::{ChannelLiquidity, HistoricalBucketRangeTracker, ProbabilisticScoringParameters, ProbabilisticScorerUsingTime};
+ use crate::blinded_path::{BlindedHop, BlindedPath};
use crate::util::config::UserConfig;
use crate::util::time::Time;
use crate::util::time::tests::SinceEpoch;
use crate::ln::channelmanager;
use crate::ln::msgs::{ChannelAnnouncement, ChannelUpdate, UnsignedChannelAnnouncement, UnsignedChannelUpdate};
use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
- use crate::routing::router::RouteHop;
+ use crate::routing::router::{BlindedTail, Path, RouteHop};
use crate::routing::scoring::{ChannelUsage, Score};
use crate::util::ser::{ReadableArgs, Writeable};
- use crate::util::test_utils::TestLogger;
+ use crate::util::test_utils::{self, TestLogger};
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::hashes::Hash;
}
}
- fn payment_path_for_amount(amount_msat: u64) -> Vec<RouteHop> {
- vec![
- path_hop(source_pubkey(), 41, 1),
- path_hop(target_pubkey(), 42, 2),
- path_hop(recipient_pubkey(), 43, amount_msat),
- ]
+ fn payment_path_for_amount(amount_msat: u64) -> Path {
+ Path {
+ hops: vec![
+ path_hop(source_pubkey(), 41, 1),
+ path_hop(target_pubkey(), 42, 2),
+ path_hop(recipient_pubkey(), 43, amount_msat),
+ ], blinded_tail: None,
+ }
}
#[test]
assert_eq!(scorer.channel_penalty_msat(41, &sender, &source, usage), 301);
- scorer.payment_path_failed(&failed_path.iter().collect::<Vec<_>>(), 41);
+ scorer.payment_path_failed(&failed_path, 41);
assert_eq!(scorer.channel_penalty_msat(41, &sender, &source, usage), 301);
- scorer.payment_path_successful(&successful_path.iter().collect::<Vec<_>>());
+ scorer.payment_path_successful(&successful_path);
assert_eq!(scorer.channel_penalty_msat(41, &sender, &source, usage), 301);
}
let usage = ChannelUsage { amount_msat: 750, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 602);
- scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&path, 43);
let usage = ChannelUsage { amount_msat: 250, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
let usage = ChannelUsage { amount_msat: 750, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 602);
- scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&path, 42);
let usage = ChannelUsage { amount_msat: 250, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
assert_eq!(scorer.channel_penalty_msat(43, &node_b, &node_c, usage), 128);
assert_eq!(scorer.channel_penalty_msat(44, &node_c, &node_d, usage), 128);
- scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&Path { hops: path, blinded_tail: None }, 43);
assert_eq!(scorer.channel_penalty_msat(42, &node_a, &node_b, usage), 80);
// Note that a default liquidity bound is used for B -> C as no channel exists
inflight_htlc_msat: 0,
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
};
- let path = payment_path_for_amount(500);
assert_eq!(scorer.channel_penalty_msat(41, &sender, &source, usage), 128);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 128);
assert_eq!(scorer.channel_penalty_msat(43, &target, &recipient, usage), 128);
- scorer.payment_path_successful(&path.iter().collect::<Vec<_>>());
+ scorer.payment_path_successful(&payment_path_for_amount(500));
assert_eq!(scorer.channel_penalty_msat(41, &sender, &source, usage), 128);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
let usage = ChannelUsage { amount_msat: 1_023, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2_000);
- scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
- scorer.payment_path_failed(&payment_path_for_amount(128).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(768), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(128), 43);
let usage = ChannelUsage { amount_msat: 128, ..usage };
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
};
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 125);
- scorer.payment_path_failed(&payment_path_for_amount(512).iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(512), 42);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 281);
// An unchecked right shift 64 bits or more in DirectedChannelLiquidity::decayed_offset_msat
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
// More knowledge gives higher confidence (256, 768), meaning a lower penalty.
- scorer.payment_path_failed(&payment_path_for_amount(768).iter().collect::<Vec<_>>(), 42);
- scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(768), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(256), 43);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 281);
// Decaying knowledge gives less confidence (128, 896), meaning a higher penalty.
// Reducing the upper bound gives more confidence (128, 832) that the payment amount (512)
// is closer to the upper bound, meaning a higher penalty.
- scorer.payment_path_successful(&payment_path_for_amount(64).iter().collect::<Vec<_>>());
+ scorer.payment_path_successful(&payment_path_for_amount(64));
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 331);
// Increasing the lower bound gives more confidence (256, 832) that the payment amount (512)
// is closer to the lower bound, meaning a lower penalty.
- scorer.payment_path_failed(&payment_path_for_amount(256).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(256), 43);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 245);
// Further decaying affects the lower bound more than the upper bound (128, 928).
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
};
- scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(500), 42);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
SinceEpoch::advance(Duration::from_secs(10));
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 473);
- scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(250), 43);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
let mut serialized_scorer = Vec::new();
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_000, htlc_maximum_msat: 1_000 },
};
- scorer.payment_path_failed(&payment_path_for_amount(500).iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(500), 42);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), u64::max_value());
let mut serialized_scorer = Vec::new();
<ProbabilisticScorer>::read(&mut serialized_scorer, (params, &network_graph, &logger)).unwrap();
assert_eq!(deserialized_scorer.channel_penalty_msat(42, &source, &target, usage), 473);
- scorer.payment_path_failed(&payment_path_for_amount(250).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(250), 43);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
SinceEpoch::advance(Duration::from_secs(10));
assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
None);
- scorer.payment_path_failed(&payment_path_for_amount(1).iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(1), 42);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 2048);
// The "it failed" increment is 32, where the probability should lie fully in the first
// octile.
// Even after we tell the scorer we definitely have enough available liquidity, it will
// still remember that there was some failure in the past, and assign a non-0 penalty.
- scorer.payment_path_failed(&payment_path_for_amount(1000).iter().collect::<Vec<_>>(), 43);
+ scorer.payment_path_failed(&payment_path_for_amount(1000), 43);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 198);
// The first octile should be decayed just slightly and the last octile has a new point.
assert_eq!(scorer.historical_estimated_channel_liquidity_probabilities(42, &target),
inflight_htlc_msat: 1024,
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_024 },
};
- scorer.payment_path_failed(&payment_path_for_amount(1).iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&payment_path_for_amount(1), 42);
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 409);
let usage = ChannelUsage {
path_hop(source_pubkey(), 42, 1),
path_hop(sender_pubkey(), 41, 0),
];
- scorer.payment_path_failed(&path.iter().collect::<Vec<_>>(), 42);
+ scorer.payment_path_failed(&Path { hops: path, blinded_tail: None }, 42);
}
#[test]
};
assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 0);
}
+
+ #[test]
+ fn scores_with_blinded_path() {
+ // Make sure we'll account for a blinded path's final_value_msat in scoring
+ let logger = TestLogger::new();
+ let network_graph = network_graph(&logger);
+ let params = ProbabilisticScoringParameters {
+ liquidity_penalty_multiplier_msat: 1_000,
+ liquidity_offset_half_life: Duration::from_secs(10),
+ ..ProbabilisticScoringParameters::zero_penalty()
+ };
+ let mut scorer = ProbabilisticScorer::new(params, &network_graph, &logger);
+ let source = source_node_id();
+ let target = target_node_id();
+ let usage = ChannelUsage {
+ amount_msat: 512,
+ inflight_htlc_msat: 0,
+ effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024, htlc_maximum_msat: 1_000 },
+ };
+ assert_eq!(scorer.channel_penalty_msat(42, &source, &target, usage), 300);
+
+ let mut path = payment_path_for_amount(768);
+ let recipient_hop = path.hops.pop().unwrap();
+ let blinded_path = BlindedPath {
+ introduction_node_id: path.hops.last().as_ref().unwrap().pubkey,
+ blinding_point: test_utils::pubkey(42),
+ blinded_hops: vec![
+ BlindedHop { blinded_node_id: test_utils::pubkey(44), encrypted_payload: Vec::new() }
+ ],
+ };
+ path.blinded_tail = Some(BlindedTail {
+ hops: blinded_path.blinded_hops,
+ blinding_point: blinded_path.blinding_point,
+ excess_final_cltv_expiry_delta: recipient_hop.cltv_expiry_delta,
+ final_value_msat: recipient_hop.fee_msat,
+ });
+
+ // Check the liquidity before and after scoring payment failures to ensure the blinded path's
+ // final value is taken into account.
+ assert!(scorer.channel_liquidities.get(&42).is_none());
+
+ scorer.payment_path_failed(&path, 42);
+ path.blinded_tail.as_mut().unwrap().final_value_msat = 256;
+ scorer.payment_path_failed(&path, 43);
+
+ let liquidity = scorer.channel_liquidities.get(&42).unwrap()
+ .as_directed(&source, &target, 0, 1_000, &scorer.params);
+ assert_eq!(liquidity.min_liquidity_msat(), 256);
+ assert_eq!(liquidity.max_liquidity_msat(), 768);
+ }
}
/// A trivial implementation of [`UtxoLookup`] which is used to call back into the network graph
/// once we have a concrete resolution of a request.
-struct UtxoResolver(Result<TxOut, UtxoLookupError>);
+pub(crate) struct UtxoResolver(Result<TxOut, UtxoLookupError>);
impl UtxoLookup for UtxoResolver {
fn get_utxo(&self, _genesis_hash: &BlockHash, _short_channel_id: u64) -> UtxoResult {
UtxoResult::Sync(self.0.clone())
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
for (idx, p) in self.0.paths.iter().enumerate() {
writeln!(f, "path {}:", idx)?;
- for h in p.iter() {
+ for h in p.hops.iter() {
writeln!(f, " node_id: {}, short_channel_id: {}, fee_msat: {}, cltv_expiry_delta: {}", log_pubkey!(h.pubkey), h.short_channel_id, h.fee_msat, h.cltv_expiry_delta)?;
}
+ writeln!(f, " blinded_tail: {:?}", p.blinded_tail)?;
}
Ok(())
}
}
// Vectors
-macro_rules! impl_for_vec {
+macro_rules! impl_writeable_for_vec {
($ty: ty $(, $name: ident)*) => {
impl<$($name : Writeable),*> Writeable for Vec<$ty> {
#[inline]
Ok(())
}
}
-
+ }
+}
+macro_rules! impl_readable_for_vec {
+ ($ty: ty $(, $name: ident)*) => {
impl<$($name : Readable),*> Readable for Vec<$ty> {
#[inline]
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
}
}
}
+macro_rules! impl_for_vec {
+ ($ty: ty $(, $name: ident)*) => {
+ impl_writeable_for_vec!($ty $(, $name)*);
+ impl_readable_for_vec!($ty $(, $name)*);
+ }
+}
impl Writeable for Vec<u8> {
#[inline]
impl_for_vec!(ecdsa::Signature);
impl_for_vec!(crate::ln::channelmanager::MonitorUpdateCompletionAction);
impl_for_vec!((A, B), A, B);
+impl_writeable_for_vec!(&crate::routing::router::BlindedTail);
+impl_readable_for_vec!(crate::routing::router::BlindedTail);
impl Writeable for Script {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
use crate::ln::script::ShutdownScript;
use crate::routing::gossip::{EffectiveCapacity, NetworkGraph, NodeId};
use crate::routing::utxo::{UtxoLookup, UtxoLookupError, UtxoResult};
-use crate::routing::router::{find_route, InFlightHtlcs, Route, RouteHop, RouteParameters, Router, ScorerAccountingForInFlightHtlcs};
+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 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<u8>);
impl Writer for TestVecWriter {
fn write_all(&mut self, buf: &[u8]) -> Result<(), io::Error> {
let scorer = ScorerAccountingForInFlightHtlcs::new(locked_scorer, inflight_htlcs);
for path in &route.paths {
let mut aggregate_msat = 0u64;
- for (idx, hop) in path.iter().rev().enumerate() {
+ for (idx, hop) in path.hops.iter().rev().enumerate() {
aggregate_msat += hop.fee_msat;
let usage = ChannelUsage {
amount_msat: aggregate_msat,
// Since the path is reversed, the last element in our iteration is the first
// hop.
- if idx == path.len() - 1 {
+ 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.len() - 1 - idx;
- scorer.channel_penalty_msat(hop.short_channel_id, &NodeId::from_pubkey(&path[curr_hop_path_idx - 1].pubkey), &NodeId::from_pubkey(&hop.pubkey), usage);
+ 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);
}
}
}
}
impl TestBroadcaster {
- pub fn new(blocks: Arc<Mutex<Vec<(Block, u32)>>>) -> 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<Mutex<Vec<(Block, u32)>>>) -> Self {
+ Self { txn_broadcasted: Mutex::new(Vec::new()), blocks }
}
pub fn txn_broadcast(&self) -> Vec<Transaction> {
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);
0
}
- fn payment_path_failed(&mut self, _actual_path: &[&RouteHop], _actual_short_channel_id: u64) {}
+ fn payment_path_failed(&mut self, _actual_path: &Path, _actual_short_channel_id: u64) {}
- fn payment_path_successful(&mut self, _actual_path: &[&RouteHop]) {}
+ fn payment_path_successful(&mut self, _actual_path: &Path) {}
- fn probe_failed(&mut self, _actual_path: &[&RouteHop], _: u64) {}
+ fn probe_failed(&mut self, _actual_path: &Path, _: u64) {}
- fn probe_successful(&mut self, _actual_path: &[&RouteHop]) {}
+ fn probe_successful(&mut self, _actual_path: &Path) {}
}
impl Drop for TestScorer {
pub(crate) fn notify(&self) {
let mut lock = self.notify_pending.lock().unwrap();
if let Some(future_state) = &lock.1 {
- if future_state.lock().unwrap().complete() {
+ if complete_future(future_state) {
lock.1 = None;
return;
}
} else {
let state = Arc::new(Mutex::new(FutureState {
callbacks: Vec::new(),
+ callbacks_with_state: Vec::new(),
complete: lock.0,
callbacks_made: false,
}));
// first bool - set to false if we're just calling a Waker, and true if we're calling an actual
// user-provided function.
callbacks: Vec<(bool, Box<dyn FutureCallback>)>,
+ callbacks_with_state: Vec<(bool, Box<dyn Fn(&Arc<Mutex<FutureState>>) -> () + Send>)>,
complete: bool,
callbacks_made: bool,
}
-impl FutureState {
- fn complete(&mut self) -> bool {
- for (counts_as_call, callback) in self.callbacks.drain(..) {
- callback.call();
- self.callbacks_made |= counts_as_call;
- }
- self.complete = true;
- self.callbacks_made
+fn complete_future(this: &Arc<Mutex<FutureState>>) -> bool {
+ let mut state_lock = this.lock().unwrap();
+ let state = &mut *state_lock;
+ for (counts_as_call, callback) in state.callbacks.drain(..) {
+ callback.call();
+ state.callbacks_made |= counts_as_call;
+ }
+ for (counts_as_call, callback) in state.callbacks_with_state.drain(..) {
+ (callback)(this);
+ state.callbacks_made |= counts_as_call;
}
+ state.complete = true;
+ state.callbacks_made
}
/// A simple future which can complete once, and calls some callback(s) when it does so.
for notifier_mtx in self.notifiers.iter() {
let cv_ref = Arc::clone(&cv);
let notified_fut_ref = Arc::clone(¬ified_fut_mtx);
- let notifier_ref = Arc::clone(¬ifier_mtx);
let mut notifier = notifier_mtx.lock().unwrap();
if notifier.complete {
- *notified_fut_mtx.lock().unwrap() = Some(notifier_ref);
+ *notified_fut_mtx.lock().unwrap() = Some(Arc::clone(¬ifier_mtx));
break;
}
- notifier.callbacks.push((false, Box::new(move || {
- *notified_fut_ref.lock().unwrap() = Some(Arc::clone(¬ifier_ref));
+ notifier.callbacks_with_state.push((false, Box::new(move |notifier_ref| {
+ *notified_fut_ref.lock().unwrap() = Some(Arc::clone(notifier_ref));
cv_ref.notify_all();
})));
}
}
}
+ #[cfg(feature = "std")]
+ #[test]
+ fn test_state_drops() {
+ // Previously, there was a leak if a `Notifier` was `drop`ed without ever being notified
+ // but after having been slept-on. This tests for that leak.
+ use crate::sync::Arc;
+ use std::thread;
+
+ let notifier_a = Arc::new(Notifier::new());
+ let notifier_b = Arc::new(Notifier::new());
+
+ let thread_notifier_a = Arc::clone(¬ifier_a);
+
+ let future_a = notifier_a.get_future();
+ let future_state_a = Arc::downgrade(&future_a.state);
+
+ let future_b = notifier_b.get_future();
+ let future_state_b = Arc::downgrade(&future_b.state);
+
+ let join_handle = thread::spawn(move || {
+ // Let the other thread get to the wait point, then notify it.
+ std::thread::sleep(Duration::from_millis(50));
+ thread_notifier_a.notify();
+ });
+
+ // Wait on the other thread to finish its sleep, note that the leak only happened if we
+ // actually have to sleep here, not if we immediately return.
+ Sleeper::from_two_futures(future_a, future_b).wait();
+
+ join_handle.join().unwrap();
+
+ // then drop the notifiers and make sure the future states are gone.
+ mem::drop(notifier_a);
+ mem::drop(notifier_b);
+
+ assert!(future_state_a.upgrade().is_none() && future_state_b.upgrade().is_none());
+ }
+
#[test]
fn test_future_callbacks() {
let future = Future {
state: Arc::new(Mutex::new(FutureState {
callbacks: Vec::new(),
+ callbacks_with_state: Vec::new(),
complete: false,
callbacks_made: false,
}))
future.register_callback(Box::new(move || assert!(!callback_ref.fetch_or(true, Ordering::SeqCst))));
assert!(!callback.load(Ordering::SeqCst));
- future.state.lock().unwrap().complete();
+ complete_future(&future.state);
assert!(callback.load(Ordering::SeqCst));
- future.state.lock().unwrap().complete();
+ complete_future(&future.state);
}
#[test]
let future = Future {
state: Arc::new(Mutex::new(FutureState {
callbacks: Vec::new(),
+ callbacks_with_state: Vec::new(),
complete: false,
callbacks_made: false,
}))
};
- future.state.lock().unwrap().complete();
+ complete_future(&future.state);
let callback = Arc::new(AtomicBool::new(false));
let callback_ref = Arc::clone(&callback);
let mut future = Future {
state: Arc::new(Mutex::new(FutureState {
callbacks: Vec::new(),
+ callbacks_with_state: Vec::new(),
complete: false,
callbacks_made: false,
}))
assert_eq!(Pin::new(&mut second_future).poll(&mut Context::from_waker(&second_waker)), Poll::Pending);
assert!(!second_woken.load(Ordering::SeqCst));
- future.state.lock().unwrap().complete();
+ complete_future(&future.state);
assert!(woken.load(Ordering::SeqCst));
assert!(second_woken.load(Ordering::SeqCst));
assert_eq!(Pin::new(&mut future).poll(&mut Context::from_waker(&waker)), Poll::Ready(()));
+++ /dev/null
-## Backwards Compatibility
-
-- Providing `ChannelMonitorUpdate`s generated by LDK 0.0.115 to a
-`ChannelMonitor` on 0.0.114 or before may panic.
+++ /dev/null
-## API Updates
-
-- `Event::PaymentPathFailed::retry` will always be `None` if we initiate a payment on 0.0.115
- then downgrade to an earlier version (#2063)
+++ /dev/null
-## Backwards Compatibility
- * Payments sent with the legacy `*_with_route` methods on LDK 0.0.115+ will no
- longer be retryable via the LDK 0.0.114- `retry_payment` method (#XXXX).