Merge pull request #355 from ariard/2019-07-fix-csv-delay-check-remote-htlc
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Wed, 24 Jul 2019 22:33:20 +0000 (22:33 +0000)
committerGitHub <noreply@github.com>
Wed, 24 Jul 2019 22:33:20 +0000 (22:33 +0000)
Fix bug in check_spend_remote_htlc and let csv delays being user configurable

README.md
fuzz/fuzz_targets/chanmon_fail_consistency.rs
src/lib.rs
src/ln/functional_test_utils.rs
src/ln/functional_tests.rs
src/ln/msgs.rs
src/ln/onion_utils.rs
src/util/internal_traits.rs [deleted file]
src/util/mod.rs

index f2a39670b552196b17fe6f5325b7ff5cf0bf8ce6..460571f0070943da516720d588b386aa48d37b31 100644 (file)
--- a/README.md
+++ b/README.md
@@ -1,9 +1,11 @@
+[![Safety Dance](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
+
 Rust-Lightning, not Rusty's Lightning!
 =====
 
 Documentation can be found at [docs.rs](https://docs.rs/lightning/)
 
-Currently somewhere near 20% towards usable, published to see if there is any
+Currently somewhere near 40% towards usable, published to see if there is any
 real interest from folks in using a lightning rust library.
 
 The goal is to provide a full-featured but also incredibly flexible lightning
index 032b6b81bb34f25f74cf9993783a52c98dafade3..e45857443b4df163cde0bb061ca4c007c3a81c3e 100644 (file)
@@ -27,21 +27,22 @@ use bitcoin::network::constants::Network;
 use bitcoin_hashes::Hash as TraitImport;
 use bitcoin_hashes::hash160::Hash as Hash160;
 use bitcoin_hashes::sha256::Hash as Sha256;
+use bitcoin_hashes::sha256d::Hash as Sha256d;
 
 use lightning::chain::chaininterface;
 use lightning::chain::transaction::OutPoint;
 use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
 use lightning::chain::keysinterface::{ChannelKeys, KeysInterface};
 use lightning::ln::channelmonitor;
-use lightning::ln::channelmonitor::{ChannelMonitorUpdateErr, HTLCUpdate};
-use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage};
+use lightning::ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, HTLCUpdate};
+use lightning::ln::channelmanager::{ChannelManager, PaymentHash, PaymentPreimage, ChannelManagerReadArgs};
 use lightning::ln::router::{Route, RouteHop};
 use lightning::ln::msgs::{CommitmentUpdate, ChannelMessageHandler, ErrorAction, HandleError, UpdateAddHTLC, LocalFeatures};
 use lightning::util::events;
 use lightning::util::logger::Logger;
 use lightning::util::config::UserConfig;
 use lightning::util::events::{EventsProvider, MessageSendEventsProvider};
-use lightning::util::ser::{Readable, Writeable};
+use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
 
 mod utils;
 use utils::test_logger;
@@ -49,8 +50,9 @@ use utils::test_logger;
 use secp256k1::key::{PublicKey,SecretKey};
 use secp256k1::Secp256k1;
 
+use std::mem;
 use std::cmp::Ordering;
-use std::collections::HashSet;
+use std::collections::{HashSet, HashMap};
 use std::sync::{Arc,Mutex};
 use std::sync::atomic;
 use std::io::Cursor;
@@ -67,22 +69,51 @@ impl BroadcasterInterface for TestBroadcaster {
        fn broadcast_transaction(&self, _tx: &Transaction) { }
 }
 
+pub struct VecWriter(pub Vec<u8>);
+impl Writer for VecWriter {
+       fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+               self.0.extend_from_slice(buf);
+               Ok(())
+       }
+       fn size_hint(&mut self, size: usize) {
+               self.0.reserve_exact(size);
+       }
+}
+
 pub struct TestChannelMonitor {
        pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
        pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
+       pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
+       pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
+       pub latest_updates_good_at_last_ser: Mutex<HashMap<OutPoint, bool>>,
+       pub should_update_manager: atomic::AtomicBool,
 }
 impl TestChannelMonitor {
        pub fn new(chain_monitor: Arc<chaininterface::ChainWatchInterface>, broadcaster: Arc<chaininterface::BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<chaininterface::FeeEstimator>) -> Self {
                Self {
                        simple_monitor: channelmonitor::SimpleManyChannelMonitor::new(chain_monitor, broadcaster, logger, feeest),
                        update_ret: Mutex::new(Ok(())),
+                       latest_good_update: Mutex::new(HashMap::new()),
+                       latest_update_good: Mutex::new(HashMap::new()),
+                       latest_updates_good_at_last_ser: Mutex::new(HashMap::new()),
+                       should_update_manager: atomic::AtomicBool::new(false),
                }
        }
 }
 impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
        fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+               let ret = self.update_ret.lock().unwrap().clone();
