Skip to content

Commit

Permalink
Add handle unkown peer test
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorTigerstrom committed Jun 14, 2022
1 parent 4991456 commit ae665ce
Showing 1 changed file with 210 additions and 1 deletion.
211 changes: 210 additions & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7363,6 +7363,12 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
mod tests {
use bitcoin::hashes::Hash;
use bitcoin::hashes::sha256::Hash as Sha256;
use bitcoin::hashes::hex::FromHex;
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
use bitcoin::secp256k1::ecdsa::Signature;
use bitcoin::secp256k1::ffi::Signature as FFISignature;
use bitcoin::blockdata::script::Script;
use bitcoin::Txid;
use core::time::Duration;
use core::sync::atomic::Ordering;
use ln::{PaymentPreimage, PaymentHash, PaymentSecret};
Expand All @@ -7371,7 +7377,7 @@ mod tests {
use ln::features::InitFeatures;
use ln::functional_test_utils::*;
use ln::msgs;
use ln::msgs::ChannelMessageHandler;
use ln::msgs::{ChannelMessageHandler, OptionalField};
use routing::router::{PaymentParameters, RouteParameters, find_route};
use util::errors::APIError;
use util::events::{Event, MessageSendEvent, MessageSendEventsProvider};
Expand Down Expand Up @@ -7980,6 +7986,209 @@ mod tests {
nodes[0].node.get_and_clear_pending_events();
nodes[1].node.get_and_clear_pending_events();
}

fn check_unkown_peer_msg_event<'a, 'b: 'a, 'c: 'b>(node: &Node<'a, 'b, 'c>, closing_node_id: PublicKey, closed_channel_id: [u8; 32]){
let close_msg_ev = node.node.get_and_clear_pending_msg_events();
let expected_error_str = format!("Can't find a peer with a node_id matching the passed counterparty_node_id {}", closing_node_id);
match close_msg_ev[0] {
MessageSendEvent::HandleError {
ref node_id, action: msgs::ErrorAction::SendErrorMessage {
msg: msgs::ErrorMessage { ref channel_id, ref data }
}
} => {
assert_eq!(*node_id, closing_node_id);
assert_eq!(*data, expected_error_str);
assert_eq!(*channel_id, closed_channel_id);
}
_ => panic!("Unexpected event"),
}
}

fn check_unkown_peer_error<T>(res_err: Result<T, APIError>, expected_public_key: PublicKey) {
match res_err {
Err(APIError::APIMisuseError { err }) => {
assert_eq!(err, format!("Can't find a peer with a node_id matching the passed counterparty_node_id {}", expected_public_key));
},
Ok(_) => panic!("Unexpected Ok"),
Err(_) => panic!("Unexpected Error"),
}
}

#[test]
fn test_api_calls_with_unkown_counterparty_node() {
// Tests that our API functions and message handlers that expects a `counterparty_node_id`
// as input, behaves as expected if the `counterparty_node_id` is an unkown peer in the
// `ChannelManager::per_peer_state` map.
let chanmon_cfg = create_chanmon_cfgs(2);
let node_cfg = create_node_cfgs(2, &chanmon_cfg);
let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[None, None]);
let nodes = create_network(2, &node_cfg, &node_chanmgr);

// Boilerplate code to produce `open_channel` and `accept_channel` msgs more densly than
// creating dummy ones.
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel_msg);
let accept_channel_msg = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());

// Dummy values
let channel_id = [4; 32];
let signature = Signature::from(unsafe { FFISignature::new() });
let unkown_public_key = PublicKey::from_secret_key(&Secp256k1::signing_only(), &SecretKey::from_slice(&[42; 32]).unwrap());

// Dummy msgs
let funding_created_msg = msgs::FundingCreated {
temporary_channel_id: open_channel_msg.temporary_channel_id,
funding_txid: Txid::from_hex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap(),
funding_output_index: 0,
signature: signature,
};

let funding_signed_msg = msgs::FundingSigned {
channel_id: channel_id,
signature: signature,
};

let channel_ready_msg = msgs::ChannelReady {
channel_id: channel_id,
next_per_commitment_point: unkown_public_key,
short_channel_id_alias: None,
};

let announcement_signatures_msg = msgs::AnnouncementSignatures {
channel_id: channel_id,
short_channel_id: 0,
node_signature: signature,
bitcoin_signature: signature,
};

let channel_reestablish_msg = msgs::ChannelReestablish {
channel_id: channel_id,
next_local_commitment_number: 0,
next_remote_commitment_number: 0,
data_loss_protect: OptionalField::Absent,
};

