Skip to content

Commit 5500c61

Browse files
author
Yuko Roodt
committed
Added tests to check the bolt 2 specs for Sending Node Channel
1 parent 4a4cdc1 commit 5500c61

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/ln/functional_tests.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use chain::transaction::OutPoint;
77
use chain::chaininterface::{ChainListener, ChainWatchInterface};
88
use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor};
99
use chain::keysinterface;
10-
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
10+
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC, BREAKDOWN_TIMEOUT};
1111
use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash};
1212
use ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdateErr, CLTV_CLAIM_BUFFER, HTLC_FAIL_TIMEOUT_BLOCKS, ManyChannelMonitor};
1313
use ln::channel::{ACCEPTED_HTLC_SCRIPT_WEIGHT, OFFERED_HTLC_SCRIPT_WEIGHT};
@@ -6716,6 +6716,68 @@ fn test_onion_failure() {
67166716
}, ||{}, true, Some(21), None);
67176717
}
67186718

6719+
#[test]
6720+
#[should_panic]
6721+
fn bolt2_open_channel_sending_node_checks_part1() { //This test needs to be on its own as we are catching a panic
6722+
let nodes = create_network(2);
6723+
//Force duplicate channel ids
6724+
for node in nodes.iter() {
6725+
*node.keys_manager.override_channel_id_priv.lock().unwrap() = Some([0; 32]);
6726+
}
6727+
6728+
// BOLT #2 spec: Sending node must ensure temporary_channel_id is unique from any other channel ID with the same peer.
6729+
let channel_value_satoshis=10000;
6730+
let push_msat=10001;
6731+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).unwrap();
6732+
let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
6733+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &node0_to_1_send_open_channel).unwrap();
6734+
6735+
//Create a second channel with a channel_id collision
6736+
assert!(nodes[0].node.create_channel(nodes[0].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
6737+
}
6738+
6739+
#[test]
6740+
fn bolt2_open_channel_sending_node_checks_part2() {
6741+
let nodes = create_network(2);
6742+
6743+
// BOLT #2 spec: Sending node must set funding_satoshis to less than 2^24 satoshis
6744+
let channel_value_satoshis=2^24;
6745+
let push_msat=10001;
6746+
assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
6747+
6748+
// BOLT #2 spec: Sending node must set push_msat to equal or less than 1000 * funding_satoshis
6749+
let channel_value_satoshis=10000;
6750+
// Test when push_msat is equal to 1000 * funding_satoshis.
6751+
let push_msat=1000*channel_value_satoshis+1;
6752+
assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_err());
6753+
6754+
// BOLT #2 spec: Sending node must set set channel_reserve_satoshis greater than or equal to dust_limit_satoshis
6755+
let channel_value_satoshis=10000;
6756+
let push_msat=10001;
6757+
assert!(nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), channel_value_satoshis, push_msat, 42).is_ok()); //Create a valid channel
6758+
let node0_to_1_send_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
6759+
assert!(node0_to_1_send_open_channel.channel_reserve_satoshis>=node0_to_1_send_open_channel.dust_limit_satoshis);
6760+
6761+
// BOLT #2 spec: Sending node must set undefined bits in channel_flags to 0
6762+
// Only the least-significant bit of channel_flags is currently defined resulting in channel_flags only having one of two possible states 0 or 1
6763+
assert!(node0_to_1_send_open_channel.channel_flags<=1);
6764+
6765+
// BOLT #2 spec: Sending node should set to_self_delay sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehaviour by the receiver.
6766+
assert!(BREAKDOWN_TIMEOUT>0);
6767+
assert!(node0_to_1_send_open_channel.to_self_delay==BREAKDOWN_TIMEOUT);
6768+
6769+
// BOLT #2 spec: Sending node must ensure the chain_hash value identifies the chain it wishes to open the channel within.
6770+
let chain_hash=genesis_block(Network::Testnet).header.bitcoin_hash();
6771+
assert_eq!(node0_to_1_send_open_channel.chain_hash,chain_hash);
6772+
6773+
// BOLT #2 spec: Sending node must set funding_pubkey, revocation_basepoint, htlc_basepoint, payment_basepoint, and delayed_payment_basepoint to valid DER-encoded, compressed, secp256k1 pubkeys.
6774+
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.funding_pubkey.serialize()).is_ok());
6775+
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.revocation_basepoint.serialize()).is_ok());
6776+
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.htlc_basepoint.serialize()).is_ok());
6777+
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.payment_basepoint.serialize()).is_ok());
6778+
assert!(PublicKey::from_slice(&node0_to_1_send_open_channel.delayed_payment_basepoint.serialize()).is_ok());
6779+
}
6780+
67196781
// BOLT 2 Requirements for the Sender when constructing and sending an update_add_htlc message.
67206782
// BOLT 2 Requirement: MUST NOT offer amount_msat it cannot pay for in the remote commitment transaction at the current feerate_per_kw (see "Updating Fees") while maintaining its channel reserve.
67216783
//TODO: I don't believe this is explicitly enforced when sending an HTLC but as the Fee aspect of the BOLT specs is in flux leaving this as a TODO.

0 commit comments

Comments
 (0)