lightning 0.0.1

A Bitcoin Lightning implementation in Rust. Still super-early code-dump quality and is missing large chunks. See README in git repo for suggested projects if you want to contribute. Don't have to bother telling you not to use this for anything serious, because you'd have to finish building it to even try.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
use bitcoin::blockdata::block::BlockHeader;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::constants::genesis_block;
use bitcoin::network::constants::Network;
use bitcoin::network::serialize::BitcoinHash;
use bitcoin::util::hash::Sha256dHash;
use bitcoin::util::uint::Uint256;

use secp256k1::key::{SecretKey,PublicKey};
use secp256k1::{Secp256k1,Message};
use secp256k1::ecdh::SharedSecret;
use secp256k1;

use chain::chaininterface::{ChainListener,ChainWatchInterface,FeeEstimator};
use ln::channel::Channel;
use ln::channelmonitor::ManyChannelMonitor;
use ln::router::Route;
use ln::msgs;
use ln::msgs::{HandleError,ChannelMessageHandler,MsgEncodable,MsgDecodable};
use util::byte_utils;
use util::events;
use util::internal_traits;

use rand::{thread_rng,Rng};

use crypto::mac::{Mac,MacResult};
use crypto::hmac::Hmac;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use crypto::symmetriccipher::SynchronousStreamCipher;
use crypto::chacha20::ChaCha20;

use std::sync::{Mutex,Arc};
use std::collections::HashMap;
use std::collections::hash_map;
use std::ptr;
use std::mem;
use std::time::{Instant,Duration};

/// Stores the info we will need to send when we want to forward an HTLC onwards
pub struct PendingForwardHTLCInfo {
	onion_packet: Option<msgs::OnionPacket>,
	payment_hash: [u8; 32],
	short_channel_id: u64,
	prev_short_channel_id: u64,
	amt_to_forward: u64,
	outgoing_cltv_value: u32,
}

struct ChannelHolder {
	by_id: HashMap<Uint256, Channel>,
	short_to_id: HashMap<u64, Uint256>,
}

/// 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;

struct PendingForwardableHTLCs {
	next_forward: Instant,
	/// short channel id -> forward infos. Key of 0 means payments received
	forward_htlcs: HashMap<u64, Vec<PendingForwardHTLCInfo>>,
}

/// Manager which keeps track of a number of channels and sends messages to the appropriate
/// channel, also tracking HTLC preimages and forwarding onion packets appropriately.
/// Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
/// to individual Channels.
pub struct ChannelManager {
	genesis_hash: Sha256dHash,
	fee_estimator: Arc<FeeEstimator>,
	monitor: Arc<ManyChannelMonitor>,
	chain_monitor: Arc<ChainWatchInterface>,

	announce_channels_publicly: bool,
	fee_proportional_millionths: u32,
	secp_ctx: Secp256k1,

	channels: Mutex<ChannelHolder>,
	our_network_key: SecretKey,

	pending_events: Mutex<Vec<events::Event>>,
	pending_htlcs_forwardable: Mutex<PendingForwardableHTLCs>,
	pending_claimable_htlcs: Mutex<HashMap<[u8; 32], u64>>,
}

const CLTV_EXPIRY_DELTA: u16 = 6 * 24 * 2; //TODO?

macro_rules! secp_call {
	( $res : expr ) => {
		match $res {
			Ok(key) => key,
			//TODO: Make the err a parameter!
			Err(_) => return Err(HandleError{err: "Key error", msg: None})
		}
	};
}

struct OnionKeys {
	#[cfg(test)]
	shared_secret: SharedSecret,
	#[cfg(test)]
	blinding_factor: [u8; 32],
	ephemeral_pubkey: PublicKey,
	rho: [u8; 32],
	mu: [u8; 32],
}

impl ChannelManager {
	/// Constructs a new ChannelManager to hold several channels and route between them. This is
	/// the main "logic hub" for all channel-related actions, and implements ChannelMessageHandler.
	/// fee_proportional_millionths is an optional fee to charge any payments routed through us.
	/// Non-proportional fees are fixed according to our risk using the provided fee estimator.
	/// panics if channel_value_satoshis is >= (1 << 24)!
	pub fn new(our_network_key: SecretKey, fee_proportional_millionths: u32, announce_channels_publicly: bool, network: Network, feeest: Arc<FeeEstimator>, monitor: Arc<ManyChannelMonitor>, chain_monitor: Arc<ChainWatchInterface>) -> Result<Arc<ChannelManager>, secp256k1::Error> {
		let secp_ctx = Secp256k1::new();

		let res = Arc::new(ChannelManager {
			genesis_hash: genesis_block(network).header.bitcoin_hash(),
			fee_estimator: feeest.clone(),
			monitor: monitor.clone(),
			chain_monitor: chain_monitor,

			announce_channels_publicly: announce_channels_publicly,
			fee_proportional_millionths: fee_proportional_millionths,
			secp_ctx: secp_ctx,

			channels: Mutex::new(ChannelHolder{by_id: HashMap::new(), short_to_id: HashMap::new()}),
			our_network_key: our_network_key,

			pending_events: Mutex::new(Vec::new()),
			pending_htlcs_forwardable: Mutex::new(PendingForwardableHTLCs {
				next_forward: Instant::now(),
				forward_htlcs: HashMap::new(),
			}),
			pending_claimable_htlcs: Mutex::new(HashMap::new()),
		});
		let weak_res = Arc::downgrade(&res);
		res.chain_monitor.register_listener(weak_res);
		Ok(res)
	}

	pub fn create_channel(&self, their_network_key: PublicKey, channel_value_satoshis: u64, user_id: u64) -> Result<msgs::OpenChannel, HandleError> {
		let channel = Channel::new_outbound(&*self.fee_estimator, their_network_key, channel_value_satoshis, self.announce_channels_publicly, user_id);
		let res = try!(channel.get_open_channel(self.genesis_hash.clone(), &*self.fee_estimator));
		let mut channels = self.channels.lock().unwrap();
		match channels.by_id.insert(channel.channel_id(), channel) {
			Some(_) => panic!("RNG is bad???"),
			None => Ok(res)
		}
	}

	#[inline]
	fn gen_rho_mu_from_shared_secret(shared_secret: &SharedSecret) -> ([u8; 32], [u8; 32]) {
		({
			let mut hmac = Hmac::new(Sha256::new(), &[0x72, 0x68, 0x6f]); // rho
			hmac.input(&shared_secret[..]);
			let mut res = [0; 32];
			hmac.raw_result(&mut res);
			res
		},
		{
			let mut hmac = Hmac::new(Sha256::new(), &[0x6d, 0x75]); // mu
			hmac.input(&shared_secret[..]);
			let mut res = [0; 32];
			hmac.raw_result(&mut res);
			res
		})
	}

	#[inline]
	fn gen_um_from_shared_secret(shared_secret: &SharedSecret) -> [u8; 32] {
		let mut hmac = Hmac::new(Sha256::new(), &[0x75, 0x6d]); // um
		hmac.input(&shared_secret[..]);
		let mut res = [0; 32];
		hmac.raw_result(&mut res);
		res
	}

	fn gen_ammag_from_shared_secret(shared_secret: &SharedSecret) -> [u8; 32] {
		let mut hmac = Hmac::new(Sha256::new(), &[0x61, 0x6d, 0x6d, 0x61, 0x67]); // ammag
		hmac.input(&shared_secret[..]);
		let mut res = [0; 32];
		hmac.raw_result(&mut res);
		res
	}