let closing_signed_msg = msgs::ClosingSigned {
channel_id: channel_id,
fee_satoshis: 1000,
signature: signature,
fee_range: None,
};

let shutdown_msg = msgs::Shutdown {
channel_id: channel_id,
scriptpubkey: Script::new(),
};

let onion_routing_packet = msgs::OnionPacket {
version: 255,
public_key: Ok(unkown_public_key),
hop_data: [1; 20*65],
hmac: [2; 32]
};

let update_add_htlc_msg = msgs::UpdateAddHTLC {
channel_id: channel_id,
htlc_id: 0,
amount_msat: 1000000,
payment_hash: PaymentHash([1; 32]),
cltv_expiry: 821716,
onion_routing_packet
};

let commitment_signed_msg = msgs::CommitmentSigned {
channel_id: channel_id,
signature: signature,
htlc_signatures: Vec::new(),
};

let update_fee_msg = msgs::UpdateFee {
channel_id: channel_id,
feerate_per_kw: 1000,
};

let malformed_update_msg = msgs::UpdateFailMalformedHTLC{
channel_id: channel_id,
htlc_id: 0,
sha256_of_onion: [1; 32],
failure_code: 0x8000,
};

let fulfill_update_msg = msgs::UpdateFulfillHTLC{
channel_id: channel_id,
htlc_id: 0,
payment_preimage: PaymentPreimage([1; 32]),
};

let fail_update_msg = msgs::UpdateFailHTLC{
channel_id: channel_id,
htlc_id: 0,
reason: msgs::OnionErrorPacket { data: Vec::new()},
};

let revoke_and_ack_msg = msgs::RevokeAndACK {
channel_id: channel_id,
per_commitment_secret: [1; 32],
next_per_commitment_point: unkown_public_key,
};

// Test the API functions and message handlers.
check_unkown_peer_error(nodes[0].node.create_channel(unkown_public_key, 1_000_000, 500_000_000, 42, None), unkown_public_key);

nodes[1].node.handle_open_channel(&unkown_public_key, InitFeatures::known(), &open_channel_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, open_channel_msg.temporary_channel_id);

nodes[0].node.handle_accept_channel(&unkown_public_key, InitFeatures::known(), &accept_channel_msg);
check_unkown_peer_msg_event(&nodes[0], unkown_public_key, open_channel_msg.temporary_channel_id);

check_unkown_peer_error(nodes[0].node.accept_inbound_channel(&open_channel_msg.temporary_channel_id, &unkown_public_key, 42), unkown_public_key);
nodes[1].node.handle_funding_created(&unkown_public_key, &funding_created_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, open_channel_msg.temporary_channel_id);

nodes[0].node.handle_funding_signed(&unkown_public_key, &funding_signed_msg);
check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);

nodes[0].node.handle_channel_ready(&unkown_public_key, &channel_ready_msg);
check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);

nodes[1].node.handle_announcement_signatures(&unkown_public_key, &announcement_signatures_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

check_unkown_peer_error(nodes[0].node.close_channel(&channel_id, &unkown_public_key), unkown_public_key);

check_unkown_peer_error(nodes[0].node.force_close_channel(&channel_id, &unkown_public_key), unkown_public_key);

nodes[0].node.handle_shutdown(&unkown_public_key, &InitFeatures::known(), &shutdown_msg);
check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);

nodes[1].node.handle_closing_signed(&unkown_public_key, &closing_signed_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[0].node.handle_channel_reestablish(&unkown_public_key, &channel_reestablish_msg);
check_unkown_peer_msg_event(&nodes[0], unkown_public_key, channel_id);

nodes[1].node.handle_update_add_htlc(&unkown_public_key, &update_add_htlc_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_commitment_signed(&unkown_public_key, &commitment_signed_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_update_fail_malformed_htlc(&unkown_public_key, &malformed_update_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_update_fail_htlc(&unkown_public_key, &fail_update_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_update_fulfill_htlc(&unkown_public_key, &fulfill_update_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_revoke_and_ack(&unkown_public_key, &revoke_and_ack_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);

nodes[1].node.handle_update_fee(&unkown_public_key, &update_fee_msg);
check_unkown_peer_msg_event(&nodes[1], unkown_public_key, channel_id);
}
}

#[cfg(all(any(test, feature = "_test_utils"), feature = "_bench_unstable"))]
Expand Down

0 comments on commit ae665ce

Please sign in to comment.