Instead, return a Duration and let the user do the work of waiting.
This is one of only a handful of steps to make us
mostly-syscall-free, at least enough to run in WASM according to
elichai.
use bitcoin_hashes::sha256::Hash as Sha256;
use bitcoin_hashes::Hash;
use bitcoin_hashes::sha256::Hash as Sha256;
use bitcoin_hashes::Hash;
-use std::time::Instant;
-
use ln::functional_test_utils::*;
#[test]
use ln::functional_test_utils::*;
#[test]
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
- nodes[0].node.channel_state.lock().unwrap().next_forward = Instant::now();
nodes[0].node.process_pending_htlc_forwards();
expect_payment_received!(nodes[0], payment_hash_2, 1000000);
nodes[0].node.process_pending_htlc_forwards();
expect_payment_received!(nodes[0], payment_hash_2, 1000000);
use std::io::Cursor;
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::io::Cursor;
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering};
-use std::time::{Instant,Duration};
+use std::time::Duration;
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
//
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
//
pub(super) struct ChannelHolder {
pub(super) by_id: HashMap<[u8; 32], Channel>,
pub(super) short_to_id: HashMap<u64, [u8; 32]>,
pub(super) struct ChannelHolder {
pub(super) by_id: HashMap<[u8; 32], Channel>,
pub(super) short_to_id: HashMap<u64, [u8; 32]>,
- pub(super) next_forward: Instant,
/// short channel id -> forward infos. Key of 0 means payments received
/// Note that while this is held in the same mutex as the channels themselves, no consistency
/// guarantees are made about the existence of a channel with the short id here, nor the short
/// short channel id -> forward infos. Key of 0 means payments received
/// Note that while this is held in the same mutex as the channels themselves, no consistency
/// guarantees are made about the existence of a channel with the short id here, nor the short
pub(super) struct MutChannelHolder<'a> {
pub(super) by_id: &'a mut HashMap<[u8; 32], Channel>,
pub(super) short_to_id: &'a mut HashMap<u64, [u8; 32]>,
pub(super) struct MutChannelHolder<'a> {
pub(super) by_id: &'a mut HashMap<[u8; 32], Channel>,
pub(super) short_to_id: &'a mut HashMap<u64, [u8; 32]>,
- pub(super) next_forward: &'a mut Instant,
pub(super) forward_htlcs: &'a mut HashMap<u64, Vec<HTLCForwardInfo>>,
pub(super) claimable_htlcs: &'a mut HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
pub(super) pending_msg_events: &'a mut Vec<events::MessageSendEvent>,
pub(super) forward_htlcs: &'a mut HashMap<u64, Vec<HTLCForwardInfo>>,
pub(super) claimable_htlcs: &'a mut HashMap<PaymentHash, Vec<(u64, HTLCPreviousHopData)>>,
pub(super) pending_msg_events: &'a mut Vec<events::MessageSendEvent>,
MutChannelHolder {
by_id: &mut self.by_id,
short_to_id: &mut self.short_to_id,
MutChannelHolder {
by_id: &mut self.by_id,
short_to_id: &mut self.short_to_id,
- next_forward: &mut self.next_forward,
forward_htlcs: &mut self.forward_htlcs,
claimable_htlcs: &mut self.claimable_htlcs,
pending_msg_events: &mut self.pending_msg_events,
forward_htlcs: &mut self.forward_htlcs,
claimable_htlcs: &mut self.claimable_htlcs,
pending_msg_events: &mut self.pending_msg_events,
channel_state: Mutex::new(ChannelHolder{
by_id: HashMap::new(),
short_to_id: HashMap::new(),
channel_state: Mutex::new(ChannelHolder{
by_id: HashMap::new(),
short_to_id: HashMap::new(),
- next_forward: Instant::now(),
forward_htlcs: HashMap::new(),
claimable_htlcs: HashMap::new(),
pending_msg_events: Vec::new(),
forward_htlcs: HashMap::new(),
claimable_htlcs: HashMap::new(),
pending_msg_events: Vec::new(),
let mut channel_state_lock = self.channel_state.lock().unwrap();
let channel_state = channel_state_lock.borrow_parts();
let mut channel_state_lock = self.channel_state.lock().unwrap();
let channel_state = channel_state_lock.borrow_parts();
- if cfg!(not(feature = "fuzztarget")) && Instant::now() < *channel_state.next_forward {
- return;
- }
-
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
if short_chan_id != 0 {
let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
if short_chan_id != 0 {
let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
let mut forward_event = None;
if channel_state_lock.forward_htlcs.is_empty() {
let mut forward_event = None;
if channel_state_lock.forward_htlcs.is_empty() {
- forward_event = Some(Instant::now() + Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
- channel_state_lock.next_forward = forward_event.unwrap();
+ forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
}
match channel_state_lock.forward_htlcs.entry(short_channel_id) {
hash_map::Entry::Occupied(mut entry) => {
}
match channel_state_lock.forward_htlcs.entry(short_channel_id) {
hash_map::Entry::Occupied(mut entry) => {
if !pending_forwards.is_empty() {
let mut channel_state = self.channel_state.lock().unwrap();
if channel_state.forward_htlcs.is_empty() {
if !pending_forwards.is_empty() {
let mut channel_state = self.channel_state.lock().unwrap();
if channel_state.forward_htlcs.is_empty() {
- forward_event = Some(Instant::now() + Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
- channel_state.next_forward = forward_event.unwrap();
+ forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
}
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
}
for (forward_info, prev_htlc_id) in pending_forwards.drain(..) {
match channel_state.forward_htlcs.entry(forward_info.short_channel_id) {
channel_state: Mutex::new(ChannelHolder {
by_id,
short_to_id,
channel_state: Mutex::new(ChannelHolder {
by_id,
short_to_id,
- next_forward: Instant::now(),
forward_htlcs,
claimable_htlcs,
pending_msg_events: Vec::new(),
forward_htlcs,
claimable_htlcs,
pending_msg_events: Vec::new(),
use std::default::Default;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::default::Default;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::mem;
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
use std::mem;
pub const CHAN_CONFIRM_DEPTH: u32 = 100;
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
Event::PendingHTLCsForwardable { .. } => { },
_ => panic!("Unexpected event"),
};
- let node_ref: &Node = &$node;
- node_ref.node.channel_state.lock().unwrap().next_forward = Instant::now();
$node.node.process_pending_htlc_forwards();
}}
}
$node.node.process_pending_htlc_forwards();
}}
}
use std::default::Default;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::default::Default;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::mem;
use ln::functional_test_utils::*;
use std::mem;
use ln::functional_test_utils::*;
_ => panic!("Unexpected event"),
};
}
_ => panic!("Unexpected event"),
};
}
- nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
nodes[1].node.process_pending_htlc_forwards();
check_added_monitors!(nodes[1], 1);
nodes[1].node.process_pending_htlc_forwards();
check_added_monitors!(nodes[1], 1);
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
nodes[1].node.peer_disconnected(&nodes[0].node.get_our_node_id(), false);
reconnect_nodes(&nodes[0], &nodes[1], (false, false), (0, 0), (0, 0), (0, 0), (0, 0), (false, false));
- nodes[1].node.channel_state.lock().unwrap().next_forward = Instant::now();
nodes[1].node.process_pending_htlc_forwards();
let events_2 = nodes[1].node.get_and_clear_pending_events();
nodes[1].node.process_pending_htlc_forwards();
let events_2 = nodes[1].node.get_and_clear_pending_events();
macro_rules! expect_htlc_forward {
($node: expr) => {{
expect_event!($node, Event::PendingHTLCsForwardable);
macro_rules! expect_htlc_forward {
($node: expr) => {{
expect_event!($node, Event::PendingHTLCsForwardable);
- $node.node.channel_state.lock().unwrap().next_forward = Instant::now();
$node.node.process_pending_htlc_forwards();
}}
}
$node.node.process_pending_htlc_forwards();
}}
}
use secp256k1::key::PublicKey;
use secp256k1::key::PublicKey;
+use std::time::Duration;
/// An Event which you should probably take some action in response to.
pub enum Event {
/// An Event which you should probably take some action in response to.
pub enum Event {
/// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
/// time in the future.
PendingHTLCsForwardable {
/// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a
/// time in the future.
PendingHTLCsForwardable {
- /// The earliest time at which process_pending_htlc_forwards should be called.
- time_forwardable: Instant,
+ /// The amount of time that should be waited prior to calling process_pending_htlc_forwards
+ time_forwardable: Duration,
},
/// Used to indicate that an output was generated on-chain which you should know how to spend.
/// Such an output will *not* ever be spent by rust-lightning, so you need to store them
},
/// Used to indicate that an output was generated on-chain which you should know how to spend.
/// Such an output will *not* ever be spent by rust-lightning, so you need to store them