+ 5 => {
+ let peer_id = get_slice!(1)[0];
+ 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; }
+ },
+ 6 => {
+ let mut channels = channelmanager.list_channels();
+ let channel_id = get_slice!(1)[0] as usize;
+ if channel_id >= channels.len() { return; }
+ channels.sort_by(|a, b| { a.channel_id.cmp(&b.channel_id) });
+ if channelmanager.close_channel(&channels[channel_id].channel_id).is_err() { return; }
+ },
+ 7 => {
+ if should_forward {
+ channelmanager.process_pending_htlc_forward();
+ handler.process_events();
+ should_forward = false;
+ }
+ },
+ 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;
+ }
+ }
+ channelmanager.claim_funds(payment_preimage.unwrap());
+ }
+ },
+ 9 => {
+ for payment in payments_received.drain(..) {
+ channelmanager.fail_htlc_backwards(&payment);
+ }
+ },
+ 10 => {
+ 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);
+ }
+ },
+ 11 => {
+ if !pending_funding_relay.is_empty() {
+ let mut txn = Vec::with_capacity(pending_funding_relay.len());
+ let mut txn_idxs = Vec::with_capacity(pending_funding_relay.len());
+ for (idx, tx) in pending_funding_relay.iter().enumerate() {
+ txn.push(tx);
+ txn_idxs.push(idx as u32 + 1);
+ }
+
+ let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+ channelmanager.block_connected(&header, 1, &txn[..], &txn_idxs[..]);
+ txn.clear();
+ txn_idxs.clear();
+ for i in 2..100 {
+ header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+ channelmanager.block_connected(&header, i, &txn[..], &txn_idxs[..]);
+ }
+ }
+ pending_funding_relay.clear();
+ },