Skip to content

Commit ba96899

Browse files
author
Antoine Riard
committed
Add bolt7 msgs serialization tests
1 parent 5764634 commit ba96899

File tree

1 file changed

+178
-11
lines changed

1 file changed

+178
-11
lines changed

src/ln/msgs.rs

Lines changed: 178 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ impl LocalFeatures {
114114
}
115115

116116
/// Tracks globalfeatures which are in init messages and routing announcements
117-
#[derive(Clone, PartialEq)]
117+
#[derive(Clone, PartialEq, Debug)]
118118
pub struct GlobalFeatures {
119+
#[cfg(not(test))]
119120
flags: Vec<u8>,
121+
// Used to test encoding of diverse msgs
122+
#[cfg(test)]
123+
pub flags: Vec<u8>
120124
}
121125

122126
impl GlobalFeatures {
@@ -326,7 +330,7 @@ pub struct ChannelReestablish {
326330
}
327331

328332
/// An announcement_signatures message to be sent or received from a peer
329-
#[derive(Clone)]
333+
#[derive(PartialEq, Clone, Debug)]
330334
pub struct AnnouncementSignatures {
331335
pub(crate) channel_id: [u8; 32],
332336
pub(crate) short_channel_id: u64,
@@ -335,7 +339,7 @@ pub struct AnnouncementSignatures {
335339
}
336340

337341
/// An address which can be used to connect to a remote peer
338-
#[derive(PartialEq, Clone)]
342+
#[derive(Clone, PartialEq, Debug)]
339343
pub enum NetAddress {
340344
/// An IPv4 address/port on which the peer is listening.
341345
IPv4 {
@@ -458,9 +462,9 @@ impl<R: ::std::io::Read> Readable<R> for Result<NetAddress, u8> {
458462
}
459463
}
460464

461-
#[derive(PartialEq, Clone)]
462465
// Only exposed as broadcast of node_announcement should be filtered by node_id
463466
/// The unsigned part of a node_announcement
467+
#[derive(PartialEq, Clone, Debug)]
464468
pub struct UnsignedNodeAnnouncement {
465469
pub(crate) features: GlobalFeatures,
466470
pub(crate) timestamp: u32,
@@ -484,7 +488,7 @@ pub struct NodeAnnouncement {
484488

485489
// Only exposed as broadcast of channel_announcement should be filtered by node_id
486490
/// The unsigned part of a channel_announcement
487-
#[derive(PartialEq, Clone)]
491+
#[derive(PartialEq, Clone, Debug)]
488492
pub struct UnsignedChannelAnnouncement {
489493
pub(crate) features: GlobalFeatures,
490494
pub(crate) chain_hash: Sha256dHash,
@@ -498,7 +502,7 @@ pub struct UnsignedChannelAnnouncement {
498502
pub(crate) excess_data: Vec<u8>,
499503
}
500504
/// A channel_announcement message to be sent or received from a peer
501-
#[derive(PartialEq, Clone)]
505+
#[derive(PartialEq, Clone, Debug)]
502506
pub struct ChannelAnnouncement {
503507
pub(crate) node_signature_1: Signature,
504508
pub(crate) node_signature_2: Signature,
@@ -507,7 +511,7 @@ pub struct ChannelAnnouncement {
507511
pub(crate) contents: UnsignedChannelAnnouncement,
508512
}
509513

510-
#[derive(PartialEq, Clone)]
514+
#[derive(PartialEq, Clone, Debug)]
511515
pub(crate) struct UnsignedChannelUpdate {
512516
pub(crate) chain_hash: Sha256dHash,
513517
pub(crate) short_channel_id: u64,
@@ -520,7 +524,7 @@ pub(crate) struct UnsignedChannelUpdate {
520524
pub(crate) excess_data: Vec<u8>,
521525
}
522526
/// A channel_update message to be sent or received from a peer
523-
#[derive(PartialEq, Clone)]
527+
#[derive(PartialEq, Clone, Debug)]
524528
pub struct ChannelUpdate {
525529
pub(crate) signature: Signature,
526530
pub(crate) contents: UnsignedChannelUpdate,
@@ -1388,10 +1392,15 @@ impl_writeable_len_match!(NodeAnnouncement, {
13881392
mod tests {
13891393
use hex;
13901394
use ln::msgs;
1391-
use ln::msgs::OptionalField;
1392-
use util::ser::Writeable;
1395+
use ln::msgs::GlobalFeatures;
1396+
use util::ser::{Writer, Writeable};
1397+
1398+
use bitcoin::util::hash::Sha256dHash;
1399+
use bitcoin::util::address::Address;
1400+
use bitcoin::network::constants::Network;
1401+
13931402
use secp256k1::key::{PublicKey,SecretKey};
1394-
use secp256k1::Secp256k1;
1403+
use secp256k1::{Secp256k1, Message};
13951404

13961405
#[test]
13971406
fn encoding_channel_reestablish_no_secret() {
@@ -1429,4 +1438,162 @@ mod tests {
14291438
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]
14301439
);
14311440
}
1441+
1442+
macro_rules! get_keys_from {
1443+
($slice: expr) => {
1444+
{
1445+
let secp_ctx = Secp256k1::new();
1446+
let privkey = SecretKey::from_slice(&secp_ctx, &hex::decode($slice).unwrap()[..]).unwrap();
1447+
let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
1448+
(privkey, pubkey, secp_ctx)
1449+
}
1450+
}
1451+
}
1452+
1453+
macro_rules! get_sig_on {
1454+
($privkey: expr, $ctx: expr, $string: expr) => {
1455+
{
1456+
let sighash = Message::from_slice(&$string.into_bytes()[..]).unwrap();
1457+
$ctx.sign(&sighash, &$privkey)
1458+
}
1459+
}
1460+
}
1461+
1462+
#[test]
1463+
fn encoding_announcement_signatures() {
1464+
let (privkey, _, secp_ctx) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1465+
let sig_1 = get_sig_on!(privkey, secp_ctx, String::from("01010101010101010101010101010101"));
1466+
let sig_2 = get_sig_on!(privkey, secp_ctx, String::from("02020202020202020202020202020202"));
1467+
let announcement_signatures = msgs::AnnouncementSignatures {
1468+
channel_id: [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],
1469+
short_channel_id: 2316138423780173,
1470+
node_signature: sig_1,
1471+
bitcoin_signature: sig_2,
1472+
};
1473+
1474+
let encoded_value = announcement_signatures.encode();
1475+
assert_eq!(
1476+
encoded_value,
1477+
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, 8, 58, 132, 0, 0, 3, 77, 217, 119, 203, 155, 83, 217, 58, 111, 246, 75, 181, 241, 225, 88, 180, 9, 75, 102, 231, 152, 251, 18, 145, 17, 104, 163, 204, 223, 128, 168, 48, 150, 52, 10, 106, 149, 218, 10, 232, 217, 247, 118, 82, 142, 236, 219, 183, 71, 235, 107, 84, 84, 149, 164, 49, 158, 213, 55, 142, 53, 178, 30, 7, 58, 207, 153, 83, 206, 244, 112, 8, 96, 245, 150, 120, 56, 235, 162, 186, 232, 146, 136, 173, 24, 142, 191, 139, 32, 191, 153, 92, 62, 165, 58, 38, 223, 24, 118, 208, 163, 160, 225, 49, 114, 186, 40, 106, 103, 49, 64, 25, 12, 2, 186, 157, 166, 10, 46, 67, 167, 69, 24, 140, 138, 131, 199, 243, 239]);
1478+
}
1479+
1480+
fn do_encoding_channel_announcement(unknown_features_bits: bool, non_bitcoin_chain_hash: bool, excess_data: bool) {
1481+
let (privkey_1, pubkey_1, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1482+
let (privkey_2, pubkey_2, secp_ctx_2) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202");
1483+
let (privkey_3, pubkey_3, secp_ctx_3) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303");
1484+
let (privkey_4, pubkey_4, secp_ctx_4) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404");
1485+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1486+
let sig_2 = get_sig_on!(privkey_2, secp_ctx_2, String::from("01010101010101010101010101010101"));
1487+
let sig_3 = get_sig_on!(privkey_3, secp_ctx_3, String::from("01010101010101010101010101010101"));
1488+
let sig_4 = get_sig_on!(privkey_4, secp_ctx_4, String::from("01010101010101010101010101010101"));
1489+
let mut features = GlobalFeatures::new();
1490+
if unknown_features_bits {
1491+
features.flags = vec![0xFF, 0xFF];
1492+
}
1493+
let unsigned_channel_announcement = msgs::UnsignedChannelAnnouncement {
1494+
features,
1495+
chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1496+
short_channel_id: 2316138423780173,
1497+
node_id_1: pubkey_1,
1498+
node_id_2: pubkey_2,
1499+
bitcoin_key_1: pubkey_3,
1500+
bitcoin_key_2: pubkey_4,
1501+
excess_data: if excess_data { vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40] } else { Vec::new() },
1502+
};
1503+
let channel_announcement = msgs::ChannelAnnouncement {
1504+
node_signature_1: sig_1,
1505+
node_signature_2: sig_2,
1506+
bitcoin_signature_1: sig_3,
1507+
bitcoin_signature_2: sig_4,
1508+
contents: unsigned_channel_announcement,
1509+
};
1510+
let encoded_value = channel_announcement.encode();
1511+
let mut target_value = vec![217, 119, 203, 155, 83, 217, 58, 111, 246, 75, 181, 241, 225, 88, 180, 9, 75, 102, 231, 152, 251, 18, 145, 17, 104, 163, 204, 223, 128, 168, 48, 150, 52, 10, 106, 149, 218, 10, 232, 217, 247, 118, 82, 142, 236, 219, 183, 71, 235, 107, 84, 84, 149, 164, 49, 158, 213, 55, 142, 53, 178, 30, 7, 58, 23, 53, 182, 164, 39, 232, 13, 95, 231, 205, 144, 162, 244, 238, 8, 220, 156, 39, 205, 167, 195, 90, 65, 114, 229, 216, 91, 18, 196, 157, 66, 50, 83, 126, 152, 249, 177, 243, 197, 230, 152, 154, 139, 150, 68, 233, 14, 137, 24, 18, 118, 128, 219, 208, 212, 4, 53, 16, 132, 15, 192, 241, 225, 26, 33, 108, 40, 11, 83, 149, 162, 84, 110, 126, 75, 38, 99, 224, 79, 129, 22, 34, 241, 90, 79, 145, 232, 58, 162, 233, 43, 162, 165, 115, 193, 57, 20, 44, 84, 174, 99, 7, 42, 30, 193, 238, 125, 192, 192, 75, 222, 92, 132, 120, 6, 23, 42, 160, 92, 146, 194, 42, 232, 227, 8, 209, 210, 105, 43, 18, 204, 25, 92, 224, 162, 209, 189, 166, 168, 139, 239, 161, 159, 160, 127, 81, 202, 167, 92, 232, 56, 55, 242, 137, 101, 96, 11, 138, 172, 171, 8, 85, 255, 176, 231, 65, 236, 95, 124, 65, 66, 30, 152, 41, 169, 212, 134, 17, 200, 200, 49, 247, 27, 229, 234, 115, 230, 101, 148, 151, 127, 253];
1512+
if unknown_features_bits {
1513+
target_value.append(&mut vec![0, 2, 255, 255]);
1514+
} else {
1515+
target_value.append(&mut vec![0, 0]);
1516+
}
1517+
if non_bitcoin_chain_hash {
1518+
target_value.append(&mut vec![67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0]);
1519+
} else {
1520+
target_value.append(&mut vec![0, 0, 0, 0, 0, 25, 214, 104, 156, 8, 90, 225, 101, 131, 30, 147, 79, 247, 99, 174, 70, 162, 166, 193, 114, 179, 241, 182, 10, 140, 226, 111]);
1521+
}
1522+
target_value.append(&mut vec![0, 8, 58, 132, 0, 0, 3, 77, 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, 2, 77, 75, 108, 209, 54, 16, 50, 202, 155, 210, 174, 185, 217, 0, 170, 77, 69, 217, 234, 216, 10, 201, 66, 51, 116, 196, 81, 167, 37, 77, 7, 102, 2, 83, 31, 230, 6, 129, 52, 80, 61, 39, 35, 19, 50, 39, 200, 103, 172, 143, 166, 200, 60, 83, 126, 154, 68, 195, 197, 189, 189, 203, 31, 227, 55, 3, 70, 39, 121, 173, 74, 173, 57, 81, 70, 20, 117, 26, 113, 8, 95, 47, 16, 225, 199, 165, 147, 228, 224, 48, 239, 181, 184, 114, 28, 229, 91, 11]);
1523+
if excess_data {
1524+
target_value.append(&mut vec![10, 0, 0, 20, 0, 0, 30, 0, 0, 40]);
1525+
}
1526+
assert_eq!(encoded_value, target_value);
1527+
}
1528+
1529+
#[test]
1530+
fn encoding_channel_announcement() {
1531+
do_encoding_channel_announcement(false, false, false);
1532+
do_encoding_channel_announcement(true, false, false);
1533+
do_encoding_channel_announcement(false, true, false);
1534+
do_encoding_channel_announcement(false, false, true);
1535+
do_encoding_channel_announcement(true, true, true);
1536+
}
1537+
1538+
#[test]
1539+
fn encoding_node_announcement() {
1540+
//TODO:
1541+
}
1542+
1543+
fn do_encoding_channel_update(non_bitcoin_chain_hash: bool, direction: bool, disable: bool, htlc_maximum_msat: bool) {
1544+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1545+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1546+
let unsigned_channel_update = msgs::UnsignedChannelUpdate {
1547+
chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1548+
short_channel_id: 2316138423780173,
1549+
timestamp: 20190119,
1550+
flags: if direction { 1 } else { 0 } | if disable { 1 << 1 } else { 0 } | if htlc_maximum_msat { 1 << 8 } else { 0 },
1551+
cltv_expiry_delta: 144,
1552+
htlc_minimum_msat: 1000000,
1553+
fee_base_msat: 10000,
1554+
fee_proportional_millionths: 20,
1555+
excess_data: if htlc_maximum_msat { vec![0, 0, 0, 0, 59, 154, 202, 0] } else { Vec::new() }
1556+
};
1557+
let channel_update = msgs::ChannelUpdate {
1558+
signature: sig_1,
1559+
contents: unsigned_channel_update
1560+
};
1561+
let encoded_value = channel_update.encode();
1562+
let mut target_value = vec![217, 119, 203, 155, 83, 217, 58, 111, 246, 75, 181, 241, 225, 88, 180, 9, 75, 102, 231, 152, 251, 18, 145, 17, 104, 163, 204, 223, 128, 168, 48, 150, 52, 10, 106, 149, 218, 10, 232, 217, 247, 118, 82, 142, 236, 219, 183, 71, 235, 107, 84, 84, 149, 164, 49, 158, 213, 55, 142, 53, 178, 30, 7, 58];
1563+
if non_bitcoin_chain_hash {
1564+
target_value.append(&mut vec![67, 73, 127, 215, 248, 38, 149, 113, 8, 244, 163, 15, 217, 206, 195, 174, 186, 121, 151, 32, 132, 233, 14, 173, 1, 234, 51, 9, 0, 0, 0, 0]);
1565+
} else {
1566+
target_value.append(&mut vec![0, 0, 0, 0, 0, 25, 214, 104, 156, 8, 90, 225, 101, 131, 30, 147, 79, 247, 99, 174, 70, 162, 166, 193, 114, 179, 241, 182, 10, 140, 226, 111]);
1567+
}
1568+
target_value.append(&mut vec![0, 8, 58, 132, 0, 0, 3, 77, 1, 52, 19, 167]);
1569+
if htlc_maximum_msat {
1570+
target_value.append(&mut vec![1]);
1571+
} else {
1572+
target_value.append(&mut vec![0]);
1573+
}
1574+
target_value.append(&mut vec![0]);
1575+
if direction {
1576+
let flag = target_value.last_mut().unwrap();
1577+
*flag = 1;
1578+
}
1579+
if disable {
1580+
let flag = target_value.last_mut().unwrap();
1581+
*flag = *flag | 1 << 1;
1582+
}
1583+
target_value.append(&mut vec![0, 144, 0, 0, 0, 0, 0, 15, 66, 64, 0, 0, 39, 16, 0, 0, 0, 20]);
1584+
if htlc_maximum_msat {
1585+
target_value.append(&mut vec![0, 0, 0, 0, 59, 154, 202, 0]);
1586+
}
1587+
assert_eq!(encoded_value, target_value);
1588+
}
1589+
1590+
#[test]
1591+
fn encoding_channel_update() {
1592+
do_encoding_channel_update(false, false, false, false);
1593+
do_encoding_channel_update(true, false, false, false);
1594+
do_encoding_channel_update(false, true, false, false);
1595+
do_encoding_channel_update(false, false, true, false);
1596+
do_encoding_channel_update(false, false, false, true);
1597+
do_encoding_channel_update(true, true, true, true);
1598+
}
14321599
}

0 commit comments

Comments
 (0)