	fn construct_onion_keys(secp_ctx: &Secp256k1, route: &Route, session_priv: &SecretKey) -> Result<Vec<OnionKeys>, HandleError> {
		let mut res = Vec::with_capacity(route.hops.len());
		let mut blinded_priv = session_priv.clone();
		let mut blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));
		let mut first_iteration = true;

		for hop in route.hops.iter() {
			let shared_secret = SharedSecret::new(secp_ctx, &hop.pubkey, &blinded_priv);

			let mut sha = Sha256::new();
			sha.input(&blinded_pub.serialize()[..]);
			sha.input(&shared_secret[..]);
			let mut blinding_factor = [0u8; 32];
			sha.result(&mut blinding_factor);

			if first_iteration {
				blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));
				first_iteration = false;
			}
			let ephemeral_pubkey = blinded_pub;

			secp_call!(blinded_priv.mul_assign(secp_ctx, &secp_call!(SecretKey::from_slice(secp_ctx, &blinding_factor))));
			blinded_pub = secp_call!(PublicKey::from_secret_key(secp_ctx, &blinded_priv));

			let (rho, mu) = ChannelManager::gen_rho_mu_from_shared_secret(&shared_secret);

			res.push(OnionKeys {
				#[cfg(test)]
				shared_secret: shared_secret,
				#[cfg(test)]
				blinding_factor: blinding_factor,
				ephemeral_pubkey: ephemeral_pubkey,
				rho: rho,
				mu: mu,
			});
		}

		Ok(res)
	}

	/// returns the hop data, as well as the first-hop value_msat and CLTV value we should send.
	fn build_onion_payloads(route: &Route) -> Result<(Vec<msgs::OnionHopData>, u64, u32), HandleError> {
		let mut cur_value_msat = 0u64;
		let mut cur_cltv = 0u32;
		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
			// exactly as it should be (and the next hop isn't trying to probe to find out if we're
			// the intended recipient).
			let value_msat = if cur_value_msat == 0 { hop.fee_msat } else { cur_value_msat };
			let cltv = if cur_cltv == 0 { hop.cltv_expiry_delta } else { cur_cltv };
			res[idx] = msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: last_short_channel_id,
					amt_to_forward: value_msat,
					outgoing_cltv_value: cltv,
				},
				hmac: [0; 32],
			};
			cur_value_msat += hop.fee_msat;
			if cur_value_msat >= 21000000 * 100000000 * 1000 {
				return Err(HandleError{err: "Channel fees overflowed?!", msg: None});
			}
			cur_cltv += hop.cltv_expiry_delta as u32;
			if cur_cltv >= 500000000 {
				return Err(HandleError{err: "Channel CLTV overflowed?!", msg: None});
			}
			last_short_channel_id = hop.short_channel_id;
		}
		Ok((res, cur_value_msat, cur_cltv))
	}

	#[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 0..65 {
			arr[i] = 0;
		}
	}

	#[inline]
	fn xor_bufs(dst: &mut[u8], src: &[u8]) {
		assert_eq!(dst.len(), src.len());

		for i in 0..dst.len() {
			dst[i] ^= src[i];
		}
	}

	const ZERO:[u8; 21*65] = [0; 21*65];
	fn construct_onion_packet(mut payloads: Vec<msgs::OnionHopData>, onion_keys: Vec<OnionKeys>, associated_data: Vec<u8>) -> Result<msgs::OnionPacket, HandleError> {
		let mut buf = Vec::with_capacity(21*65);
		buf.resize(21*65, 0);

		let filler = {
			let iters = payloads.len() - 1;
			let end_len = iters * 65;
			let mut res = Vec::with_capacity(end_len);
			res.resize(end_len, 0);

			for (i, keys) in onion_keys.iter().enumerate() {
				if i == payloads.len() - 1 { continue; }
				let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
				chacha.process(&ChannelManager::ZERO, &mut buf); // We dont have a seek function :(
				ChannelManager::xor_bufs(&mut res[0..(i + 1)*65], &buf[(20 - i)*65..21*65]);
			}
			res
		};

		let mut packet_data = [0; 20*65];
		let mut hmac_res = [0; 32];

		for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() {
			ChannelManager::shift_arr_right(&mut packet_data);
			payload.hmac = hmac_res;
			packet_data[0..65].copy_from_slice(&payload.encode()[..]);

			let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
			chacha.process(&packet_data, &mut buf[0..20*65]);
			packet_data[..].copy_from_slice(&buf[0..20*65]);

			if i == 0 {
				packet_data[20*65 - filler.len()..20*65].copy_from_slice(&filler[..]);
			}

			let mut hmac = Hmac::new(Sha256::new(), &keys.mu);
			hmac.input(&packet_data);
			hmac.input(&associated_data[..]);
			hmac.raw_result(&mut hmac_res);
		}

		Ok(msgs::OnionPacket{
			version: 0,
			public_key: onion_keys.first().unwrap().ephemeral_pubkey,
			hop_data: packet_data,
			hmac: hmac_res,
		})
	}

	/// Encrypts a failure packet. raw_packet can either be a
	/// msgs::DecodedOnionErrorPacket.encode() result or a msgs::OnionErrorPacket.data element.
	fn encrypt_failure_packet(shared_secret: &SharedSecret, raw_packet: &[u8]) -> msgs::OnionErrorPacket {
		let ammag = ChannelManager::gen_ammag_from_shared_secret(&shared_secret);

		let mut packet_crypted = Vec::with_capacity(raw_packet.len());
		packet_crypted.resize(raw_packet.len(), 0);
		let mut chacha = ChaCha20::new(&ammag, &[0u8; 8]);
		chacha.process(&raw_packet, &mut packet_crypted[..]);
		msgs::OnionErrorPacket {
			data: packet_crypted,
		}
	}

	fn build_failure_packet(shared_secret: &SharedSecret, failure_type: u16, failure_data: &[u8]) -> msgs::DecodedOnionErrorPacket {
		assert!(failure_data.len() <= 256 - 2);

		let um = ChannelManager::gen_um_from_shared_secret(&shared_secret);

		let failuremsg = {
			let mut res = Vec::with_capacity(2 + failure_data.len());
			res.push(((failure_type >> 8) & 0xff) as u8);
			res.push(((failure_type >> 0) & 0xff) as u8);
			res.extend_from_slice(&failure_data[..]);
			res
		};
		let pad = {
			let mut res = Vec::with_capacity(256 - 2 - failure_data.len());
			res.resize(256 - 2 - failure_data.len(), 0);
			res
		};
		let mut packet = msgs::DecodedOnionErrorPacket {
			hmac: [0; 32],
			failuremsg: failuremsg,
			pad: pad,
		};

		let mut hmac = Hmac::new(Sha256::new(), &um);
		hmac.input(&packet.encode()[32..]);
		hmac.raw_result(&mut packet.hmac);

		packet
	}

	fn build_first_hop_failure_packet(shared_secret: &SharedSecret, failure_type: u16, failure_data: &[u8]) -> msgs::OnionErrorPacket {
		let failure_packet = ChannelManager::build_failure_packet(shared_secret, failure_type, failure_data);
		ChannelManager::encrypt_failure_packet(shared_secret, &failure_packet.encode()[..])
	}

	/// only fails if the channel does not yet have an assigned short_id
	fn get_channel_update(&self, chan: &mut Channel) -> Result<msgs::ChannelUpdate, HandleError> {
		let short_channel_id = match chan.get_short_channel_id() {
			None => return Err(HandleError{err: "Channel not yet established", msg: None}),
			Some(id) => id,
		};

		let were_node_one = PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).unwrap().serialize()[..] < chan.get_their_node_id().serialize()[..];

		let unsigned = msgs::UnsignedChannelUpdate {
			chain_hash: self.genesis_hash,
			short_channel_id: short_channel_id,
			timestamp: chan.get_channel_update_count(),
			flags: (!were_node_one) as u16 | ((!chan.is_live() as u16) << 1),
			cltv_expiry_delta: CLTV_EXPIRY_DELTA,
			htlc_minimum_msat: chan.get_our_htlc_minimum_msat(),
			fee_base_msat: chan.get_our_fee_base_msat(&*self.fee_estimator),
			fee_proportional_millionths: self.fee_proportional_millionths,
		};

		let msg_hash = Sha256dHash::from_data(&unsigned.encode()[..]);
		let sig = self.secp_ctx.sign(&Message::from_slice(&msg_hash[..]).unwrap(), &self.our_network_key).unwrap(); //TODO Can we unwrap here?

		Ok(msgs::ChannelUpdate {
			signature: sig,
			contents: unsigned
		})
	}

	/// Sends a payment along a given route, returning the UpdateAddHTLC message to give to the
	/// first hop in route. Value parameters are provided via the last hop in route, see
	/// documentation for RouteHop fields for more info.
	/// See-also docs on Channel::send_htlc_and_commit.
	pub fn send_payment(&self, route: &Route, payment_hash: [u8; 32]) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned)>, HandleError> {
		if route.hops.len() < 1 || route.hops.len() > 20 {
			return Err(HandleError{err: "Route didn't go anywhere/had bogus size", msg: None});
		}

		let mut rng = thread_rng();
		let session_priv = secp_call!(SecretKey::from_slice(&self.secp_ctx, & {
			let mut session_key = [0; 32];
			rng.fill_bytes(&mut session_key);
			session_key
		}));

		let associated_data = Vec::new(); //TODO: What to put here?

		let onion_keys = try!(ChannelManager::construct_onion_keys(&self.secp_ctx, route, &session_priv));
		let (onion_payloads, htlc_msat, htlc_cltv) = try!(ChannelManager::build_onion_payloads(route));
		let onion_packet = try!(ChannelManager::construct_onion_packet(onion_payloads, onion_keys, associated_data));

		let mut channels = self.channels.lock().unwrap();
		let id = match channels.short_to_id.get(&route.hops.first().unwrap().short_channel_id) {
			None => return Err(HandleError{err: "No channel available with first hop!", msg: None}),
			Some(id) => id.clone()
		};
		let chan = channels.by_id.get_mut(&id).unwrap();
		if chan.get_their_node_id() != route.hops.first().unwrap().pubkey {
			return Err(HandleError{err: "Node ID mismatch on first hop!", msg: None});
		}
		chan.send_htlc_and_commit(htlc_msat, payment_hash, htlc_cltv, onion_packet)
	}

	/// Call this upon creation of a funding transaction for the given channel.
	/// Panics if a funding transaction has already been provided for this channel.
	pub fn funding_transaction_generated(&self, temporary_channel_id: &Uint256, funding_txo: (Sha256dHash, u16)) {
		let (chan, msg) = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.remove(&temporary_channel_id) {
				Some(mut chan) => {
					match chan.get_outbound_funding_created(funding_txo.0, funding_txo.1) {
						Ok(funding_msg) => {
							(chan, funding_msg)
						},
						Err(_e) => {
							//TODO: Push e to pendingevents
							return;
						}
					}
				},
				None => return
			}
		}; // Release channel lock for install_watch_outpoint call,
		let chan_monitor = chan.channel_monitor();
		match self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
			Ok(()) => {},
			Err(_e) => {
				//TODO: Push e to pendingevents?
				return;
			}
		};

		{
			let mut pending_events = self.pending_events.lock().unwrap();
			pending_events.push(events::Event::SendFundingCreated {
				node_id: chan.get_their_node_id(),
				msg: msg,
			});
		}

		let mut channels = self.channels.lock().unwrap();
		channels.by_id.insert(chan.channel_id(), chan);
	}

	fn get_announcement_sigs(&self, chan: &Channel) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
		if !chan.is_usable() { return Ok(None) }

		let (announcement, our_bitcoin_sig) = try!(chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone()));
		let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
		let our_node_sig = secp_call!(self.secp_ctx.sign(&msghash, &self.our_network_key));

		Ok(Some(msgs::AnnouncementSignatures {
			channel_id: chan.channel_id(),
			short_channel_id: chan.get_short_channel_id().unwrap(),
			node_signature: our_node_sig,
			bitcoin_signature: our_bitcoin_sig,
		}))
	}

	pub fn process_pending_htlc_forward(&self) {
		let forward_htlcs = {
			let mut pending_forwards = self.pending_htlcs_forwardable.lock().unwrap();
			if Instant::now() < pending_forwards.next_forward {
				return;
			}
			let mut new_map = HashMap::new();
			mem::swap(&mut new_map, &mut pending_forwards.forward_htlcs);
			new_map
		};

		let mut new_events = Vec::new();

		{
			for (short_chan_id, pending_forwards) in forward_htlcs.iter() {
				let mut pending_claimable_htlcs = self.pending_claimable_htlcs.lock().unwrap();
				for forward_info in pending_forwards {
					pending_claimable_htlcs.insert(forward_info.payment_hash, forward_info.prev_short_channel_id);
					if *short_chan_id == 0 {
						new_events.push(events::Event::PaymentReceived {
							payment_hash: forward_info.payment_hash,
							amt: forward_info.amt_to_forward,
						});
					}
				}
			}
		}
		{
			let mut channels = self.channels.lock().unwrap();
			for (short_chan_id, pending_forwards) in forward_htlcs {
				if short_chan_id != 0 {
					let forward_chan_id = match channels.short_to_id.get(&short_chan_id) {
						Some(chan_id) => chan_id.clone(),
						None => {
							// TODO: Send a failure packet back on each pending_forward
							continue;
						}
					};
					let forward_chan = &mut channels.by_id.get_mut(&forward_chan_id).unwrap();

					let mut add_htlc_msgs = Vec::new();
					for forward_info in pending_forwards {
						match forward_chan.send_htlc(forward_info.amt_to_forward, forward_info.payment_hash, forward_info.outgoing_cltv_value, forward_info.onion_packet.unwrap()) {
							Err(_e) => {
								// TODO: Send a failure packet back
								continue;
							},
							Ok(update_add) => {
								match update_add {
									Some(msg) => { add_htlc_msgs.push(msg); },
									None => {
										// Nothing to do here...we're waiting on a remote
										// revoke_and_ack before we can add anymore HTLCs. The Channel
										// will automatically handle building the update_add_htlc and
										// commitment_signed messages when we can.
										// TODO: Do some kind of timer to set the channel as !is_live()
										// as we dont really want others relying on us relaying through
										// this channel currently :/.
									}
								}
							}
						}
					}

					if !add_htlc_msgs.is_empty() {
						let commitment_msg = match forward_chan.send_commitment() {
							Ok(msg) => msg,
							Err(_) => {
								//TODO: Handle...this is bad!
								continue;
							},
						};
						new_events.push(events::Event::SendHTLCs {
							node_id: forward_chan.get_their_node_id(),
							msgs: add_htlc_msgs,
							commitment_msg: commitment_msg,
						});
					}
				}
			}
		}

		if new_events.is_empty() { return }

		let mut events = self.pending_events.lock().unwrap();
		events.reserve(new_events.len());
		for event in new_events.drain(..) {
			events.push(event);
		}
	}

	/// Provides a payment preimage in response to a PaymentReceived event, returning true and
	/// generating message events for the net layer to claim the payment, if possible. Thus, you
	/// should probably kick the net layer to go send messages if this returns true!
	pub fn claim_funds(&self, payment_preimage: [u8; 32]) -> bool {
		let mut sha = Sha256::new();
		sha.input(&payment_preimage);
		let mut payment_hash = [0; 32];
		sha.result(&mut payment_hash);

		let short_chan_id = {
			let mut pending_claimable_htlcs = self.pending_claimable_htlcs.lock().unwrap();
			match pending_claimable_htlcs.remove(&payment_hash) {
				Some(short_id) => short_id,
				None => return false,
			}
		};

		let (node_id, fulfill_msg) = {
			let mut channels = self.channels.lock().unwrap();
			let chan_id = match channels.short_to_id.get(&short_chan_id) {
				Some(chan_id) => chan_id.clone(),
				None => return false
			};

			let chan = channels.by_id.get_mut(&chan_id).unwrap();
			match chan.get_update_fulfill_htlc(payment_preimage) {
				Ok(msg) => (chan.get_their_node_id(), msg),
				Err(_e) => {
					//TODO: Do something with e?
					return false;
				},
			}
		};

		let mut pending_events = self.pending_events.lock().unwrap();
		pending_events.push(events::Event::SendFulfillHTLC {
			node_id: node_id,
			msg: fulfill_msg
		});

		true
	}

	/// Gets the node_id held by this ChannelManager
	pub fn get_our_node_id(&self) -> PublicKey {
		PublicKey::from_secret_key(&self.secp_ctx, &self.our_network_key).unwrap()
	}
}

