Skip to content

Commit 282dac8

Browse files
author
Antoine Riard
committed
Add bolt2 msgs serialization tests
1 parent ba96899 commit 282dac8

File tree

1 file changed

+182
-3
lines changed

1 file changed

+182
-3
lines changed

src/ln/msgs.rs

Lines changed: 182 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,12 +1392,14 @@ impl_writeable_len_match!(NodeAnnouncement, {
13921392
mod tests {
13931393
use hex;
13941394
use ln::msgs;
1395-
use ln::msgs::GlobalFeatures;
1396-
use util::ser::{Writer, Writeable};
1395+
use ln::msgs::{GlobalFeatures, OptionalField};
1396+
use util::ser::Writeable;
13971397

13981398
use bitcoin::util::hash::Sha256dHash;
13991399
use bitcoin::util::address::Address;
14001400
use bitcoin::network::constants::Network;
1401+
use bitcoin::blockdata::script::Builder;
1402+
use bitcoin::blockdata::opcodes;
14011403

14021404
use secp256k1::key::{PublicKey,SecretKey};
14031405
use secp256k1::{Secp256k1, Message};
@@ -1443,7 +1445,7 @@ mod tests {
14431445
($slice: expr) => {
14441446
{
14451447
let secp_ctx = Secp256k1::new();
1446-
let privkey = SecretKey::from_slice(&secp_ctx, &hex::decode($slice).unwrap()[..]).unwrap();
1448+
let privkey = SecretKey::from_slice(&hex::decode($slice).unwrap()[..]).unwrap();
14471449
let pubkey = PublicKey::from_secret_key(&secp_ctx, &privkey);
14481450
(privkey, pubkey, secp_ctx)
14491451
}
@@ -1596,4 +1598,181 @@ mod tests {
15961598
do_encoding_channel_update(false, false, false, true);
15971599
do_encoding_channel_update(true, true, true, true);
15981600
}
1601+
1602+
fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
1603+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1604+
let (_, pubkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202");
1605+
let (_, pubkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303");
1606+
let (_, pubkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404");
1607+
let (_, pubkey_5, _) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505");
1608+
let (_, pubkey_6, _) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606");
1609+
let open_channel = msgs::OpenChannel {
1610+
chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1611+
temporary_channel_id: [2; 32],
1612+
funding_satoshis: 2316138423780173,
1613+
push_msat: 2316138423780173,
1614+
dust_limit_satoshis: 2316138423780173,
1615+
max_htlc_value_in_flight_msat: 2316138423780173,
1616+
channel_reserve_satoshis: 2316138423780173,
1617+
htlc_minimum_msat: 2316138423780173,
1618+
feerate_per_kw: 821716,
1619+
to_self_delay: 49340,
1620+
max_accepted_htlcs: 49340,
1621+
funding_pubkey: pubkey_1,
1622+
revocation_basepoint: pubkey_2,
1623+
payment_basepoint: pubkey_3,
1624+
delayed_payment_basepoint: pubkey_4,
1625+
htlc_basepoint: pubkey_5,
1626+
first_per_commitment_point: pubkey_6,
1627+
channel_flags: if random_bit { 1 << 5 } else { 0 },
1628+
shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1629+
};
1630+
let encoded_value = open_channel.encode();
1631+
let mut target_value = Vec::new();
1632+
if non_bitcoin_chain_hash {
1633+
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]);
1634+
} else {
1635+
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]);
1636+
}
1637+
target_value.append(&mut vec![2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 8, 58, 132, 0, 0, 3, 77, 0, 8, 58, 132, 0, 0, 3, 77, 0, 8, 58, 132, 0, 0, 3, 77, 0, 8, 58, 132, 0, 0, 3, 77, 0, 8, 58, 132, 0, 0, 3, 77, 0, 8, 58, 132, 0, 0, 3, 77, 0, 12, 137, 212, 192, 188, 192, 188, 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, 3, 98, 192, 160, 70, 218, 204, 232, 109, 221, 3, 67, 198, 211, 199, 199, 156, 34, 8, 186, 13, 156, 156, 242, 74, 109, 4, 109, 33, 210, 31, 144, 247, 3, 240, 6, 161, 141, 86, 83, 196, 237, 245, 57, 31, 242, 58, 97, 240, 63, 248, 61, 35, 126, 136, 14, 230, 17, 135, 250, 159, 55, 154, 2, 142, 10]);
1638+
if random_bit {
1639+
target_value.append(&mut vec![1 << 5]);
1640+
} else {
1641+
target_value.append(&mut vec![0]);
1642+
}
1643+
if shutdown {
1644+
target_value.append(&mut vec![0, 25, 118, 169, 20, 121, 176, 0, 136, 118, 38, 178, 148, 169, 20, 80, 26, 76, 210, 38, 181, 139, 35, 89, 131, 136, 172]);
1645+
}
1646+
assert_eq!(encoded_value, target_value);
1647+
}
1648+
1649+
#[test]
1650+
fn encoding_open_channel() {
1651+
do_encoding_open_channel(false, false, false);
1652+
do_encoding_open_channel(true, false, false);
1653+
do_encoding_open_channel(false, true, false);
1654+
do_encoding_open_channel(false, false, true);
1655+
do_encoding_open_channel(true, true, true);
1656+
}
1657+
1658+
fn do_encoding_accept_channel(shutdown: bool) {
1659+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1660+
let (_, pubkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202");
1661+
let (_, pubkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303");
1662+
let (_, pubkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404");
1663+
let (_, pubkey_5, _) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505");
1664+
let (_, pubkey_6, _) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606");
1665+
let accept_channel = msgs::AcceptChannel {
1666+
temporary_channel_id: [2; 32],
1667+
dust_limit_satoshis: 2316138423780173,
1668+
max_htlc_value_in_flight_msat: 2316138423780173,
1669+
channel_reserve_satoshis: 2316138423780173,
1670+
htlc_minimum_msat: 2316138423780173,
1671+
minimum_depth: 821716,
1672+
to_self_delay: 49340,
1673+
max_accepted_htlcs: 49340,
1674+
funding_pubkey: pubkey_1,
1675+
revocation_basepoint: pubkey_2,
1676+
payment_basepoint: pubkey_3,
1677+
delayed_payment_basepoint: pubkey_4,
1678+
htlc_basepoint: pubkey_5,
1679+
first_per_commitment_point: pubkey_6,
1680+
shutdown_scriptpubkey: if shutdown { OptionalField::Present(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { OptionalField::Absent }
1681+
};
1682+
let encoded_value = accept_channel.encode();
1683+
let mut target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* dust_limit_satoshis */ 0, 8, 58, 132, 0, 0, 3, 77, /* max_htlc_value_in_flight_msat */ 0, 8, 58, 132, 0, 0, 3, 77, /* channel_reserve_satoshis */ 0, 8, 58, 132, 0, 0, 3, 77, /* htlc_minimum_msat */ 0, 8, 58, 132, 0, 0, 3, 77, /* mimimum_depth */ 0, 12, 137, 212, /* to_self_delay */ 192, 188, /* max_accepted_htlcs */ 192, 188, /* funding_publey */ 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, /* revocation_basepoint */ 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, /* payment_basepoint */ 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, /* delayed_payment_basepoint */ 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, 3, 98, 192, 160, 70, 218, 204, 232, 109, 221, /* htlc_basepoint */ 3, 67, 198, 211, 199, 199, 156, 34, 8, 186, 13, 156, 156, 242, 74, 109, 4, 109, 33, 210, 31, 144, 247, /* first_per_commitment_point */ 3, 240, 6, 161, 141, 86, 83, 196, 237, 245, 57, 31, 242, 58, 97, 240, 63, 248, 61, 35, 126, 136, 14, 230, 17, 135, 250, 159, 55, 154, 2, 142, 10];
1684+
if shutdown {
1685+
target_value.append(&mut vec![0, 25, 118, 169, 20, 121, 176, 0, 136, 118, 38, 178, 148, 169, 20, 80, 26, 76, 210, 38, 181, 139, 35, 89, 131, 136, 172]);
1686+
}
1687+
assert_eq!(encoded_value, target_value);
1688+
}
1689+
1690+
#[test]
1691+
fn encoding_accept_channel() {
1692+
do_encoding_accept_channel(false);
1693+
do_encoding_accept_channel(true);
1694+
}
1695+
1696+
#[test]
1697+
fn encoding_funding_created() {
1698+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1699+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1700+
let funding_created = msgs::FundingCreated {
1701+
temporary_channel_id: [2; 32],
1702+
funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
1703+
funding_output_index: 255,
1704+
signature: sig_1,
1705+
};
1706+
let encoded_value = funding_created.encode();
1707+
let target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* funding_txid */ 110, 150, 254, 159, 139, 13, 220, 215, 41, 186, 3, 207, 175, 165, 162, 123, 5, 11, 57, 211, 84, 221, 152, 8, 20, 38, 141, 250, 154, 68, 212, 194, /* funding_output_index */ 0, 255, /* signature */ 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];
1708+
assert_eq!(encoded_value, target_value);
1709+
}
1710+
1711+
#[test]
1712+
fn encoding_funding_signed() {
1713+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1714+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1715+
let funding_signed = msgs::FundingSigned {
1716+
channel_id: [2; 32],
1717+
signature: sig_1,
1718+
};
1719+
let encoded_value = funding_signed.encode();
1720+
let target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* signature */ 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];
1721+
assert_eq!(encoded_value, target_value);
1722+
}
1723+
1724+
#[test]
1725+
fn encoding_funding_locked() {
1726+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1727+
let funding_locked = msgs::FundingLocked {
1728+
channel_id: [2; 32],
1729+
next_per_commitment_point: pubkey_1,
1730+
};
1731+
let encoded_value = funding_locked.encode();
1732+
let target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* next_per_commitment_point */ 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];
1733+
assert_eq!(encoded_value, target_value);
1734+
}
1735+
1736+
fn do_encoding_shutdown(script_type: u8) {
1737+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1738+
let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1739+
let shutdown = msgs::Shutdown {
1740+
channel_id: [2; 32],
1741+
scriptpubkey: if script_type == 1 { Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey() } else if script_type == 2 { Address::p2sh(&script, Network::Testnet).script_pubkey() } else if script_type == 3 { Address::p2wpkh(&pubkey_1, Network::Testnet).script_pubkey() } else { Address::p2wsh(&script, Network::Testnet).script_pubkey() },
1742+
};
1743+
let encoded_value = shutdown.encode();
1744+
let mut target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2];
1745+
if script_type == 1 {
1746+
target_value.append(&mut vec![/* len */ 0, 25, /* P2PKH */ 118, 169, 20, 121, 176, 0, 136, 118, 38, 178, 148, 169, 20, 80, 26, 76, 210, 38, 181, 139, 35, 89, 131, 136, 172]);
1747+
} else if script_type == 2 {
1748+
target_value.append(&mut vec![/* len */ 0, 23, /* P2SH */ 169, 20, 218, 23, 69, 233, 181, 73, 189, 11, 250, 26, 86, 153, 113, 199, 126, 186, 48, 205, 90, 75, 135]);
1749+
} else if script_type == 3 {
1750+
target_value.append(&mut vec![/* len */ 0, 22, /* P2WPKH */ 0, 20, 121, 176, 0, 136, 118, 38, 178, 148, 169, 20, 80, 26, 76, 210, 38, 181, 139, 35, 89, 131]);
1751+
} else if script_type == 4 {
1752+
target_value.append(&mut vec![/* len */ 0, 34, /* P2WPKH */ 0, 32, 74, 232, 21, 114, 240, 110, 27, 136, 253, 92, 237, 122, 26, 0, 9, 69, 67, 46, 131, 225, 85, 30, 111, 114, 30, 233, 192, 11, 140, 195, 50, 96]);
1753+
}
1754+
assert_eq!(encoded_value, target_value);
1755+
}
1756+
1757+
#[test]
1758+
fn encoding_shutdown() {
1759+
do_encoding_shutdown(1);
1760+
do_encoding_shutdown(2);
1761+
do_encoding_shutdown(3);
1762+
do_encoding_shutdown(4);
1763+
}
1764+
1765+
#[test]
1766+
fn encoding_closing_signed() {
1767+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1768+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1769+
let closing_signed = msgs::ClosingSigned {
1770+
channel_id: [2; 32],
1771+
fee_satoshis: 2316138423780173,
1772+
signature: sig_1,
1773+
};
1774+
let encoded_value = closing_signed.encode();
1775+
let target_value = vec![/* temporary_channel_id */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* fee_satoshis */ 0, 8, 58, 132, 0, 0, 3, 77, /* signature */ 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];
1776+
assert_eq!(encoded_value, target_value);
1777+
}
15991778
}

0 commit comments

Comments
 (0)