Skip to content

Commit eaa82a2

Browse files
author
Antoine Riard
committed
Add bolt2 msgs serialization tests
1 parent d66e387 commit eaa82a2

File tree

1 file changed

+180
-1
lines changed

1 file changed

+180
-1
lines changed

src/ln/msgs.rs

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,11 +1328,13 @@ mod tests {
13281328
use hex;
13291329
use ln::msgs;
13301330
use ln::msgs::GlobalFeatures;
1331-
use util::ser::{Writer, Writeable};
1331+
use util::ser::Writeable;
13321332

13331333
use bitcoin::util::hash::Sha256dHash;
13341334
use bitcoin::util::address::Address;
13351335
use bitcoin::network::constants::Network;
1336+
use bitcoin::blockdata::script::Builder;
1337+
use bitcoin::blockdata::opcodes;
13361338

13371339
use secp256k1::key::{PublicKey,SecretKey};
13381340
use secp256k1::{Secp256k1, Message};
@@ -1531,4 +1533,181 @@ mod tests {
15311533
do_encoding_channel_update(false, false, false, true);
15321534
do_encoding_channel_update(true, true, true, true);
15331535
}
1536+
1537+
fn do_encoding_open_channel(non_bitcoin_chain_hash: bool, random_bit: bool, shutdown: bool) {
1538+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1539+
let (_, pubkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202");
1540+
let (_, pubkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303");
1541+
let (_, pubkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404");
1542+
let (_, pubkey_5, _) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505");
1543+
let (_, pubkey_6, _) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606");
1544+
let open_channel = msgs::OpenChannel {
1545+
chain_hash: if !non_bitcoin_chain_hash { Sha256dHash::from_hex("6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000").unwrap() } else { Sha256dHash::from_hex("000000000933ea01ad0ee984209779baaec3ced90fa3f408719526f8d77f4943").unwrap() },
1546+
temporary_channel_id: [2; 32],
1547+
funding_satoshis: 2316138423780173,
1548+
push_msat: 2316138423780173,
1549+
dust_limit_satoshis: 2316138423780173,
1550+
max_htlc_value_in_flight_msat: 2316138423780173,
1551+
channel_reserve_satoshis: 2316138423780173,
1552+
htlc_minimum_msat: 2316138423780173,
1553+
feerate_per_kw: 821716,
1554+
to_self_delay: 49340,
1555+
max_accepted_htlcs: 49340,
1556+
funding_pubkey: pubkey_1,
1557+
revocation_basepoint: pubkey_2,
1558+
payment_basepoint: pubkey_3,
1559+
delayed_payment_basepoint: pubkey_4,
1560+
htlc_basepoint: pubkey_5,
1561+
first_per_commitment_point: pubkey_6,
1562+
channel_flags: if random_bit { 1 << 5 } else { 0 },
1563+
shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { None }
1564+
};
1565+
let encoded_value = open_channel.encode();
1566+
let mut target_value = Vec::new();
1567+
if non_bitcoin_chain_hash {
1568+
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]);
1569+
} else {
1570+
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]);
1571+
}
1572+
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]);
1573+
if random_bit {
1574+
target_value.append(&mut vec![1 << 5]);
1575+
} else {
1576+
target_value.append(&mut vec![0]);
1577+
}
1578+
if shutdown {
1579+
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]);
1580+
}
1581+
assert_eq!(encoded_value, target_value);
1582+
}
1583+
1584+
#[test]
1585+
fn encoding_open_channel() {
1586+
do_encoding_open_channel(false, false, false);
1587+
do_encoding_open_channel(true, false, false);
1588+
do_encoding_open_channel(false, true, false);
1589+
do_encoding_open_channel(false, false, true);
1590+
do_encoding_open_channel(true, true, true);
1591+
}
1592+
1593+
fn do_encoding_accept_channel(shutdown: bool) {
1594+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1595+
let (_, pubkey_2, _) = get_keys_from!("0202020202020202020202020202020202020202020202020202020202020202");
1596+
let (_, pubkey_3, _) = get_keys_from!("0303030303030303030303030303030303030303030303030303030303030303");
1597+
let (_, pubkey_4, _) = get_keys_from!("0404040404040404040404040404040404040404040404040404040404040404");
1598+
let (_, pubkey_5, _) = get_keys_from!("0505050505050505050505050505050505050505050505050505050505050505");
1599+
let (_, pubkey_6, _) = get_keys_from!("0606060606060606060606060606060606060606060606060606060606060606");
1600+
let accept_channel = msgs::AcceptChannel {
1601+
temporary_channel_id: [2; 32],
1602+
dust_limit_satoshis: 2316138423780173,
1603+
max_htlc_value_in_flight_msat: 2316138423780173,
1604+
channel_reserve_satoshis: 2316138423780173,
1605+
htlc_minimum_msat: 2316138423780173,
1606+
minimum_depth: 821716,
1607+
to_self_delay: 49340,
1608+
max_accepted_htlcs: 49340,
1609+
funding_pubkey: pubkey_1,
1610+
revocation_basepoint: pubkey_2,
1611+
payment_basepoint: pubkey_3,
1612+
delayed_payment_basepoint: pubkey_4,
1613+
htlc_basepoint: pubkey_5,
1614+
first_per_commitment_point: pubkey_6,
1615+
shutdown_scriptpubkey: if shutdown { Some(Address::p2pkh(&pubkey_1, Network::Testnet).script_pubkey()) } else { None }
1616+
};
1617+
let encoded_value = accept_channel.encode();
1618+
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];
1619+
if shutdown {
1620+
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]);
1621+
}
1622+
assert_eq!(encoded_value, target_value);
1623+
}
1624+
1625+
#[test]
1626+
fn encoding_accept_channel() {
1627+
do_encoding_accept_channel(false);
1628+
do_encoding_accept_channel(true);
1629+
}
1630+
1631+
#[test]
1632+
fn encoding_funding_created() {
1633+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1634+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1635+
let funding_created = msgs::FundingCreated {
1636+
temporary_channel_id: [2; 32],
1637+
funding_txid: Sha256dHash::from_hex("c2d4449afa8d26140898dd54d3390b057ba2a5afcf03ba29d7dc0d8b9ffe966e").unwrap(),
1638+
funding_output_index: 255,
1639+
signature: sig_1,
1640+
};
1641+
let encoded_value = funding_created.encode();
1642+
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];
1643+
assert_eq!(encoded_value, target_value);
1644+
}
1645+
1646+
#[test]
1647+
fn encoding_funding_signed() {
1648+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1649+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1650+
let funding_signed = msgs::FundingSigned {
1651+
channel_id: [2; 32],
1652+
signature: sig_1,
1653+
};
1654+
let encoded_value = funding_signed.encode();
1655+
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];
1656+
assert_eq!(encoded_value, target_value);
1657+
}
1658+
1659+
#[test]
1660+
fn encoding_funding_locked() {
1661+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1662+
let funding_locked = msgs::FundingLocked {
1663+
channel_id: [2; 32],
1664+
next_per_commitment_point: pubkey_1,
1665+
};
1666+
let encoded_value = funding_locked.encode();
1667+
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];
1668+
assert_eq!(encoded_value, target_value);
1669+
}
1670+
1671+
fn do_encoding_shutdown(script_type: u8) {
1672+
let (_, pubkey_1, _) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1673+
let script = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
1674+
let shutdown = msgs::Shutdown {
1675+
channel_id: [2; 32],
1676+
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() },
1677+
};
1678+
let encoded_value = shutdown.encode();
1679+
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];
1680+
if script_type == 1 {
1681+
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]);
1682+
} else if script_type == 2 {
1683+
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]);
1684+
} else if script_type == 3 {
1685+
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]);
1686+
} else if script_type == 4 {
1687+
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]);
1688+
}
1689+
assert_eq!(encoded_value, target_value);
1690+
}
1691+
1692+
#[test]
1693+
fn encoding_shutdown() {
1694+
do_encoding_shutdown(1);
1695+
do_encoding_shutdown(2);
1696+
do_encoding_shutdown(3);
1697+
do_encoding_shutdown(4);
1698+
}
1699+
1700+
#[test]
1701+
fn encoding_closing_signed() {
1702+
let (privkey_1, _, secp_ctx_1) = get_keys_from!("0101010101010101010101010101010101010101010101010101010101010101");
1703+
let sig_1 = get_sig_on!(privkey_1, secp_ctx_1, String::from("01010101010101010101010101010101"));
1704+
let closing_signed = msgs::ClosingSigned {
1705+
channel_id: [2; 32],
1706+
fee_satoshis: 2316138423780173,
1707+
signature: sig_1,
1708+
};
1709+
let encoded_value = closing_signed.encode();
1710+
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];
1711+
assert_eq!(encoded_value, target_value);
1712+
}
15341713
}

0 commit comments

Comments
 (0)