impl events::EventsProvider for ChannelManager {
	fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
		let mut pending_events = self.pending_events.lock().unwrap();
		let mut ret = Vec::new();
		mem::swap(&mut ret, &mut *pending_events);
		ret
	}
}

impl ChainListener for ChannelManager {
	fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
		let mut new_funding_locked_messages = Vec::new();
		{
			let mut channels = self.channels.lock().unwrap();
			let mut short_to_ids_to_insert = Vec::new();
			for channel in channels.by_id.values_mut() {
				match channel.block_connected(header, height, txn_matched, indexes_of_txn_matched) {
					Some(funding_locked) => {
						let announcement_sigs = match self.get_announcement_sigs(channel) {
							Ok(res) => res,
							Err(_e) => {
								//TODO: push e on events and blow up the channel (it has bad keys)
								continue;
							}
						};
						new_funding_locked_messages.push(events::Event::SendFundingLocked {
							node_id: channel.get_their_node_id(),
							msg: funding_locked,
							announcement_sigs: announcement_sigs
						});
						short_to_ids_to_insert.push((channel.get_short_channel_id().unwrap(), channel.channel_id()));
					},
					None => {}
				}
			}
			for to_insert in short_to_ids_to_insert {
				channels.short_to_id.insert(to_insert.0, to_insert.1);
			}
		}
		let mut pending_events = self.pending_events.lock().unwrap();
		for funding_locked in new_funding_locked_messages.drain(..) {
			pending_events.push(funding_locked);
		}
	}

	fn block_disconnected(&self, header: &BlockHeader) {
		let mut channels = self.channels.lock().unwrap();
		for channel in channels.by_id.values_mut() {
			if channel.block_disconnected(header) {
				//TODO Close channel here
			}
		}
	}
}

impl ChannelMessageHandler for ChannelManager {
	//TODO: Handle errors and close channel (or so)
	fn handle_open_channel(&self, their_node_id: &PublicKey, msg: &msgs::OpenChannel) -> Result<msgs::AcceptChannel, HandleError> {
		if msg.chain_hash != self.genesis_hash {
			return Err(HandleError{err: "Unknown genesis block hash", msg: None});
		}
		let mut channels = self.channels.lock().unwrap();
		if channels.by_id.contains_key(&msg.temporary_channel_id) {
			return Err(HandleError{err: "temporary_channel_id collision!", msg: None});
		}
		let channel = try!(Channel::new_from_req(&*self.fee_estimator, their_node_id.clone(), msg, 0, self.announce_channels_publicly));
		let accept_msg = try!(channel.get_accept_channel());
		channels.by_id.insert(channel.channel_id(), channel);
		Ok(accept_msg)
	}

	fn handle_accept_channel(&self, their_node_id: &PublicKey, msg: &msgs::AcceptChannel) -> Result<(), HandleError> {
		let (value, output_script, user_id) = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.temporary_channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					try!(chan.accept_channel(&msg));
					(chan.get_value_satoshis(), chan.get_funding_redeemscript().to_v0_p2wsh(), chan.get_user_id())
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		let mut pending_events = self.pending_events.lock().unwrap();
		pending_events.push(events::Event::FundingGenerationReady {
			temporary_channel_id: msg.temporary_channel_id,
			channel_value_satoshis: value,
			output_script: output_script,
			user_channel_id: user_id,
		});
		Ok(())
	}

	fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
		//TODO: broke this - a node shouldnt be able to get their channel removed by sending a
		//funding_created a second time, or long after the first, or whatever (note this also
		//leaves the short_to_id map in a busted state.
		let chan = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.remove(&msg.temporary_channel_id) {
				Some(mut chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					match chan.funding_created(msg) {
						Ok(funding_msg) => {
							(chan, funding_msg)
						},
						Err(e) => {
							return Err(e);
						}
					}
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		}; // Release channel lock for install_watch_outpoint call,
		   // note that this means if the remote end is misbehaving and sends a message for the same
		   // channel back-to-back with funding_created, we'll end up thinking they sent a message
		   // for a bogus channel.
		let chan_monitor = chan.0.channel_monitor();
		try!(self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor));
		let mut channels = self.channels.lock().unwrap();
		channels.by_id.insert(chan.1.channel_id, chan.0);
		Ok(chan.1)
	}

	fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), HandleError> {
		let (funding_txo, user_id) = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					try!(chan.funding_signed(&msg));
					(chan.get_funding_txo().unwrap(), chan.get_user_id())
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		let mut pending_events = self.pending_events.lock().unwrap();
		pending_events.push(events::Event::FundingBroadcastSafe {
			funding_txo: funding_txo,
			user_channel_id: user_id,
		});
		Ok(())
	}

	fn handle_funding_locked(&self, their_node_id: &PublicKey, msg: &msgs::FundingLocked) -> Result<Option<msgs::AnnouncementSignatures>, HandleError> {
		let mut channels = self.channels.lock().unwrap();
		match channels.by_id.get_mut(&msg.channel_id) {
			Some(chan) => {
				if chan.get_their_node_id() != *their_node_id {
					return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
				}
				try!(chan.funding_locked(&msg));
				return Ok(try!(self.get_announcement_sigs(chan)));
			},
			None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
		};
	}

	fn handle_shutdown(&self, _their_node_id: &PublicKey, _msg: &msgs::Shutdown) -> Result<(), HandleError> {
		unimplemented!()
	}

	fn handle_closing_signed(&self, _their_node_id: &PublicKey, _msg: &msgs::ClosingSigned) -> Result<(), HandleError> {
		unimplemented!()
	}

	fn handle_update_add_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) -> Result<(), msgs::HandleError> {
		//TODO: BOLT 4 points out a specific attack where a peer may re-send a onion packet and
		//determine the state of the payment based on our response/if we forward anything/the time
		//we take to respond. We should take care to avoid allowing such an attack.
		//
		//TODO: There exists a further attack where a node may garble the onion data, forward it to
		//us repeatedly garbled in different ways, and compare our error messages, which are
		//encrypted with the same key. Its not immediately obvious how to usefully exploit that,
		//but we should prevent it anyway.

		let shared_secret = SharedSecret::new(&self.secp_ctx, &msg.onion_routing_packet.public_key, &self.our_network_key);
		let (rho, mu) = ChannelManager::gen_rho_mu_from_shared_secret(&shared_secret);

		let associated_data = Vec::new(); //TODO: What to put here?

		if msg.onion_routing_packet.version != 0 {
			//TODO: Spec doesnt indicate if we should only hash hop_data here (and in other
			//sha256_of_onion error data packets), or the entire onion_routing_packet. Either way,
			//the hash doesn't really serve any purpuse - in the case of hashing all data, the
			//receiving node would have to brute force to figure out which version was put in the
			//packet by the node that send us the message, in the case of hashing the hop_data, the
			//node knows the HMAC matched, so they already know what is there...
			let mut sha = Sha256::new();
			sha.input(&msg.onion_routing_packet.hop_data);
			let mut onion_hash = [0; 32];
			sha.result(&mut onion_hash);
			return Err(msgs::HandleError {
				err: "Unknown onion packet version",
				msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
					msg: msgs::UpdateFailHTLC {
						channel_id: msg.channel_id,
						htlc_id: msg.htlc_id,
						reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x8000 | 0x4000 | 4, &onion_hash),
					}
				}),
			});
		}

		let mut hmac = Hmac::new(Sha256::new(), &mu);
		hmac.input(&msg.onion_routing_packet.hop_data);
		hmac.input(&associated_data[..]);
		if hmac.result() != MacResult::new(&msg.onion_routing_packet.hmac) {
			let mut sha = Sha256::new();
			sha.input(&msg.onion_routing_packet.hop_data);
			let mut onion_hash = [0; 32];
			sha.result(&mut onion_hash);
			return Err(HandleError{err: "HMAC Check failed",
				msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
					msg: msgs::UpdateFailHTLC {
						channel_id: msg.channel_id,
						htlc_id: msg.htlc_id,
						reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x8000 | 0x4000 | 5, &onion_hash),
					}
				}),
			});
		}

		let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
		let next_hop_data = {
			let mut decoded = [0; 65];
			chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
			match msgs::OnionHopData::decode(&decoded[..]) {
				Err(err) => {
					let error_code = match err {
						msgs::DecodeError::UnknownRealmByte => 0x4000 | 1,
						_ => 0x2000 | 2, // Should never happen
					};
					return Err(HandleError{err: "Unable to decode our hop data",
						msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
							msg: msgs::UpdateFailHTLC {
								channel_id: msg.channel_id,
								htlc_id: msg.htlc_id,
								reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, error_code, &[0;0]),
							}
						}),
					});
				},
				Ok(msg) => msg
			}
		};

		let mut pending_forward_info = if next_hop_data.hmac == [0; 32] {
				// OUR PAYMENT!
				if next_hop_data.data.amt_to_forward != msg.amount_msat {
					return Err(HandleError{err: "Upstream node sent less than we were supposed to receive in payment",
						msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
							msg: msgs::UpdateFailHTLC {
								channel_id: msg.channel_id,
								htlc_id: msg.htlc_id,
								reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 19, &byte_utils::be64_to_array(msg.amount_msat)),
							}
						}),
					});
				}
				if next_hop_data.data.outgoing_cltv_value != msg.cltv_expiry {
					return Err(HandleError{err: "Upstream node set CLTV to the wrong value",
						msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
							msg: msgs::UpdateFailHTLC {
								channel_id: msg.channel_id,
								htlc_id: msg.htlc_id,
								reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 18, &byte_utils::be32_to_array(msg.cltv_expiry)),
							}
						}),
					});
				}

				// Note that we could obviously respond immediately with a update_fulfill_htlc
				// message, however that would leak that we are the recipient of this payment, so
				// instead we stay symmetric with the forwarding case, only responding (after a
				// delay) once they've send us a commitment_signed!

				PendingForwardHTLCInfo {
					onion_packet: None,
					payment_hash: msg.payment_hash.clone(),
					short_channel_id: 0,
					prev_short_channel_id: 0,
					amt_to_forward: next_hop_data.data.amt_to_forward,
					outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
				}
			} else {
				let mut new_packet_data = [0; 20*65];
				chacha.process(&msg.onion_routing_packet.hop_data[65..], &mut new_packet_data[0..19*65]);
				chacha.process(&ChannelManager::ZERO[0..65], &mut new_packet_data[19*65..]);

				let mut new_pubkey = msg.onion_routing_packet.public_key.clone();

				let blinding_factor = {
					let mut sha = Sha256::new();
					sha.input(&new_pubkey.serialize()[..]);
					sha.input(&shared_secret[..]);
					let mut res = [0u8; 32];
					sha.result(&mut res);
					match SecretKey::from_slice(&self.secp_ctx, &res) {
						Err(_) => {
							// Return temporary node failure as its technically our issue, not the
							// channel's issue.
							return Err(HandleError{err: "Blinding factor is an invalid private key",
								msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
									msg: msgs::UpdateFailHTLC {
										channel_id: msg.channel_id,
										htlc_id: msg.htlc_id,
										reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x2000 | 2, &[0;0]),
									}
								}),
							});
						},
						Ok(key) => key
					}
				};

				match new_pubkey.mul_assign(&self.secp_ctx, &blinding_factor) {
					Err(_) => {
						// Return temporary node failure as its technically our issue, not the
						// channel's issue.
						return Err(HandleError{err: "New blinding factor is an invalid private key",
							msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
								msg: msgs::UpdateFailHTLC {
									channel_id: msg.channel_id,
									htlc_id: msg.htlc_id,
									reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x2000 | 2, &[0;0]),
								}
							}),
						});
					},
					Ok(_) => {}
				};

				let outgoing_packet = msgs::OnionPacket {
					version: 0,
					public_key: new_pubkey,
					hop_data: new_packet_data,
					hmac: next_hop_data.hmac.clone(),
				};

				//TODO: Check amt_to_forward and outgoing_cltv_value are within acceptable ranges!

				PendingForwardHTLCInfo {
					onion_packet: Some(outgoing_packet),
					payment_hash: msg.payment_hash.clone(),
					short_channel_id: next_hop_data.data.short_channel_id,
					prev_short_channel_id: 0,
					amt_to_forward: next_hop_data.data.amt_to_forward,
					outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
				}
			};

		let mut channels = self.channels.lock().unwrap();

		if pending_forward_info.short_channel_id != 0 {
			let forwarding_id = match channels.short_to_id.get(&pending_forward_info.short_channel_id) {
				None => {
					return Err(HandleError{err: "Don't have available channel for forwarding as requested.",
						msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
							msg: msgs::UpdateFailHTLC {
								channel_id: msg.channel_id,
								htlc_id: msg.htlc_id,
								reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 10, &[0;0]),
							}
						}),
					});
				},
				Some(id) => id.clone(),
			};
			let chan = channels.by_id.get_mut(&forwarding_id).unwrap();
			if !chan.is_live() {
				let chan_update = self.get_channel_update(chan).unwrap();
				return Err(HandleError{err: "Forwarding channel is not in a ready state.",
					msg: Some(msgs::ErrorMessage::UpdateFailHTLC {
						msg: msgs::UpdateFailHTLC {
							channel_id: msg.channel_id,
							htlc_id: msg.htlc_id,
							reason: ChannelManager::build_first_hop_failure_packet(&shared_secret, 0x4000 | 10, &chan_update.encode()[..]),
						}
					}),
				});
			}
		}

		match channels.by_id.get_mut(&msg.channel_id) {
			Some(chan) => {
				if chan.get_their_node_id() != *their_node_id {
					return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
				}
				if !chan.is_usable() {
					return Err(HandleError{err: "Channel not yet available for receiving HTLCs", msg: None});
				}
				pending_forward_info.prev_short_channel_id = chan.get_short_channel_id().unwrap();
				chan.update_add_htlc(&msg, pending_forward_info)
			},
			None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None}), //TODO: panic?
		}
	}

	fn handle_update_fulfill_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
		let res = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					chan.update_fulfill_htlc(&msg)
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		//TODO: Delay the claimed_funds relaying just like we do outbound relay!
		self.claim_funds(msg.payment_preimage.clone());
		res
	}

	fn handle_update_fail_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
		let mut channels = self.channels.lock().unwrap();
		match channels.by_id.get_mut(&msg.channel_id) {
			Some(chan) => {
				if chan.get_their_node_id() != *their_node_id {
					return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
				}
				chan.update_fail_htlc(&msg)
			},
			None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
		}
		//TODO Pass the reason backwards through the onion channel
	}

	fn handle_update_fail_malformed_htlc(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) -> Result<Option<(Vec<msgs::UpdateAddHTLC>, msgs::CommitmentSigned)>, HandleError> {
		let mut channels = self.channels.lock().unwrap();
		match channels.by_id.get_mut(&msg.channel_id) {
			Some(chan) => {
				if chan.get_their_node_id() != *their_node_id {
					return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
				}
				chan.update_fail_malformed_htlc(&msg)
			},
			None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
		}
	}

	fn handle_commitment_signed(&self, their_node_id: &PublicKey, msg: &msgs::CommitmentSigned) -> Result<msgs::RevokeAndACK, HandleError> {
		let ((res, mut forwarding_infos), monitor) = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					(try!(chan.commitment_signed(&msg)), chan.channel_monitor())
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		//TODO: Only if we store HTLC sigs
		try!(self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor));

		let mut forward_event = None;
		{
			let mut pending_htlcs = self.pending_htlcs_forwardable.lock().unwrap();
			if pending_htlcs.forward_htlcs.is_empty() {
				let mut rng = thread_rng();
				forward_event = Some(Instant::now() + Duration::from_millis(((rng.next_f32() * 4.0 + 1.0) * MIN_HTLC_RELAY_HOLDING_CELL_MILLIS as f32) as u64));
				pending_htlcs.next_forward = forward_event.unwrap();
			}
			for forward_info in forwarding_infos.drain(..) {
				match pending_htlcs.forward_htlcs.entry(forward_info.short_channel_id) {
					hash_map::Entry::Occupied(mut entry) => {
						entry.get_mut().push(forward_info);
					},
					hash_map::Entry::Vacant(entry) => {
						entry.insert(vec!(forward_info));
					}
				}
			}
		}
		match forward_event {
			Some(time) => {
				let mut pending_events = self.pending_events.lock().unwrap();
				pending_events.push(events::Event::PendingHTLCsForwardable {
					time_forwardable: time
				});
			}
			None => {},
		}

		Ok(res)
	}

	fn handle_revoke_and_ack(&self, their_node_id: &PublicKey, msg: &msgs::RevokeAndACK) -> Result<(), HandleError> {
		let monitor = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					try!(chan.revoke_and_ack(&msg));
					chan.channel_monitor()
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		try!(self.monitor.add_update_monitor(monitor.get_funding_txo().unwrap(), monitor));
		Ok(())
	}

	fn handle_update_fee(&self, their_node_id: &PublicKey, msg: &msgs::UpdateFee) -> Result<(), HandleError> {
		let mut channels = self.channels.lock().unwrap();
		match channels.by_id.get_mut(&msg.channel_id) {
			Some(chan) => {
				if chan.get_their_node_id() != *their_node_id {
					return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
				}
				chan.update_fee(&*self.fee_estimator, &msg)
			},
			None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
		}
	}

	fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
		let (chan_announcement, chan_update) = {
			let mut channels = self.channels.lock().unwrap();
			match channels.by_id.get_mut(&msg.channel_id) {
				Some(chan) => {
					if chan.get_their_node_id() != *their_node_id {
						return Err(HandleError{err: "Got a message for a channel from the wrong node!", msg: None})
					}
					if !chan.is_usable() {
						return Err(HandleError{err: "Got an announcement_signatures before we were ready for it", msg: None });
					}

					let our_node_id = self.get_our_node_id();
					let (announcement, our_bitcoin_sig) = try!(chan.get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone()));

					let were_node_one = announcement.node_id_1 == our_node_id;
					let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
					secp_call!(self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }));
					secp_call!(self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }));

					let our_node_sig = secp_call!(self.secp_ctx.sign(&msghash, &self.our_network_key));

					(msgs::ChannelAnnouncement {
						node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
						node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
						bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
						bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
						contents: announcement,
					}, self.get_channel_update(chan).unwrap()) // can only fail if we're not in a ready state
				},
				None => return Err(HandleError{err: "Failed to find corresponding channel", msg: None})
			}
		};
		let mut pending_events = self.pending_events.lock().unwrap();
		pending_events.push(events::Event::BroadcastChannelAnnouncement { msg: chan_announcement, update_msg: chan_update });
		Ok(())
	}
}