+               if let Ok(()) = ret {
+                       let mut ser = VecWriter(Vec::new());
+                       monitor.write_for_disk(&mut ser).unwrap();
+                       self.latest_good_update.lock().unwrap().insert(funding_txo, ser.0);
+                       self.latest_update_good.lock().unwrap().insert(funding_txo, true);
+                       self.should_update_manager.store(true, atomic::Ordering::Relaxed);
+               } else {
+                       self.latest_update_good.lock().unwrap().insert(funding_txo, false);
+               }
                assert!(self.simple_monitor.add_update_monitor(funding_txo, monitor).is_ok());
-               self.update_ret.lock().unwrap().clone()
+               ret
        }
 
        fn fetch_pending_htlc_updated(&self) -> Vec<HTLCUpdate> {
@@ -155,6 +186,55 @@ pub fn do_test(data: &[u8]) {
                } }
        }
 
+       macro_rules! reload_node {
+               ($ser: expr, $node_id: expr, $old_monitors: expr) => { {
+                       let logger: Arc<Logger> = Arc::new(test_logger::TestLogger::new($node_id.to_string()));
+                       let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger)));
+                       let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone()));
+
+                       let keys_manager = Arc::new(KeyProvider { node_id: $node_id, session_id: atomic::AtomicU8::new(0), channel_id: atomic::AtomicU8::new(0) });
+                       let mut config = UserConfig::new();
+                       config.channel_options.fee_proportional_millionths = 0;
+                       config.channel_options.announced_channel = true;
+                       config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
+
+                       let mut monitors = HashMap::new();
+                       let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
+                       for (outpoint, monitor_ser) in old_monitors.drain() {
+                               monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
+                               monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
+                       }
+                       let mut monitor_refs = HashMap::new();
+                       for (outpoint, monitor) in monitors.iter() {
+                               monitor_refs.insert(*outpoint, monitor);
+                       }
+
+                       let read_args = ChannelManagerReadArgs {
+                               keys_manager,
+                               fee_estimator: fee_est.clone(),
+                               monitor: monitor.clone(),
+                               chain_monitor: watch,
+                               tx_broadcaster: broadcast.clone(),
+                               logger,
+                               default_config: config,
+                               channel_monitors: &monitor_refs,
+                       };
+
+                       let res = (<(Sha256d, ChannelManager)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
+                       for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
+                               if !was_good {
+                                       // If the last time we updated a monitor we didn't successfully update (and we
+                                       // have sense updated our serialized copy of the ChannelManager) we may
+                                       // force-close the channel on our counterparty cause we know we're missing
+                                       // something. Thus, we just return here since we can't continue to test.
+                                       return;
+                               }
+                       }
+                       res
+               } }
+       }
+
+
        let mut channel_txn = Vec::new();
        macro_rules! make_channel {
                ($source: expr, $dest: expr, $chan_id: expr) => { {
@@ -264,11 +344,11 @@ pub fn do_test(data: &[u8]) {
 
        // 3 nodes is enough to hit all the possible cases, notably unknown-source-unknown-dest
        // forwarding.
-       let (node_a, monitor_a) = make_node!(0);
-       let (node_b, monitor_b) = make_node!(1);
-       let (node_c, monitor_c) = make_node!(2);
+       let (mut node_a, mut monitor_a) = make_node!(0);
+       let (mut node_b, mut monitor_b) = make_node!(1);
+       let (mut node_c, mut monitor_c) = make_node!(2);
 
-       let nodes = [node_a, node_b, node_c];
+       let mut nodes = [node_a, node_b, node_c];
 
        make_channel!(nodes[0], nodes[1], 0);
        make_channel!(nodes[1], nodes[2], 1);
@@ -286,8 +366,15 @@ pub fn do_test(data: &[u8]) {
 
        let mut chan_a_disconnected = false;
        let mut chan_b_disconnected = false;
-       let mut chan_a_reconnecting = false;
-       let mut chan_b_reconnecting = false;
+       let mut ba_events = Vec::new();
+       let mut bc_events = Vec::new();
+
+       let mut node_a_ser = VecWriter(Vec::new());
+       nodes[0].write(&mut node_a_ser).unwrap();
+       let mut node_b_ser = VecWriter(Vec::new());
+       nodes[1].write(&mut node_b_ser).unwrap();
+       let mut node_c_ser = VecWriter(Vec::new());
+       nodes[2].write(&mut node_c_ser).unwrap();
 
        macro_rules! test_err {
                ($res: expr) => {
@@ -363,13 +450,18 @@ pub fn do_test(data: &[u8]) {
 
                macro_rules! process_msg_events {
                        ($node: expr, $corrupt_forward: expr) => { {
-                               for event in nodes[$node].get_and_clear_pending_msg_events() {
+                               let events = if $node == 1 {
+                                       let mut new_events = Vec::new();
+                                       mem::swap(&mut new_events, &mut ba_events);
+                                       new_events.extend_from_slice(&bc_events[..]);
+                                       bc_events.clear();
+                                       new_events
+                               } else { Vec::new() };
+                               for event in events.iter().chain(nodes[$node].get_and_clear_pending_msg_events().iter()) {
                                        match event {
                                                events::MessageSendEvent::UpdateHTLCs { ref node_id, updates: CommitmentUpdate { ref update_add_htlcs, ref update_fail_htlcs, ref update_fulfill_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
-                                                       for (idx, dest) in nodes.iter().enumerate() {
-                                                               if dest.get_our_node_id() == *node_id &&
-                                                                               (($node != 0 && idx != 0) || !chan_a_disconnected) &&
-                                                                               (($node != 2 && idx != 2) || !chan_b_disconnected) {
+                                                       for dest in nodes.iter() {
+                                                               if dest.get_our_node_id() == *node_id {
                                                                        assert!(update_fee.is_none());
                                                                        for update_add in update_add_htlcs {
                                                                                if !$corrupt_forward {
@@ -399,25 +491,16 @@ pub fn do_test(data: &[u8]) {
                                                        }
                                                },
                                                events::MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
-                                                       for (idx, dest) in nodes.iter().enumerate() {
-                                                               if dest.get_our_node_id() == *node_id &&
-                                                                               (($node != 0 && idx != 0) || !chan_a_disconnected) &&
-                                                                               (($node != 2 && idx != 2) || !chan_b_disconnected) {
+                                                       for dest in nodes.iter() {
+                                                               if dest.get_our_node_id() == *node_id {
                                                                        test_err!(dest.handle_revoke_and_ack(&nodes[$node].get_our_node_id(), msg));
                                                                }
                                                        }
                                                },
                                                events::MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
-                                                       for (idx, dest) in nodes.iter().enumerate() {
+                                                       for dest in nodes.iter() {
                                                                if dest.get_our_node_id() == *node_id {
                                                                        test_err!(dest.handle_channel_reestablish(&nodes[$node].get_our_node_id(), msg));
-                                                                       if $node == 0 || idx == 0 {
-                                                                               chan_a_reconnecting = false;
-                                                                               chan_a_disconnected = false;
-                                                                       } else {
-                                                                               chan_b_reconnecting = false;
-                                                                               chan_b_disconnected = false;
-                                                                       }
                                                                }
                                                        }
                                                },
@@ -434,6 +517,56 @@ pub fn do_test(data: &[u8]) {
                        } }
                }
 
+               macro_rules! drain_msg_events_on_disconnect {
+                       ($counterparty_id: expr) => { {
+                               if $counterparty_id == 0 {
+                                       for event in nodes[0].get_and_clear_pending_msg_events() {
+                                               match event {
+                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
+                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
+                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       _ => panic!("Unhandled message event"),
+                                               }
+                                       }
+                                       ba_events.clear();
+                               } else {
+                                       for event in nodes[2].get_and_clear_pending_msg_events() {
+                                               match event {
+                                                       events::MessageSendEvent::UpdateHTLCs { .. } => {},
+                                                       events::MessageSendEvent::SendRevokeAndACK { .. } => {},
+                                                       events::MessageSendEvent::SendChannelReestablish { .. } => {},
+                                                       events::MessageSendEvent::SendFundingLocked { .. } => {},
+                                                       events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => {},
+                                                       _ => panic!("Unhandled message event"),
+                                               }
+                                       }
+                                       bc_events.clear();
+                               }
+                               let mut events = nodes[1].get_and_clear_pending_msg_events();
+                               let drop_node_id = if $counterparty_id == 0 { nodes[0].get_our_node_id() } else { nodes[2].get_our_node_id() };
+                               let msg_sink = if $counterparty_id == 0 { &mut bc_events } else { &mut ba_events };
+                               for event in events.drain(..) {
+                                       let push = match event {
+                                               events::MessageSendEvent::UpdateHTLCs { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendRevokeAndACK { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendChannelReestablish { ref node_id, .. } => {
+                                                       if *node_id != drop_node_id { true } else { false }
+                                               },
+                                               events::MessageSendEvent::SendFundingLocked { .. } => false,
+                                               events::MessageSendEvent::PaymentFailureNetworkUpdate { .. } => false,
+                                               _ => panic!("Unhandled message event"),
+                                       };
+                                       if push { msg_sink.push(event); }
+                               }
+                       } }
+               }
+
                macro_rules! process_events {
                        ($node: expr, $fail: expr) => { {
                                // In case we get 256 payments we may have a hash collision, resulting in the
@@ -500,6 +633,7 @@ pub fn do_test(data: &[u8]) {
                                        nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
                                        nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
                                        chan_a_disconnected = true;
+                                       drain_msg_events_on_disconnect!(0);
                                }
                        },
                        0x10 => {
@@ -507,20 +641,21 @@ pub fn do_test(data: &[u8]) {
                                        nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
                                        nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
                                        chan_b_disconnected = true;
+                                       drain_msg_events_on_disconnect!(2);
                                }
                        },
                        0x11 => {
-                               if chan_a_disconnected && !chan_a_reconnecting {
+                               if chan_a_disconnected {
                                        nodes[0].peer_connected(&nodes[1].get_our_node_id());
                                        nodes[1].peer_connected(&nodes[0].get_our_node_id());
-                                       chan_a_reconnecting = true;
+                                       chan_a_disconnected = false;
                                }
                        },
                        0x12 => {
-                               if chan_b_disconnected && !chan_b_reconnecting {
+                               if chan_b_disconnected {
                                        nodes[1].peer_connected(&nodes[2].get_our_node_id());
                                        nodes[2].peer_connected(&nodes[1].get_our_node_id());
-                                       chan_b_reconnecting = true;
+                                       chan_b_disconnected = false;
                                }
                        },
                        0x13 => process_msg_events!(0, true),
@@ -535,8 +670,67 @@ pub fn do_test(data: &[u8]) {
                        0x1c => process_msg_events!(2, false),
                        0x1d => process_events!(2, true),
                        0x1e => process_events!(2, false),
+                       0x1f => {
+                               if !chan_a_disconnected {
+                                       nodes[1].peer_disconnected(&nodes[0].get_our_node_id(), false);
+                                       chan_a_disconnected = true;
+                                       drain_msg_events_on_disconnect!(0);
+                               }
+                               let (new_node_a, new_monitor_a) = reload_node!(node_a_ser, 0, monitor_a);
+                               node_a = Arc::new(new_node_a);
+                               nodes[0] = node_a.clone();
+                               monitor_a = new_monitor_a;
+                       },
+                       0x20 => {
+                               if !chan_a_disconnected {
+                                       nodes[0].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       chan_a_disconnected = true;
+                                       nodes[0].get_and_clear_pending_msg_events();
+                                       ba_events.clear();
+                               }
+                               if !chan_b_disconnected {
+                                       nodes[2].peer_disconnected(&nodes[1].get_our_node_id(), false);
+                                       chan_b_disconnected = true;
+                                       nodes[2].get_and_clear_pending_msg_events();
+                                       bc_events.clear();
+                               }
+                               let (new_node_b, new_monitor_b) = reload_node!(node_b_ser, 1, monitor_b);
+                               node_b = Arc::new(new_node_b);
+                               nodes[1] = node_b.clone();
+                               monitor_b = new_monitor_b;
+                       },
+                       0x21 => {
+                               if !chan_b_disconnected {
+                                       nodes[1].peer_disconnected(&nodes[2].get_our_node_id(), false);
+                                       chan_b_disconnected = true;
+                                       drain_msg_events_on_disconnect!(2);
+                               }
+                               let (new_node_c, new_monitor_c) = reload_node!(node_c_ser, 2, monitor_c);
+                               node_c = Arc::new(new_node_c);
+                               nodes[2] = node_c.clone();
+                               monitor_c = new_monitor_c;
+                       },
                        _ => test_return!(),
                }
+
+               if monitor_a.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_a_ser.0.clear();
+                       nodes[0].write(&mut node_a_ser).unwrap();
+                       monitor_a.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_a.latest_updates_good_at_last_ser.lock().unwrap() = monitor_a.latest_update_good.lock().unwrap().clone();
+               }
+               if monitor_b.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_b_ser.0.clear();
+                       nodes[1].write(&mut node_b_ser).unwrap();
+                       monitor_b.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_b.latest_updates_good_at_last_ser.lock().unwrap() = monitor_b.latest_update_good.lock().unwrap().clone();
+               }
+               if monitor_c.should_update_manager.load(atomic::Ordering::Relaxed) {
+                       node_c_ser.0.clear();
+                       nodes[2].write(&mut node_c_ser).unwrap();
+                       monitor_c.should_update_manager.store(false, atomic::Ordering::Relaxed);
+                       *monitor_c.latest_updates_good_at_last_ser.lock().unwrap() = monitor_c.latest_update_good.lock().unwrap().clone();
+               }
        }
 }
 
index aae543e073838e605c6996241aea2b05f22e123d..c6e4708a8ebadb933e794889d1378e6b34f24fdf 100644 (file)
@@ -10,6 +10,7 @@
 //! instead of having a rather-separate lightning appendage to a wallet.
 
 #![cfg_attr(not(feature = "fuzztarget"), deny(missing_docs))]
+#![forbid(unsafe_code)]
 
 extern crate bitcoin;
 extern crate bitcoin_hashes;
index 777ec829287a48b56a18266094a736f1a7729c31..e1be9cc173fdc59f54ba5b61b30469bd8e2a9623 100644 (file)
@@ -15,7 +15,7 @@ use util::logger::Logger;
 use util::config::UserConfig;
 
 use bitcoin::util::hash::BitcoinHash;
-use bitcoin::blockdata::block::{BlockHeader, Block};
+use bitcoin::blockdata::block::BlockHeader;
 use bitcoin::blockdata::transaction::{Transaction, TxOut};
 use bitcoin::network::constants::Network;
 
@@ -56,20 +56,6 @@ pub fn connect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u3
        header.bitcoin_hash()
 }
 
-pub fn disconnect_blocks(chain: &chaininterface::ChainWatchInterfaceUtil, depth: u32, height: u32, parent: bool, prev_blockhash: Sha256d) {
-       let mut header = BlockHeader { version: 0x2000000, prev_blockhash: if parent { prev_blockhash } else { Default::default() }, merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       let mut blocks = Vec::new();
-       for _ in 0..depth {
-               blocks.push(Block { header, txdata: Vec::new() });
-               header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
-       }
-       let mut height = height;
-       for block in blocks.pop() {
-               chain.block_disconnected(&block.header, height);
-               height -= 1;
-       }
-}
-
 pub struct Node {
        pub chain_monitor: Arc<chaininterface::ChainWatchInterfaceUtil>,
        pub tx_broadcaster: Arc<test_utils::TestBroadcaster>,
index 8c0283828641e6ead41cd887a78415c313ade9ca..3e5a21b0ee79f0b86042e48970d12e3f618a7b69 100644 (file)
@@ -5672,8 +5672,8 @@ fn do_test_sweep_outbound_htlc_failure_update(revoked: bool, local: bool) {
 
        let bs_dust_limit = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().our_dust_limit_satoshis;
 
-       let (payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
-       let (payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
+       let (_payment_preimage_1, dust_hash) = route_payment(&nodes[0], &[&nodes[1]], bs_dust_limit*1000);
+       let (_payment_preimage_2, non_dust_hash) = route_payment(&nodes[0], &[&nodes[1]], 1000000);
 
        let as_commitment_tx = nodes[0].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
        let bs_commitment_tx = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().last_local_commitment_txn.clone();
index 6c910865388d965e69f7c92ba6f91d10d5e9d04a..a61ec562884d143e358ffbbaad842deeb137c137 100644 (file)
@@ -94,6 +94,7 @@ impl LocalFeatures {
        pub(crate) fn supports_upfront_shutdown_script(&self) -> bool {
                self.flags.len() > 0 && (self.flags[0] & (3 << 4)) != 0
        }
+       #[cfg(test)]
        pub(crate) fn unset_upfront_shutdown_script(&mut self) {
                self.flags[0] ^= 1 << 4;
        }
@@ -713,7 +714,6 @@ mod fuzzy_internal_msgs {
                pub(crate) data: OnionRealm0HopData,
                pub(crate) hmac: [u8; 32],
        }
-       unsafe impl ::util::internal_traits::NoDealloc for OnionHopData{}
 
        pub struct DecodedOnionErrorPacket {
                pub(crate) hmac: [u8; 32],
index 8783aa0bde0e107708122c62fb06f2dd9102bbf4..11b279073447e4118c06a96eb8912431ed47ee18 100644 (file)
@@ -1,7 +1,7 @@
 use ln::channelmanager::{PaymentHash, HTLCSource};
 use ln::msgs;
 use ln::router::{Route,RouteHop};
-use util::{byte_utils, internal_traits};
+use util::byte_utils;
 use util::chacha20::ChaCha20;
 use util::errors::{self, APIError};
 use util::ser::{Readable, Writeable};
@@ -17,7 +17,6 @@ use secp256k1::Secp256k1;
 use secp256k1::ecdh::SharedSecret;
 use secp256k1;
 
-use std::ptr;
 use std::io::Cursor;
 use std::sync::Arc;
 
@@ -114,8 +113,6 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
        let mut cur_cltv = starting_htlc_offset;
        let mut last_short_channel_id = 0;
        let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(route.hops.len());
-       internal_traits::test_no_dealloc::<msgs::OnionHopData>(None);
-       unsafe { res.set_len(route.hops.len()); }
 
        for (idx, hop) in route.hops.iter().enumerate().rev() {
                // First hop gets special values so that it can check, on receipt, that everything is
@@ -123,7 +120,7 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
                // the intended recipient).
                let value_msat = if cur_value_msat == 0 { hop.fee_msat } else { cur_value_msat };
                let cltv = if cur_cltv == starting_htlc_offset { hop.cltv_expiry_delta + starting_htlc_offset } else { cur_cltv };
-               res[idx] = msgs::OnionHopData {
+               res.insert(0, msgs::OnionHopData {
                        realm: 0,
                        data: msgs::OnionRealm0HopData {
                                short_channel_id: last_short_channel_id,
@@ -131,7 +128,7 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
                                outgoing_cltv_value: cltv,
                        },
                        hmac: [0; 32],
-               };
+               });
                cur_value_msat += hop.fee_msat;
                if cur_value_msat >= 21000000 * 100000000 * 1000 {
                        return Err(APIError::RouteError{err: "Channel fees overflowed?!"});
@@ -147,8 +144,8 @@ pub(super) fn build_onion_payloads(route: &Route, starting_htlc_offset: u32) ->
 
 #[inline]
 fn shift_arr_right(arr: &mut [u8; 20*65]) {
-       unsafe {
-               ptr::copy(arr[0..].as_ptr(), arr[65..].as_mut_ptr(), 19*65);
+       for i in (65..20*65).rev() {
+               arr[i] = arr[i-65];
        }
        for i in 0..65 {
                arr[i] = 0;
diff --git a/src/util/internal_traits.rs b/src/util/internal_traits.rs
deleted file mode 100644 (file)
index c12276c..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-/// A simple marker trait that indicates a type requires no deallocation. Implies we can set_len()
-/// on a Vec of these things and will be safe to overwrite them with =.
-pub unsafe trait NoDealloc {}
-
-/// Just call with test_no_dealloc::<Type>(None)
-#[inline]
-pub fn test_no_dealloc<T : NoDealloc>(_: Option<T>) { }
index 1a48507df58984414b9800ca94125cf702481ad5..aab77035d387d452d469067329e6db9ad77a69e2 100644 (file)
@@ -9,7 +9,6 @@ pub(crate) mod chacha20;
 #[cfg(not(feature = "fuzztarget"))]
 pub(crate) mod poly1305;
 pub(crate) mod chacha20poly1305rfc;
-pub(crate) mod internal_traits;
 pub(crate) mod transaction_utils;
 
 #[macro_use]