Skip to content

Commit b9f69f1

Browse files
author
Antoine Riard
committed
Add bolt7 msgs serialization tests
1 parent 301f91e commit b9f69f1

File tree

1 file changed

+152
-10
lines changed

1 file changed

+152
-10
lines changed

src/ln/msgs.rs

Lines changed: 152 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl LocalFeatures {
115115
}
116116

117117
/// Tracks globalfeatures which are in init messages and routing announcements
118-
#[derive(Clone, PartialEq)]
118+
#[derive(Clone, PartialEq, Debug)]
119119
pub struct GlobalFeatures {
120120
flags: Vec<u8>,
121121
}
@@ -327,7 +327,7 @@ pub struct ChannelReestablish {
327327
}
328328

329329
/// An announcement_signatures message to be sent or received from a peer
330-
#[derive(Clone)]
330+
#[derive(PartialEq, Clone, Debug)]
331331
pub struct AnnouncementSignatures {
332332
pub(crate) channel_id: [u8; 32],
333333
pub(crate) short_channel_id: u64,
@@ -336,7 +336,7 @@ pub struct AnnouncementSignatures {
336336
}
337337

338338
/// An address which can be used to connect to a remote peer
339-
#[derive(Clone)]
339+
#[derive(Clone, PartialEq, Debug)]
340340
pub enum NetAddress {
341341
/// An IPv4 address/port on which the peer is listenting.
342342
IPv4 {
@@ -387,6 +387,7 @@ impl NetAddress {
387387
#[derive(Clone)]
388388
// Only exposed as broadcast of node_announcement should be filtered by node_id
389389
/// The unsigned part of a node_announcement
390+
#[derive(PartialEq, Debug)]
390391
pub struct UnsignedNodeAnnouncement {
391392
pub(crate) features: GlobalFeatures,
392393
pub(crate) timestamp: u32,
@@ -401,7 +402,7 @@ pub struct UnsignedNodeAnnouncement {
401402
pub(crate) excess_address_data: Vec<u8>,
402403
pub(crate) excess_data: Vec<u8>,
403404
}
404-
#[derive(Clone)]
405+
#[derive(PartialEq, Clone, Debug)]
405406
/// A node_announcement message to be sent or received from a peer
406407
pub struct NodeAnnouncement {
407408
pub(crate) signature: Signature,
@@ -410,7 +411,7 @@ pub struct NodeAnnouncement {
410411

411412
// Only exposed as broadcast of channel_announcement should be filtered by node_id
412413
/// The unsigned part of a channel_announcement
413-
#[derive(PartialEq, Clone)]
414+
#[derive(PartialEq, Clone, Debug)]
414415
pub struct UnsignedChannelAnnouncement {
415416
pub(crate) features: GlobalFeatures,
416417
pub(crate) chain_hash: Sha256dHash,
@@ -424,7 +425,7 @@ pub struct UnsignedChannelAnnouncement {
424425
pub(crate) excess_data: Vec<u8>,
425426
}
426427
/// A channel_announcement message to be sent or received from a peer
427-
#[derive(PartialEq, Clone)]
428+
#[derive(PartialEq, Clone, Debug)]
428429
pub struct ChannelAnnouncement {
429430
pub(crate) node_signature_1: Signature,
430431
pub(crate) node_signature_2: Signature,
@@ -433,7 +434,7 @@ pub struct ChannelAnnouncement {
433434
pub(crate) contents: UnsignedChannelAnnouncement,
434435
}
435436

436-
#[derive(PartialEq, Clone)]
437+
#[derive(PartialEq, Clone, Debug)]
437438
pub(crate) struct UnsignedChannelUpdate {
438439
pub(crate) chain_hash: Sha256dHash,
439440
pub(crate) short_channel_id: u64,
@@ -446,7 +447,7 @@ pub(crate) struct UnsignedChannelUpdate {
446447
pub(crate) excess_data: Vec<u8>,
447448
}
448449
/// A channel_update message to be sent or received from a peer
449-
#[derive(PartialEq, Clone)]
450+
#[derive(PartialEq, Clone, Debug)]
450451
pub struct ChannelUpdate {
451452
pub(crate) signature: Signature,
452453
pub(crate) contents: UnsignedChannelUpdate,
@@ -1322,9 +1323,27 @@ impl_writeable_len_match!(NodeAnnouncement, {
13221323
mod tests {
13231324
use hex;
13241325
use ln::msgs;
1325-
use util::ser::Writeable;
1326+
use ln::msgs::GlobalFeatures;
1327+
use util::ser::{Writer, Writeable, Readable};
1328+
1329+
use bitcoin::util::hash::Sha256dHash;
1330+
13261331
use secp256k1::key::{PublicKey,SecretKey};
1327-
use secp256k1::Secp256k1;
1332+
use secp256k1::{Secp256k1, Message};
1333+
1334+
use rand::{Rng, thread_rng};
1335+
1336+
1337+
struct VecWriter(Vec<u8>);
1338+
impl Writer for VecWriter {
1339+
fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
1340+
self.0.extend_from_slice(buf);
1341+
Ok(())
1342+
}
1343+
fn size_hint(&mut self, size: usize) {
1344+
self.0.reserve_exact(size);
1345+
}
1346+
}
13281347

13291348
#[test]
13301349
fn encoding_channel_reestablish_no_secret() {
@@ -1362,4 +1381,127 @@ mod tests {
13621381
vec![4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 3, 27, 132, 197, 86, 123, 18, 100, 64, 153, 93, 62, 213, 170, 186, 5, 101, 215, 30, 24, 52, 96, 72, 25, 255, 156, 23, 245, 233, 213, 221, 7, 143]
13631382
);
13641383
}
1384+
1385+
#[test]
1386+
fn test_serialization_bolt7_msgs() {
1387+
1388+
let mut rng = thread_rng();
1389+
let secp_ctx = Secp256k1::new();
1390+
1391+
let mut channel_id = [0; 32];
1392+
rng.fill_bytes(&mut channel_id);
1393+
let secret_key_1 = SecretKey::new(&secp_ctx, &mut rng);
1394+
let secret_key_2 = SecretKey::new(&secp_ctx, &mut rng);
1395+
let secret_key_3 = SecretKey::new(&secp_ctx, &mut rng);
1396+
let secret_key_4 = SecretKey::new(&secp_ctx, &mut rng);
1397+
let mut msg = [0; 32];
1398+
rng.fill_bytes(&mut msg);
1399+
let sighash = Message::from_slice(&msg).unwrap();
1400+
let signature_1 = secp_ctx.sign(&sighash, &secret_key_1);
1401+
let signature_2 = secp_ctx.sign(&sighash, &secret_key_2);
1402+
let signature_3 = secp_ctx.sign(&sighash, &secret_key_3);
1403+
let signature_4 = secp_ctx.sign(&sighash, &secret_key_4);
1404+
let pubkey_1 = PublicKey::from_secret_key(&secp_ctx, &secret_key_1);
1405+
let pubkey_2 = PublicKey::from_secret_key(&secp_ctx, &secret_key_2);
1406+
let pubkey_3 = PublicKey::from_secret_key(&secp_ctx, &secret_key_3);
1407+
let pubkey_4 = PublicKey::from_secret_key(&secp_ctx, &secret_key_4);
1408+
let bitcoin_hash = Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap();
1409+
let features = GlobalFeatures::new();
1410+
let mut rgb = [0; 3];
1411+
rng.fill_bytes(&mut rgb);
1412+
let ipv4 = msgs::NetAddress::IPv4 {
1413+
addr: [127, 0, 0, 1],
1414+
port: 2
1415+
};
1416+
let ipv6 = msgs::NetAddress::IPv6 {
1417+
addr: [128, 91, 45, 157, 220, 40, 0, 0, 0, 0, 252, 87, 212, 200, 31, 255],
1418+
port: 2
1419+
};
1420+
let onionv2 = msgs::NetAddress::OnionV2 {
1421+
addr: [20, 122, 45, 90, 0, 0, 12, 245, 203, 10],
1422+
port: 2
1423+
};
1424+
//TODO: generate OnionV3
1425+
let addresses = vec![ipv4, ipv6, onionv2];
1426+
1427+
macro_rules! test_serialization {
1428+
($msg: expr, $type:ty) => {
1429+
let mut towire = VecWriter(Vec::new());
1430+
$msg.write(&mut towire).unwrap();
1431+
let mut r = ::std::io::Cursor::new(towire.0.as_slice());
1432+
if let Ok(fromwire) = <$type as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
1433+
let p = r.position() as usize;
1434+
assert_eq!(towire.0.len(), p);
1435+
assert_eq!(&r.into_inner()[..p], &towire.0[..p]);
1436+
assert_eq!(fromwire, $msg);
1437+
}
1438+
}
1439+
}
1440+
1441+
let announcement_signatures = msgs::AnnouncementSignatures {
1442+
channel_id: channel_id.clone(),
1443+
short_channel_id: rng.gen(),
1444+
node_signature: signature_1.clone(),
1445+
bitcoin_signature: signature_2.clone()
1446+
};
1447+
test_serialization!(announcement_signatures, msgs::AnnouncementSignatures);
1448+
1449+
let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
1450+
features: features.clone(),
1451+
chain_hash: bitcoin_hash.clone(),
1452+
short_channel_id: rng.gen(),
1453+
node_id_1: pubkey_1.clone(),
1454+
node_id_2: pubkey_2.clone(),
1455+
bitcoin_key_1: pubkey_3.clone(),
1456+
bitcoin_key_2: pubkey_4.clone(),
1457+
excess_data: Vec::new()
1458+
};
1459+
test_serialization!(unsigned_channel_announcement, msgs::UnsignedChannelAnnouncement);
1460+
1461+
let channel_announcement = msgs::ChannelAnnouncement {
1462+
node_signature_1: signature_1.clone(),
1463+
node_signature_2: signature_2.clone(),
1464+
bitcoin_signature_1: signature_3.clone(),
1465+
bitcoin_signature_2: signature_4.clone(),
1466+
contents: unsigned_channel_announcement
1467+
};
1468+
test_serialization!(channel_announcement, msgs::ChannelAnnouncement);
1469+
1470+
let unsigned_node_announcement = msgs::UnsignedNodeAnnouncement {
1471+
features: features.clone(),
1472+
timestamp: rng.gen(),
1473+
node_id: pubkey_1.clone(),
1474+
rgb,
1475+
alias: channel_id.clone(),
1476+
addresses,
1477+
excess_address_data: Vec::new(),
1478+
excess_data: Vec::new()
1479+
};
1480+
test_serialization!(unsigned_node_announcement, msgs::UnsignedNodeAnnouncement);
1481+
1482+
let node_announcement = msgs::NodeAnnouncement {
1483+
signature: signature_1.clone(),
1484+
contents: unsigned_node_announcement
1485+
};
1486+
test_serialization!(node_announcement, msgs::NodeAnnouncement);
1487+
1488+
let unsigned_channel_update = msgs::UnsignedChannelUpdate {
1489+
chain_hash: bitcoin_hash.clone(),
1490+
short_channel_id: rng.gen(),
1491+
timestamp: rng.gen(),
1492+
flags: rng.gen(),
1493+
cltv_expiry_delta: rng.gen(),
1494+
htlc_minimum_msat: rng.gen(),
1495+
fee_base_msat: rng.gen(),
1496+
fee_proportional_millionths: rng.gen(),
1497+
excess_data: Vec::new()
1498+
};
1499+
test_serialization!(unsigned_channel_update, msgs::UnsignedChannelUpdate);
1500+
1501+
let channel_update = msgs::ChannelUpdate {
1502+
signature: signature_1.clone(),
1503+
contents: unsigned_channel_update
1504+
};
1505+
test_serialization!(channel_update, msgs::ChannelUpdate);
1506+
}
13651507
}

0 commit comments

Comments
 (0)