#[cfg(test)]
mod tests {
	use ln::channelmanager::{ChannelManager,OnionKeys};
	use ln::router::{Route, RouteHop, Router};
	use ln::msgs;
	use ln::msgs::{MsgEncodable,ChannelMessageHandler,RoutingMessageHandler};
	use util::test_utils;
	use util::events::{Event, EventsProvider};

	use bitcoin::util::misc::hex_bytes;
	use bitcoin::util::hash::Sha256dHash;
	use bitcoin::blockdata::block::BlockHeader;
	use bitcoin::blockdata::transaction::Transaction;
	use bitcoin::network::constants::Network;
	use bitcoin::network::serialize::serialize;
	use bitcoin::network::serialize::BitcoinHash;

	use secp256k1::Secp256k1;
	use secp256k1::key::{PublicKey,SecretKey};

	use crypto::sha2::Sha256;
	use crypto::digest::Digest;

	use rand::{thread_rng,Rng};

	use std::sync::Arc;
	use std::default::Default;
	use std::time::Instant;

	fn build_test_onion_keys() -> Vec<OnionKeys> {
		// Keys from BOLT 4, used in both test vector tests
		let secp_ctx = Secp256k1::new();

		let route = Route {
			hops: vec!(
					RouteHop {
						pubkey: PublicKey::from_slice(&secp_ctx, &hex_bytes("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]).unwrap(),
						short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
					},
					RouteHop {
						pubkey: PublicKey::from_slice(&secp_ctx, &hex_bytes("0324653eac434488002cc06bbfb7f10fe18991e35f9fe4302dbea6d2353dc0ab1c").unwrap()[..]).unwrap(),
						short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
					},
					RouteHop {
						pubkey: PublicKey::from_slice(&secp_ctx, &hex_bytes("027f31ebc5462c1fdce1b737ecff52d37d75dea43ce11c74d25aa297165faa2007").unwrap()[..]).unwrap(),
						short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
					},
					RouteHop {
						pubkey: PublicKey::from_slice(&secp_ctx, &hex_bytes("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]).unwrap(),
						short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
					},
					RouteHop {
						pubkey: PublicKey::from_slice(&secp_ctx, &hex_bytes("02edabbd16b41c8371b92ef2f04c1185b4f03b6dcd52ba9b78d9d7c89c8f221145").unwrap()[..]).unwrap(),
						short_channel_id: 0, fee_msat: 0, cltv_expiry_delta: 0 // Test vectors are garbage and not generateble from a RouteHop, we fill in payloads manually
					},
			),
		};

		let session_priv = SecretKey::from_slice(&secp_ctx, &hex_bytes("4141414141414141414141414141414141414141414141414141414141414141").unwrap()[..]).unwrap();

		let onion_keys = ChannelManager::construct_onion_keys(&secp_ctx, &route, &session_priv).unwrap();
		assert_eq!(onion_keys.len(), route.hops.len());
		onion_keys
	}

	#[test]
	fn onion_vectors() {
		// Packet creation test vectors from BOLT 4
		let onion_keys = build_test_onion_keys();

		assert_eq!(onion_keys[0].shared_secret[..], hex_bytes("53eb63ea8a3fec3b3cd433b85cd62a4b145e1dda09391b348c4e1cd36a03ea66").unwrap()[..]);
		assert_eq!(onion_keys[0].blinding_factor[..], hex_bytes("2ec2e5da605776054187180343287683aa6a51b4b1c04d6dd49c45d8cffb3c36").unwrap()[..]);
		assert_eq!(onion_keys[0].ephemeral_pubkey.serialize()[..], hex_bytes("02eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619").unwrap()[..]);
		assert_eq!(onion_keys[0].rho, hex_bytes("ce496ec94def95aadd4bec15cdb41a740c9f2b62347c4917325fcc6fb0453986").unwrap()[..]);
		assert_eq!(onion_keys[0].mu, hex_bytes("b57061dc6d0a2b9f261ac410c8b26d64ac5506cbba30267a649c28c179400eba").unwrap()[..]);

		assert_eq!(onion_keys[1].shared_secret[..], hex_bytes("a6519e98832a0b179f62123b3567c106db99ee37bef036e783263602f3488fae").unwrap()[..]);
		assert_eq!(onion_keys[1].blinding_factor[..], hex_bytes("bf66c28bc22e598cfd574a1931a2bafbca09163df2261e6d0056b2610dab938f").unwrap()[..]);
		assert_eq!(onion_keys[1].ephemeral_pubkey.serialize()[..], hex_bytes("028f9438bfbf7feac2e108d677e3a82da596be706cc1cf342b75c7b7e22bf4e6e2").unwrap()[..]);
		assert_eq!(onion_keys[1].rho, hex_bytes("450ffcabc6449094918ebe13d4f03e433d20a3d28a768203337bc40b6e4b2c59").unwrap()[..]);
		assert_eq!(onion_keys[1].mu, hex_bytes("05ed2b4a3fb023c2ff5dd6ed4b9b6ea7383f5cfe9d59c11d121ec2c81ca2eea9").unwrap()[..]);

		assert_eq!(onion_keys[2].shared_secret[..], hex_bytes("3a6b412548762f0dbccce5c7ae7bb8147d1caf9b5471c34120b30bc9c04891cc").unwrap()[..]);
		assert_eq!(onion_keys[2].blinding_factor[..], hex_bytes("a1f2dadd184eb1627049673f18c6325814384facdee5bfd935d9cb031a1698a5").unwrap()[..]);
		assert_eq!(onion_keys[2].ephemeral_pubkey.serialize()[..], hex_bytes("03bfd8225241ea71cd0843db7709f4c222f62ff2d4516fd38b39914ab6b83e0da0").unwrap()[..]);
		assert_eq!(onion_keys[2].rho, hex_bytes("11bf5c4f960239cb37833936aa3d02cea82c0f39fd35f566109c41f9eac8deea").unwrap()[..]);
		assert_eq!(onion_keys[2].mu, hex_bytes("caafe2820fa00eb2eeb78695ae452eba38f5a53ed6d53518c5c6edf76f3f5b78").unwrap()[..]);

		assert_eq!(onion_keys[3].shared_secret[..], hex_bytes("21e13c2d7cfe7e18836df50872466117a295783ab8aab0e7ecc8c725503ad02d").unwrap()[..]);
		assert_eq!(onion_keys[3].blinding_factor[..], hex_bytes("7cfe0b699f35525029ae0fa437c69d0f20f7ed4e3916133f9cacbb13c82ff262").unwrap()[..]);
		assert_eq!(onion_keys[3].ephemeral_pubkey.serialize()[..], hex_bytes("031dde6926381289671300239ea8e57ffaf9bebd05b9a5b95beaf07af05cd43595").unwrap()[..]);
		assert_eq!(onion_keys[3].rho, hex_bytes("cbe784ab745c13ff5cffc2fbe3e84424aa0fd669b8ead4ee562901a4a4e89e9e").unwrap()[..]);
		assert_eq!(onion_keys[3].mu, hex_bytes("5052aa1b3d9f0655a0932e50d42f0c9ba0705142c25d225515c45f47c0036ee9").unwrap()[..]);

		assert_eq!(onion_keys[4].shared_secret[..], hex_bytes("b5756b9b542727dbafc6765a49488b023a725d631af688fc031217e90770c328").unwrap()[..]);
		assert_eq!(onion_keys[4].blinding_factor[..], hex_bytes("c96e00dddaf57e7edcd4fb5954be5b65b09f17cb6d20651b4e90315be5779205").unwrap()[..]);
		assert_eq!(onion_keys[4].ephemeral_pubkey.serialize()[..], hex_bytes("03a214ebd875aab6ddfd77f22c5e7311d7f77f17a169e599f157bbcdae8bf071f4").unwrap()[..]);
		assert_eq!(onion_keys[4].rho, hex_bytes("034e18b8cc718e8af6339106e706c52d8df89e2b1f7e9142d996acf88df8799b").unwrap()[..]);
		assert_eq!(onion_keys[4].mu, hex_bytes("8e45e5c61c2b24cb6382444db6698727afb063adecd72aada233d4bf273d975a").unwrap()[..]);

		// Test vectors below are flat-out wrong: they claim to set outgoing_cltv_value to non-0 :/
		let payloads = vec!(
			msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: 0,
					amt_to_forward: 0,
					outgoing_cltv_value: 0,
				},
				hmac: [0; 32],
			},
			msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: 0x0101010101010101,
					amt_to_forward: 0x0100000001,
					outgoing_cltv_value: 0,
				},
				hmac: [0; 32],
			},
			msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: 0x0202020202020202,
					amt_to_forward: 0x0200000002,
					outgoing_cltv_value: 0,
				},
				hmac: [0; 32],
			},
			msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: 0x0303030303030303,
					amt_to_forward: 0x0300000003,
					outgoing_cltv_value: 0,
				},
				hmac: [0; 32],
			},
			msgs::OnionHopData {
				realm: 0,
				data: msgs::OnionRealm0HopData {
					short_channel_id: 0x0404040404040404,
					amt_to_forward: 0x0400000004,
					outgoing_cltv_value: 0,
				},
				hmac: [0; 32],
			},
		);

		let packet = ChannelManager::construct_onion_packet(payloads, onion_keys, hex_bytes("4242424242424242424242424242424242424242424242424242424242424242").unwrap()).unwrap();
		// Just check the final packet encoding, as it includes all the per-hop vectors in it
		// anyway...
		assert_eq!(packet.encode(), hex_bytes("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a716a996c7845c93d90e4ecbb9bde4ece2f69425c99e4bc820e44485455f135edc0d10f7d61ab590531cf08000179a333a347f8b4072f216400406bdf3bf038659793d4a1fd7b246979e3150a0a4cb052c9ec69acf0f48c3d39cd55675fe717cb7d80ce721caad69320c3a469a202f1e468c67eaf7a7cd8226d0fd32f7b48084dca885d56047694762b67021713ca673929c163ec36e04e40ca8e1c6d17569419d3039d9a1ec866abe044a9ad635778b961fc0776dc832b3a451bd5d35072d2269cf9b040f6b7a7dad84fb114ed413b1426cb96ceaf83825665ed5a1d002c1687f92465b49ed4c7f0218ff8c6c7dd7221d589c65b3b9aaa71a41484b122846c7c7b57e02e679ea8469b70e14fe4f70fee4d87b910cf144be6fe48eef24da475c0b0bcc6565ae82cd3f4e3b24c76eaa5616c6111343306ab35c1fe5ca4a77c0e314ed7dba39d6f1e0de791719c241a939cc493bea2bae1c1e932679ea94d29084278513c77b899cc98059d06a27d171b0dbdf6bee13ddc4fc17a0c4d2827d488436b57baa167544138ca2e64a11b43ac8a06cd0c2fba2d4d900ed2d9205305e2d7383cc98dacb078133de5f6fb6bed2ef26ba92cea28aafc3b9948dd9ae5559e8bd6920b8cea462aa445ca6a95e0e7ba52961b181c79e73bd581821df2b10173727a810c92b83b5ba4a0403eb710d2ca10689a35bec6c3a708e9e92f7d78ff3c5d9989574b00c6736f84c199256e76e19e78f0c98a9d580b4a658c84fc8f2096c2fbea8f5f8c59d0fdacb3be2802ef802abbecb3aba4acaac69a0e965abd8981e9896b1f6ef9d60f7a164b371af869fd0e48073742825e9434fc54da837e120266d53302954843538ea7c6c3dbfb4ff3b2fdbe244437f2a153ccf7bdb4c92aa08102d4f3cff2ae5ef86fab4653595e6a5837fa2f3e29f27a9cde5966843fb847a4a61f1e76c281fe8bb2b0a181d096100db5a1a5ce7a910238251a43ca556712eaadea167fb4d7d75825e440f3ecd782036d7574df8bceacb397abefc5f5254d2722215c53ff54af8299aaaad642c6d72a14d27882d9bbd539e1cc7a527526ba89b8c037ad09120e98ab042d3e8652b31ae0e478516bfaf88efca9f3676ffe99d2819dcaeb7610a626695f53117665d267d3f7abebd6bbd6733f645c72c389f03855bdf1e4b8075b516569b118233a0f0971d24b83113c0b096f5216a207ca99a7cddc81c130923fe3d91e7508c9ac5f2e914ff5dccab9e558566fa14efb34ac98d878580814b94b73acbfde9072f30b881f7f0fff42d4045d1ace6322d86a97d164aa84d93a60498065cc7c20e636f5862dc81531a88c60305a2e59a985be327a6902e4bed986dbf4a0b50c217af0ea7fdf9ab37f9ea1a1aaa72f54cf40154ea9b269f1a7c09f9f43245109431a175d50e2db0132337baa0ef97eed0fcf20489da36b79a1172faccc2f7ded7c60e00694282d93359c4682135642bc81f433574aa8ef0c97b4ade7ca372c5ffc23c7eddd839bab4e0f14d6df15c9dbeab176bec8b5701cf054eb3072f6dadc98f88819042bf10c407516ee58bce33fbe3b3d86a54255e577db4598e30a135361528c101683a5fcde7e8ba53f3456254be8f45fe3a56120ae96ea3773631fcb3873aa3abd91bcff00bd38bd43697a2e789e00da6077482e7b1b1a677b5afae4c54e6cbdf7377b694eb7d7a5b913476a5be923322d3de06060fd5e819635232a2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780df54bfdb0dd5cd9855179f602f9172307c7268724c3618e6817abd793adc214a0dc0bc616816632f27ea336fb56dfd").unwrap());
	}

	#[test]
	fn test_failure_packet_onion() {
		// Returning Errors test vectors from BOLT 4

		let onion_keys = build_test_onion_keys();
		let onion_error = ChannelManager::build_failure_packet(&onion_keys[4].shared_secret, 0x2002, &[0; 0]);
		assert_eq!(onion_error.encode(), hex_bytes("4c2fc8bc08510334b6833ad9c3e79cd1b52ae59dfe5c2a4b23ead50f09f7ee0b0002200200fe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap());

		let onion_packet_1 = ChannelManager::encrypt_failure_packet(&onion_keys[4].shared_secret, &onion_error.encode()[..]);
		assert_eq!(onion_packet_1.data, hex_bytes("a5e6bd0c74cb347f10cce367f949098f2457d14c046fd8a22cb96efb30b0fdcda8cb9168b50f2fd45edd73c1b0c8b33002df376801ff58aaa94000bf8a86f92620f343baef38a580102395ae3abf9128d1047a0736ff9b83d456740ebbb4aeb3aa9737f18fb4afb4aa074fb26c4d702f42968888550a3bded8c05247e045b866baef0499f079fdaeef6538f31d44deafffdfd3afa2fb4ca9082b8f1c465371a9894dd8c243fb4847e004f5256b3e90e2edde4c9fb3082ddfe4d1e734cacd96ef0706bf63c9984e22dc98851bcccd1c3494351feb458c9c6af41c0044bea3c47552b1d992ae542b17a2d0bba1a096c78d169034ecb55b6e3a7263c26017f033031228833c1daefc0dedb8cf7c3e37c9c37ebfe42f3225c326e8bcfd338804c145b16e34e4").unwrap());

		let onion_packet_2 = ChannelManager::encrypt_failure_packet(&onion_keys[3].shared_secret, &onion_packet_1.data[..]);
		assert_eq!(onion_packet_2.data, hex_bytes("c49a1ce81680f78f5f2000cda36268de34a3f0a0662f55b4e837c83a8773c22aa081bab1616a0011585323930fa5b9fae0c85770a2279ff59ec427ad1bbff9001c0cd1497004bd2a0f68b50704cf6d6a4bf3c8b6a0833399a24b3456961ba00736785112594f65b6b2d44d9f5ea4e49b5e1ec2af978cbe31c67114440ac51a62081df0ed46d4a3df295da0b0fe25c0115019f03f15ec86fabb4c852f83449e812f141a9395b3f70b766ebbd4ec2fae2b6955bd8f32684c15abfe8fd3a6261e52650e8807a92158d9f1463261a925e4bfba44bd20b166d532f0017185c3a6ac7957adefe45559e3072c8dc35abeba835a8cb01a71a15c736911126f27d46a36168ca5ef7dccd4e2886212602b181463e0dd30185c96348f9743a02aca8ec27c0b90dca270").unwrap());

		let onion_packet_3 = ChannelManager::encrypt_failure_packet(&onion_keys[2].shared_secret, &onion_packet_2.data[..]);
		assert_eq!(onion_packet_3.data, hex_bytes("a5d3e8634cfe78b2307d87c6d90be6fe7855b4f2cc9b1dfb19e92e4b79103f61ff9ac25f412ddfb7466e74f81b3e545563cdd8f5524dae873de61d7bdfccd496af2584930d2b566b4f8d3881f8c043df92224f38cf094cfc09d92655989531524593ec6d6caec1863bdfaa79229b5020acc034cd6deeea1021c50586947b9b8e6faa83b81fbfa6133c0af5d6b07c017f7158fa94f0d206baf12dda6b68f785b773b360fd0497e16cc402d779c8d48d0fa6315536ef0660f3f4e1865f5b38ea49c7da4fd959de4e83ff3ab686f059a45c65ba2af4a6a79166aa0f496bf04d06987b6d2ea205bdb0d347718b9aeff5b61dfff344993a275b79717cd815b6ad4c0beb568c4ac9c36ff1c315ec1119a1993c4b61e6eaa0375e0aaf738ac691abd3263bf937e3").unwrap());

		let onion_packet_4 = ChannelManager::encrypt_failure_packet(&onion_keys[1].shared_secret, &onion_packet_3.data[..]);
		assert_eq!(onion_packet_4.data, hex_bytes("aac3200c4968f56b21f53e5e374e3a2383ad2b1b6501bbcc45abc31e59b26881b7dfadbb56ec8dae8857add94e6702fb4c3a4de22e2e669e1ed926b04447fc73034bb730f4932acd62727b75348a648a1128744657ca6a4e713b9b646c3ca66cac02cdab44dd3439890ef3aaf61708714f7375349b8da541b2548d452d84de7084bb95b3ac2345201d624d31f4d52078aa0fa05a88b4e20202bd2b86ac5b52919ea305a8949de95e935eed0319cf3cf19ebea61d76ba92532497fcdc9411d06bcd4275094d0a4a3c5d3a945e43305a5a9256e333e1f64dbca5fcd4e03a39b9012d197506e06f29339dfee3331995b21615337ae060233d39befea925cc262873e0530408e6990f1cbd233a150ef7b004ff6166c70c68d9f8c853c1abca640b8660db2921").unwrap());

		let onion_packet_5 = ChannelManager::encrypt_failure_packet(&onion_keys[0].shared_secret, &onion_packet_4.data[..]);
		assert_eq!(onion_packet_5.data, hex_bytes("9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d").unwrap());
	}

	static mut CHAN_COUNT: u16 = 0;
	fn confirm_transaction(chain: &test_utils::TestWatchInterface, tx: &Transaction) {
		let mut header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
		let chan_id = unsafe { CHAN_COUNT };
		chain.watch_util.do_call_block_connected(&header, 1, &[tx; 1], &[chan_id as u32; 1]);
		for i in 2..100 {
			header = BlockHeader { version: 0x20000000, prev_blockhash: header.bitcoin_hash(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
			chain.watch_util.do_call_block_connected(&header, i, &[tx; 0], &[0; 0]);
		}
	}

	fn create_chan_between_nodes(node_a: &ChannelManager, chain_a: &test_utils::TestWatchInterface, node_b: &ChannelManager, chain_b: &test_utils::TestWatchInterface) -> (msgs::ChannelAnnouncement, msgs::ChannelUpdate, msgs::ChannelUpdate) {
		let open_chan = node_a.create_channel(node_b.get_our_node_id(), (1 << 24) - 1, 42).unwrap();
		let accept_chan = node_b.handle_open_channel(&node_a.get_our_node_id(), &open_chan).unwrap();
		node_a.handle_accept_channel(&node_b.get_our_node_id(), &accept_chan).unwrap();

		let chan_id = unsafe { CHAN_COUNT };
		let tx = Transaction { version: chan_id as u32, lock_time: 0, input: Vec::new(), output: Vec::new(), witness: Vec::new() };
		let funding_output = (Sha256dHash::from_data(&serialize(&tx).unwrap()[..]), chan_id);

		let events_1 = node_a.get_and_clear_pending_events();
		assert_eq!(events_1.len(), 1);
		match events_1[0] {
			Event::FundingGenerationReady { ref temporary_channel_id, ref channel_value_satoshis, output_script: _, user_channel_id } => {
				assert_eq!(*channel_value_satoshis, (1 << 24) - 1);
				assert_eq!(user_channel_id, 42);

				node_a.funding_transaction_generated(&temporary_channel_id, funding_output.clone());
				//TODO: Check that we got added to chan_monitor_a!
			},
			_ => panic!("Unexpected event"),
		}

		let events_2 = node_a.get_and_clear_pending_events();
		assert_eq!(events_2.len(), 1);
		let funding_signed = match events_2[0] {
			Event::SendFundingCreated { ref node_id, ref msg } => {
				assert_eq!(*node_id, node_b.get_our_node_id());
				node_b.handle_funding_created(&node_a.get_our_node_id(), msg).unwrap()
				//TODO: Check that we got added to chan_monitor_b!
			},
			_ => panic!("Unexpected event"),
		};

		node_a.handle_funding_signed(&node_b.get_our_node_id(), &funding_signed).unwrap();

		let events_3 = node_a.get_and_clear_pending_events();
		assert_eq!(events_3.len(), 1);
		match events_3[0] {
			Event::FundingBroadcastSafe { ref funding_txo, user_channel_id } => {
				assert_eq!(user_channel_id, 42);
				assert_eq!(*funding_txo, funding_output);
			},
			_ => panic!("Unexpected event"),
		};

		confirm_transaction(&chain_a, &tx);
		let events_4 = node_a.get_and_clear_pending_events();
		assert_eq!(events_4.len(), 1);
		match events_4[0] {
			Event::SendFundingLocked { ref node_id, ref msg, ref announcement_sigs } => {
				assert_eq!(*node_id, node_b.get_our_node_id());
				assert!(announcement_sigs.is_none());
				node_b.handle_funding_locked(&node_a.get_our_node_id(), msg).unwrap()
			},
			_ => panic!("Unexpected event"),
		};

		confirm_transaction(&chain_b, &tx);
		let events_5 = node_b.get_and_clear_pending_events();
		assert_eq!(events_5.len(), 1);
		let as_announcement_sigs = match events_5[0] {
			Event::SendFundingLocked { ref node_id, ref msg, ref announcement_sigs } => {
				assert_eq!(*node_id, node_a.get_our_node_id());
				let as_announcement_sigs = node_a.handle_funding_locked(&node_b.get_our_node_id(), msg).unwrap().unwrap();
				node_a.handle_announcement_signatures(&node_b.get_our_node_id(), &(*announcement_sigs).clone().unwrap()).unwrap();
				as_announcement_sigs
			},
			_ => panic!("Unexpected event"),
		};

		let events_6 = node_a.get_and_clear_pending_events();
		assert_eq!(events_6.len(), 1);
		let (announcement, as_update) = match events_6[0] {
			Event::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
				(msg, update_msg)
			},
			_ => panic!("Unexpected event"),
		};

		node_b.handle_announcement_signatures(&node_a.get_our_node_id(), &as_announcement_sigs).unwrap();
		let events_7 = node_b.get_and_clear_pending_events();
		assert_eq!(events_7.len(), 1);
		let bs_update = match events_7[0] {
			Event::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
				assert!(*announcement == *msg);
				update_msg
			},
			_ => panic!("Unexpected event"),
		};

		unsafe {
			CHAN_COUNT += 1;
		}

		((*announcement).clone(), (*as_update).clone(), (*bs_update).clone())
	}

	struct SendEvent {
		node_id: PublicKey,
		msgs: Vec<msgs::UpdateAddHTLC>,
		commitment_msg: msgs::CommitmentSigned,
	}
	impl SendEvent {
		fn from_event(event: Event) -> SendEvent {
			match event {
				Event::SendHTLCs{ node_id, msgs, commitment_msg } => {
					SendEvent { node_id: node_id, msgs: msgs, commitment_msg: commitment_msg }
				},
				_ => panic!("Unexpected event type!"),
			}
		}
	}

	static mut PAYMENT_COUNT: u8 = 0;
	fn send_payment(origin_node: &ChannelManager, origin_router: &Router, expected_route: &[&ChannelManager]) {
		let route = origin_router.get_route(&expected_route.last().unwrap().get_our_node_id(), &Vec::new(), 1000000, 142).unwrap();
		assert_eq!(route.hops.len(), expected_route.len());
		for (node, hop) in expected_route.iter().zip(route.hops.iter()) {
			assert_eq!(hop.pubkey, node.get_our_node_id());
		}

		let payment_preimage = unsafe { [PAYMENT_COUNT; 32] };
		unsafe { PAYMENT_COUNT += 1 };
		let our_payment_hash = {
			let mut sha = Sha256::new();
			sha.input(&payment_preimage[..]);
			let mut ret = [0; 32];
			sha.result(&mut ret);
			ret
		};

		let mut payment_event = {
			let msgs = origin_node.send_payment(&route, our_payment_hash).unwrap().unwrap();
			SendEvent {
				node_id: expected_route[0].get_our_node_id(),
				msgs: vec!(msgs.0),
				commitment_msg: msgs.1,
			}
		};
		let mut prev_node = origin_node;

		for (idx, node) in expected_route.iter().enumerate() {
			assert_eq!(node.get_our_node_id(), payment_event.node_id);

			node.handle_update_add_htlc(&prev_node.get_our_node_id(), &payment_event.msgs[0]).unwrap();
			let revoke_and_ack = node.handle_commitment_signed(&prev_node.get_our_node_id(), &payment_event.commitment_msg).unwrap();
			prev_node.handle_revoke_and_ack(&node.get_our_node_id(), &revoke_and_ack).unwrap();

			let events_1 = node.get_and_clear_pending_events();
			assert_eq!(events_1.len(), 1);
			match events_1[0] {
				Event::PendingHTLCsForwardable { .. } => { },
				_ => panic!("Unexpected event"),
			};

			node.pending_htlcs_forwardable.lock().unwrap().next_forward = Instant::now();
			node.process_pending_htlc_forward();

			let mut events_2 = node.get_and_clear_pending_events();
			assert_eq!(events_2.len(), 1);
			if idx == expected_route.len() - 1 {
				match events_2[0] {
					Event::PaymentReceived { ref payment_hash, amt } => {
						assert_eq!(our_payment_hash, *payment_hash);
						assert_eq!(amt, 1000000);
					},
					_ => panic!("Unexpected event"),
				}
			} else {
				for event in events_2.drain(..) {
					payment_event = SendEvent::from_event(event);
				}
				assert_eq!(payment_event.msgs.len(), 1);
			}

			prev_node = node;
		}

		assert!(expected_route.last().unwrap().claim_funds(payment_preimage));

		let mut expected_next_node = expected_route.last().unwrap().get_our_node_id();
		let mut prev_node = expected_route.last().unwrap();
		let mut next_msg = None;
		for node in expected_route.iter().rev() {
			assert_eq!(expected_next_node, node.get_our_node_id());
			match next_msg {
				Some(msg) => {
					assert!(node.handle_update_fulfill_htlc(&prev_node.get_our_node_id(), &msg).unwrap().is_none());
				}, None => {}
			}

			let events = node.get_and_clear_pending_events();
			assert_eq!(events.len(), 1);
			match events[0] {
				Event::SendFulfillHTLC { ref node_id, ref msg } => {
					expected_next_node = node_id.clone();
					next_msg = Some(msg.clone());
				},
				_ => panic!("Unexpected event"),
			};

			prev_node = node;
		}

		assert_eq!(expected_next_node, origin_node.get_our_node_id());
		assert!(origin_node.handle_update_fulfill_htlc(&expected_route.first().unwrap().get_our_node_id(), &next_msg.unwrap()).unwrap().is_none());
	}

	#[test]
	fn fake_network_test() {
		// Simple test which builds a network of ChannelManagers, connects them to each other, and
		// tests that payments get routed and transactions broadcast in semi-reasonable ways.
		let mut rng = thread_rng();
		let secp_ctx = Secp256k1::new();

		let feeest_1 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
		let chain_monitor_1 = Arc::new(test_utils::TestWatchInterface::new());
		let chan_monitor_1 = Arc::new(test_utils::TestChannelMonitor{});
		let node_id_1 = {
			let mut key_slice = [0; 32];
			rng.fill_bytes(&mut key_slice);
			SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
		};
		let node_1 = ChannelManager::new(node_id_1.clone(), 0, true, Network::Testnet, feeest_1.clone(), chan_monitor_1.clone(), chain_monitor_1.clone()).unwrap();
		let router_1 = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id_1).unwrap());

		let feeest_2 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
		let chain_monitor_2 = Arc::new(test_utils::TestWatchInterface::new());
		let chan_monitor_2 = Arc::new(test_utils::TestChannelMonitor{});
		let node_id_2 = {
			let mut key_slice = [0; 32];
			rng.fill_bytes(&mut key_slice);
			SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
		};
		let node_2 = ChannelManager::new(node_id_2.clone(), 0, true, Network::Testnet, feeest_2.clone(), chan_monitor_2.clone(), chain_monitor_2.clone()).unwrap();
		let router_2 = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id_2).unwrap());

		let feeest_3 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
		let chain_monitor_3 = Arc::new(test_utils::TestWatchInterface::new());
		let chan_monitor_3 = Arc::new(test_utils::TestChannelMonitor{});
		let node_id_3 = {
			let mut key_slice = [0; 32];
			rng.fill_bytes(&mut key_slice);
			SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
		};
		let node_3 = ChannelManager::new(node_id_3.clone(), 0, true, Network::Testnet, feeest_3.clone(), chan_monitor_3.clone(), chain_monitor_3.clone()).unwrap();
		let router_3 = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id_3).unwrap());

		let feeest_4 = Arc::new(test_utils::TestFeeEstimator { sat_per_vbyte: 1 });
		let chain_monitor_4 = Arc::new(test_utils::TestWatchInterface::new());
		let chan_monitor_4 = Arc::new(test_utils::TestChannelMonitor{});
		let node_id_4 = {
			let mut key_slice = [0; 32];
			rng.fill_bytes(&mut key_slice);
			SecretKey::from_slice(&secp_ctx, &key_slice).unwrap()
		};
		let node_4 = ChannelManager::new(node_id_4.clone(), 0, true, Network::Testnet, feeest_4.clone(), chan_monitor_4.clone(), chain_monitor_4.clone()).unwrap();
		let router_4 = Router::new(PublicKey::from_secret_key(&secp_ctx, &node_id_4).unwrap());

		// Create some initial channels
		let chan_announcement_1 = create_chan_between_nodes(&node_1, &chain_monitor_1, &node_2, &chain_monitor_2);
		for router in vec!(&router_1, &router_2, &router_3, &router_4) {
			assert!(router.handle_channel_announcement(&chan_announcement_1.0).unwrap());
			router.handle_channel_update(&chan_announcement_1.1).unwrap();
			router.handle_channel_update(&chan_announcement_1.2).unwrap();
		}
		let chan_announcement_2 = create_chan_between_nodes(&node_2, &chain_monitor_2, &node_3, &chain_monitor_3);
		for router in vec!(&router_1, &router_2, &router_3, &router_4) {
			assert!(router.handle_channel_announcement(&chan_announcement_2.0).unwrap());
			router.handle_channel_update(&chan_announcement_2.1).unwrap();
			router.handle_channel_update(&chan_announcement_2.2).unwrap();
		}
		let chan_announcement_3 = create_chan_between_nodes(&node_3, &chain_monitor_3, &node_4, &chain_monitor_4);
		for router in vec!(&router_1, &router_2, &router_3, &router_4) {
			assert!(router.handle_channel_announcement(&chan_announcement_3.0).unwrap());
			router.handle_channel_update(&chan_announcement_3.1).unwrap();
			router.handle_channel_update(&chan_announcement_3.2).unwrap();
		}

		// Send some payments
		send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_3, &*node_4)[..]);
		send_payment(&node_2, &router_2, &vec!(&*node_3, &*node_4)[..]);
		send_payment(&node_4, &router_4, &vec!(&*node_3, &*node_2, &*node_1)[..]);

		// Add a new channel that skips 3
		let chan_announcement_4 = create_chan_between_nodes(&node_2, &chain_monitor_2, &node_4, &chain_monitor_4);
		for router in vec!(&router_1, &router_2, &router_3, &router_4) {
			assert!(router.handle_channel_announcement(&chan_announcement_4.0).unwrap());
			router.handle_channel_update(&chan_announcement_4.1).unwrap();
			router.handle_channel_update(&chan_announcement_4.2).unwrap();
		}

		send_payment(&node_1, &router_1, &vec!(&*node_2, &*node_4)[..]);

		// Check that we processed all pending events
		for node in vec!(&node_1, &node_2, &node_3, &node_4) {
			assert_eq!(node.get_and_clear_pending_events().len(), 0);
		}
	}
}