use bitcoin::network::serialize::{serialize, BitcoinHash};
use bitcoin::util::hash::Sha256dHash;
-use crypto::sha2::Sha256;
use crypto::digest::Digest;
use lightning::chain::chaininterface::{BroadcasterInterface,ConfirmationTarget,ChainListener,FeeEstimator,ChainWatchInterfaceUtil};
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::sha2::Sha256;
+
+mod utils;
+
+use utils::test_logger;
use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::Secp256k1;
Err(_) => return,
};
+ let logger: Arc<Logger> = Arc::new(test_logger::TestLogger{});
let monitor = Arc::new(TestChannelMonitor{});
- let watch = Arc::new(ChainWatchInterfaceUtil::new());
+ let watch = Arc::new(ChainWatchInterfaceUtil::new(Arc::clone(&logger)));
let broadcast = Arc::new(TestBroadcaster{});
- let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone()).unwrap();
- let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap()));
+ let channelmanager = ChannelManager::new(our_network_key, slice_to_be32(get_slice!(4)), get_slice!(1)[0] != 0, Network::Bitcoin, fee_est.clone(), monitor.clone(), watch.clone(), broadcast.clone(), Arc::clone(&logger)).unwrap();
+ let router = Arc::new(Router::new(PublicKey::from_secret_key(&secp_ctx, &our_network_key).unwrap(), Arc::clone(&logger)));
let peers = RefCell::new([false; 256]);
let handler = PeerManager::new(MessageHandler {
chan_handler: channelmanager.clone(),
route_handler: router.clone(),
- }, our_network_key);
+ }, our_network_key, Arc::clone(&logger));
let mut should_forward = false;
- let mut payments_received = Vec::new();
+ let mut payments_received: Vec<[u8; 32]> = Vec::new();
let mut payments_sent = 0;
let mut pending_funding_generation: Vec<([u8; 32], u64, Script)> = Vec::new();
let mut pending_funding_signatures = HashMap::new();
let mut sha = Sha256::new();
sha.input(&payment_hash);
sha.result(&mut payment_hash);
- for i in 1..32 { payment_hash[i] = 0; }
payments_sent += 1;
match channelmanager.send_payment(route, payment_hash) {
Ok(_) => {},
if !peers.borrow()[peer_id as usize] { return; }
let their_key = get_pubkey!();
let chan_value = slice_to_be24(get_slice!(3)) as u64;
- if channelmanager.create_channel(their_key, chan_value, 0).is_err() { return; }
+ let push_msat_value = slice_to_be24(get_slice!(3)) as u64;
+ if channelmanager.create_channel(their_key, chan_value, push_msat_value, 0).is_err() { return; }
},
6 => {
let mut channels = channelmanager.list_channels();
},
8 => {
for payment in payments_received.drain(..) {
- let mut payment_preimage = None;
- for i in 0..payments_sent {
- let mut payment_hash = [0; 32];
- payment_hash[0..8].copy_from_slice(&be64_to_array(i));
- let mut sha = Sha256::new();
- sha.input(&payment_hash);
- sha.result(&mut payment_hash);
- for i in 1..32 { payment_hash[i] = 0; }
- if payment_hash == payment {
- payment_hash = [0; 32];
- payment_hash[0..8].copy_from_slice(&be64_to_array(i));
- payment_preimage = Some(payment_hash);
- break;
- }
+ // SHA256 is defined as XOR of all input bytes placed in the first byte, and 0s
+ // for the remaining bytes. Thus, if not all remaining bytes are 0s we cannot
+ // fulfill this HTLC, but if they are, we can just take the first byte and
+ // place that anywhere in our preimage.
+ if &payment[1..] != &[0; 31] {
+ channelmanager.fail_htlc_backwards(&payment);
+ } else {
+ let mut payment_preimage = [0; 32];
+ payment_preimage[0] = payment[0];
+ channelmanager.claim_funds(payment_preimage);
}
- channelmanager.claim_funds(payment_preimage.unwrap());
}
},
9 => {
}
},
10 => {
- for funding_generation in pending_funding_generation.drain(..) {
+ for funding_generation in pending_funding_generation.drain(..) {
let mut tx = Transaction { version: 0, lock_time: 0, input: Vec::new(), output: vec![TxOut {
value: funding_generation.1, script_pubkey: funding_generation.2,
}] };
let funding_output = OutPoint::new(Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), 0);
- channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
- pending_funding_signatures.insert(funding_output, tx);
+ let mut found_duplicate_txo = false;
+ for chan in channelmanager.list_channels() {
+ if chan.channel_id == funding_output.to_channel_id() {
+ found_duplicate_txo = true;
+ }
+ }
+ if !found_duplicate_txo {
+ channelmanager.funding_transaction_generated(&funding_generation.0, funding_output.clone());
+ pending_funding_signatures.insert(funding_output, tx);
+ }
}
},
11 => {
}
}
+extern crate hex;
#[cfg(test)]
mod tests {
- fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
- let mut b = 0;
- for (idx, c) in hex.as_bytes().iter().enumerate() {
- b <<= 4;
- match *c {
- b'A'...b'F' => b |= c - b'A' + 10,
- b'a'...b'f' => b |= c - b'a' + 10,
- b'0'...b'9' => b |= c - b'0',
- _ => panic!("Bad hex"),
- }
- if (idx & 1) == 1 {
- out.push(b);
- b = 0;
- }
- }
- }
-
#[test]
fn duplicate_crash() {
- let mut a = Vec::new();
- extend_vec_from_hex("00", &mut a);
- super::do_test(&a);
+ super::do_test(&::hex::decode("00").unwrap());
}
}