From: Matt Corallo Date: Fri, 19 Jul 2019 02:21:00 +0000 (-0400) Subject: Make rand a dev-dep by having the user randomize HTLC forward delay X-Git-Tag: v0.0.12~204^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=refs%2Fheads%2F2019-07-no-rand;p=rust-lightning Make rand a dev-dep by having the user randomize HTLC forward delay This removes the last calls to rand outside of test and moves the dep to a dev-dependency, dropping our fuzz rng wrapper in the process. --- diff --git a/Cargo.toml b/Cargo.toml index 63251c618..c0e7083d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ max_level_debug = [] [dependencies] bitcoin = "0.18" bitcoin_hashes = "0.3" -rand = "0.4" secp256k1 = "0.12" [dev-dependencies.bitcoin] @@ -33,6 +32,7 @@ features = ["bitcoinconsensus"] [dev-dependencies] hex = "0.3" +rand = "0.4" [profile.dev] opt-level = 1 diff --git a/fuzz/fuzz_targets/chanmon_deser_target.rs b/fuzz/fuzz_targets/chanmon_deser_target.rs index 4a1044478..f74183258 100644 --- a/fuzz/fuzz_targets/chanmon_deser_target.rs +++ b/fuzz/fuzz_targets/chanmon_deser_target.rs @@ -8,7 +8,6 @@ extern crate lightning; use bitcoin_hashes::sha256d::Hash as Sha256dHash; use lightning::ln::channelmonitor; -use lightning::util::reset_rng_state; use lightning::util::ser::{ReadableArgs, Writer}; mod utils; @@ -30,7 +29,6 @@ impl Writer for VecWriter { #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); let logger = Arc::new(test_logger::TestLogger::new("".to_owned())); if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) { let mut w = VecWriter(Vec::new()); diff --git a/fuzz/fuzz_targets/chanmon_fail_consistency.rs b/fuzz/fuzz_targets/chanmon_fail_consistency.rs index bebedda5e..032b6b81b 100644 --- a/fuzz/fuzz_targets/chanmon_fail_consistency.rs +++ b/fuzz/fuzz_targets/chanmon_fail_consistency.rs @@ -37,7 +37,7 @@ use lightning::ln::channelmonitor::{ChannelMonitorUpdateErr, HTLCUpdate}; use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage}; use lightning::ln::router::{Route, RouteHop}; use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC, LocalFeatures}; -use lightning::util::{reset_rng_state, events}; +use lightning::util::events; use lightning::util::logger::Logger; use lightning::util::config::UserConfig; use lightning::util::events::{EventsProvider, MessageSendEventsProvider}; @@ -136,8 +136,6 @@ impl KeysInterface for KeyProvider { #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); - let fee_est = Arc::new(FuzzEstimator{}); let broadcast = Arc::new(TestBroadcaster{}); diff --git a/fuzz/fuzz_targets/full_stack_target.rs b/fuzz/fuzz_targets/full_stack_target.rs index 8e0977978..acd757882 100644 --- a/fuzz/fuzz_targets/full_stack_target.rs +++ b/fuzz/fuzz_targets/full_stack_target.rs @@ -34,7 +34,6 @@ use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor}; use lightning::ln::router::Router; use lightning::util::events::{EventsProvider,Event}; -use lightning::util::reset_rng_state; use lightning::util::logger::Logger; use lightning::util::config::UserConfig; @@ -292,8 +291,6 @@ impl KeysInterface for KeyProvider { #[inline] pub fn do_test(data: &[u8], logger: &Arc) { - reset_rng_state(); - let input = Arc::new(InputData { data: data.to_vec(), read_pos: AtomicUsize::new(0), diff --git a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs index bd058b1ed..0f18d0232 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_accept_channel_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::AcceptChannel, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs index 233a4a77a..226028ea0 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_announcement_signatures_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::AnnouncementSignatures, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs index fbaca76db..0bdc10e85 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_announcement_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_exact!(msgs::ChannelAnnouncement, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs index debd89466..4af593759 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_reestablish_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::ChannelReestablish, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs index 4f38e2f48..724dca412 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_channel_update_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_exact!(msgs::ChannelUpdate, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs index 7d63131e7..faeeae3eb 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_closing_signed_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::ClosingSigned, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs index 93e72bd75..97c4b3088 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_commitment_signed_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::CommitmentSigned, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs b/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs index 4b97b17ef..9b190b086 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_decoded_onion_error_packet_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::DecodedOnionErrorPacket, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs index 5ccbd2646..d749dc91f 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_error_message_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_hole!(msgs::ErrorMessage, data, 32, 2); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs index 57713196c..45b257bb0 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_created_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::FundingCreated, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs index b1ad5b3a0..cd1e89741 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_locked_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::FundingLocked, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs index a3b33dc64..5992d6902 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_funding_signed_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::FundingSigned, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs index f7eb0671f..cdca84821 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_init_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_init_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::Init, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs index a847d0dad..f0a7a4c0d 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_node_announcement_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_exact!(msgs::NodeAnnouncement, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs b/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs index 558842918..058c050c5 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_onion_hop_data_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_hole!(msgs::OnionHopData, data, 1+8+8+4, 12); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs index 36f6519a4..aa13e96dd 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_open_channel_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::OpenChannel, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs index c470e1bbf..d2ea91335 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_ping_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_simple!(msgs::Ping, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs index ef1e4ada8..18120e2d1 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_pong_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_simple!(msgs::Pong, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs index 1e38a5d04..d82268d93 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_revoke_and_ack_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::RevokeAndACK, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs index d64efcc9f..34d4d2003 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_shutdown_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::Shutdown, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt index 50e433738..2704bcdff 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_target_template.txt +++ b/fuzz/fuzz_targets/msg_targets/msg_target_template.txt @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); TEST_MSG!(msgs::MSG_TARGET, dataEXTRA_ARGS); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs index 45bc9d1b9..e64a5c29f 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_add_htlc_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg_hole!(msgs::UpdateAddHTLC, data, 85, 33); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs index 8432a57da..fedce568e 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_htlc_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::UpdateFailHTLC, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs index ad129e002..377378fcc 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fail_malformed_htlc_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::UpdateFailMalformedHTLC, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs index f345711cd..56b9ac424 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fee_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::UpdateFee, data); } diff --git a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs index 380b02a47..f0c936d1e 100644 --- a/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs +++ b/fuzz/fuzz_targets/msg_targets/msg_update_fulfill_htlc_target.rs @@ -4,14 +4,12 @@ extern crate lightning; use lightning::ln::msgs; -use lightning::util::reset_rng_state; mod utils; use utils::VecWriter; #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); test_msg!(msgs::UpdateFulfillHTLC, data); } diff --git a/fuzz/fuzz_targets/peer_crypt_target.rs b/fuzz/fuzz_targets/peer_crypt_target.rs index a4ddbf5e4..0b82303f8 100644 --- a/fuzz/fuzz_targets/peer_crypt_target.rs +++ b/fuzz/fuzz_targets/peer_crypt_target.rs @@ -2,7 +2,6 @@ extern crate lightning; extern crate secp256k1; use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor; -use lightning::util::reset_rng_state; use secp256k1::key::{PublicKey,SecretKey}; @@ -14,8 +13,6 @@ fn slice_to_be16(v: &[u8]) -> u16 { #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); - let mut read_pos = 0; macro_rules! get_slice { ($len: expr) => { diff --git a/fuzz/fuzz_targets/router_target.rs b/fuzz/fuzz_targets/router_target.rs index 2dcad55a1..25bf38274 100644 --- a/fuzz/fuzz_targets/router_target.rs +++ b/fuzz/fuzz_targets/router_target.rs @@ -11,7 +11,6 @@ use lightning::ln::channelmanager::ChannelDetails; use lightning::ln::msgs; use lightning::ln::msgs::{RoutingMessageHandler}; use lightning::ln::router::{Router, RouteHint}; -use lightning::util::reset_rng_state; use lightning::util::logger::Logger; use lightning::util::ser::Readable; @@ -96,8 +95,6 @@ impl ChainWatchInterface for DummyChainWatcher { #[inline] pub fn do_test(data: &[u8]) { - reset_rng_state(); - let input = Arc::new(InputData { data: data.to_vec(), read_pos: AtomicUsize::new(0), diff --git a/src/lib.rs b/src/lib.rs index 52ec66402..aae543e07 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,8 +13,8 @@ extern crate bitcoin; extern crate bitcoin_hashes; -extern crate rand; extern crate secp256k1; +#[cfg(test)] extern crate rand; #[cfg(test)] extern crate hex; #[macro_use] diff --git a/src/ln/channelmanager.rs b/src/ln/channelmanager.rs index 14491f1aa..d48dcea42 100644 --- a/src/ln/channelmanager.rs +++ b/src/ln/channelmanager.rs @@ -36,7 +36,7 @@ use ln::onion_utils; use ln::msgs::{ChannelMessageHandler, DecodeError, HandleError}; use chain::keysinterface::KeysInterface; use util::config::UserConfig; -use util::{byte_utils, events, rng}; +use util::{byte_utils, events}; use util::ser::{Readable, ReadableArgs, Writeable, Writer}; use util::chacha20::ChaCha20; use util::logger::Logger; @@ -214,11 +214,11 @@ impl MsgHandleErrInternal { } } -/// We hold back HTLCs we intend to relay for a random interval in the range (this, 5*this). This -/// provides some limited amount of privacy. Ideally this would range from somewhere like 1 second -/// to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly. We could -/// probably increase this significantly. -const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u32 = 50; +/// We hold back HTLCs we intend to relay for a random interval greater than this (see +/// Event::PendingHTLCsForwardable for the API guidelines indicating how long should be waited). +/// This provides some limited amount of privacy. Ideally this would range from somewhere like one +/// second to 30 seconds, but people expect lightning to be, you know, kinda fast, sadly. +const MIN_HTLC_RELAY_HOLDING_CELL_MILLIS: u64 = 100; pub(super) enum HTLCForwardInfo { AddHTLC { @@ -1486,7 +1486,7 @@ impl ChannelManager { let mut forward_event = None; if channel_state_lock.forward_htlcs.is_empty() { - forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64)); + forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS)); } match channel_state_lock.forward_htlcs.entry(short_channel_id) { hash_map::Entry::Occupied(mut entry) => { @@ -2095,7 +2095,7 @@ impl ChannelManager { if !pending_forwards.is_empty() { let mut channel_state = self.channel_state.lock().unwrap(); if channel_state.forward_htlcs.is_empty() { - forward_event = Some(Duration::from_millis(((rng::rand_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64)); + forward_event = Some(Duration::from_millis(MIN_HTLC_RELAY_HOLDING_CELL_MILLIS)) } for (forward_info, prev_htlc_id) in pending_forwards.drain(..) { match channel_state.forward_htlcs.entry(forward_info.short_channel_id) { diff --git a/src/util/events.rs b/src/util/events.rs index e4bed8f2e..96a5b48a7 100644 --- a/src/util/events.rs +++ b/src/util/events.rs @@ -92,7 +92,10 @@ pub enum Event { /// Used to indicate that ChannelManager::process_pending_htlc_forwards should be called at a /// time in the future. PendingHTLCsForwardable { - /// The amount of time that should be waited prior to calling process_pending_htlc_forwards + /// The minimum amount of time that should be waited prior to calling + /// process_pending_htlc_forwards. To increase the effort required to correlate payments, + /// you should wait a random amount of time in roughly the range (now + time_forwardable, + /// now + 5*time_forwardable). time_forwardable: Duration, }, /// Used to indicate that an output was generated on-chain which you should know how to spend. diff --git a/src/util/mod.rs b/src/util/mod.rs index e4170590d..1a48507df 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -10,7 +10,6 @@ pub(crate) mod chacha20; pub(crate) mod poly1305; pub(crate) mod chacha20poly1305rfc; pub(crate) mod internal_traits; -pub(crate) mod rng; pub(crate) mod transaction_utils; #[macro_use] @@ -22,9 +21,6 @@ pub(crate) mod macro_logger; pub mod logger; pub mod config; -#[cfg(feature = "fuzztarget")] -pub use self::rng::{reset_rng_state, fill_bytes}; - #[cfg(test)] pub(crate) mod test_utils; diff --git a/src/util/rng.rs b/src/util/rng.rs deleted file mode 100644 index 63fbc99b5..000000000 --- a/src/util/rng.rs +++ /dev/null @@ -1,44 +0,0 @@ -#[cfg(not(feature = "fuzztarget"))] -mod real_rng { - use rand::{thread_rng,Rng}; - - pub fn fill_bytes(data: &mut [u8]) { - let mut rng = thread_rng(); - rng.fill_bytes(data); - } - - pub fn rand_f32() -> f32 { - let mut rng = thread_rng(); - rng.next_f32() - } -} -#[cfg(not(feature = "fuzztarget"))] -pub use self::real_rng::*; - -#[cfg(feature = "fuzztarget")] -mod fuzzy_rng { - use util::byte_utils; - - static mut RNG_ITER: u64 = 0; - - pub fn fill_bytes(data: &mut [u8]) { - let rng = unsafe { RNG_ITER += 1; RNG_ITER -1 }; - for i in 0..data.len() / 8 { - data[i*8..(i+1)*8].copy_from_slice(&byte_utils::be64_to_array(rng)); - } - let rem = data.len() % 8; - let off = data.len() - rem; - data[off..].copy_from_slice(&byte_utils::be64_to_array(rng)[0..rem]); - } - - pub fn rand_f32() -> f32 { - let rng = unsafe { RNG_ITER += 1; RNG_ITER - 1 }; - f64::from_bits(rng) as f32 - } - - pub fn reset_rng_state() { - unsafe { RNG_ITER = 0; } - } -} -#[cfg(feature = "fuzztarget")] -pub use self::fuzzy_rng::*;