Documentation can be found at [docs.rs](https://docs.rs/lightning/)
-Currently somewhere near 50% towards usable, published to see if there is any
-real interest from folks in using a lightning rust library.
+The project implements all of the BOLT specifications in the 1.0 spec except
+for [channel queries](https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#query-messages). The
+implementation has pretty good test coverage that is expected to continue to
+improve. There are a number of internal refactorings being done now that will
+make the code base more welcoming to new contributors. It is also anticipated
+that as developers begin using the API, the lessons from that will result in
+changes to the API, so any developer using this API at this stage should be prepared
+to embrace that. The current state is sufficient for a developer or project to
+experiment with it. Recent increased contribution rate to the project is expected
+to lead to a high quality, stable, production-worthy implementation in 2020.
The goal is to provide a full-featured but also incredibly flexible lightning
implementation, allowing the user to decide how they wish to use it. With that
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::chanmon_consistency::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
chanmon_consistency_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ chanmon_consistency_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/chanmon_consistency") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ chanmon_consistency_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::chanmon_deser::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
chanmon_deser_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ chanmon_deser_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/chanmon_deser") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ chanmon_deser_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::full_stack::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
full_stack_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ full_stack_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/full_stack") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ full_stack_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_accept_channel::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_accept_channel_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_accept_channel_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_accept_channel") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_accept_channel_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_announcement_signatures::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_announcement_signatures_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_announcement_signatures_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_announcement_signatures") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_announcement_signatures_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_channel_announcement::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_channel_announcement_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_channel_announcement_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_channel_announcement") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_channel_announcement_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_channel_reestablish::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_channel_reestablish_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_channel_reestablish_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_channel_reestablish") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_channel_reestablish_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_channel_update::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_channel_update_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_channel_update_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_channel_update") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_channel_update_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_closing_signed::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_closing_signed_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_closing_signed_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_closing_signed") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_closing_signed_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_commitment_signed::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_commitment_signed_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_commitment_signed_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_commitment_signed") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_commitment_signed_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_decoded_onion_error_packet::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_decoded_onion_error_packet_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_decoded_onion_error_packet_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_decoded_onion_error_packet") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_decoded_onion_error_packet_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_error_message::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_error_message_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_error_message_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_error_message") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_error_message_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_funding_created::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_funding_created_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_funding_created_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_funding_created") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_funding_created_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_funding_locked::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_funding_locked_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_funding_locked_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_funding_locked") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_funding_locked_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_funding_signed::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_funding_signed_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_funding_signed_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_funding_signed") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_funding_signed_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_init::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_init_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_init_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_init") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_init_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_node_announcement::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_node_announcement_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_node_announcement_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_node_announcement") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_node_announcement_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_onion_hop_data::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_onion_hop_data_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_onion_hop_data_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_onion_hop_data") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_onion_hop_data_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_open_channel::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_open_channel_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_open_channel_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_open_channel") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_open_channel_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_ping::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_ping_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_ping_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_ping") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_ping_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_pong::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_pong_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_pong_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_pong") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_pong_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_revoke_and_ack::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_revoke_and_ack_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_revoke_and_ack_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_revoke_and_ack") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_revoke_and_ack_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_shutdown::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_shutdown_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_shutdown_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_shutdown") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_shutdown_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_update_add_htlc::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_update_add_htlc_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_update_add_htlc_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_update_add_htlc") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_update_add_htlc_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_update_fail_htlc::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_update_fail_htlc_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_update_fail_htlc_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_update_fail_htlc") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_update_fail_htlc_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_update_fail_malformed_htlc::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_update_fail_malformed_htlc") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_update_fee::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_update_fee_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_update_fee_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_update_fee") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_update_fee_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::msg_targets::msg_update_fulfill_htlc::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
msg_update_fulfill_htlc_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ msg_update_fulfill_htlc_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/msg_update_fulfill_htlc") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ msg_update_fulfill_htlc_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::peer_crypt::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
peer_crypt_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ peer_crypt_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/peer_crypt") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ peer_crypt_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::router::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
router_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ router_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/router") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ router_run(data.as_ptr(), data.len());
+ }
+ }
+}
// This file is auto-generated by gen_target.sh based on target_template.txt
// To modify it, modify target_template.txt and run gen_target.sh instead.
-//Uncomment this for libfuzzer builds:
-//#![no_main]
+#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
extern crate lightning_fuzz;
use lightning_fuzz::TARGET_MOD::*;
+use std::fs;
use std::io::Read;
#[cfg(feature = "afl")]
std::io::stdin().read_to_end(&mut data).unwrap();
TARGET_NAME_run(data.as_ptr(), data.len());
}
+
+#[test]
+fn run_test_cases() {
+ let mut data: Vec<u8> = vec![0];
+ TARGET_NAME_run(data.as_ptr(), data.len());
+ if let Ok(tests) = fs::read_dir("test_cases/TARGET_NAME") {
+ for test in tests {
+ data.clear();
+ let path = test.unwrap().path();
+ println!("Running test {}...", path.file_name().unwrap().to_str().unwrap());
+ fs::File::open(path).unwrap().read_to_end(&mut data).unwrap();
+ TARGET_NAME_run(data.as_ptr(), data.len());
+ }
+ }
+}
static mut IN_RESTORE: bool = false;
pub struct TestChannelMonitor {
- pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
+ pub simple_monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
pub latest_good_update: Mutex<HashMap<OutPoint, Vec<u8>>>,
pub latest_update_good: Mutex<HashMap<OutPoint, bool>>,
}
}
}
-impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
- fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor {
+ fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
let ret = self.update_ret.lock().unwrap().clone();
if let Ok(()) = ret {
let mut ser = VecWriter(Vec::new());
}
fn get_channel_keys(&self, _inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
- EnforcingChannelKeys::new(InMemoryChannelKeys {
- funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
- revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
- payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
- delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
- htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
- commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
- remote_channel_pubkeys: None,
+ let secp_ctx = Secp256k1::signing_only();
+ EnforcingChannelKeys::new(InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, self.node_id]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, self.node_id]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, self.node_id]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, self.node_id]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, self.node_id]).unwrap(),
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, self.node_id],
channel_value_satoshis,
- })
+ ))
}
fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) {
config.channel_options.fee_proportional_millionths = 0;
config.channel_options.announced_channel = true;
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
- (Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
+ (Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap()),
monitor)
} }
}
let mut monitors = HashMap::new();
let mut old_monitors = $old_monitors.latest_good_update.lock().unwrap();
for (outpoint, monitor_ser) in old_monitors.drain() {
- monitors.insert(outpoint, <(Sha256d, ChannelMonitor)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
+ monitors.insert(outpoint, <(Sha256d, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&monitor_ser), Arc::clone(&logger)).expect("Failed to read monitor").1);
monitor.latest_good_update.lock().unwrap().insert(outpoint, monitor_ser);
}
let mut monitor_refs = HashMap::new();
let read_args = ChannelManagerReadArgs {
keys_manager,
fee_estimator: fee_est.clone(),
- monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>,
+ monitor: monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>,
tx_broadcaster: broadcast.clone(),
logger,
default_config: config,
channel_monitors: &mut monitor_refs,
};
- let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
+ let res = (<(Sha256d, ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>)>::read(&mut Cursor::new(&$ser.0), read_args).expect("Failed to read manager").1, monitor);
for (_, was_good) in $old_monitors.latest_updates_good_at_last_ser.lock().unwrap().iter() {
if !was_good {
// If the last time we updated a monitor we didn't successfully update (and we
pub extern "C" fn chanmon_consistency_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
+use lightning::util::enforcing_trait_impls::EnforcingChannelKeys;
use lightning::ln::channelmonitor;
use lightning::util::ser::{ReadableArgs, Writer};
#[inline]
pub fn do_test(data: &[u8]) {
let logger = Arc::new(test_logger::TestLogger::new("".to_owned()));
- if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) {
+ if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(data), logger.clone()) {
let mut w = VecWriter(Vec::new());
monitor.write_for_disk(&mut w).unwrap();
- let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
+ let deserialized_copy = <(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(&mut Cursor::new(&w.0), logger.clone()).unwrap();
assert!(latest_block_hash == deserialized_copy.0);
assert!(monitor == deserialized_copy.1);
w.0.clear();
pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
}
struct MoneyLossDetector<'a> {
- manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>,
- monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>,
- handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>>,
+ manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
+ monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
+ handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>,
peers: &'a RefCell<[bool; 256]>,
funding_txn: Vec<Transaction>,
blocks_connected: u32,
}
impl<'a> MoneyLossDetector<'a> {
- pub fn new(peers: &'a RefCell<[bool; 256]>, manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>, monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint>>, handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor>>>>) -> Self {
+ pub fn new(peers: &'a RefCell<[bool; 256]>,
+ manager: Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>,
+ monitor: Arc<channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>>,
+ handler: PeerManager<Peer<'a>, Arc<ChannelManager<EnforcingChannelKeys, Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>>>>) -> Self {
MoneyLossDetector {
manager,
monitor,
fn get_channel_keys(&self, inbound: bool, channel_value_satoshis: u64) -> EnforcingChannelKeys {
let ctr = self.counter.fetch_add(1, Ordering::Relaxed) as u8;
+ let secp_ctx = Secp256k1::signing_only();
EnforcingChannelKeys::new(if inbound {
- InMemoryChannelKeys {
- funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
- revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
- payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
- delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
- htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
- commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
- remote_channel_pubkeys: None,
- channel_value_satoshis: channel_value_satoshis,
- }
+ InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, ctr]).unwrap(),
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, ctr],
+ channel_value_satoshis,
+ )
} else {
- InMemoryChannelKeys {
- funding_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
- revocation_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
- payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
- delayed_payment_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
- htlc_base_key: SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
- commitment_seed: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
- remote_channel_pubkeys: None,
- channel_value_satoshis: channel_value_satoshis,
- }
+ InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, ctr]).unwrap(),
+ SecretKey::from_slice(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, ctr]).unwrap(),
+ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, ctr],
+ channel_value_satoshis,
+ )
})
}
config.channel_options.fee_proportional_millionths = slice_to_be32(get_slice!(4));
config.channel_options.announced_channel = get_slice!(1)[0] != 0;
config.peer_channel_config_limits.min_dust_limit_satoshis = 0;
- let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap());
+ let channelmanager = Arc::new(ChannelManager::new(Network::Bitcoin, fee_est.clone(), monitor.clone() as Arc<channelmonitor::ManyChannelMonitor<EnforcingChannelKeys>>, broadcast.clone(), Arc::clone(&logger), keys_manager.clone(), config, 0).unwrap());
let router = Arc::new(Router::new(PublicKey::from_secret_key(&Secp256k1::signing_only(), &keys_manager.get_node_secret()), watch.clone(), Arc::clone(&logger)));
let peers = RefCell::new([false; 256]);
} else {
let txres: Result<Transaction, _> = deserialize(get_slice!(txlen));
if let Ok(tx) = txres {
+ let mut output_val = 0;
+ for out in tx.output.iter() {
+ if out.value > 21_000_000_0000_0000 { return; }
+ output_val += out.value;
+ if output_val > 21_000_000_0000_0000 { return; }
+ }
loss_detector.connect_block(&[tx]);
} else {
return;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
- #[test]
- fn duplicate_crash() {
- let logger: Arc<dyn Logger> = Arc::new(test_logger::TestLogger::new("".to_owned()));
- super::do_test(&::hex::decode("00").unwrap(), &logger);
- }
-
struct TrackingLogger {
/// (module, message) -> count
pub lines: Mutex<HashMap<(String, String), usize>>,
GEN_TEST UpdateAddHTLC test_msg_hole ", 85, 33"
GEN_TEST ErrorMessage test_msg_hole ", 32, 2"
-GEN_TEST OnionHopData test_msg_hole ", 1+8+8+4, 12"
GEN_TEST Init test_msg_simple ""
+GEN_TEST OnionHopData test_msg_simple ""
GEN_TEST Ping test_msg_simple ""
GEN_TEST Pong test_msg_simple ""
pub mod msg_node_announcement;
pub mod msg_update_add_htlc;
pub mod msg_error_message;
-pub mod msg_onion_hop_data;
pub mod msg_init;
+pub mod msg_onion_hop_data;
pub mod msg_ping;
pub mod msg_pong;
pub extern "C" fn msg_accept_channel_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_announcement_signatures_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_channel_announcement_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_channel_reestablish_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_channel_update_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_closing_signed_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_commitment_signed_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_decoded_onion_error_packet_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_error_message_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_funding_created_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_funding_locked_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_funding_signed_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_init_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_node_announcement_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
#[inline]
pub fn do_test(data: &[u8]) {
- test_msg_hole!(msgs::OnionHopData, data, 1+8+8+4, 12);
+ test_msg_simple!(msgs::OnionHopData, data);
}
#[no_mangle]
pub extern "C" fn msg_onion_hop_data_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_open_channel_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_ping_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_pong_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_revoke_and_ack_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_shutdown_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn TARGET_NAME_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_update_add_htlc_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_update_fail_htlc_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_update_fail_malformed_htlc_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_update_fee_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
pub extern "C" fn msg_update_fulfill_htlc_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
}
}
+// We attempt to test the strictest behavior we can for a given message, however, some messages
+// have different expected behavior. You can see which messages have which behavior in
+// gen_target.sh, but, in general, the *_announcement messages have to round-trip exactly (as
+// otherwise we'd invalidate the signatures), most messages just need to round-trip up to the
+// amount of data we know how to interpret, and some messages we may throw out invalid stuff (eg
+// if an error message isn't valid UTF-8 we cant String-ize it), so they wont roundtrip correctly.
+
+// Tests a message that must survive roundtrip exactly, though may not empty the read buffer
+// entirely
#[macro_export]
macro_rules! test_msg {
($MsgType: path, $data: ident) => {
}
}
+// Tests a message that may lose data on roundtrip, but shoulnd't lose data compared to our
+// re-serialization.
#[macro_export]
macro_rules! test_msg_simple {
($MsgType: path, $data: ident) => {
if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
let mut w = VecWriter(Vec::new());
msg.write(&mut w).unwrap();
+
+ let msg = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap();
+ let mut w_two = VecWriter(Vec::new());
+ msg.write(&mut w_two).unwrap();
+ assert_eq!(&w.0[..], &w_two.0[..]);
}
}
}
}
+// Tests a message that must survive roundtrip exactly, and must exactly empty the read buffer and
+// split it back out on re-serialization.
#[macro_export]
macro_rules! test_msg_exact {
($MsgType: path, $data: ident) => {
if let Ok(msg) = <$MsgType as Readable<::std::io::Cursor<&[u8]>>>::read(&mut r) {
let mut w = VecWriter(Vec::new());
msg.write(&mut w).unwrap();
-
assert_eq!(&r.into_inner()[..], &w.0[..]);
}
}
}
}
+// Tests a message that must survive roundtrip exactly, modulo one "hole" which may be set to 0s on
+// re-serialization.
#[macro_export]
macro_rules! test_msg_hole {
($MsgType: path, $data: ident, $hole: expr, $hole_len: expr) => {
pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("01").unwrap());
- }
-}
pub extern "C" fn router_run(data: *const u8, datalen: usize) {
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
}
-
-#[cfg(test)]
-mod tests {
- #[test]
- fn duplicate_crash() {
- super::do_test(&::hex::decode("00").unwrap());
- }
-}
use bitcoin_hashes::hash160::Hash as Hash160;
use secp256k1::key::{SecretKey, PublicKey};
-use secp256k1::{Secp256k1, Signature};
+use secp256k1::{Secp256k1, Signature, Signing};
use secp256k1;
use util::byte_utils;
use util::logger::Logger;
-use util::ser::Writeable;
+use util::ser::{Writeable, Writer, Readable};
use ln::chan_utils;
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
+use std::io::Error;
+use ln::msgs::DecodeError;
/// When on-chain outputs are created by rust-lightning (which our counterparty is not able to
/// claim at any point in the future) an event is generated which you must track and be able to
/// (TODO: We shouldn't require that, and should have an API to get them at deser time, due mostly
/// to the possibility of reentrancy issues by calling the user's code during our deserialization
/// routine).
-pub trait ChannelKeys : Send {
+/// TODO: remove Clone once we start returning ChannelUpdate objects instead of copying ChannelMonitor
+pub trait ChannelKeys : Send+Clone {
/// Gets the private key for the anchor tx
fn funding_key<'a>(&'a self) -> &'a SecretKey;
/// Gets the local secret key for blinded revocation pubkey
fn htlc_base_key<'a>(&'a self) -> &'a SecretKey;
/// Gets the commitment seed
fn commitment_seed<'a>(&'a self) -> &'a [u8; 32];
+ /// Gets the local channel public keys and basepoints
+ fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys;
/// Create a signature for a remote commitment transaction and associated HTLC transactions.
///
/// A simple implementation of ChannelKeys that just keeps the private keys in memory.
pub struct InMemoryChannelKeys {
/// Private key of anchor tx
- pub funding_key: SecretKey,
+ funding_key: SecretKey,
/// Local secret key for blinded revocation pubkey
- pub revocation_base_key: SecretKey,
+ revocation_base_key: SecretKey,
/// Local secret key used in commitment tx htlc outputs
- pub payment_base_key: SecretKey,
+ payment_base_key: SecretKey,
/// Local secret key used in HTLC tx
- pub delayed_payment_base_key: SecretKey,
+ delayed_payment_base_key: SecretKey,
/// Local htlc secret key used in commitment tx htlc outputs
- pub htlc_base_key: SecretKey,
+ htlc_base_key: SecretKey,
/// Commitment seed
- pub commitment_seed: [u8; 32],
+ commitment_seed: [u8; 32],
+ /// Local public keys and basepoints
+ pub(crate) local_channel_pubkeys: ChannelPublicKeys,
/// Remote public keys and base points
- pub remote_channel_pubkeys: Option<ChannelPublicKeys>,
+ pub(crate) remote_channel_pubkeys: Option<ChannelPublicKeys>,
/// The total value of this channel
- pub channel_value_satoshis: u64,
+ channel_value_satoshis: u64,
+}
+
+impl InMemoryChannelKeys {
+ /// Create a new InMemoryChannelKeys
+ pub fn new<C: Signing>(
+ secp_ctx: &Secp256k1<C>,
+ funding_key: SecretKey,
+ revocation_base_key: SecretKey,
+ payment_base_key: SecretKey,
+ delayed_payment_base_key: SecretKey,
+ htlc_base_key: SecretKey,
+ commitment_seed: [u8; 32],
+ channel_value_satoshis: u64) -> InMemoryChannelKeys {
+ let local_channel_pubkeys =
+ InMemoryChannelKeys::make_local_keys(secp_ctx, &funding_key, &revocation_base_key,
+ &payment_base_key, &delayed_payment_base_key,
+ &htlc_base_key);
+ InMemoryChannelKeys {
+ funding_key,
+ revocation_base_key,
+ payment_base_key,
+ delayed_payment_base_key,
+ htlc_base_key,
+ commitment_seed,
+ channel_value_satoshis,
+ local_channel_pubkeys,
+ remote_channel_pubkeys: None,
+ }
+ }
+
+ fn make_local_keys<C: Signing>(secp_ctx: &Secp256k1<C>,
+ funding_key: &SecretKey,
+ revocation_base_key: &SecretKey,
+ payment_base_key: &SecretKey,
+ delayed_payment_base_key: &SecretKey,
+ htlc_base_key: &SecretKey) -> ChannelPublicKeys {
+ let from_secret = |s: &SecretKey| PublicKey::from_secret_key(secp_ctx, s);
+ ChannelPublicKeys {
+ funding_pubkey: from_secret(&funding_key),
+ revocation_basepoint: from_secret(&revocation_base_key),
+ payment_basepoint: from_secret(&payment_base_key),
+ delayed_payment_basepoint: from_secret(&delayed_payment_base_key),
+ htlc_basepoint: from_secret(&htlc_base_key),
+ }
+ }
}
impl ChannelKeys for InMemoryChannelKeys {
fn delayed_payment_base_key(&self) -> &SecretKey { &self.delayed_payment_base_key }
fn htlc_base_key(&self) -> &SecretKey { &self.htlc_base_key }
fn commitment_seed(&self) -> &[u8; 32] { &self.commitment_seed }
+ fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys { &self.local_channel_pubkeys }
fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
if commitment_tx.input.len() != 1 { return Err(()); }
}
}
-impl_writeable!(InMemoryChannelKeys, 0, {
- funding_key,
- revocation_base_key,
- payment_base_key,
- delayed_payment_base_key,
- htlc_base_key,
- commitment_seed,
- remote_channel_pubkeys,
- channel_value_satoshis
-});
+impl Writeable for InMemoryChannelKeys {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
+ self.funding_key.write(writer)?;
+ self.revocation_base_key.write(writer)?;
+ self.payment_base_key.write(writer)?;
+ self.delayed_payment_base_key.write(writer)?;
+ self.htlc_base_key.write(writer)?;
+ self.commitment_seed.write(writer)?;
+ self.remote_channel_pubkeys.write(writer)?;
+ self.channel_value_satoshis.write(writer)?;
+
+ Ok(())
+ }
+}
+
+impl<R: ::std::io::Read> Readable<R> for InMemoryChannelKeys {
+ fn read(reader: &mut R) -> Result<Self, DecodeError> {
+ let funding_key = Readable::read(reader)?;
+ let revocation_base_key = Readable::read(reader)?;
+ let payment_base_key = Readable::read(reader)?;
+ let delayed_payment_base_key = Readable::read(reader)?;
+ let htlc_base_key = Readable::read(reader)?;
+ let commitment_seed = Readable::read(reader)?;
+ let remote_channel_pubkeys = Readable::read(reader)?;
+ let channel_value_satoshis = Readable::read(reader)?;
+ let secp_ctx = Secp256k1::signing_only();
+ let local_channel_pubkeys =
+ InMemoryChannelKeys::make_local_keys(&secp_ctx, &funding_key, &revocation_base_key,
+ &payment_base_key, &delayed_payment_base_key,
+ &htlc_base_key);
+
+ Ok(InMemoryChannelKeys {
+ funding_key,
+ revocation_base_key,
+ payment_base_key,
+ delayed_payment_base_key,
+ htlc_base_key,
+ commitment_seed,
+ channel_value_satoshis,
+ local_channel_pubkeys,
+ remote_channel_pubkeys
+ })
+ }
+}
/// Simple KeysInterface implementor that takes a 32-byte seed for use as a BIP 32 extended key
/// and derives keys from that.
let delayed_payment_base_key = key_step!(b"delayed payment base key", payment_base_key);
let htlc_base_key = key_step!(b"HTLC base key", delayed_payment_base_key);
- InMemoryChannelKeys {
+ InMemoryChannelKeys::new(
+ &self.secp_ctx,
funding_key,
revocation_base_key,
payment_base_key,
delayed_payment_base_key,
htlc_base_key,
commitment_seed,
- remote_channel_pubkeys: None,
- channel_value_satoshis,
- }
+ channel_value_satoshis
+ )
}
fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) {
}
/// One counterparty's public keys which do not change over the life of a channel.
-#[derive(Clone)]
+#[derive(Clone, PartialEq)]
pub struct ChannelPublicKeys {
/// The public key which is used to sign all commitment transactions, as it appears in the
/// on-chain channel lock-in 2-of-2 multisig output.
secp_ctx: Secp256k1<secp256k1::All>,
channel_value_satoshis: u64,
+ #[cfg(not(test))]
local_keys: ChanSigner,
+ #[cfg(test)]
+ pub(super) local_keys: ChanSigner,
shutdown_pubkey: PublicKey,
// Our commitment numbers start at 2^48-1 and count down, whereas the ones used in transaction
their_shutdown_scriptpubkey: Option<Script>,
- channel_monitor: ChannelMonitor,
+ channel_monitor: ChannelMonitor<ChanSigner>,
network_sync: UpdateStatus,
/// Used to return a simple Error back to ChannelManager. Will get converted to a
/// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our
/// channel_id in ChannelManager.
-pub(super) enum ChannelError {
+pub(super) enum ChannelError<ChanSigner: ChannelKeys> {
Ignore(&'static str),
Close(&'static str),
CloseDelayBroadcast {
msg: &'static str,
- update: Option<ChannelMonitor>
+ update: Option<ChannelMonitor<ChanSigner>>,
},
}
-impl fmt::Debug for ChannelError {
+impl<ChanSigner: ChannelKeys> fmt::Debug for ChannelError<ChanSigner> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
&ChannelError::Ignore(e) => write!(f, "Ignore : {}", e),
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
let secp_ctx = Secp256k1::new();
- let channel_monitor = ChannelMonitor::new(chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
+ let channel_monitor = ChannelMonitor::new(chan_keys.clone(),
+ chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
keys_provider.get_destination_script(), logger.clone());
})
}
- fn check_remote_fee(fee_estimator: &FeeEstimator, feerate_per_kw: u32) -> Result<(), ChannelError> {
+ fn check_remote_fee(fee_estimator: &FeeEstimator, feerate_per_kw: u32) -> Result<(), ChannelError<ChanSigner>> {
if (feerate_per_kw as u64) < fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Background) {
return Err(ChannelError::Close("Peer's feerate much too low"));
}
/// Creates a new channel from a remote sides' request for one.
/// Assumes chain_hash has already been checked and corresponds with what we expect!
- pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface<ChanKeySigner = ChanSigner>>, their_node_id: PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel<ChanSigner>, ChannelError> {
+ pub fn new_from_req(fee_estimator: &FeeEstimator, keys_provider: &Arc<KeysInterface<ChanKeySigner = ChanSigner>>, their_node_id: PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel, user_id: u64, logger: Arc<Logger>, config: &UserConfig) -> Result<Channel<ChanSigner>, ChannelError<ChanSigner>> {
let mut chan_keys = keys_provider.get_channel_keys(true, msg.funding_satoshis);
let their_pubkeys = ChannelPublicKeys {
funding_pubkey: msg.funding_pubkey,
}
let secp_ctx = Secp256k1::new();
- let channel_monitor = ChannelMonitor::new(chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
+ let channel_monitor = ChannelMonitor::new(chan_keys.clone(),
+ chan_keys.funding_key(), chan_keys.revocation_base_key(), chan_keys.delayed_payment_base_key(),
chan_keys.htlc_base_key(), chan_keys.payment_base_key(), &keys_provider.get_shutdown_pubkey(), config.own_channel_config.our_to_self_delay,
keys_provider.get_destination_script(), logger.clone());
/// our counterparty!)
/// The result is a transaction which we can revoke ownership of (ie a "local" transaction)
/// TODO Some magic rust shit to compile-time check this?
- fn build_local_transaction_keys(&self, commitment_number: u64) -> Result<TxCreationKeys, ChannelError> {
+ fn build_local_transaction_keys(&self, commitment_number: u64) -> Result<TxCreationKeys, ChannelError<ChanSigner>> {
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(commitment_number));
let delayed_payment_base = PublicKey::from_secret_key(&self.secp_ctx, self.local_keys.delayed_payment_base_key());
let htlc_basepoint = PublicKey::from_secret_key(&self.secp_ctx, self.local_keys.htlc_base_key());
/// Creates a set of keys for build_commitment_transaction to generate a transaction which we
/// will sign and send to our counterparty.
/// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
- fn build_remote_transaction_keys(&self) -> Result<TxCreationKeys, ChannelError> {
+ fn build_remote_transaction_keys(&self) -> Result<TxCreationKeys, ChannelError<ChanSigner>> {
//TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
//may see payments to it!
let payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, self.local_keys.payment_base_key());
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
/// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
/// Ok(_) if debug assertions are turned on and preconditions are met.
- fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), ChannelError> {
+ fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor<ChanSigner>>), ChannelError<ChanSigner>> {
// Either ChannelFunded got set (which means it won't be unset) or there is no way any
// caller thought we could have something claimed (cause we wouldn't have accepted in an
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
}), Some(self.channel_monitor.clone())))
}
- pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), ChannelError> {
+ pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor<ChanSigner>>), ChannelError<ChanSigner>> {
match self.get_update_fulfill_htlc(htlc_id, payment_preimage)? {
(Some(update_fulfill_htlc), _) => {
let (commitment, monitor_update) = self.send_commitment_no_status_check()?;
/// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
/// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
/// Ok(_) if debug assertions are turned on and preconditions are met.
- pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError> {
+ pub fn get_update_fail_htlc(&mut self, htlc_id_arg: u64, err_packet: msgs::OnionErrorPacket) -> Result<Option<msgs::UpdateFailHTLC>, ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
panic!("Was asked to fail an HTLC when channel was not in an operational state");
}
// Message handlers:
- pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_features: InitFeatures) -> Result<(), ChannelError> {
+ pub fn accept_channel(&mut self, msg: &msgs::AcceptChannel, config: &UserConfig, their_features: InitFeatures) -> Result<(), ChannelError<ChanSigner>> {
// Check sanity of message fields:
if !self.channel_outbound {
return Err(ChannelError::Close("Got an accept_channel message from an inbound peer"));
Ok(())
}
- fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, LocalCommitmentTransaction, Signature, TxCreationKeys), ChannelError> {
+ fn funding_created_signature(&mut self, sig: &Signature) -> Result<(Transaction, LocalCommitmentTransaction, Signature, TxCreationKeys), ChannelError<ChanSigner>> {
let funding_script = self.get_funding_redeemscript();
let local_keys = self.build_local_transaction_keys(self.cur_local_commitment_transaction_number)?;
&self.their_pubkeys.as_ref().expect("their_funding_pubkey() only allowed after accept_channel").funding_pubkey
}
- pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor), ChannelError> {
+ pub fn funding_created(&mut self, msg: &msgs::FundingCreated) -> Result<(msgs::FundingSigned, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
if self.channel_outbound {
return Err(ChannelError::Close("Received funding_created for an outbound channel?"));
}
/// Handles a funding_signed message from the remote end.
/// If this call is successful, broadcast the funding transaction (and not before!)
- pub fn funding_signed(&mut self, msg: &msgs::FundingSigned) -> Result<ChannelMonitor, ChannelError> {
+ pub fn funding_signed(&mut self, msg: &msgs::FundingSigned) -> Result<ChannelMonitor<ChanSigner>, ChannelError<ChanSigner>> {
if !self.channel_outbound {
return Err(ChannelError::Close("Received funding_signed for an inbound channel?"));
}
}
}
- pub fn funding_locked(&mut self, msg: &msgs::FundingLocked) -> Result<(), ChannelError> {
+ pub fn funding_locked(&mut self, msg: &msgs::FundingLocked) -> Result<(), ChannelError<ChanSigner>> {
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
return Err(ChannelError::Close("Peer sent funding_locked when we needed a channel_reestablish"));
}
cmp::min(self.value_to_self_msat as i64 - self.get_outbound_pending_htlc_stats().1 as i64, 0) as u64)
}
- pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError> {
+ pub fn update_add_htlc(&mut self, msg: &msgs::UpdateAddHTLC, pending_forward_state: PendingHTLCStatus) -> Result<(), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32 | ChannelState::RemoteShutdownSent as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got add HTLC message when channel was not in an operational state"));
}
/// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
#[inline]
- fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentHash>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
+ fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentHash>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError<ChanSigner>> {
for htlc in self.pending_outbound_htlcs.iter_mut() {
if htlc.htlc_id == htlc_id {
match check_preimage {
Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
}
- pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<HTLCSource, ChannelError> {
+ pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<HTLCSource, ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
}
self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None).map(|source| source.clone())
}
- pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
+ pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
}
Ok(())
}
- pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
+ pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
}
Ok(())
}
- pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
+ pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got commitment signed message when channel was not in an operational state"));
}
/// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
/// fulfilling or failing the last pending HTLC)
- fn free_holding_cell_htlcs(&mut self) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitor)>, ChannelError> {
+ fn free_holding_cell_htlcs(&mut self) -> Result<Option<(msgs::CommitmentUpdate, ChannelMonitor<ChanSigner>)>, ChannelError<ChanSigner>> {
assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
if self.holding_cell_htlc_updates.len() != 0 || self.holding_cell_update_fee.is_some() {
log_trace!(self, "Freeing holding cell with {} HTLC updates{}", self.holding_cell_htlc_updates.len(), if self.holding_cell_update_fee.is_some() { " and a fee update" } else { "" });
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
/// generating an appropriate error *after* the channel state has been updated based on the
/// revoke_and_ack message.
- pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
+ pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state"));
}
self.channel_monitor.provide_secret(self.cur_remote_commitment_transaction_number + 1, msg.per_commitment_secret)
.map_err(|e| ChannelError::Close(e.0))?;
+ if self.channel_state & ChannelState::AwaitingRemoteRevoke as u32 == 0 {
+ // Our counterparty seems to have burned their coins to us (by revoking a state when we
+ // haven't given them a new commitment transaction to broadcast). We should probably
+ // take advantage of this by updating our channel monitor, sending them an error, and
+ // waiting for them to broadcast their latest (now-revoked claim). But, that would be a
+ // lot of work, and there's some chance this is all a misunderstanding anyway.
+ // We have to do *something*, though, since our signer may get mad at us for otherwise
+ // jumping a remote commitment number, so best to just force-close and move on.
+ return Err(ChannelError::Close("Received an unexpected revoke_and_ack"));
+ }
+
// Update state now that we've passed all the can-fail calls...
// (note that we may still fail to generate the new commitment_signed message, but that's
// OK, we step the channel here and *then* if the new generation fails we can fail the
})
}
- pub fn send_update_fee_and_commit(&mut self, feerate_per_kw: u64) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
+ pub fn send_update_fee_and_commit(&mut self, feerate_per_kw: u64) -> Result<Option<(msgs::UpdateFee, msgs::CommitmentSigned, ChannelMonitor<ChanSigner>)>, ChannelError<ChanSigner>> {
match self.send_update_fee(feerate_per_kw) {
Some(update_fee) => {
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
(raa, commitment_update, order, forwards, failures, needs_broadcast_safe, funding_locked)
}
- pub fn update_fee(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::UpdateFee) -> Result<(), ChannelError> {
+ pub fn update_fee(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::UpdateFee) -> Result<(), ChannelError<ChanSigner>> {
if self.channel_outbound {
return Err(ChannelError::Close("Non-funding remote tried to update channel fee"));
}
/// May panic if some calls other than message-handling calls (which will all Err immediately)
/// have been called between remove_uncommitted_htlcs_and_mark_paused and this call.
- pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError> {
+ pub fn channel_reestablish(&mut self, msg: &msgs::ChannelReestablish) -> Result<(Option<msgs::FundingLocked>, Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, Option<ChannelMonitor<ChanSigner>>, RAACommitmentOrder, Option<msgs::Shutdown>), ChannelError<ChanSigner>> {
if self.channel_state & (ChannelState::PeerDisconnected as u32) == 0 {
// While BOLT 2 doesn't indicate explicitly we should error this channel here, it
// almost certainly indicates we are going to end up out-of-sync in some way, so we
}
if msg.next_remote_commitment_number > INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
self.channel_monitor.provide_rescue_remote_commitment_tx_info(data_loss.my_current_per_commitment_point);
- return Err(ChannelError::CloseDelayBroadcast { msg: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting", update: Some(self.channel_monitor.clone())
- });
+ return Err(ChannelError::CloseDelayBroadcast { msg: "We have fallen behind - we have received proof that if we broadcast remote is going to claim our funds - we can't do any automated broadcasting", update: Some(self.channel_monitor.clone())});
}
},
OptionalField::Absent => {}
})
}
- pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, PaymentHash)>), ChannelError> {
+ pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, PaymentHash)>), ChannelError<ChanSigner>> {
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish"));
}
tx.input[0].witness.push(self.get_funding_redeemscript().into_bytes());
}
- pub fn closing_signed(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::ClosingSigned) -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError> {
+ pub fn closing_signed(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::ClosingSigned) -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError<ChanSigner>> {
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK != BOTH_SIDES_SHUTDOWN_MASK {
return Err(ChannelError::Close("Remote end sent us a closing_signed before both sides provided a shutdown"));
}
}
/// May only be called after funding has been initiated (ie is_funding_initiated() is true)
- pub fn channel_monitor(&mut self) -> &mut ChannelMonitor {
+ pub fn channel_monitor(&mut self) -> &mut ChannelMonitor<ChanSigner> {
if self.channel_state < ChannelState::FundingCreated as u32 {
panic!("Can't get a channel monitor until funding has been created");
}
pub fn block_connected(&mut self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> Result<Option<msgs::FundingLocked>, msgs::ErrorMessage> {
let non_shutdown_state = self.channel_state & (!MULTI_STATE_FLAGS);
if header.bitcoin_hash() != self.last_block_connected {
- self.last_block_connected = header.bitcoin_hash();
- self.channel_monitor.last_block_hash = self.last_block_connected;
if self.funding_tx_confirmations > 0 {
self.funding_tx_confirmations += 1;
- if self.funding_tx_confirmations == self.minimum_depth as u64 {
- let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
- self.channel_state |= ChannelState::OurFundingLocked as u32;
- true
- } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
- self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
- self.channel_update_count += 1;
- true
- } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
- // We got a reorg but not enough to trigger a force close, just update
- // funding_tx_confirmed_in and return.
- false
- } else if self.channel_state < ChannelState::ChannelFunded as u32 {
- panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
- } else {
- // We got a reorg but not enough to trigger a force close, just update
- // funding_tx_confirmed_in and return.
- false
- };
- self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
-
- //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
- //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
- //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
- //a protocol oversight, but I assume I'm just missing something.
- if need_commitment_update {
- if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
- let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
- let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
- return Ok(Some(msgs::FundingLocked {
- channel_id: self.channel_id,
- next_per_commitment_point: next_per_commitment_point,
- }));
- } else {
- self.monitor_pending_funding_locked = true;
- return Ok(None);
- }
- }
- }
}
}
if non_shutdown_state & !(ChannelState::TheirFundingLocked as u32) == ChannelState::FundingSent as u32 {
}
}
}
+ if header.bitcoin_hash() != self.last_block_connected {
+ self.last_block_connected = header.bitcoin_hash();
+ self.channel_monitor.last_block_hash = self.last_block_connected;
+ if self.funding_tx_confirmations > 0 {
+ if self.funding_tx_confirmations == self.minimum_depth as u64 {
+ let need_commitment_update = if non_shutdown_state == ChannelState::FundingSent as u32 {
+ self.channel_state |= ChannelState::OurFundingLocked as u32;
+ true
+ } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::TheirFundingLocked as u32) {
+ self.channel_state = ChannelState::ChannelFunded as u32 | (self.channel_state & MULTI_STATE_FLAGS);
+ self.channel_update_count += 1;
+ true
+ } else if non_shutdown_state == (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) {
+ // We got a reorg but not enough to trigger a force close, just update
+ // funding_tx_confirmed_in and return.
+ false
+ } else if self.channel_state < ChannelState::ChannelFunded as u32 {
+ panic!("Started confirming a channel in a state pre-FundingSent?: {}", self.channel_state);
+ } else {
+ // We got a reorg but not enough to trigger a force close, just update
+ // funding_tx_confirmed_in and return.
+ false
+ };
+ self.funding_tx_confirmed_in = Some(header.bitcoin_hash());
+
+ //TODO: Note that this must be a duplicate of the previous commitment point they sent us,
+ //as otherwise we will have a commitment transaction that they can't revoke (well, kinda,
+ //they can by sending two revoke_and_acks back-to-back, but not really). This appears to be
+ //a protocol oversight, but I assume I'm just missing something.
+ if need_commitment_update {
+ if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) == 0 {
+ let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
+ let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
+ return Ok(Some(msgs::FundingLocked {
+ channel_id: self.channel_id,
+ next_per_commitment_point: next_per_commitment_point,
+ }));
+ } else {
+ self.monitor_pending_funding_locked = true;
+ return Ok(None);
+ }
+ }
+ }
+ }
+ }
Ok(None)
}
}
/// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
- fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), ChannelError> {
+ fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), ChannelError<ChanSigner>> {
let remote_keys = self.build_remote_transaction_keys()?;
let remote_initial_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, false, self.feerate_per_kw).0;
Ok((self.local_keys.sign_remote_commitment(self.feerate_per_kw, &remote_initial_commitment_tx, &remote_keys, &Vec::new(), self.our_to_self_delay, &self.secp_ctx)
/// Note that channel_id changes during this call!
/// Do NOT broadcast the funding transaction until after a successful funding_signed call!
/// If an Err is returned, it is a ChannelError::Close.
- pub fn get_outbound_funding_created(&mut self, funding_txo: OutPoint) -> Result<(msgs::FundingCreated, ChannelMonitor), ChannelError> {
+ pub fn get_outbound_funding_created(&mut self, funding_txo: OutPoint) -> Result<(msgs::FundingCreated, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
if !self.channel_outbound {
panic!("Tried to create outbound funding_created message on an inbound channel!");
}
/// closing).
/// Note that the "channel must be funded" requirement is stricter than BOLT 7 requires - see
/// https://github.com/lightningnetwork/lightning-rfc/issues/468
- pub fn get_channel_announcement(&self, our_node_id: PublicKey, chain_hash: Sha256dHash) -> Result<(msgs::UnsignedChannelAnnouncement, Signature), ChannelError> {
+ pub fn get_channel_announcement(&self, our_node_id: PublicKey, chain_hash: Sha256dHash) -> Result<(msgs::UnsignedChannelAnnouncement, Signature), ChannelError<ChanSigner>> {
if !self.config.announced_channel {
return Err(ChannelError::Ignore("Channel is not available for public announcements"));
}
/// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
/// You MUST call send_commitment prior to any other calls on this Channel
/// If an Err is returned, it's a ChannelError::Ignore!
- pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> {
+ pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) {
return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down"));
}
/// Always returns a ChannelError::Close if an immediately-preceding (read: the
/// last call to this Channel) send_htlc returned Ok(Some(_)) and there is an Err.
/// May panic if called except immediately after a successful, Ok(Some(_))-returning send_htlc.
- pub fn send_commitment(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
+ pub fn send_commitment(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
panic!("Cannot create commitment tx until channel is fully established");
}
self.send_commitment_no_status_check()
}
/// Only fails in case of bad keys
- fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor), ChannelError> {
+ fn send_commitment_no_status_check(&mut self) -> Result<(msgs::CommitmentSigned, ChannelMonitor<ChanSigner>), ChannelError<ChanSigner>> {
// We can upgrade the status of some HTLCs that are waiting on a commitment, even if we
// fail to generate this, we still are at least at a position where upgrading their status
// is acceptable.
/// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
/// when we shouldn't change HTLC/channel state.
- fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> {
+ fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError<ChanSigner>> {
let mut feerate_per_kw = self.feerate_per_kw;
if let Some(feerate) = self.pending_update_fee {
if self.channel_outbound {
/// to send to the remote peer in one go.
/// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
/// more info.
- pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
+ pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor<ChanSigner>)>, ChannelError<ChanSigner>> {
match self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet)? {
Some(update_add_htlc) => {
let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
let their_shutdown_scriptpubkey = Readable::read(reader)?;
let (monitor_last_block, channel_monitor) = ReadableArgs::read(reader, logger.clone())?;
// We drop the ChannelMonitor's last block connected hash cause we don't actually bother
- // doing full block connection operations on the internal CHannelMonitor copies
+ // doing full block connection operations on the internal ChannelMonitor copies
if monitor_last_block != last_block_connected {
return Err(DecodeError::InvalidValue);
}
PublicKey::from_secret_key(&secp_ctx, &channel_close_key)
}
- fn get_channel_keys(&self, _inbound: bool, channel_value_satoshis: u64) -> InMemoryChannelKeys {
+ fn get_channel_keys(&self, _inbound: bool, _channel_value_satoshis: u64) -> InMemoryChannelKeys {
self.chan_keys.clone()
}
fn get_onion_rand(&self) -> (SecretKey, [u8; 32]) { panic!(); }
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let secp_ctx = Secp256k1::new();
- let chan_keys = InMemoryChannelKeys {
- funding_key: SecretKey::from_slice(&hex::decode("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f3749").unwrap()[..]).unwrap(),
- payment_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
- delayed_payment_base_key: SecretKey::from_slice(&hex::decode("3333333333333333333333333333333333333333333333333333333333333333").unwrap()[..]).unwrap(),
- htlc_base_key: SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
+ let chan_keys = InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&hex::decode("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f3749").unwrap()[..]).unwrap(),
+ SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
+ SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
+ SecretKey::from_slice(&hex::decode("3333333333333333333333333333333333333333333333333333333333333333").unwrap()[..]).unwrap(),
+ SecretKey::from_slice(&hex::decode("1111111111111111111111111111111111111111111111111111111111111111").unwrap()[..]).unwrap(),
// These aren't set in the test vectors:
- revocation_base_key: SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
- commitment_seed: [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
- remote_channel_pubkeys: None,
- channel_value_satoshis: 7000000000,
- };
+ [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
+ 7000000000,
+ );
+
assert_eq!(PublicKey::from_secret_key(&secp_ctx, chan_keys.funding_key()).serialize()[..],
hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]);
let keys_provider: Arc<KeysInterface<ChanKeySigner = InMemoryChannelKeys>> = Arc::new(Keys { chan_keys });
use util::config::UserConfig;
use util::{byte_utils, events};
use util::ser::{Readable, ReadableArgs, Writeable, Writer};
-use util::chacha20::ChaCha20;
+use util::chacha20::{ChaCha20, ChaChaReader};
use util::logger::Logger;
use util::errors::APIError;
use std::{cmp, mem};
use std::collections::{HashMap, hash_map, HashSet};
-use std::io::Cursor;
+use std::io::{Cursor, Read};
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use std::marker::{Sync, Send};
use std::ops::Deref;
-const SIXTY_FIVE_ZEROS: [u8; 65] = [0; 65];
-
// We hold various information about HTLC relay in the HTLC objects in Channel itself:
//
// Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should
}
}
#[inline]
- fn from_chan_no_close(err: ChannelError, channel_id: [u8; 32]) -> Self {
+ fn from_chan_no_close<ChanSigner: ChannelKeys>(err: ChannelError<ChanSigner>, channel_id: [u8; 32]) -> Self {
Self {
err: match err {
ChannelError::Ignore(msg) => LightningError {
/// essentially you should default to using a SimpleRefChannelManager, and use a
/// SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
/// you're using lightning-net-tokio.
-pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref> where M::Target: ManyChannelMonitor {
+pub struct ChannelManager<ChanSigner: ChannelKeys, M: Deref> where M::Target: ManyChannelMonitor<ChanSigner> {
default_configuration: UserConfig,
genesis_hash: Sha256dHash,
fee_estimator: Arc<FeeEstimator>,
match $res {
Ok(res) => res,
Err(ChannelError::Ignore(msg)) => {
- break Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
+ break Err(MsgHandleErrInternal::from_chan_no_close::<ChanSigner>(ChannelError::Ignore(msg), $entry.key().clone()))
},
Err(ChannelError::Close(msg)) => {
log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
match $res {
Ok(res) => res,
Err(ChannelError::Ignore(msg)) => {
- return Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore(msg), $entry.key().clone()))
+ return Err(MsgHandleErrInternal::from_chan_no_close::<ChanSigner>(ChannelError::Ignore(msg), $entry.key().clone()))
},
Err(ChannelError::Close(msg)) => {
log_trace!($self, "Closing channel {} due to Close-required error: {}", log_bytes!($entry.key()[..]), msg);
$channel_state.short_to_id.remove(&short_id);
}
if let Some(update) = update {
- if let Err(e) = $self.monitor.add_update_monitor(update.get_funding_txo().unwrap(), update) {
+ if let Err(e) = $self.monitor.add_update_monitor(update.get_funding_txo().unwrap(), update.clone()) {
match e {
// Upstream channel is dead, but we want at least to fail backward HTLCs to save
// downstream channels. In case of PermanentFailure, we are not going to be able
debug_assert!($action_type == RAACommitmentOrder::CommitmentFirst || !$resend_commitment);
}
$entry.get_mut().monitor_update_failed($resend_raa, $resend_commitment, $failed_forwards, $failed_fails);
- Err(MsgHandleErrInternal::from_chan_no_close(ChannelError::Ignore("Failed to update ChannelMonitor"), *$entry.key()))
+ Err(MsgHandleErrInternal::from_chan_no_close::<ChanSigner>(ChannelError::Ignore("Failed to update ChannelMonitor"), *$entry.key()))
},
}
}
}
}
-impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys, M: Deref> ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
/// Constructs a new ChannelManager to hold several channels and route between them.
///
/// This is the main "logic hub" for all channel-related actions, and implements
}
let mut chacha = ChaCha20::new(&rho, &[0u8; 8]);
- let next_hop_data = {
- let mut decoded = [0; 65];
- chacha.process(&msg.onion_routing_packet.hop_data[0..65], &mut decoded);
- match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
+ let mut chacha_stream = ChaChaReader { chacha: &mut chacha, read: Cursor::new(&msg.onion_routing_packet.hop_data[..]) };
+ let (next_hop_data, next_hop_hmac) = {
+ match msgs::OnionHopData::read(&mut chacha_stream) {
Err(err) => {
let error_code = match err {
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
+ msgs::DecodeError::UnknownRequiredFeature|
+ msgs::DecodeError::InvalidValue|
+ msgs::DecodeError::ShortRead => 0x4000 | 22, // invalid_onion_payload
_ => 0x2000 | 2, // Should never happen
};
return_err!("Unable to decode our hop data", error_code, &[0;0]);
},
- Ok(msg) => msg
+ Ok(msg) => {
+ let mut hmac = [0; 32];
+ if let Err(_) = chacha_stream.read_exact(&mut hmac[..]) {
+ return_err!("Unable to decode hop data", 0x4000 | 22, &[0;0]);
+ }
+ (msg, hmac)
+ },
}
};
- let pending_forward_info = if next_hop_data.hmac == [0; 32] {
+ let pending_forward_info = if next_hop_hmac == [0; 32] {
#[cfg(test)]
{
// In tests, make sure that the initial onion pcket data is, at least, non-0.
// as-is (and were originally 0s).
// Of course reverse path calculation is still pretty easy given naive routing
// algorithms, but this fixes the most-obvious case.
- let mut new_packet_data = [0; 19*65];
- chacha.process(&msg.onion_routing_packet.hop_data[65..], &mut new_packet_data[0..19*65]);
- assert_ne!(new_packet_data[0..65], [0; 65][..]);
- assert_ne!(new_packet_data[..], [0; 19*65][..]);
+ let mut next_bytes = [0; 32];
+ chacha_stream.read_exact(&mut next_bytes).unwrap();
+ assert_ne!(next_bytes[..], [0; 32][..]);
+ chacha_stream.read_exact(&mut next_bytes).unwrap();
+ assert_ne!(next_bytes[..], [0; 32][..]);
}
// OUR PAYMENT!
return_err!("The final CLTV expiry is too soon to handle", 17, &[0;0]);
}
// final_incorrect_htlc_amount
- if next_hop_data.data.amt_to_forward > msg.amount_msat {
+ if next_hop_data.amt_to_forward > msg.amount_msat {
return_err!("Upstream node sent less than we were supposed to receive in payment", 19, &byte_utils::be64_to_array(msg.amount_msat));
}
// final_incorrect_cltv_expiry
- if next_hop_data.data.outgoing_cltv_value != msg.cltv_expiry {
+ if next_hop_data.outgoing_cltv_value != msg.cltv_expiry {
return_err!("Upstream node set CLTV to the wrong value", 18, &byte_utils::be32_to_array(msg.cltv_expiry));
}
payment_hash: msg.payment_hash.clone(),
short_channel_id: 0,
incoming_shared_secret: shared_secret,
- amt_to_forward: next_hop_data.data.amt_to_forward,
- outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
+ amt_to_forward: next_hop_data.amt_to_forward,
+ outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
})
} else {
let mut new_packet_data = [0; 20*65];
- chacha.process(&msg.onion_routing_packet.hop_data[65..], &mut new_packet_data[0..19*65]);
- chacha.process(&SIXTY_FIVE_ZEROS[..], &mut new_packet_data[19*65..]);
+ let read_pos = chacha_stream.read(&mut new_packet_data).unwrap();
+ #[cfg(debug_assertions)]
+ {
+ // Check two things:
+ // a) that the behavior of our stream here will return Ok(0) even if the TLV
+ // read above emptied out our buffer and the unwrap() wont needlessly panic
+ // b) that we didn't somehow magically end up with extra data.
+ let mut t = [0; 1];
+ debug_assert!(chacha_stream.read(&mut t).unwrap() == 0);
+ }
+ // Once we've emptied the set of bytes our peer gave us, encrypt 0 bytes until we
+ // fill the onion hop data we'll forward to our next-hop peer.
+ chacha_stream.chacha.process_in_place(&mut new_packet_data[read_pos..]);
let mut new_pubkey = msg.onion_routing_packet.public_key.unwrap();
version: 0,
public_key,
hop_data: new_packet_data,
- hmac: next_hop_data.hmac.clone(),
+ hmac: next_hop_hmac.clone(),
+ };
+
+ let short_channel_id = match next_hop_data.format {
+ msgs::OnionHopDataFormat::Legacy { short_channel_id } => short_channel_id,
+ msgs::OnionHopDataFormat::NonFinalNode { short_channel_id } => short_channel_id,
+ msgs::OnionHopDataFormat::FinalNode => {
+ return_err!("Final Node OnionHopData provided for us as an intermediary node", 0x4000 | 22, &[0;0]);
+ },
};
PendingHTLCStatus::Forward(PendingForwardHTLCInfo {
onion_packet: Some(outgoing_packet),
payment_hash: msg.payment_hash.clone(),
- short_channel_id: next_hop_data.data.short_channel_id,
+ short_channel_id: short_channel_id,
incoming_shared_secret: shared_secret,
- amt_to_forward: next_hop_data.data.amt_to_forward,
- outgoing_cltv_value: next_hop_data.data.outgoing_cltv_value,
+ amt_to_forward: next_hop_data.amt_to_forward,
+ outgoing_cltv_value: next_hop_data.outgoing_cltv_value,
})
};
let onion_keys = secp_call!(onion_utils::construct_onion_keys(&self.secp_ctx, &route, &session_priv),
APIError::RouteError{err: "Pubkey along hop was maliciously selected"});
let (onion_payloads, htlc_msat, htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height)?;
+ if onion_utils::route_size_insane(&onion_payloads) {
+ return Err(APIError::RouteError{err: "Route size too large considering onion data"});
+ }
let onion_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, prng_seed, &payment_hash);
let _ = self.total_consistency_lock.read().unwrap();
}
fn get_announcement_sigs(&self, chan: &Channel<ChanSigner>) -> Option<msgs::AnnouncementSignatures> {
- if !chan.should_announce() { return None }
+ if !chan.should_announce() {
+ log_trace!(self, "Can't send announcement_signatures for private channel {}", log_bytes!(chan.channel_id()));
+ return None
+ }
let (announcement, our_bitcoin_sig) = match chan.get_channel_announcement(self.get_our_node_id(), self.genesis_hash.clone()) {
Ok(res) => res,
}
try_chan_entry!(self, chan.get_mut().funding_locked(&msg), channel_state, chan);
if let Some(announcement_sigs) = self.get_announcement_sigs(chan.get()) {
+ log_trace!(self, "Sending announcement_signatures for {} in response to funding_locked", log_bytes!(chan.get().channel_id()));
// If we see locking block before receiving remote funding_locked, we broadcast our
// announcement_sigs at remote funding_locked reception. If we receive remote
// funding_locked before seeing locking block, we broadcast our announcement_sigs at locking
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
}
if (msg.failure_code & 0x8000) == 0 {
- try_chan_entry!(self, Err(ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set")), channel_state, chan);
+ let chan_err: ChannelError<ChanSigner> = ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set");
+ try_chan_entry!(self, Err(chan_err), channel_state, chan);
}
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), channel_state, chan);
Ok(())
let msghash = hash_to_message!(&Sha256dHash::hash(&announcement.encode()[..])[..]);
if self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }).is_err() ||
self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }).is_err() {
- try_chan_entry!(self, Err(ChannelError::Close("Bad announcement_signatures node_signature")), channel_state, chan);
+ let chan_err: ChannelError<ChanSigner> = ChannelError::Close("Bad announcement_signatures node_signature");
+ try_chan_entry!(self, Err(chan_err), channel_state, chan);
}
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
}
}
-impl<ChanSigner: ChannelKeys, M: Deref> events::MessageSendEventsProvider for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys, M: Deref> events::MessageSendEventsProvider for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
fn get_and_clear_pending_msg_events(&self) -> Vec<events::MessageSendEvent> {
// TODO: Event release to users and serialization is currently race-y: it's very easy for a
// user to serialize a ChannelManager with pending events in it and lose those events on
}
}
-impl<ChanSigner: ChannelKeys, M: Deref> events::EventsProvider for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys, M: Deref> events::EventsProvider for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
// TODO: Event release to users and serialization is currently race-y: it's very easy for a
// user to serialize a ChannelManager with pending events in it and lose those events on
}
}
-impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send> ChainListener for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send> ChainListener for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) {
let header_hash = header.bitcoin_hash();
log_trace!(self, "Block {} at height {} connected with {} txn matched", header_hash, height, txn_matched.len());
msg: funding_locked,
});
if let Some(announcement_sigs) = self.get_announcement_sigs(channel) {
+ log_trace!(self, "Sending funding_locked and announcement_signatures for {}", log_bytes!(channel.channel_id()));
pending_msg_events.push(events::MessageSendEvent::SendAnnouncementSignatures {
node_id: channel.get_their_node_id(),
msg: announcement_sigs,
});
+ } else {
+ log_trace!(self, "Sending funding_locked WITHOUT announcement_signatures for {}", log_bytes!(channel.channel_id()));
}
short_to_id.insert(channel.get_short_channel_id().unwrap(), channel.channel_id());
} else if let Err(e) = chan_res {
}
}
-impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send> ChannelMessageHandler for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys, M: Deref + Sync + Send> ChannelMessageHandler for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
fn handle_open_channel(&self, their_node_id: &PublicKey, their_features: InitFeatures, msg: &msgs::OpenChannel) {
let _ = self.total_consistency_lock.read().unwrap();
let res = self.internal_open_channel(their_node_id, their_features, msg);
}
}
-impl<ChanSigner: ChannelKeys + Writeable, M: Deref> Writeable for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor {
+impl<ChanSigner: ChannelKeys + Writeable, M: Deref> Writeable for ChannelManager<ChanSigner, M> where M::Target: ManyChannelMonitor<ChanSigner> {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
let _ = self.total_consistency_lock.write().unwrap();
/// 5) Move the ChannelMonitors into your local ManyChannelMonitor.
/// 6) Disconnect/connect blocks on the ChannelManager.
/// 7) Register the new ChannelManager with your ChainWatchInterface.
-pub struct ChannelManagerReadArgs<'a, ChanSigner: ChannelKeys, M: Deref> where M::Target: ManyChannelMonitor {
+pub struct ChannelManagerReadArgs<'a, ChanSigner: 'a + ChannelKeys, M: Deref> where M::Target: ManyChannelMonitor<ChanSigner> {
/// The keys provider which will give us relevant keys. Some keys will be loaded during
/// deserialization.
pub keys_manager: Arc<KeysInterface<ChanKeySigner = ChanSigner>>,
///
/// In such cases the latest local transactions will be sent to the tx_broadcaster included in
/// this struct.
- pub channel_monitors: &'a mut HashMap<OutPoint, &'a mut ChannelMonitor>,
+ pub channel_monitors: &'a mut HashMap<OutPoint, &'a mut ChannelMonitor<ChanSigner>>,
}
-impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> ReadableArgs<R, ChannelManagerReadArgs<'a, ChanSigner, M>> for (Sha256dHash, ChannelManager<ChanSigner, M>) where M::Target: ManyChannelMonitor {
+impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>, M: Deref> ReadableArgs<R, ChannelManagerReadArgs<'a, ChanSigner, M>> for (Sha256dHash, ChannelManager<ChanSigner, M>) where M::Target: ManyChannelMonitor<ChanSigner> {
fn read(reader: &mut R, args: ChannelManagerReadArgs<'a, ChanSigner, M>) -> Result<Self, DecodeError> {
let _ver: u8 = Readable::read(reader)?;
let min_ver: u8 = Readable::read(reader)?;
use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
use chain::chaininterface::{ChainListener, ChainWatchInterface, BroadcasterInterface, FeeEstimator, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
use chain::transaction::OutPoint;
-use chain::keysinterface::SpendableOutputDescriptor;
+use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
use util::logger::Logger;
use util::ser::{ReadableArgs, Readable, Writer, Writeable, U48};
use util::{byte_utils, events};
/// than calling these methods directly, the user should register implementors as listeners to the
/// BlockNotifier and call the BlockNotifier's `block_(dis)connected` methods, which will notify
/// all registered listeners in one go.
-pub trait ManyChannelMonitor: Send + Sync {
+pub trait ManyChannelMonitor<ChanSigner: ChannelKeys>: Send + Sync {
/// Adds or updates a monitor for the given `funding_txo`.
///
/// Implementor must also ensure that the funding_txo outpoint is registered with any relevant
/// ChainWatchInterfaces such that the provided monitor receives block_connected callbacks with
/// any spends of it.
- fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr>;
+ fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr>;
/// Used by ChannelManager to get list of HTLC resolved onchain and which needed to be updated
/// with success or failure backward
///
/// If you're using this for local monitoring of your own channels, you probably want to use
/// `OutPoint` as the key, which will give you a ManyChannelMonitor implementation.
-pub struct SimpleManyChannelMonitor<Key> {
+pub struct SimpleManyChannelMonitor<Key, ChanSigner: ChannelKeys> {
#[cfg(test)] // Used in ChannelManager tests to manipulate channels directly
- pub monitors: Mutex<HashMap<Key, ChannelMonitor>>,
+ pub monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
#[cfg(not(test))]
- monitors: Mutex<HashMap<Key, ChannelMonitor>>,
+ monitors: Mutex<HashMap<Key, ChannelMonitor<ChanSigner>>>,
chain_monitor: Arc<ChainWatchInterface>,
broadcaster: Arc<BroadcasterInterface>,
pending_events: Mutex<Vec<events::Event>>,
fee_estimator: Arc<FeeEstimator>
}
-impl<'a, Key : Send + cmp::Eq + hash::Hash> ChainListener for SimpleManyChannelMonitor<Key> {
+impl<'a, Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys> ChainListener for SimpleManyChannelMonitor<Key, ChanSigner> {
fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], _indexes_of_txn_matched: &[u32]) {
let block_hash = header.bitcoin_hash();
let mut new_events: Vec<events::Event> = Vec::with_capacity(0);
}
}
-impl<Key : Send + cmp::Eq + hash::Hash + 'static> SimpleManyChannelMonitor<Key> {
+impl<Key : Send + cmp::Eq + hash::Hash + 'static, ChanSigner: ChannelKeys> SimpleManyChannelMonitor<Key, ChanSigner> {
/// Creates a new object which can be used to monitor several channels given the chain
/// interface with which to register to receive notifications.
- pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> SimpleManyChannelMonitor<Key> {
+ pub fn new(chain_monitor: Arc<ChainWatchInterface>, broadcaster: Arc<BroadcasterInterface>, logger: Arc<Logger>, feeest: Arc<FeeEstimator>) -> SimpleManyChannelMonitor<Key, ChanSigner> {
let res = SimpleManyChannelMonitor {
monitors: Mutex::new(HashMap::new()),
chain_monitor,
}
/// Adds or updates the monitor which monitors the channel referred to by the given key.
- pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor) -> Result<(), MonitorUpdateError> {
+ pub fn add_update_monitor_by_key(&self, key: Key, monitor: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
let mut monitors = self.monitors.lock().unwrap();
match monitors.get_mut(&key) {
Some(orig_monitor) => {
}
}
-impl ManyChannelMonitor for SimpleManyChannelMonitor<OutPoint> {
- fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor) -> Result<(), ChannelMonitorUpdateErr> {
+impl<ChanSigner: ChannelKeys> ManyChannelMonitor<ChanSigner> for SimpleManyChannelMonitor<OutPoint, ChanSigner> {
+ fn add_update_monitor(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), ChannelMonitorUpdateErr> {
match self.add_update_monitor_by_key(funding_txo, monitor) {
Ok(_) => Ok(()),
Err(_) => Err(ChannelMonitorUpdateErr::PermanentFailure),
}
}
-impl<Key : Send + cmp::Eq + hash::Hash> events::EventsProvider for SimpleManyChannelMonitor<Key> {
+impl<Key : Send + cmp::Eq + hash::Hash, ChanSigner: ChannelKeys> events::EventsProvider for SimpleManyChannelMonitor<Key, ChanSigner> {
fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
let mut pending_events = self.pending_events.lock().unwrap();
let mut ret = Vec::new();
/// keeping bumping another claim tx to solve the outpoint.
pub(crate) const ANTI_REORG_DELAY: u32 = 6;
-#[derive(Clone, PartialEq)]
-enum Storage {
+#[derive(Clone)]
+enum Storage<ChanSigner: ChannelKeys> {
Local {
+ keys: ChanSigner,
funding_key: SecretKey,
revocation_base_key: SecretKey,
htlc_base_key: SecretKey,
}
}
+#[cfg(any(test, feature = "fuzztarget"))]
+impl<ChanSigner: ChannelKeys> PartialEq for Storage<ChanSigner> {
+ fn eq(&self, other: &Self) -> bool {
+ match *self {
+ Storage::Local { ref keys, .. } => {
+ let k = keys;
+ match *other {
+ Storage::Local { ref keys, .. } => keys.pubkeys() == k.pubkeys(),
+ Storage::Watchtower { .. } => false,
+ }
+ },
+ Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} => {
+ let (rbk, hbk) = (revocation_base_key, htlc_base_key);
+ match *other {
+ Storage::Local { .. } => false,
+ Storage::Watchtower {ref revocation_base_key, ref htlc_base_key} =>
+ revocation_base_key == rbk && htlc_base_key == hbk,
+ }
+ },
+ }
+ }
+}
+
#[derive(Clone, PartialEq)]
struct LocalSignedTx {
/// txid of the transaction in tx, just used to make comparison faster
/// You MUST ensure that no ChannelMonitors for a given channel anywhere contain out-of-date
/// information and are actively monitoring the chain.
#[derive(Clone)]
-pub struct ChannelMonitor {
+pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
commitment_transaction_number_obscure_factor: u64,
- key_storage: Storage,
+ key_storage: Storage<ChanSigner>,
their_htlc_base_key: Option<PublicKey>,
their_delayed_payment_base_key: Option<PublicKey>,
funding_redeemscript: Option<Script>,
#[cfg(any(test, feature = "fuzztarget"))]
/// Used only in testing and fuzztarget to check serialization roundtrips don't change the
/// underlying object
-impl PartialEq for ChannelMonitor {
+impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
fn eq(&self, other: &Self) -> bool {
if self.commitment_transaction_number_obscure_factor != other.commitment_transaction_number_obscure_factor ||
self.key_storage != other.key_storage ||
}
}
-impl ChannelMonitor {
- pub(super) fn new(funding_key: &SecretKey, revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor {
+impl<ChanSigner: ChannelKeys + Writeable> ChannelMonitor<ChanSigner> {
+ /// Serializes into a vec, with various modes for the exposed pub fns
+ fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
+ //TODO: We still write out all the serialization here manually instead of using the fancy
+ //serialization framework we have, we should migrate things over to it.
+ writer.write_all(&[SERIALIZATION_VERSION; 1])?;
+ writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
+
+ // Set in initial Channel-object creation, so should always be set by now:
+ U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
+
+ macro_rules! write_option {
+ ($thing: expr) => {
+ match $thing {
+ &Some(ref t) => {
+ 1u8.write(writer)?;
+ t.write(writer)?;
+ },
+ &None => 0u8.write(writer)?,
+ }
+ }
+ }
+
+ match self.key_storage {
+ Storage::Local { ref keys, ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
+ writer.write_all(&[0; 1])?;
+ keys.write(writer)?;
+ writer.write_all(&funding_key[..])?;
+ writer.write_all(&revocation_base_key[..])?;
+ writer.write_all(&htlc_base_key[..])?;
+ writer.write_all(&delayed_payment_base_key[..])?;
+ writer.write_all(&payment_base_key[..])?;
+ writer.write_all(&shutdown_pubkey.serialize())?;
+ match funding_info {
+ &Some((ref outpoint, ref script)) => {
+ writer.write_all(&outpoint.txid[..])?;
+ writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
+ script.write(writer)?;
+ },
+ &None => {
+ debug_assert!(false, "Try to serialize a useless Local monitor !");
+ },
+ }
+ current_remote_commitment_txid.write(writer)?;
+ prev_remote_commitment_txid.write(writer)?;
+ },
+ Storage::Watchtower { .. } => unimplemented!(),
+ }
+
+ writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
+ writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
+ self.funding_redeemscript.as_ref().unwrap().write(writer)?;
+ self.channel_value_satoshis.unwrap().write(writer)?;
+
+ match self.their_cur_revocation_points {
+ Some((idx, pubkey, second_option)) => {
+ writer.write_all(&byte_utils::be48_to_array(idx))?;
+ writer.write_all(&pubkey.serialize())?;
+ match second_option {
+ Some(second_pubkey) => {
+ writer.write_all(&second_pubkey.serialize())?;
+ },
+ None => {
+ writer.write_all(&[0; 33])?;
+ },
+ }
+ },
+ None => {
+ writer.write_all(&byte_utils::be48_to_array(0))?;
+ },
+ }
+
+ writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
+ writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
+
+ for &(ref secret, ref idx) in self.old_secrets.iter() {
+ writer.write_all(secret)?;
+ writer.write_all(&byte_utils::be64_to_array(*idx))?;
+ }
+
+ macro_rules! serialize_htlc_in_commitment {
+ ($htlc_output: expr) => {
+ writer.write_all(&[$htlc_output.offered as u8; 1])?;
+ writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
+ writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
+ writer.write_all(&$htlc_output.payment_hash.0[..])?;
+ $htlc_output.transaction_output_index.write(writer)?;
+ }
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
+ for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
+ writer.write_all(&txid[..])?;
+ writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
+ for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
+ serialize_htlc_in_commitment!(htlc_output);
+ write_option!(htlc_source);
+ }
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
+ for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
+ writer.write_all(&txid[..])?;
+ writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
+ (txouts.len() as u64).write(writer)?;
+ for script in txouts.iter() {
+ script.write(writer)?;
+ }
+ }
+
+ if for_local_storage {
+ writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
+ for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
+ writer.write_all(&payment_hash.0[..])?;
+ writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
+ }
+ } else {
+ writer.write_all(&byte_utils::be64_to_array(0))?;
+ }
+
+ macro_rules! serialize_local_tx {
+ ($local_tx: expr) => {
+ $local_tx.tx.write(writer)?;
+ writer.write_all(&$local_tx.revocation_key.serialize())?;
+ writer.write_all(&$local_tx.a_htlc_key.serialize())?;
+ writer.write_all(&$local_tx.b_htlc_key.serialize())?;
+ writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
+ writer.write_all(&$local_tx.per_commitment_point.serialize())?;
+
+ writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
+ writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
+ for &(ref htlc_output, ref sig, ref htlc_source) in $local_tx.htlc_outputs.iter() {
+ serialize_htlc_in_commitment!(htlc_output);
+ if let &Some(ref their_sig) = sig {
+ 1u8.write(writer)?;
+ writer.write_all(&their_sig.serialize_compact())?;
+ } else {
+ 0u8.write(writer)?;
+ }
+ write_option!(htlc_source);
+ }
+ }
+ }
+
+ if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
+ writer.write_all(&[1; 1])?;
+ serialize_local_tx!(prev_local_tx);
+ } else {
+ writer.write_all(&[0; 1])?;
+ }
+
+ if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
+ writer.write_all(&[1; 1])?;
+ serialize_local_tx!(cur_local_tx);
+ } else {
+ writer.write_all(&[0; 1])?;
+ }
+
+ if for_local_storage {
+ writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
+ } else {
+ writer.write_all(&byte_utils::be48_to_array(0))?;
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
+ for payment_preimage in self.payment_preimages.values() {
+ writer.write_all(&payment_preimage.0[..])?;
+ }
+
+ self.last_block_hash.write(writer)?;
+ self.destination_script.write(writer)?;
+ if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
+ writer.write_all(&[1; 1])?;
+ to_remote_script.write(writer)?;
+ local_key.write(writer)?;
+ } else {
+ writer.write_all(&[0; 1])?;
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
+ for (ref ancestor_claim_txid, claim_tx_data) in self.pending_claim_requests.iter() {
+ ancestor_claim_txid.write(writer)?;
+ claim_tx_data.write(writer)?;
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.claimable_outpoints.len() as u64))?;
+ for (ref outp, ref claim_and_height) in self.claimable_outpoints.iter() {
+ outp.write(writer)?;
+ claim_and_height.0.write(writer)?;
+ claim_and_height.1.write(writer)?;
+ }
+
+ writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
+ for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
+ writer.write_all(&byte_utils::be32_to_array(**target))?;
+ writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
+ for ev in events.iter() {
+ match *ev {
+ OnchainEvent::Claim { ref claim_request } => {
+ writer.write_all(&[0; 1])?;
+ claim_request.write(writer)?;
+ },
+ OnchainEvent::HTLCUpdate { ref htlc_update } => {
+ writer.write_all(&[1; 1])?;
+ htlc_update.0.write(writer)?;
+ htlc_update.1.write(writer)?;
+ },
+ OnchainEvent::ContentiousOutpoint { ref outpoint, ref input_material } => {
+ writer.write_all(&[2; 1])?;
+ outpoint.write(writer)?;
+ input_material.write(writer)?;
+ }
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ /// Writes this monitor into the given writer, suitable for writing to disk.
+ ///
+ /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
+ /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+ /// the "reorg path" (ie not just starting at the same height but starting at the highest
+ /// common block that appears on your best chain as well as on the chain which contains the
+ /// last block hash returned) upon deserializing the object!
+ pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ self.write(writer, true)
+ }
+
+ /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
+ ///
+ /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
+ /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
+ /// the "reorg path" (ie not just starting at the same height but starting at the highest
+ /// common block that appears on your best chain as well as on the chain which contains the
+ /// last block hash returned) upon deserializing the object!
+ pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ self.write(writer, false)
+ }
+}
+
+impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
+ pub(super) fn new(keys: ChanSigner, funding_key: &SecretKey, revocation_base_key: &SecretKey, delayed_payment_base_key: &SecretKey, htlc_base_key: &SecretKey, payment_base_key: &SecretKey, shutdown_pubkey: &PublicKey, our_to_self_delay: u16, destination_script: Script, logger: Arc<Logger>) -> ChannelMonitor<ChanSigner> {
ChannelMonitor {
commitment_transaction_number_obscure_factor: 0,
key_storage: Storage::Local {
+ keys,
funding_key: funding_key.clone(),
revocation_base_key: revocation_base_key.clone(),
htlc_base_key: htlc_base_key.clone(),
/// needed by local commitment transactions HTCLs nor by remote ones. Unless we haven't already seen remote
/// commitment transaction's secret, they are de facto pruned (we can use revocation key).
pub(super) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), MonitorUpdateError> {
- let pos = ChannelMonitor::place_secret(idx);
+ let pos = ChannelMonitor::<ChanSigner>::place_secret(idx);
for i in 0..pos {
let (old_secret, old_idx) = self.old_secrets[i as usize];
- if ChannelMonitor::derive_secret(secret, pos, old_idx) != old_secret {
+ if ChannelMonitor::<ChanSigner>::derive_secret(secret, pos, old_idx) != old_secret {
return Err(MonitorUpdateError("Previous secret did not match new one"));
}
}
pub(super) fn provide_rescue_remote_commitment_tx_info(&mut self, their_revocation_point: PublicKey) {
match self.key_storage {
- Storage::Local { ref payment_base_key, .. } => {
- if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &payment_base_key)) {
+ Storage::Local { ref payment_base_key, ref keys, .. } => {
+ if let Ok(payment_key) = chan_utils::derive_public_key(&self.secp_ctx, &their_revocation_point, &keys.pubkeys().payment_basepoint) {
let to_remote_script = Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
.push_slice(&Hash160::hash(&payment_key.serialize())[..])
.into_script();
/// Combines this ChannelMonitor with the information contained in the other ChannelMonitor.
/// After a successful call this ChannelMonitor is up-to-date and is safe to use to monitor the
/// chain for new blocks/transactions.
- pub fn insert_combine(&mut self, mut other: ChannelMonitor) -> Result<(), MonitorUpdateError> {
+ pub fn insert_combine(&mut self, mut other: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
match self.key_storage {
Storage::Local { ref funding_info, .. } => {
if funding_info.is_none() { return Err(MonitorUpdateError("Try to combine a Local monitor without funding_info")); }
res
}
- /// Serializes into a vec, with various modes for the exposed pub fns
- fn write<W: Writer>(&self, writer: &mut W, for_local_storage: bool) -> Result<(), ::std::io::Error> {
- //TODO: We still write out all the serialization here manually instead of using the fancy
- //serialization framework we have, we should migrate things over to it.
- writer.write_all(&[SERIALIZATION_VERSION; 1])?;
- writer.write_all(&[MIN_SERIALIZATION_VERSION; 1])?;
-
- // Set in initial Channel-object creation, so should always be set by now:
- U48(self.commitment_transaction_number_obscure_factor).write(writer)?;
-
- macro_rules! write_option {
- ($thing: expr) => {
- match $thing {
- &Some(ref t) => {
- 1u8.write(writer)?;
- t.write(writer)?;
- },
- &None => 0u8.write(writer)?,
- }
- }
- }
-
- match self.key_storage {
- Storage::Local { ref funding_key, ref revocation_base_key, ref htlc_base_key, ref delayed_payment_base_key, ref payment_base_key, ref shutdown_pubkey, ref funding_info, ref current_remote_commitment_txid, ref prev_remote_commitment_txid } => {
- writer.write_all(&[0; 1])?;
- writer.write_all(&funding_key[..])?;
- writer.write_all(&revocation_base_key[..])?;
- writer.write_all(&htlc_base_key[..])?;
- writer.write_all(&delayed_payment_base_key[..])?;
- writer.write_all(&payment_base_key[..])?;
- writer.write_all(&shutdown_pubkey.serialize())?;
- match funding_info {
- &Some((ref outpoint, ref script)) => {
- writer.write_all(&outpoint.txid[..])?;
- writer.write_all(&byte_utils::be16_to_array(outpoint.index))?;
- script.write(writer)?;
- },
- &None => {
- debug_assert!(false, "Try to serialize a useless Local monitor !");
- },
- }
- current_remote_commitment_txid.write(writer)?;
- prev_remote_commitment_txid.write(writer)?;
- },
- Storage::Watchtower { .. } => unimplemented!(),
- }
-
- writer.write_all(&self.their_htlc_base_key.as_ref().unwrap().serialize())?;
- writer.write_all(&self.their_delayed_payment_base_key.as_ref().unwrap().serialize())?;
- self.funding_redeemscript.as_ref().unwrap().write(writer)?;
- self.channel_value_satoshis.unwrap().write(writer)?;
-
- match self.their_cur_revocation_points {
- Some((idx, pubkey, second_option)) => {
- writer.write_all(&byte_utils::be48_to_array(idx))?;
- writer.write_all(&pubkey.serialize())?;
- match second_option {
- Some(second_pubkey) => {
- writer.write_all(&second_pubkey.serialize())?;
- },
- None => {
- writer.write_all(&[0; 33])?;
- },
- }
- },
- None => {
- writer.write_all(&byte_utils::be48_to_array(0))?;
- },
- }
-
- writer.write_all(&byte_utils::be16_to_array(self.our_to_self_delay))?;
- writer.write_all(&byte_utils::be16_to_array(self.their_to_self_delay.unwrap()))?;
-
- for &(ref secret, ref idx) in self.old_secrets.iter() {
- writer.write_all(secret)?;
- writer.write_all(&byte_utils::be64_to_array(*idx))?;
- }
-
- macro_rules! serialize_htlc_in_commitment {
- ($htlc_output: expr) => {
- writer.write_all(&[$htlc_output.offered as u8; 1])?;
- writer.write_all(&byte_utils::be64_to_array($htlc_output.amount_msat))?;
- writer.write_all(&byte_utils::be32_to_array($htlc_output.cltv_expiry))?;
- writer.write_all(&$htlc_output.payment_hash.0[..])?;
- $htlc_output.transaction_output_index.write(writer)?;
- }
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.remote_claimable_outpoints.len() as u64))?;
- for (ref txid, ref htlc_infos) in self.remote_claimable_outpoints.iter() {
- writer.write_all(&txid[..])?;
- writer.write_all(&byte_utils::be64_to_array(htlc_infos.len() as u64))?;
- for &(ref htlc_output, ref htlc_source) in htlc_infos.iter() {
- serialize_htlc_in_commitment!(htlc_output);
- write_option!(htlc_source);
- }
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.remote_commitment_txn_on_chain.len() as u64))?;
- for (ref txid, &(commitment_number, ref txouts)) in self.remote_commitment_txn_on_chain.iter() {
- writer.write_all(&txid[..])?;
- writer.write_all(&byte_utils::be48_to_array(commitment_number))?;
- (txouts.len() as u64).write(writer)?;
- for script in txouts.iter() {
- script.write(writer)?;
- }
- }
-
- if for_local_storage {
- writer.write_all(&byte_utils::be64_to_array(self.remote_hash_commitment_number.len() as u64))?;
- for (ref payment_hash, commitment_number) in self.remote_hash_commitment_number.iter() {
- writer.write_all(&payment_hash.0[..])?;
- writer.write_all(&byte_utils::be48_to_array(*commitment_number))?;
- }
- } else {
- writer.write_all(&byte_utils::be64_to_array(0))?;
- }
-
- macro_rules! serialize_local_tx {
- ($local_tx: expr) => {
- $local_tx.tx.write(writer)?;
- writer.write_all(&$local_tx.revocation_key.serialize())?;
- writer.write_all(&$local_tx.a_htlc_key.serialize())?;
- writer.write_all(&$local_tx.b_htlc_key.serialize())?;
- writer.write_all(&$local_tx.delayed_payment_key.serialize())?;
- writer.write_all(&$local_tx.per_commitment_point.serialize())?;
-
- writer.write_all(&byte_utils::be64_to_array($local_tx.feerate_per_kw))?;
- writer.write_all(&byte_utils::be64_to_array($local_tx.htlc_outputs.len() as u64))?;
- for &(ref htlc_output, ref sig, ref htlc_source) in $local_tx.htlc_outputs.iter() {
- serialize_htlc_in_commitment!(htlc_output);
- if let &Some(ref their_sig) = sig {
- 1u8.write(writer)?;
- writer.write_all(&their_sig.serialize_compact())?;
- } else {
- 0u8.write(writer)?;
- }
- write_option!(htlc_source);
- }
- }
- }
-
- if let Some(ref prev_local_tx) = self.prev_local_signed_commitment_tx {
- writer.write_all(&[1; 1])?;
- serialize_local_tx!(prev_local_tx);
- } else {
- writer.write_all(&[0; 1])?;
- }
-
- if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
- writer.write_all(&[1; 1])?;
- serialize_local_tx!(cur_local_tx);
- } else {
- writer.write_all(&[0; 1])?;
- }
-
- if for_local_storage {
- writer.write_all(&byte_utils::be48_to_array(self.current_remote_commitment_number))?;
- } else {
- writer.write_all(&byte_utils::be48_to_array(0))?;
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.payment_preimages.len() as u64))?;
- for payment_preimage in self.payment_preimages.values() {
- writer.write_all(&payment_preimage.0[..])?;
- }
-
- self.last_block_hash.write(writer)?;
- self.destination_script.write(writer)?;
- if let Some((ref to_remote_script, ref local_key)) = self.to_remote_rescue {
- writer.write_all(&[1; 1])?;
- to_remote_script.write(writer)?;
- local_key.write(writer)?;
- } else {
- writer.write_all(&[0; 1])?;
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.pending_claim_requests.len() as u64))?;
- for (ref ancestor_claim_txid, claim_tx_data) in self.pending_claim_requests.iter() {
- ancestor_claim_txid.write(writer)?;
- claim_tx_data.write(writer)?;
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.claimable_outpoints.len() as u64))?;
- for (ref outp, ref claim_and_height) in self.claimable_outpoints.iter() {
- outp.write(writer)?;
- claim_and_height.0.write(writer)?;
- claim_and_height.1.write(writer)?;
- }
-
- writer.write_all(&byte_utils::be64_to_array(self.onchain_events_waiting_threshold_conf.len() as u64))?;
- for (ref target, ref events) in self.onchain_events_waiting_threshold_conf.iter() {
- writer.write_all(&byte_utils::be32_to_array(**target))?;
- writer.write_all(&byte_utils::be64_to_array(events.len() as u64))?;
- for ev in events.iter() {
- match *ev {
- OnchainEvent::Claim { ref claim_request } => {
- writer.write_all(&[0; 1])?;
- claim_request.write(writer)?;
- },
- OnchainEvent::HTLCUpdate { ref htlc_update } => {
- writer.write_all(&[1; 1])?;
- htlc_update.0.write(writer)?;
- htlc_update.1.write(writer)?;
- },
- OnchainEvent::ContentiousOutpoint { ref outpoint, ref input_material } => {
- writer.write_all(&[2; 1])?;
- outpoint.write(writer)?;
- input_material.write(writer)?;
- }
- }
- }
- }
-
- Ok(())
- }
-
- /// Writes this monitor into the given writer, suitable for writing to disk.
- ///
- /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
- /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
- /// the "reorg path" (ie not just starting at the same height but starting at the highest
- /// common block that appears on your best chain as well as on the chain which contains the
- /// last block hash returned) upon deserializing the object!
- pub fn write_for_disk<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
- self.write(writer, true)
- }
-
- /// Encodes this monitor into the given writer, suitable for sending to a remote watchtower
- ///
- /// Note that the deserializer is only implemented for (Sha256dHash, ChannelMonitor), which
- /// tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
- /// the "reorg path" (ie not just starting at the same height but starting at the highest
- /// common block that appears on your best chain as well as on the chain which contains the
- /// last block hash returned) upon deserializing the object!
- pub fn write_for_watchtower<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
- self.write(writer, false)
- }
-
/// Can only fail if idx is < get_min_seen_secret
pub(super) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
for i in 0..self.old_secrets.len() {
if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
- return Some(ChannelMonitor::derive_secret(self.old_secrets[i].0, i as u8, idx))
+ return Some(ChannelMonitor::<ChanSigner>::derive_secret(self.old_secrets[i].0, i as u8, idx))
}
}
assert!(idx < self.get_min_seen_secret());
let secret = self.get_secret(commitment_number).unwrap();
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
let (revocation_pubkey, b_htlc_key, local_payment_key) = match self.key_storage {
- Storage::Local { ref revocation_base_key, ref htlc_base_key, ref payment_base_key, .. } => {
+ Storage::Local { ref keys, ref payment_base_key, .. } => {
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
- (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
- ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))),
+ (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint)),
+ ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().htlc_basepoint)),
Some(ignore_error!(chan_utils::derive_private_key(&self.secp_ctx, &per_commitment_point, &payment_base_key))))
},
Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
} else { None };
if let Some(revocation_point) = revocation_point_option {
let (revocation_pubkey, b_htlc_key) = match self.key_storage {
- Storage::Local { ref revocation_base_key, ref htlc_base_key, .. } => {
- (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key))),
- ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &PublicKey::from_secret_key(&self.secp_ctx, &htlc_base_key))))
+ Storage::Local { ref keys, .. } => {
+ (ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &keys.pubkeys().revocation_basepoint)),
+ ignore_error!(chan_utils::derive_public_key(&self.secp_ctx, revocation_point, &keys.pubkeys().htlc_basepoint)))
},
Storage::Watchtower { ref revocation_base_key, ref htlc_base_key, .. } => {
(ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, revocation_point, &revocation_base_key)),
let per_commitment_key = ignore_error!(SecretKey::from_slice(&secret));
let per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &per_commitment_key);
let revocation_pubkey = match self.key_storage {
- Storage::Local { ref revocation_base_key, .. } => {
- ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &PublicKey::from_secret_key(&self.secp_ctx, &revocation_base_key)))
+ Storage::Local { ref keys, .. } => {
+ ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &keys.pubkeys().revocation_basepoint))
},
Storage::Watchtower { ref revocation_base_key, .. } => {
ignore_error!(chan_utils::derive_public_revocation_key(&self.secp_ctx, &per_commitment_point, &revocation_base_key))
}
fn block_connected(&mut self, txn_matched: &[&Transaction], height: u32, block_hash: &Sha256dHash, broadcaster: &BroadcasterInterface, fee_estimator: &FeeEstimator)-> (Vec<(Sha256dHash, Vec<TxOut>)>, Vec<SpendableOutputDescriptor>, Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)>) {
+ for tx in txn_matched {
+ let mut output_val = 0;
+ for out in tx.output.iter() {
+ if out.value > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
+ output_val += out.value;
+ if output_val > 21_000_000_0000_0000 { panic!("Value-overflowing transaction provided to block connected"); }
+ }
+ }
+
log_trace!(self, "Block {} at height {} connected with {} txn matched", block_hash, height, txn_matched.len());
let mut watch_outputs = Vec::new();
let mut spendable_outputs = Vec::new();
const MAX_ALLOC_SIZE: usize = 64*1024;
-impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor) {
+impl<R: ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelMonitor<ChanSigner>) {
fn read(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
let secp_ctx = Secp256k1::new();
macro_rules! unwrap_obj {
let key_storage = match <u8 as Readable<R>>::read(reader)? {
0 => {
+ let keys = Readable::read(reader)?;
let funding_key = Readable::read(reader)?;
let revocation_base_key = Readable::read(reader)?;
let htlc_base_key = Readable::read(reader)?;
let current_remote_commitment_txid = Readable::read(reader)?;
let prev_remote_commitment_txid = Readable::read(reader)?;
Storage::Local {
+ keys,
funding_key,
revocation_base_key,
htlc_base_key,
use secp256k1::Secp256k1;
use rand::{thread_rng,Rng};
use std::sync::Arc;
+ use chain::keysinterface::InMemoryChannelKeys;
+
#[test]
fn test_per_commitment_storage() {
// Test vectors from BOLT 3:
let mut secrets: Vec<[u8; 32]> = Vec::new();
- let mut monitor: ChannelMonitor;
+ let mut monitor: ChannelMonitor<InMemoryChannelKeys>;
let secp_ctx = Secp256k1::new();
let logger = Arc::new(TestLogger::new());
};
}
+ let keys = InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ [41; 32],
+ 0,
+ );
+
{
// insert_secret correct sequence
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #1 incorrect
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #2 incorrect (#1 derived from incorrect)
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #3 incorrect
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #4 incorrect (1,2,3 derived from incorrect)
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #5 incorrect
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #6 incorrect (5 derived from incorrect)
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #7 incorrect
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
{
// insert_secret #8 incorrect
- monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ monitor = ChannelMonitor::new(keys.clone(), &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
secrets.clear();
secrets.push([0; 32]);
}
}
+ let keys = InMemoryChannelKeys::new(
+ &secp_ctx,
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ SecretKey::from_slice(&[41; 32]).unwrap(),
+ [41; 32],
+ 0,
+ );
+
// Prune with one old state and a local commitment tx holding a few overlaps with the
// old state.
- let mut monitor = ChannelMonitor::new(&SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
+ let mut monitor = ChannelMonitor::new(keys, &SecretKey::from_slice(&[41; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[43; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &SecretKey::from_slice(&[44; 32]).unwrap(), &PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[45; 32]).unwrap()), 0, Script::new(), logger.clone());
monitor.their_to_self_delay = Some(10);
monitor.provide_latest_local_commitment_tx_info(LocalCommitmentTransaction::dummy(), dummy_keys!(), 0, preimages_to_local_htlcs!(preimages[0..10]));
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
- assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
+ assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
// Claim tx with 1 offered HTLCs, 3 received HTLCs
claim_tx.input.clear();
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
- assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
+ assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_sig */ (73 * inputs_des.len() - sum_actual_sigs));
// Justice tx with 1 revoked HTLC-Success tx output
claim_tx.input.clear();
for (idx, inp) in claim_tx.input.iter_mut().zip(inputs_des.iter()).enumerate() {
sign_input!(sighash_parts, inp.0, idx as u32, 0, inp.1, sum_actual_sigs);
}
- assert_eq!(base_weight + ChannelMonitor::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
+ assert_eq!(base_weight + ChannelMonitor::<InMemoryChannelKeys>::get_witnesses_weight(&inputs_des[..]), claim_tx.get_weight() + /* max_length_isg */ (73 * inputs_des.len() - sum_actual_sigs));
}
// Further testing is done in the ChannelManager integration tests.
pub trait UpfrontShutdownScript: Context {}
impl UpfrontShutdownScript for InitContext {}
impl UpfrontShutdownScript for NodeContext {}
+
+ pub trait VariableLengthOnion: Context {}
+ impl VariableLengthOnion for InitContext {}
+ impl VariableLengthOnion for NodeContext {}
}
/// Tracks the set of features which a node implements, templated by the context in which it
/// Create a Features with the features we support
pub fn supported() -> InitFeatures {
InitFeatures {
- flags: vec![2 | 1 << 5],
+ flags: vec![2 | 1 << 5, 1 << (9-8)],
mark: PhantomData,
}
}
#[cfg(not(feature = "fuzztarget"))]
pub(crate) fn supported() -> NodeFeatures {
NodeFeatures {
- flags: vec![2 | 1 << 5],
+ flags: vec![2 | 1 << 5, 1 << (9-8)],
mark: PhantomData,
}
}
#[cfg(feature = "fuzztarget")]
pub fn supported() -> NodeFeatures {
NodeFeatures {
- flags: vec![2 | 1 << 5],
+ flags: vec![2 | 1 << 5, 1 << (9-8)],
mark: PhantomData,
}
}
pub(crate) fn requires_unknown_bits(&self) -> bool {
self.flags.iter().enumerate().any(|(idx, &byte)| {
- ( idx != 0 && (byte & 0x55) != 0 ) || ( idx == 0 && (byte & 0x14) != 0 )
+ (match idx {
+ 0 => (byte & 0b00010100),
+ 1 => (byte & 0b01010100),
+ _ => (byte & 0b01010101),
+ }) != 0
})
}
pub(crate) fn supports_unknown_bits(&self) -> bool {
self.flags.iter().enumerate().any(|(idx, &byte)| {
- ( idx != 0 && byte != 0 ) || ( idx == 0 && (byte & 0xc4) != 0 )
+ (match idx {
+ 0 => (byte & 0b11000100),
+ 1 => (byte & 0b11111100),
+ _ => byte,
+ }) != 0
})
}
}
}
+impl<T: sealed::VariableLengthOnion> Features<T> {
+ pub(crate) fn supports_variable_length_onion(&self) -> bool {
+ self.flags.len() > 1 && (self.flags[1] & 3) != 0
+ }
+}
+
impl<T: sealed::InitialRoutingSync> Features<T> {
pub(crate) fn initial_routing_sync(&self) -> bool {
self.flags.len() > 0 && (self.flags[0] & (1 << 3)) != 0
//! claim outputs on-chain.
use chain::transaction::OutPoint;
-use chain::keysinterface::{KeysInterface, SpendableOutputDescriptor};
+use chain::keysinterface::{ChannelKeys, KeysInterface, SpendableOutputDescriptor};
use chain::chaininterface::{ChainListener, ChainWatchInterfaceUtil, BlockNotifier};
use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
use ln::channelmanager::{ChannelManager,ChannelManagerReadArgs,HTLCForwardInfo,RAACommitmentOrder, PaymentPreimage, PaymentHash, BREAKDOWN_TIMEOUT};
use ln::channelmonitor::{ChannelMonitor, CLTV_CLAIM_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS, ManyChannelMonitor, ANTI_REORG_DELAY};
use ln::channel::{Channel, ChannelError};
-use ln::onion_utils;
+use ln::{chan_utils, onion_utils};
use ln::router::{Route, RouteHop};
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
use ln::msgs;
use util::test_utils;
use util::events::{Event, EventsProvider, MessageSendEvent, MessageSendEventsProvider};
use util::errors::APIError;
-use util::ser::{Writeable, ReadableArgs};
+use util::ser::{Writeable, Writer, ReadableArgs};
use util::config::UserConfig;
use util::logger::Logger;
use std::default::Default;
use std::sync::{Arc, Mutex};
use std::sync::atomic::Ordering;
-use std::mem;
+use std::{mem, io};
use rand::{thread_rng, Rng};
check_added_monitors!(nodes[1], 1);
}
+#[test]
+fn test_1_conf_open() {
+ // Previously, if the minium_depth config was set to 1, we'd never send a funding_locked. This
+ // tests that we properly send one in that case.
+ let mut alice_config = UserConfig::default();
+ alice_config.own_channel_config.minimum_depth = 1;
+ alice_config.channel_options.announced_channel = true;
+ alice_config.peer_channel_config_limits.force_announced_channel_preference = false;
+ let mut bob_config = UserConfig::default();
+ bob_config.own_channel_config.minimum_depth = 1;
+ bob_config.channel_options.announced_channel = true;
+ bob_config.peer_channel_config_limits.force_announced_channel_preference = false;
+ let node_cfgs = create_node_cfgs(2);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(alice_config), Some(bob_config)]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+ let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::supported(), InitFeatures::supported());
+ assert!(nodes[0].chain_monitor.does_match_tx(&tx));
+ assert!(nodes[1].chain_monitor.does_match_tx(&tx));
+
+ let header = BlockHeader { version: 0x20000000, prev_blockhash: Default::default(), merkle_root: Default::default(), time: 42, bits: 42, nonce: 42 };
+ nodes[1].block_notifier.block_connected_checked(&header, 1, &[&tx; 1], &[tx.version; 1]);
+ nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id()));
+
+ nodes[0].block_notifier.block_connected_checked(&header, 1, &[&tx; 1], &[tx.version; 1]);
+ let (funding_locked, _) = create_chan_between_nodes_with_value_confirm_second(&nodes[1], &nodes[0]);
+ let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
+
+ for node in nodes {
+ assert!(node.router.handle_channel_announcement(&announcement).unwrap());
+ node.router.handle_channel_update(&as_update).unwrap();
+ node.router.handle_channel_update(&bs_update).unwrap();
+ }
+}
+
#[test]
fn test_update_fee_vanilla() {
let node_cfgs = create_node_cfgs(2);
nodes[0].router.handle_htlc_fail_channel_update(&msgs::HTLCFailChannelUpdate::ChannelClosed { short_channel_id : as_chan.get_short_channel_id().unwrap(), is_permanent: false } );
- let as_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &as_chan.get_local_keys().inner.funding_key);
- let bs_bitcoin_key = PublicKey::from_secret_key(&secp_ctx, &bs_chan.get_local_keys().inner.funding_key);
+ let as_bitcoin_key = as_chan.get_local_keys().inner.local_channel_pubkeys.funding_pubkey;
+ let bs_bitcoin_key = bs_chan.get_local_keys().inner.local_channel_pubkeys.funding_pubkey;
let as_network_key = nodes[0].node.get_our_node_id();
let bs_network_key = nodes[1].node.get_our_node_id();
macro_rules! sign_msg {
($unsigned_msg: expr) => {
let msghash = Message::from_slice(&Sha256dHash::hash(&$unsigned_msg.encode()[..])[..]).unwrap();
- let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().inner.funding_key);
- let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().inner.funding_key);
+ let as_bitcoin_sig = secp_ctx.sign(&msghash, &as_chan.get_local_keys().inner.funding_key());
+ let bs_bitcoin_sig = secp_ctx.sign(&msghash, &bs_chan.get_local_keys().inner.funding_key());
let as_node_sig = secp_ctx.sign(&msghash, &nodes[0].keys_manager.get_node_secret());
let bs_node_sig = secp_ctx.sign(&msghash, &nodes[1].keys_manager.get_node_secret());
chan_announcement = msgs::ChannelAnnouncement {
new_chan_monitor = test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }));
nodes[0].chan_monitor = &new_chan_monitor;
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
- let (_, mut chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
+ let (_, mut chan_0_monitor) = <(Sha256dHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
assert!(chan_0_monitor_read.is_empty());
let mut nodes_0_read = &nodes_0_serialized[..];
new_chan_monitor = test_utils::TestChannelMonitor::new(nodes[0].chain_monitor.clone(), nodes[0].tx_broadcaster.clone(), Arc::new(test_utils::TestLogger::new()), Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 }));
nodes[0].chan_monitor = &new_chan_monitor;
let mut chan_0_monitor_read = &chan_0_monitor_serialized.0[..];
- let (_, mut chan_0_monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
+ let (_, mut chan_0_monitor) = <(Sha256dHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut chan_0_monitor_read, Arc::new(test_utils::TestLogger::new())).unwrap();
assert!(chan_0_monitor_read.is_empty());
let mut nodes_0_read = &nodes_0_serialized[..];
let mut node_0_monitors = Vec::new();
for serialized in node_0_monitors_serialized.iter() {
let mut read = &serialized[..];
- let (_, monitor) = <(Sha256dHash, ChannelMonitor)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap();
+ let (_, monitor) = <(Sha256dHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut read, Arc::new(test_utils::TestLogger::new())).unwrap();
assert!(read.is_empty());
node_0_monitors.push(monitor);
}
}
}
+struct BogusOnionHopData {
+ data: Vec<u8>
+}
+impl BogusOnionHopData {
+ fn new(orig: msgs::OnionHopData) -> Self {
+ Self { data: orig.encode() }
+ }
+}
+impl Writeable for BogusOnionHopData {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
+ writer.write_all(&self.data[..])
+ }
+}
+
#[test]
fn test_onion_failure() {
use ln::msgs::ChannelUpdate;
let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
- onion_payloads[0].realm = 3;
- msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
- }, ||{}, true, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));//XXX incremented channels idx here
+ let mut new_payloads = Vec::new();
+ for payload in onion_payloads.drain(..) {
+ new_payloads.push(BogusOnionHopData::new(payload));
+ }
+ // break the first (non-final) hop payload by swapping the realm (0) byte for a byte
+ // describing a length-1 TLV payload, which is obviously bogus.
+ new_payloads[0].data[0] = 1;
+ msg.onion_routing_packet = onion_utils::construct_onion_packet_bogus_hopdata(new_payloads, onion_keys, [0; 32], &payment_hash);
+ }, ||{}, true, Some(PERM|22), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));//XXX incremented channels idx here
// final node failure
run_onion_failure_test("invalid_realm", 3, &nodes, &route, &payment_hash, |msg| {
let cur_height = nodes[0].node.latest_block_height.load(Ordering::Acquire) as u32 + 1;
let onion_keys = onion_utils::construct_onion_keys(&Secp256k1::new(), &route, &session_priv).unwrap();
let (mut onion_payloads, _htlc_msat, _htlc_cltv) = onion_utils::build_onion_payloads(&route, cur_height).unwrap();
- onion_payloads[1].realm = 3;
- msg.onion_routing_packet = onion_utils::construct_onion_packet(onion_payloads, onion_keys, [0; 32], &payment_hash);
- }, ||{}, false, Some(PERM|1), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
+ let mut new_payloads = Vec::new();
+ for payload in onion_payloads.drain(..) {
+ new_payloads.push(BogusOnionHopData::new(payload));
+ }
+ // break the last-hop payload by swapping the realm (0) byte for a byte describing a
+ // length-1 TLV payload, which is obviously bogus.
+ new_payloads[1].data[0] = 1;
+ msg.onion_routing_packet = onion_utils::construct_onion_packet_bogus_hopdata(new_payloads, onion_keys, [0; 32], &payment_hash);
+ }, ||{}, false, Some(PERM|22), Some(msgs::HTLCFailChannelUpdate::ChannelClosed{short_channel_id: channels[1].0.contents.short_channel_id, is_permanent: true}));
// the following three with run_onion_failure_test_with_fail_intercept() test only the origin node
// receiving simulated fail messages
// Restore node A from previous state
let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::with_id(format!("node {}", 0)));
- let mut chan_monitor = <(Sha256dHash, ChannelMonitor)>::read(&mut ::std::io::Cursor::new(previous_chan_monitor_state.0), Arc::clone(&logger)).unwrap().1;
+ let mut chan_monitor = <(Sha256dHash, ChannelMonitor<EnforcingChannelKeys>)>::read(&mut ::std::io::Cursor::new(previous_chan_monitor_state.0), Arc::clone(&logger)).unwrap().1;
let chain_monitor = Arc::new(ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
let tx_broadcaster = Arc::new(test_utils::TestBroadcaster{txn_broadcasted: Mutex::new(Vec::new()), broadcasted_txn: Mutex::new(HashSet::new())});
let feeest = Arc::new(test_utils::TestFeeEstimator { sat_per_kw: 253 });
}
}
+#[test]
+fn test_counterparty_raa_skip_no_crash() {
+ // Previously, if our counterparty sent two RAAs in a row without us having provided a
+ // commitment transaction, we would have happily carried on and provided them the next
+ // commitment transaction based on one RAA forward. This would probably eventually have led to
+ // channel closure, but it would not have resulted in funds loss. Still, our
+ // EnforcingChannelKeys would have paniced as it doesn't like jumps into the future. Here, we
+ // check simply that the channel is closed in response to such an RAA, but don't check whether
+ // we decide to punish our counterparty for revoking their funds (as we don't currently
+ // implement that).
+ let node_cfgs = create_node_cfgs(2);
+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+ let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, InitFeatures::supported(), InitFeatures::supported()).2;
+
+ let commitment_seed = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&channel_id).unwrap().local_keys.commitment_seed().clone();
+ const INITIAL_COMMITMENT_NUMBER: u64 = (1 << 48) - 1;
+ let next_per_commitment_point = PublicKey::from_secret_key(&Secp256k1::new(),
+ &SecretKey::from_slice(&chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER - 2)).unwrap());
+ let per_commitment_secret = chan_utils::build_commitment_secret(&commitment_seed, INITIAL_COMMITMENT_NUMBER);
+
+ nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(),
+ &msgs::RevokeAndACK { channel_id, per_commitment_secret, next_per_commitment_point });
+ assert_eq!(check_closed_broadcast!(nodes[1], true).unwrap().data, "Received an unexpected revoke_and_ack");
+}
+
#[test]
fn test_bump_txn_sanitize_tracking_maps() {
// Sanitizing pendning_claim_request and claimable_outpoints used to be buggy,
mod channel;
mod onion_utils;
+mod wire;
#[cfg(test)]
#[macro_use] mod functional_test_utils;
use std::result::Result;
use util::events;
-use util::ser::{Readable, Writeable, Writer};
+use util::ser::{Readable, Writeable, Writer, FixedLengthReader, HighZeroBytesDroppedVarInt};
use ln::channelmanager::{PaymentPreimage, PaymentHash};
/// A version byte specified something we don't know how to handle.
/// Includes unknown realm byte in an OnionHopData packet
UnknownVersion,
- /// Unknown feature mandating we fail to parse message
+ /// Unknown feature mandating we fail to parse message (eg TLV with an even, unknown type)
UnknownRequiredFeature,
/// Value was invalid, eg a byte which was supposed to be a bool was something other than a 0
- /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, etc
+ /// or 1, a public key/private key/signature was invalid, text wasn't UTF-8, TLV was
+ /// syntactically incorrect, etc
InvalidValue,
/// Buffer too short
ShortRead,
/// starting at the node *after* the provided publickey and including batch_amount entries.
/// If None is provided for starting_point, we start at the first node.
fn get_next_node_announcements(&self, starting_point: Option<&PublicKey>, batch_amount: u8) -> Vec<NodeAnnouncement>;
-}
-
-pub(crate) struct OnionRealm0HopData {
- pub(crate) short_channel_id: u64,
- pub(crate) amt_to_forward: u64,
- pub(crate) outgoing_cltv_value: u32,
- // 12 bytes of 0-padding
+ /// Returns whether a full sync should be requested from a peer.
+ fn should_request_full_sync(&self, node_id: &PublicKey) -> bool;
}
mod fuzzy_internal_msgs {
// These types aren't intended to be pub, but are exposed for direct fuzzing (as we deserialize
// them from untrusted input):
- use super::OnionRealm0HopData;
+ pub(crate) enum OnionHopDataFormat {
+ Legacy { // aka Realm-0
+ short_channel_id: u64,
+ },
+ NonFinalNode {
+ short_channel_id: u64,
+ },
+ FinalNode,
+ }
+
pub struct OnionHopData {
- pub(crate) realm: u8,
- pub(crate) data: OnionRealm0HopData,
- pub(crate) hmac: [u8; 32],
+ pub(crate) format: OnionHopDataFormat,
+ pub(crate) amt_to_forward: u64,
+ pub(crate) outgoing_cltv_value: u32,
+ // 12 bytes of 0-padding for Legacy format
}
pub struct DecodedOnionErrorPacket {
onion_routing_packet
});
-impl Writeable for OnionRealm0HopData {
- fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
- w.size_hint(32);
- self.short_channel_id.write(w)?;
- self.amt_to_forward.write(w)?;
- self.outgoing_cltv_value.write(w)?;
- w.write_all(&[0;12])?;
- Ok(())
- }
-}
-
-impl<R: Read> Readable<R> for OnionRealm0HopData {
- fn read(r: &mut R) -> Result<Self, DecodeError> {
- Ok(OnionRealm0HopData {
- short_channel_id: Readable::read(r)?,
- amt_to_forward: Readable::read(r)?,
- outgoing_cltv_value: {
- let v: u32 = Readable::read(r)?;
- r.read_exact(&mut [0; 12])?;
- v
- }
- })
- }
-}
-
impl Writeable for OnionHopData {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
- w.size_hint(65);
- self.realm.write(w)?;
- self.data.write(w)?;
- self.hmac.write(w)?;
+ w.size_hint(33);
+ match self.format {
+ OnionHopDataFormat::Legacy { short_channel_id } => {
+ 0u8.write(w)?;
+ short_channel_id.write(w)?;
+ self.amt_to_forward.write(w)?;
+ self.outgoing_cltv_value.write(w)?;
+ w.write_all(&[0;12])?;
+ },
+ OnionHopDataFormat::NonFinalNode { short_channel_id } => {
+ encode_varint_length_prefixed_tlv!(w, {
+ (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
+ (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value)),
+ (6, short_channel_id)
+ });
+ },
+ OnionHopDataFormat::FinalNode => {
+ encode_varint_length_prefixed_tlv!(w, {
+ (2, HighZeroBytesDroppedVarInt(self.amt_to_forward)),
+ (4, HighZeroBytesDroppedVarInt(self.outgoing_cltv_value))
+ });
+ },
+ }
Ok(())
}
}
impl<R: Read> Readable<R> for OnionHopData {
- fn read(r: &mut R) -> Result<Self, DecodeError> {
- Ok(OnionHopData {
- realm: {
- let r: u8 = Readable::read(r)?;
- if r != 0 {
- return Err(DecodeError::UnknownVersion);
+ fn read(mut r: &mut R) -> Result<Self, DecodeError> {
+ use bitcoin::consensus::encode::{Decodable, Error, VarInt};
+ let v: VarInt = Decodable::consensus_decode(&mut r)
+ .map_err(|e| match e {
+ Error::Io(ioe) => DecodeError::from(ioe),
+ _ => DecodeError::InvalidValue
+ })?;
+ const LEGACY_ONION_HOP_FLAG: u64 = 0;
+ let (format, amt, cltv_value) = if v.0 != LEGACY_ONION_HOP_FLAG {
+ let mut rd = FixedLengthReader::new(r, v.0);
+ let mut amt = HighZeroBytesDroppedVarInt(0u64);
+ let mut cltv_value = HighZeroBytesDroppedVarInt(0u32);
+ let mut short_id: Option<u64> = None;
+ decode_tlv!(&mut rd, {
+ (2, amt),
+ (4, cltv_value)
+ }, {
+ (6, short_id)
+ });
+ rd.eat_remaining().map_err(|_| DecodeError::ShortRead)?;
+ let format = if let Some(short_channel_id) = short_id {
+ OnionHopDataFormat::NonFinalNode {
+ short_channel_id,
}
- r
- },
- data: Readable::read(r)?,
- hmac: Readable::read(r)?,
+ } else {
+ OnionHopDataFormat::FinalNode
+ };
+ (format, amt.0, cltv_value.0)
+ } else {
+ let format = OnionHopDataFormat::Legacy {
+ short_channel_id: Readable::read(r)?,
+ };
+ let amt: u64 = Readable::read(r)?;
+ let cltv_value: u32 = Readable::read(r)?;
+ r.read_exact(&mut [0; 12])?;
+ (format, amt, cltv_value)
+ };
+
+ Ok(OnionHopData {
+ format,
+ amt_to_forward: amt,
+ outgoing_cltv_value: cltv_value,
})
}
}
mod tests {
use hex;
use ln::msgs;
- use ln::msgs::{ChannelFeatures, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket};
+ use ln::msgs::{ChannelFeatures, InitFeatures, NodeFeatures, OptionalField, OnionErrorPacket, OnionHopDataFormat};
use ln::channelmanager::{PaymentPreimage, PaymentHash};
- use util::ser::Writeable;
+ use util::ser::{Writeable, Readable};
use bitcoin_hashes::sha256d::Hash as Sha256dHash;
use bitcoin_hashes::hex::FromHex;
use secp256k1::key::{PublicKey,SecretKey};
use secp256k1::{Secp256k1, Message};
+ use std::io::Cursor;
+
#[test]
fn encoding_channel_reestablish_no_secret() {
let cr = msgs::ChannelReestablish {
let target_value = hex::decode("004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
assert_eq!(encoded_value, target_value);
}
+
+ #[test]
+ fn encoding_legacy_onion_hop_data() {
+ let msg = msgs::OnionHopData {
+ format: OnionHopDataFormat::Legacy {
+ short_channel_id: 0xdeadbeef1bad1dea,
+ },
+ amt_to_forward: 0x0badf00d01020304,
+ outgoing_cltv_value: 0xffffffff,
+ };
+ let encoded_value = msg.encode();
+ let target_value = hex::decode("00deadbeef1bad1dea0badf00d01020304ffffffff000000000000000000000000").unwrap();
+ assert_eq!(encoded_value, target_value);
+ }
+
+ #[test]
+ fn encoding_nonfinal_onion_hop_data() {
+ let mut msg = msgs::OnionHopData {
+ format: OnionHopDataFormat::NonFinalNode {
+ short_channel_id: 0xdeadbeef1bad1dea,
+ },
+ amt_to_forward: 0x0badf00d01020304,
+ outgoing_cltv_value: 0xffffffff,
+ };
+ let encoded_value = msg.encode();
+ let target_value = hex::decode("1a02080badf00d010203040404ffffffff0608deadbeef1bad1dea").unwrap();
+ assert_eq!(encoded_value, target_value);
+ msg = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ if let OnionHopDataFormat::NonFinalNode { short_channel_id } = msg.format {
+ assert_eq!(short_channel_id, 0xdeadbeef1bad1dea);
+ } else { panic!(); }
+ assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
+ assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
+ }
+
+ #[test]
+ fn encoding_final_onion_hop_data() {
+ let mut msg = msgs::OnionHopData {
+ format: OnionHopDataFormat::FinalNode,
+ amt_to_forward: 0x0badf00d01020304,
+ outgoing_cltv_value: 0xffffffff,
+ };
+ let encoded_value = msg.encode();
+ let target_value = hex::decode("1002080badf00d010203040404ffffffff").unwrap();
+ assert_eq!(encoded_value, target_value);
+ msg = Readable::read(&mut Cursor::new(&target_value[..])).unwrap();
+ if let OnionHopDataFormat::FinalNode = msg.format { } else { panic!(); }
+ assert_eq!(msg.amt_to_forward, 0x0badf00d01020304);
+ assert_eq!(msg.outgoing_cltv_value, 0xffffffff);
+ }
}
use util::byte_utils;
use util::chacha20::ChaCha20;
use util::errors::{self, APIError};
-use util::ser::{Readable, Writeable};
+use util::ser::{Readable, Writeable, LengthCalculatingWriter};
use util::logger::{Logger, LogHolder};
use bitcoin_hashes::{Hash, HashEngine};
let mut last_short_channel_id = 0;
let mut res: Vec<msgs::OnionHopData> = Vec::with_capacity(route.hops.len());
- for hop in route.hops.iter().rev() {
+ for (idx, hop) in route.hops.iter().rev().enumerate() {
// First hop gets special values so that it can check, on receipt, that everything is
// exactly as it should be (and the next hop isn't trying to probe to find out if we're
// the intended recipient).
let value_msat = if cur_value_msat == 0 { hop.fee_msat } else { cur_value_msat };
let cltv = if cur_cltv == starting_htlc_offset { hop.cltv_expiry_delta + starting_htlc_offset } else { cur_cltv };
res.insert(0, msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
- short_channel_id: last_short_channel_id,
- amt_to_forward: value_msat,
- outgoing_cltv_value: cltv,
+ format: if hop.node_features.supports_variable_length_onion() {
+ if idx == 0 {
+ msgs::OnionHopDataFormat::FinalNode
+ } else {
+ msgs::OnionHopDataFormat::NonFinalNode {
+ short_channel_id: last_short_channel_id,
+ }
+ }
+ } else {
+ msgs::OnionHopDataFormat::Legacy {
+ short_channel_id: last_short_channel_id,
+ }
},
- hmac: [0; 32],
+ amt_to_forward: value_msat,
+ outgoing_cltv_value: cltv,
});
cur_value_msat += hop.fee_msat;
if cur_value_msat >= 21000000 * 100000000 * 1000 {
Ok((res, cur_value_msat, cur_cltv))
}
+/// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
+/// the hops can be of variable length.
+pub(crate) const ONION_DATA_LEN: usize = 20*65;
+
#[inline]
-fn shift_arr_right(arr: &mut [u8; 20*65]) {
- for i in (65..20*65).rev() {
- arr[i] = arr[i-65];
+fn shift_arr_right(arr: &mut [u8; ONION_DATA_LEN], amt: usize) {
+ for i in (amt..ONION_DATA_LEN).rev() {
+ arr[i] = arr[i-amt];
}
- for i in 0..65 {
+ for i in 0..amt {
arr[i] = 0;
}
}
-#[inline]
-fn xor_bufs(dst: &mut[u8], src: &[u8]) {
- assert_eq!(dst.len(), src.len());
-
- for i in 0..dst.len() {
- dst[i] ^= src[i];
+pub(super) fn route_size_insane(payloads: &Vec<msgs::OnionHopData>) -> bool {
+ let mut len = 0;
+ for payload in payloads.iter() {
+ let mut payload_len = LengthCalculatingWriter(0);
+ payload.write(&mut payload_len).expect("Failed to calculate length");
+ assert!(payload_len.0 + 32 < ONION_DATA_LEN);
+ len += payload_len.0 + 32;
+ if len > ONION_DATA_LEN {
+ return true;
+ }
}
+ false
}
-
-const ZERO:[u8; 21*65] = [0; 21*65];
+/// panics if route_size_insane(paylods)
pub(super) fn construct_onion_packet(payloads: Vec<msgs::OnionHopData>, onion_keys: Vec<OnionKeys>, prng_seed: [u8; 32], associated_data: &PaymentHash) -> msgs::OnionPacket {
- let mut packet_data = [0; 20*65];
+ let mut packet_data = [0; ONION_DATA_LEN];
let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]);
- chacha.process(&[0; 20*65], &mut packet_data);
+ chacha.process(&[0; ONION_DATA_LEN], &mut packet_data);
construct_onion_packet_with_init_noise(payloads, onion_keys, packet_data, associated_data)
}
-fn construct_onion_packet_with_init_noise(mut payloads: Vec<msgs::OnionHopData>, onion_keys: Vec<OnionKeys>, mut packet_data: [u8; 20*65], associated_data: &PaymentHash) -> msgs::OnionPacket {
- let mut buf = Vec::with_capacity(21*65);
- buf.resize(21*65, 0);
+#[cfg(test)]
+// Used in testing to write bogus OnionHopDatas, which is otherwise not representable in
+// msgs::OnionHopData.
+pub(super) fn construct_onion_packet_bogus_hopdata<HD: Writeable>(payloads: Vec<HD>, onion_keys: Vec<OnionKeys>, prng_seed: [u8; 32], associated_data: &PaymentHash) -> msgs::OnionPacket {
+ let mut packet_data = [0; ONION_DATA_LEN];
+
+ let mut chacha = ChaCha20::new(&prng_seed, &[0; 8]);
+ chacha.process(&[0; ONION_DATA_LEN], &mut packet_data);
+ construct_onion_packet_with_init_noise(payloads, onion_keys, packet_data, associated_data)
+}
+
+/// panics if route_size_insane(paylods)
+fn construct_onion_packet_with_init_noise<HD: Writeable>(mut payloads: Vec<HD>, onion_keys: Vec<OnionKeys>, mut packet_data: [u8; ONION_DATA_LEN], associated_data: &PaymentHash) -> msgs::OnionPacket {
let filler = {
- let iters = payloads.len() - 1;
- let end_len = iters * 65;
- let mut res = Vec::with_capacity(end_len);
- res.resize(end_len, 0);
+ const ONION_HOP_DATA_LEN: usize = 65; // We may decrease this eventually after TLV is common
+ let mut res = Vec::with_capacity(ONION_HOP_DATA_LEN * (payloads.len() - 1));
+
+ let mut pos = 0;
+ for (i, (payload, keys)) in payloads.iter().zip(onion_keys.iter()).enumerate() {
+ if i == payloads.len() - 1 { break; }
- for (i, keys) in onion_keys.iter().enumerate() {
- if i == payloads.len() - 1 { continue; }
let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
- chacha.process(&ZERO, &mut buf); // We don't have a seek function :(
- xor_bufs(&mut res[0..(i + 1)*65], &buf[(20 - i)*65..21*65]);
+ for _ in 0..(ONION_DATA_LEN - pos) { // TODO: Batch this.
+ let mut dummy = [0; 1];
+ chacha.process_in_place(&mut dummy); // We don't have a seek function :(
+ }
+
+ let mut payload_len = LengthCalculatingWriter(0);
+ payload.write(&mut payload_len).expect("Failed to calculate length");
+ pos += payload_len.0 + 32;
+ assert!(pos <= ONION_DATA_LEN);
+
+ res.resize(pos, 0u8);
+ chacha.process_in_place(&mut res);
}
res
};
let mut hmac_res = [0; 32];
-
for (i, (payload, keys)) in payloads.iter_mut().zip(onion_keys.iter()).rev().enumerate() {
- shift_arr_right(&mut packet_data);
- payload.hmac = hmac_res;
- packet_data[0..65].copy_from_slice(&payload.encode()[..]);
+ let mut payload_len = LengthCalculatingWriter(0);
+ payload.write(&mut payload_len).expect("Failed to calculate length");
+ shift_arr_right(&mut packet_data, payload_len.0 + 32);
+ packet_data[0..payload_len.0].copy_from_slice(&payload.encode()[..]);
+ packet_data[payload_len.0..(payload_len.0 + 32)].copy_from_slice(&hmac_res);
let mut chacha = ChaCha20::new(&keys.rho, &[0u8; 8]);
- chacha.process(&packet_data, &mut buf[0..20*65]);
- packet_data[..].copy_from_slice(&buf[0..20*65]);
+ chacha.process_in_place(&mut packet_data);
if i == 0 {
- packet_data[20*65 - filler.len()..20*65].copy_from_slice(&filler[..]);
+ packet_data[ONION_DATA_LEN - filler.len()..ONION_DATA_LEN].copy_from_slice(&filler[..]);
}
let mut hmac = HmacEngine::<Sha256>::new(&keys.mu);
hmac_res = Hmac::from_engine(hmac).into_inner();
}
- msgs::OnionPacket{
+ msgs::OnionPacket {
version: 0,
public_key: Ok(onion_keys.first().unwrap().ephemeral_pubkey),
hop_data: packet_data,
use ln::features::{ChannelFeatures, NodeFeatures};
use ln::router::{Route, RouteHop};
use ln::msgs;
- use util::ser::Writeable;
+ use util::ser::{Writeable, Writer};
use hex;
#[test]
fn onion_vectors() {
- // Packet creation test vectors from BOLT 4
+ // Legacy packet creation test vectors from BOLT 4
let onion_keys = build_test_onion_keys();
assert_eq!(onion_keys[0].shared_secret[..], hex::decode("53eb63ea8a3fec3b3cd433b85cd62a4b145e1dda09391b348c4e1cd36a03ea66").unwrap()[..]);
// Test vectors below are flat-out wrong: they claim to set outgoing_cltv_value to non-0 :/
let payloads = vec!(
msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
+ format: msgs::OnionHopDataFormat::Legacy {
short_channel_id: 0,
- amt_to_forward: 0,
- outgoing_cltv_value: 0,
},
- hmac: [0; 32],
+ amt_to_forward: 0,
+ outgoing_cltv_value: 0,
},
msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
+ format: msgs::OnionHopDataFormat::Legacy {
short_channel_id: 0x0101010101010101,
- amt_to_forward: 0x0100000001,
- outgoing_cltv_value: 0,
},
- hmac: [0; 32],
+ amt_to_forward: 0x0100000001,
+ outgoing_cltv_value: 0,
},
msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
+ format: msgs::OnionHopDataFormat::Legacy {
short_channel_id: 0x0202020202020202,
- amt_to_forward: 0x0200000002,
- outgoing_cltv_value: 0,
},
- hmac: [0; 32],
+ amt_to_forward: 0x0200000002,
+ outgoing_cltv_value: 0,
},
msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
+ format: msgs::OnionHopDataFormat::Legacy {
short_channel_id: 0x0303030303030303,
- amt_to_forward: 0x0300000003,
- outgoing_cltv_value: 0,
},
- hmac: [0; 32],
+ amt_to_forward: 0x0300000003,
+ outgoing_cltv_value: 0,
},
msgs::OnionHopData {
- realm: 0,
- data: msgs::OnionRealm0HopData {
+ format: msgs::OnionHopDataFormat::Legacy {
short_channel_id: 0x0404040404040404,
- amt_to_forward: 0x0400000004,
- outgoing_cltv_value: 0,
},
- hmac: [0; 32],
+ amt_to_forward: 0x0400000004,
+ outgoing_cltv_value: 0,
},
);
- let packet = [0; 20*65];
- let packet = super::construct_onion_packet_with_init_noise(payloads, onion_keys, packet, &PaymentHash([0x42; 32]));
+ let packet = super::construct_onion_packet_with_init_noise(payloads, onion_keys, [0; super::ONION_DATA_LEN], &PaymentHash([0x42; 32]));
// Just check the final packet encoding, as it includes all the per-hop vectors in it
// anyway...
assert_eq!(packet.encode(), hex::decode("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a716a996c7845c93d90e4ecbb9bde4ece2f69425c99e4bc820e44485455f135edc0d10f7d61ab590531cf08000179a333a347f8b4072f216400406bdf3bf038659793d4a1fd7b246979e3150a0a4cb052c9ec69acf0f48c3d39cd55675fe717cb7d80ce721caad69320c3a469a202f1e468c67eaf7a7cd8226d0fd32f7b48084dca885d56047694762b67021713ca673929c163ec36e04e40ca8e1c6d17569419d3039d9a1ec866abe044a9ad635778b961fc0776dc832b3a451bd5d35072d2269cf9b040f6b7a7dad84fb114ed413b1426cb96ceaf83825665ed5a1d002c1687f92465b49ed4c7f0218ff8c6c7dd7221d589c65b3b9aaa71a41484b122846c7c7b57e02e679ea8469b70e14fe4f70fee4d87b910cf144be6fe48eef24da475c0b0bcc6565ae82cd3f4e3b24c76eaa5616c6111343306ab35c1fe5ca4a77c0e314ed7dba39d6f1e0de791719c241a939cc493bea2bae1c1e932679ea94d29084278513c77b899cc98059d06a27d171b0dbdf6bee13ddc4fc17a0c4d2827d488436b57baa167544138ca2e64a11b43ac8a06cd0c2fba2d4d900ed2d9205305e2d7383cc98dacb078133de5f6fb6bed2ef26ba92cea28aafc3b9948dd9ae5559e8bd6920b8cea462aa445ca6a95e0e7ba52961b181c79e73bd581821df2b10173727a810c92b83b5ba4a0403eb710d2ca10689a35bec6c3a708e9e92f7d78ff3c5d9989574b00c6736f84c199256e76e19e78f0c98a9d580b4a658c84fc8f2096c2fbea8f5f8c59d0fdacb3be2802ef802abbecb3aba4acaac69a0e965abd8981e9896b1f6ef9d60f7a164b371af869fd0e48073742825e9434fc54da837e120266d53302954843538ea7c6c3dbfb4ff3b2fdbe244437f2a153ccf7bdb4c92aa08102d4f3cff2ae5ef86fab4653595e6a5837fa2f3e29f27a9cde5966843fb847a4a61f1e76c281fe8bb2b0a181d096100db5a1a5ce7a910238251a43ca556712eaadea167fb4d7d75825e440f3ecd782036d7574df8bceacb397abefc5f5254d2722215c53ff54af8299aaaad642c6d72a14d27882d9bbd539e1cc7a527526ba89b8c037ad09120e98ab042d3e8652b31ae0e478516bfaf88efca9f3676ffe99d2819dcaeb7610a626695f53117665d267d3f7abebd6bbd6733f645c72c389f03855bdf1e4b8075b516569b118233a0f0971d24b83113c0b096f5216a207ca99a7cddc81c130923fe3d91e7508c9ac5f2e914ff5dccab9e558566fa14efb34ac98d878580814b94b73acbfde9072f30b881f7f0fff42d4045d1ace6322d86a97d164aa84d93a60498065cc7c20e636f5862dc81531a88c60305a2e59a985be327a6902e4bed986dbf4a0b50c217af0ea7fdf9ab37f9ea1a1aaa72f54cf40154ea9b269f1a7c09f9f43245109431a175d50e2db0132337baa0ef97eed0fcf20489da36b79a1172faccc2f7ded7c60e00694282d93359c4682135642bc81f433574aa8ef0c97b4ade7ca372c5ffc23c7eddd839bab4e0f14d6df15c9dbeab176bec8b5701cf054eb3072f6dadc98f88819042bf10c407516ee58bce33fbe3b3d86a54255e577db4598e30a135361528c101683a5fcde7e8ba53f3456254be8f45fe3a56120ae96ea3773631fcb3873aa3abd91bcff00bd38bd43697a2e789e00da6077482e7b1b1a677b5afae4c54e6cbdf7377b694eb7d7a5b913476a5be923322d3de06060fd5e819635232a2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780df54bfdb0dd5cd9855179f602f9172307c7268724c3618e6817abd793adc214a0dc0bc616816632f27ea336fb56dfd").unwrap());
let onion_packet_5 = super::encrypt_failure_packet(&onion_keys[0].shared_secret[..], &onion_packet_4.data[..]);
assert_eq!(onion_packet_5.data, hex::decode("9c5add3963fc7f6ed7f148623c84134b5647e1306419dbe2174e523fa9e2fbed3a06a19f899145610741c83ad40b7712aefaddec8c6baf7325d92ea4ca4d1df8bce517f7e54554608bf2bd8071a4f52a7a2f7ffbb1413edad81eeea5785aa9d990f2865dc23b4bc3c301a94eec4eabebca66be5cf638f693ec256aec514620cc28ee4a94bd9565bc4d4962b9d3641d4278fb319ed2b84de5b665f307a2db0f7fbb757366067d88c50f7e829138fde4f78d39b5b5802f1b92a8a820865af5cc79f9f30bc3f461c66af95d13e5e1f0381c184572a91dee1c849048a647a1158cf884064deddbf1b0b88dfe2f791428d0ba0f6fb2f04e14081f69165ae66d9297c118f0907705c9c4954a199bae0bb96fad763d690e7daa6cfda59ba7f2c8d11448b604d12d").unwrap());
}
+
+ struct RawOnionHopData {
+ data: Vec<u8>
+ }
+ impl RawOnionHopData {
+ fn new(orig: msgs::OnionHopData) -> Self {
+ Self { data: orig.encode() }
+ }
+ }
+ impl Writeable for RawOnionHopData {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ writer.write_all(&self.data[..])
+ }
+ }
+
+ #[test]
+ fn variable_length_onion_vectors() {
+ // Packet creation test vectors from BOLT 4 (as of this writing at
+ // bolt04/onion-test-multi-frame.json in the spec repo).
+ // Note that we use he RawOnionHopData for everything except Legacy hops, as even the hops
+ // with "type": "tlv" are not valid TLV (they were for a previous version of TLV that
+ // didn't move forward), and, thus, cannot be directly represented in our in-memory enums.
+ let onion_keys = build_test_onion_keys();
+
+ let payloads = vec!(
+ RawOnionHopData::new(msgs::OnionHopData {
+ format: msgs::OnionHopDataFormat::Legacy {
+ short_channel_id: 0,
+ },
+ amt_to_forward: 0,
+ outgoing_cltv_value: 0,
+ }),
+ RawOnionHopData {
+ data: hex::decode("140101010101010101000000000000000100000001").unwrap(),
+ },
+ RawOnionHopData {
+ data: hex::decode("fd0100000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f404142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f606162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff").unwrap(),
+ },
+ RawOnionHopData {
+ data: hex::decode("140303030303030303000000000000000300000003").unwrap(),
+ },
+ RawOnionHopData::new(msgs::OnionHopData {
+ format: msgs::OnionHopDataFormat::Legacy {
+ short_channel_id: 0x0404040404040404,
+ },
+ amt_to_forward: 4,
+ outgoing_cltv_value: 4,
+ }),
+ );
+
+ let packet = super::construct_onion_packet_with_init_noise(payloads, onion_keys, [0; super::ONION_DATA_LEN], &PaymentHash([0x42; 32]));
+ // Just check the final packet encoding, as it includes all the per-hop vectors in it
+ // anyway...
+ assert_eq!(packet.encode(), hex::decode("0002eec7245d6b7d2ccb30380bfbe2a3648cd7a942653f5aa340edcea1f283686619e5f14350c2a76fc232b5e46d421e9615471ab9e0bc887beff8c95fdb878f7b3a71a060daf367132b378b3a3883c0e2c0e026b8900b2b5cdbc784e1a3bb913f88a9c50f7d61ab590531cf08000178a333a347f8b4072ed056f820f77774345e183a342ec4729f3d84accf515e88adddb85ecc08daba68404bae9a8e8d7178977d7094a1ae549f89338c0777551f874159eb42d3a59fb9285ad4e24883f27de23942ec966611e99bee1cee503455be9e8e642cef6cef7b9864130f692283f8a973d47a8f1c1726b6e59969385975c766e35737c8d76388b64f748ee7943ffb0e2ee45c57a1abc40762ae598723d21bd184e2b338f68ebff47219357bd19cd7e01e2337b806ef4d717888e129e59cd3dc31e6201ccb2fd6d7499836f37a993262468bcb3a4dcd03a22818aca49c6b7b9b8e9e870045631d8e039b066ff86e0d1b7291f71cefa7264c70404a8e538b566c17ccc5feab231401e6c08a01bd5edfc1aa8e3e533b96e82d1f91118d508924b923531929aea889fcdf050597c681185f336b1da63b0939aa2b7c50b21b5eb7b6ad66c81fab98a3cdf73f658149e7e9ced4edde5d38c9b8f92e16f6b4ab13d7fca6a0e4ecc9f9de611a90da6e99c39551094c56e3196f282c5dffd9fc4b2fc12f3bca8e6fe47eb45fbdd3be21a8a8d200797eae3c9a0497132f92410d804977408494dff49dd3d8bce248e0b74fd9e6f0f7102c25ddfa02bd9ad9f746abbfa337ef811d5345a9e16b60de1767b209645ba40bd1f9a5f75bc04feca9b27c5554be4fe83fac2cb83aa447a817bb85ae966c68b420063833fada375e2f515965e687a45699632902672c654d1d18d7bcbf55e8fa57f63f2da449f8e1e606e8722df081e5f193fc4179feb99ad22819afdeef211f7c54afdba92aeef0c00b7bc2b65a4813c01f907a8377585708f2d4c940a25328e585714c8ded0a9a4d7a6de1027c1cb7a0198cd3db68b58c0704dfd0cfbe624e9cd18cc0ae5d96697bb476708b9ee0403d211e64e0d5a7683a7a9a140c02f0ff1c6e67a302941b4052bdea8a63e70a3ad62c5b89c698f1fd3c7685cb49705096cad702d02d93bcb1c27a409f4c9bddec001205ca4a2740f19b50900be81c7e847f1a863deea8d35701f1355cad8db57b1d4eb2ab4e29587734785abfb46ddede71928213d7d089dfdeda052827f459f1688cc0935bd47e7bcec27427c8376dcce7e22699567c0d145f8a7db33f6758815f1f15f9f7a9760dec4f34ae095edda4c64e9735bdd029c4e32c2ee31ba47ec5e6bdb97813d52dbd15b4e0b7a2c7f790ae64104d99f38c127f0a093288fa34144adb16b8968d4fa7656fcec99de8503dd46d3b03620a71c7cd085364abd30dccf7fbda25a1cdc102600149c9af1c97aa0372cd2e1909f28ac5c686f432b310e79528c9b8b9e8f314c1e74621ce6308ad2278b81d460892e0d9dd38b7c76d58be6dfd10ae7583ee1e7ef5b3f6f78dc60af0950df1b00cc55b6d178ba2e476bea0eaeef49323b83f05804159e7aef4eed4cc60dd07be76f067dfd0bcfb0b806b69ba921336a20c43c832d0cab8fa3ddeb29e3bf07b0d98a112eb07802756235a49d44a8b82a950d84e95e01971f0e106ccb337f07384e21620e0ad39e16ed9edca123226cf55ac44f449eeb53e38a7f27d101806e4823e4efcc887414240ee6826c4a5cb1c6443ad36ebf905a435c1d9054e54173911b17b5b40f60b3d9fd5f12eac54ca1e20191f5f18544d5fd3d665e9bcef96fb44b76110aa64d9db4c86c9513cbdad546538e8aec521fbe83ceac5e74a15629f1ed0b870a1d0d1e5680b6d6100d1bd3f3b9043bd35b8919c4088f1949b8be89e4701eb870f8ed64fafa446c78df3ea").unwrap());
+ }
}
use ln::msgs;
use ln::msgs::ChannelMessageHandler;
use ln::channelmanager::{SimpleArcChannelManager, SimpleRefChannelManager};
-use util::ser::{Writeable, Writer, Readable};
+use util::ser::VecWriter;
use ln::peer_channel_encryptor::{PeerChannelEncryptor,NextNoiseStep};
+use ln::wire;
+use ln::wire::Encode;
use util::byte_utils;
use util::events::{MessageSendEvent, MessageSendEventsProvider};
use util::logger::Logger;
peer_counter_low: AtomicUsize,
peer_counter_high: AtomicUsize,
- initial_syncs_sent: AtomicUsize,
logger: Arc<Logger>,
}
-struct VecWriter(Vec<u8>);
-impl Writer for VecWriter {
- fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
- self.0.extend_from_slice(buf);
- Ok(())
- }
- fn size_hint(&mut self, size: usize) {
- self.0.reserve_exact(size);
- }
-}
-
macro_rules! encode_msg {
- ($msg: expr, $msg_code: expr) => {{
- let mut msg = VecWriter(Vec::new());
- ($msg_code as u16).write(&mut msg).unwrap();
- $msg.write(&mut msg).unwrap();
- msg.0
+ ($msg: expr) => {{
+ let mut buffer = VecWriter(Vec::new());
+ wire::write($msg, &mut buffer).unwrap();
+ buffer.0
}}
}
-//TODO: Really should do something smarter for this
-const INITIAL_SYNCS_TO_SEND: usize = 5;
-
/// Manages and reacts to connection events. You probably want to use file descriptors as PeerIds.
/// PeerIds may repeat, but only after disconnect_event() has been called.
impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where CM::Target: msgs::ChannelMessageHandler {
ephemeral_key_midstate,
peer_counter_low: AtomicUsize::new(0),
peer_counter_high: AtomicUsize::new(0),
- initial_syncs_sent: AtomicUsize::new(0),
logger,
}
}
fn do_attempt_write_data(&self, descriptor: &mut Descriptor, peer: &mut Peer) {
macro_rules! encode_and_send_msg {
- ($msg: expr, $msg_code: expr) => {
+ ($msg: expr) => {
{
- log_trace!(self, "Encoding and sending sync update message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
+ log_trace!(self, "Encoding and sending sync update message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg)[..]));
}
}
}
let steps = ((MSG_BUFF_SIZE - peer.pending_outbound_buffer.len() + 2) / 3) as u8;
let all_messages = self.message_handler.route_handler.get_next_channel_announcements(0, steps);
for &(ref announce, ref update_a, ref update_b) in all_messages.iter() {
- encode_and_send_msg!(announce, 256);
- encode_and_send_msg!(update_a, 258);
- encode_and_send_msg!(update_b, 258);
+ encode_and_send_msg!(announce);
+ encode_and_send_msg!(update_a);
+ encode_and_send_msg!(update_b);
peer.sync_status = InitSyncTracker::ChannelsSyncing(announce.contents.short_channel_id + 1);
}
if all_messages.is_empty() || all_messages.len() != steps as usize {
let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
let all_messages = self.message_handler.route_handler.get_next_node_announcements(None, steps);
for msg in all_messages.iter() {
- encode_and_send_msg!(msg, 256);
+ encode_and_send_msg!(msg);
peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
}
if all_messages.is_empty() || all_messages.len() != steps as usize {
let steps = (MSG_BUFF_SIZE - peer.pending_outbound_buffer.len()) as u8;
let all_messages = self.message_handler.route_handler.get_next_node_announcements(Some(&key), steps);
for msg in all_messages.iter() {
- encode_and_send_msg!(msg, 256);
+ encode_and_send_msg!(msg);
peer.sync_status = InitSyncTracker::NodesSyncing(msg.contents.node_id);
}
if all_messages.is_empty() || all_messages.len() != steps as usize {
peer.pending_read_buffer_pos = 0;
macro_rules! encode_and_send_msg {
- ($msg: expr, $msg_code: expr) => {
+ ($msg: expr) => {
{
- log_trace!(self, "Encoding and sending message of type {} to {}", $msg_code, log_pubkey!(peer.their_node_id.unwrap()));
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!($msg, $msg_code)[..]));
+ log_trace!(self, "Encoding and sending message of type {} to {}", $msg.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(&$msg)[..]));
peers.peers_needing_send.insert(peer_descriptor.clone());
}
}
},
msgs::ErrorAction::SendErrorMessage { msg } => {
log_trace!(self, "Got Err handling message, sending Error message because {}", e.err);
- encode_and_send_msg!(msg, 17);
- continue;
- },
- }
- }
- };
- }
- }
-
- macro_rules! try_potential_decodeerror {
- ($thing: expr) => {
- match $thing {
- Ok(x) => x,
- Err(e) => {
- match e {
- msgs::DecodeError::UnknownVersion => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::UnknownRequiredFeature => {
- log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
- continue;
- },
- msgs::DecodeError::InvalidValue => {
- log_debug!(self, "Got an invalid value while deserializing message");
- return Err(PeerHandleError{ no_connection_possible: false });
- },
- msgs::DecodeError::ShortRead => {
- log_debug!(self, "Deserialization failed due to shortness of message");
- return Err(PeerHandleError{ no_connection_possible: false });
- },
- msgs::DecodeError::ExtraAddressesPerType => {
- log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
+ encode_and_send_msg!(msg);
continue;
},
- msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError{ no_connection_possible: false }),
- msgs::DecodeError::Io(_) => return Err(PeerHandleError{ no_connection_possible: false }),
}
}
};
peer.their_node_id = Some(their_node_id);
insert_node_id!();
let mut features = InitFeatures::supported();
- if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
- self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+ if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
features.set_initial_routing_sync();
}
- encode_and_send_msg!(msgs::Init {
- features,
- }, 16);
+
+ let resp = msgs::Init { features };
+ encode_and_send_msg!(resp);
},
NextNoiseStep::ActThree => {
let their_node_id = try_potential_handleerror!(peer.channel_encryptor.process_act_three(&peer.pending_read_buffer[..]));
peer.pending_read_buffer = [0; 18].to_vec();
peer.pending_read_is_header = true;
- let msg_type = byte_utils::slice_to_be16(&msg_data[0..2]);
- log_trace!(self, "Received message of type {} from {}", msg_type, log_pubkey!(peer.their_node_id.unwrap()));
- if msg_type != 16 && peer.their_features.is_none() {
- // Need an init message as first message
+ let mut reader = ::std::io::Cursor::new(&msg_data[..]);
+ let message_result = wire::read(&mut reader);
+ let message = match message_result {
+ Ok(x) => x,
+ Err(e) => {
+ match e {
+ msgs::DecodeError::UnknownVersion => return Err(PeerHandleError { no_connection_possible: false }),
+ msgs::DecodeError::UnknownRequiredFeature => {
+ log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
+ continue;
+ }
+ msgs::DecodeError::InvalidValue => {
+ log_debug!(self, "Got an invalid value while deserializing message");
+ return Err(PeerHandleError { no_connection_possible: false });
+ }
+ msgs::DecodeError::ShortRead => {
+ log_debug!(self, "Deserialization failed due to shortness of message");
+ return Err(PeerHandleError { no_connection_possible: false });
+ }
+ msgs::DecodeError::ExtraAddressesPerType => {
+ log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
+ continue;
+ }
+ msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
+ msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
+ }
+ }
+ };
+
+ log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
+
+ // Need an Init as first message
+ if let wire::Message::Init(_) = message {
+ } else if peer.their_features.is_none() {
log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
return Err(PeerHandleError{ no_connection_possible: false });
}
- let mut reader = ::std::io::Cursor::new(&msg_data[2..]);
- match msg_type {
- // Connection control:
- 16 => {
- let msg = try_potential_decodeerror!(msgs::Init::read(&mut reader));
+
+ match message {
+ // Setup and Control messages:
+ wire::Message::Init(msg) => {
if msg.features.requires_unknown_bits() {
log_info!(self, "Peer global features required unknown version bits");
return Err(PeerHandleError{ no_connection_possible: true });
if !peer.outbound {
let mut features = InitFeatures::supported();
- if self.initial_syncs_sent.load(Ordering::Acquire) < INITIAL_SYNCS_TO_SEND {
- self.initial_syncs_sent.fetch_add(1, Ordering::AcqRel);
+ if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
features.set_initial_routing_sync();
}
- encode_and_send_msg!(msgs::Init {
- features,
- }, 16);
+ let resp = msgs::Init { features };
+ encode_and_send_msg!(resp);
}
self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
peer.their_features = Some(msg.features);
},
- 17 => {
- let msg = try_potential_decodeerror!(msgs::ErrorMessage::read(&mut reader));
+ wire::Message::Error(msg) => {
let mut data_is_printable = true;
for b in msg.data.bytes() {
if b < 32 || b > 126 {
}
},
- 18 => {
- let msg = try_potential_decodeerror!(msgs::Ping::read(&mut reader));
+ wire::Message::Ping(msg) => {
if msg.ponglen < 65532 {
let resp = msgs::Pong { byteslen: msg.ponglen };
- encode_and_send_msg!(resp, 19);
+ encode_and_send_msg!(resp);
}
},
- 19 => {
+ wire::Message::Pong(_msg) => {
peer.awaiting_pong = false;
- try_potential_decodeerror!(msgs::Pong::read(&mut reader));
},
- // Channel control:
- 32 => {
- let msg = try_potential_decodeerror!(msgs::OpenChannel::read(&mut reader));
+
+ // Channel messages:
+ wire::Message::OpenChannel(msg) => {
self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
},
- 33 => {
- let msg = try_potential_decodeerror!(msgs::AcceptChannel::read(&mut reader));
+ wire::Message::AcceptChannel(msg) => {
self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
},
- 34 => {
- let msg = try_potential_decodeerror!(msgs::FundingCreated::read(&mut reader));
+ wire::Message::FundingCreated(msg) => {
self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg);
},
- 35 => {
- let msg = try_potential_decodeerror!(msgs::FundingSigned::read(&mut reader));
+ wire::Message::FundingSigned(msg) => {
self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg);
},
- 36 => {
- let msg = try_potential_decodeerror!(msgs::FundingLocked::read(&mut reader));
+ wire::Message::FundingLocked(msg) => {
self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg);
},
- 38 => {
- let msg = try_potential_decodeerror!(msgs::Shutdown::read(&mut reader));
+ wire::Message::Shutdown(msg) => {
self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg);
},
- 39 => {
- let msg = try_potential_decodeerror!(msgs::ClosingSigned::read(&mut reader));
+ wire::Message::ClosingSigned(msg) => {
self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg);
},
- 128 => {
- let msg = try_potential_decodeerror!(msgs::UpdateAddHTLC::read(&mut reader));
+ // Commitment messages:
+ wire::Message::UpdateAddHTLC(msg) => {
self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg);
},
- 130 => {
- let msg = try_potential_decodeerror!(msgs::UpdateFulfillHTLC::read(&mut reader));
+ wire::Message::UpdateFulfillHTLC(msg) => {
self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg);
},
- 131 => {
- let msg = try_potential_decodeerror!(msgs::UpdateFailHTLC::read(&mut reader));
+ wire::Message::UpdateFailHTLC(msg) => {
self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg);
},
- 135 => {
- let msg = try_potential_decodeerror!(msgs::UpdateFailMalformedHTLC::read(&mut reader));
+ wire::Message::UpdateFailMalformedHTLC(msg) => {
self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg);
},
- 132 => {
- let msg = try_potential_decodeerror!(msgs::CommitmentSigned::read(&mut reader));
+ wire::Message::CommitmentSigned(msg) => {
self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg);
},
- 133 => {
- let msg = try_potential_decodeerror!(msgs::RevokeAndACK::read(&mut reader));
+ wire::Message::RevokeAndACK(msg) => {
self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg);
},
- 134 => {
- let msg = try_potential_decodeerror!(msgs::UpdateFee::read(&mut reader));
+ wire::Message::UpdateFee(msg) => {
self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg);
},
- 136 => {
- let msg = try_potential_decodeerror!(msgs::ChannelReestablish::read(&mut reader));
+ wire::Message::ChannelReestablish(msg) => {
self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg);
},
- // Routing control:
- 259 => {
- let msg = try_potential_decodeerror!(msgs::AnnouncementSignatures::read(&mut reader));
+ // Routing messages:
+ wire::Message::AnnouncementSignatures(msg) => {
self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg);
},
- 256 => {
- let msg = try_potential_decodeerror!(msgs::ChannelAnnouncement::read(&mut reader));
+ wire::Message::ChannelAnnouncement(msg) => {
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
if should_forward {
// TODO: forward msg along to all our other peers!
}
},
- 257 => {
- let msg = try_potential_decodeerror!(msgs::NodeAnnouncement::read(&mut reader));
+ wire::Message::NodeAnnouncement(msg) => {
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
if should_forward {
// TODO: forward msg along to all our other peers!
}
},
- 258 => {
- let msg = try_potential_decodeerror!(msgs::ChannelUpdate::read(&mut reader));
+ wire::Message::ChannelUpdate(msg) => {
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
if should_forward {
// TODO: forward msg along to all our other peers!
}
},
- _ => {
- if (msg_type & 1) == 0 {
- return Err(PeerHandleError{ no_connection_possible: true });
- }
+
+ // Unknown messages:
+ wire::Message::Unknown(msg_type) if msg_type.is_even() => {
+ // Fail the channel if message is an even, unknown type as per BOLT #1.
+ return Err(PeerHandleError{ no_connection_possible: true });
},
+ wire::Message::Unknown(_) => {},
}
}
}
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Drop the pending channel? (or just let it timeout, but that sucks)
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 33)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendOpenChannel { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Drop the pending channel? (or just let it timeout, but that sucks)
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 32)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendFundingCreated { ref node_id, ref msg } => {
//TODO: generate a DiscardFunding event indicating to the wallet that
//they should just throw away this funding transaction
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 34)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendFundingSigned { ref node_id, ref msg } => {
//TODO: generate a DiscardFunding event indicating to the wallet that
//they should just throw away this funding transaction
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 35)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendFundingLocked { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 36)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendAnnouncementSignatures { ref node_id, ref msg } => {
//TODO: generate a DiscardFunding event indicating to the wallet that
//they should just throw away this funding transaction
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 259)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::UpdateHTLCs { ref node_id, updates: msgs::CommitmentUpdate { ref update_add_htlcs, ref update_fulfill_htlcs, ref update_fail_htlcs, ref update_fail_malformed_htlcs, ref update_fee, ref commitment_signed } } => {
//TODO: Do whatever we're gonna do for handling dropped messages
});
for msg in update_add_htlcs {
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 128)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
}
for msg in update_fulfill_htlcs {
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 130)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
}
for msg in update_fail_htlcs {
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 131)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
}
for msg in update_fail_malformed_htlcs {
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 135)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
}
if let &Some(ref msg) = update_fee {
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 134)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
}
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(commitment_signed, 132)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(commitment_signed)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendRevokeAndACK { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 133)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendClosingSigned { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 39)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendShutdown { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 38)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::SendChannelReestablish { ref node_id, ref msg } => {
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 136)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => {
log_trace!(self, "Handling BroadcastChannelAnnouncement event in peer_handler for short channel id {}", msg.contents.short_channel_id);
if self.message_handler.route_handler.handle_channel_announcement(msg).is_ok() && self.message_handler.route_handler.handle_channel_update(update_msg).is_ok() {
- let encoded_msg = encode_msg!(msg, 256);
- let encoded_update_msg = encode_msg!(update_msg, 258);
+ let encoded_msg = encode_msg!(msg);
+ let encoded_update_msg = encode_msg!(update_msg);
for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
MessageSendEvent::BroadcastChannelUpdate { ref msg } => {
log_trace!(self, "Handling BroadcastChannelUpdate event in peer_handler for short channel id {}", msg.contents.short_channel_id);
if self.message_handler.route_handler.handle_channel_update(msg).is_ok() {
- let encoded_msg = encode_msg!(msg, 258);
+ let encoded_msg = encode_msg!(msg);
for (ref descriptor, ref mut peer) in peers.peers.iter_mut() {
if !peer.channel_encryptor.is_ready_for_encryption() || peer.their_features.is_none() ||
log_trace!(self, "Handling DisconnectPeer HandleError event in peer_handler for node {} with message {}",
log_pubkey!(node_id),
msg.data);
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
// This isn't guaranteed to work, but if there is enough free
// room in the send buffer, put the error message there...
self.do_attempt_write_data(&mut descriptor, &mut peer);
let (mut descriptor, peer) = get_peer_for_forwarding!(node_id, {
//TODO: Do whatever we're gonna do for handling dropped messages
});
- peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg, 17)));
+ peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(&encode_msg!(msg)));
self.do_attempt_write_data(&mut descriptor, peer);
},
}
ponglen: 0,
byteslen: 64,
};
- peer.pending_outbound_buffer.push_back(encode_msg!(ping, 18));
+ peer.pending_outbound_buffer.push_back(encode_msg!(&ping));
let mut descriptor_clone = descriptor.clone();
self.do_attempt_write_data(&mut descriptor_clone, peer);
use std::cmp;
use std::sync::{RwLock,Arc};
+use std::sync::atomic::{AtomicUsize, Ordering};
use std::collections::{HashMap,BinaryHeap,BTreeMap};
use std::collections::btree_map::Entry as BtreeEntry;
use std;
pub struct Router {
secp_ctx: Secp256k1<secp256k1::VerifyOnly>,
network_map: RwLock<NetworkMap>,
+ full_syncs_requested: AtomicUsize,
chain_monitor: Arc<ChainWatchInterface>,
logger: Arc<Logger>,
}
Ok(Router {
secp_ctx: Secp256k1::verification_only(),
network_map: RwLock::new(network_map),
+ full_syncs_requested: AtomicUsize::new(0),
chain_monitor: args.chain_monitor,
logger: args.logger,
})
}
impl RoutingMessageHandler for Router {
+
fn handle_node_announcement(&self, msg: &msgs::NodeAnnouncement) -> Result<bool, LightningError> {
let msg_hash = hash_to_message!(&Sha256dHash::hash(&msg.contents.encode()[..])[..]);
secp_verify_sig!(self.secp_ctx, &msg_hash, &msg.signature, &msg.contents.node_id);
}
result
}
+
+ fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
+ //TODO: Determine whether to request a full sync based on the network map.
+ const FULL_SYNCS_TO_REQUEST: usize = 5;
+ if self.full_syncs_requested.load(Ordering::Acquire) < FULL_SYNCS_TO_REQUEST {
+ self.full_syncs_requested.fetch_add(1, Ordering::AcqRel);
+ true
+ } else {
+ false
+ }
+ }
}
#[derive(Eq, PartialEq)]
our_node_id: our_pubkey,
nodes: nodes,
}),
+ full_syncs_requested: AtomicUsize::new(0),
chain_monitor,
logger,
}
use ln::channelmanager;
use ln::router::{Router,NodeInfo,NetworkMap,ChannelInfo,DirectionalChannelInfo,RouteHint};
use ln::features::{ChannelFeatures, InitFeatures, NodeFeatures};
- use ln::msgs::{LightningError, ErrorAction};
+ use ln::msgs::{ErrorAction, LightningError, RoutingMessageHandler};
use util::test_utils;
use util::test_utils::TestVecWriter;
use util::logger::Logger;
use hex;
use secp256k1::key::{PublicKey,SecretKey};
+ use secp256k1::All;
use secp256k1::Secp256k1;
use std::sync::Arc;
- #[test]
- fn route_test() {
+ fn create_router() -> (Secp256k1<All>, PublicKey, Router) {
let secp_ctx = Secp256k1::new();
let our_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()[..]).unwrap());
let logger: Arc<Logger> = Arc::new(test_utils::TestLogger::new());
let chain_monitor = Arc::new(chaininterface::ChainWatchInterfaceUtil::new(Network::Testnet, Arc::clone(&logger)));
let router = Router::new(our_id, chain_monitor, Arc::clone(&logger));
+ (secp_ctx, our_id, router)
+ }
+
+ #[test]
+ fn route_test() {
+ let (secp_ctx, our_id, router) = create_router();
// Build network from our_id to node8:
//
assert!(<NetworkMap>::read(&mut ::std::io::Cursor::new(&w.0)).unwrap() == *network);
}
}
+
+ #[test]
+ fn request_full_sync_finite_times() {
+ let (secp_ctx, _, router) = create_router();
+ let node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()[..]).unwrap());
+
+ assert!(router.should_request_full_sync(&node_id));
+ assert!(router.should_request_full_sync(&node_id));
+ assert!(router.should_request_full_sync(&node_id));
+ assert!(router.should_request_full_sync(&node_id));
+ assert!(router.should_request_full_sync(&node_id));
+ assert!(!router.should_request_full_sync(&node_id));
+ }
}
--- /dev/null
+//! Wire encoding/decoding for Lightning messages according to [BOLT #1].
+//!
+//! Messages known by this module can be read from the wire using [`read`].
+//! The [`Message`] enum returned by [`read`] wraps the decoded message or the message type (if
+//! unknown) to use with pattern matching.
+//!
+//! Messages implementing the [`Encode`] trait define a message type and can be sent over the wire
+//! using [`write`].
+//!
+//! [BOLT #1]: https://github.com/lightningnetwork/lightning-rfc/blob/master/01-messaging.md
+//! [`read`]: fn.read.html
+//! [`write`]: fn.write.html
+//! [`Encode`]: trait.Encode.html
+//! [`Message`]: enum.Message.html
+
+use ln::msgs;
+use util::ser::{Readable, Writeable, Writer};
+
+/// A Lightning message returned by [`read`] when decoding bytes received over the wire. Each
+/// variant contains a message from [`ln::msgs`] or otherwise the message type if unknown.
+///
+/// [`read`]: fn.read.html
+/// [`ln::msgs`]: ../msgs/index.html
+#[allow(missing_docs)]
+pub enum Message {
+ Init(msgs::Init),
+ Error(msgs::ErrorMessage),
+ Ping(msgs::Ping),
+ Pong(msgs::Pong),
+ OpenChannel(msgs::OpenChannel),
+ AcceptChannel(msgs::AcceptChannel),
+ FundingCreated(msgs::FundingCreated),
+ FundingSigned(msgs::FundingSigned),
+ FundingLocked(msgs::FundingLocked),
+ Shutdown(msgs::Shutdown),
+ ClosingSigned(msgs::ClosingSigned),
+ UpdateAddHTLC(msgs::UpdateAddHTLC),
+ UpdateFulfillHTLC(msgs::UpdateFulfillHTLC),
+ UpdateFailHTLC(msgs::UpdateFailHTLC),
+ UpdateFailMalformedHTLC(msgs::UpdateFailMalformedHTLC),
+ CommitmentSigned(msgs::CommitmentSigned),
+ RevokeAndACK(msgs::RevokeAndACK),
+ UpdateFee(msgs::UpdateFee),
+ ChannelReestablish(msgs::ChannelReestablish),
+ AnnouncementSignatures(msgs::AnnouncementSignatures),
+ ChannelAnnouncement(msgs::ChannelAnnouncement),
+ NodeAnnouncement(msgs::NodeAnnouncement),
+ ChannelUpdate(msgs::ChannelUpdate),
+ /// A message that could not be decoded because its type is unknown.
+ Unknown(MessageType),
+}
+
+/// A number identifying a message to determine how it is encoded on the wire.
+#[derive(Clone, Copy)]
+pub struct MessageType(u16);
+
+impl Message {
+ /// Returns the type that was used to decode the message payload.
+ pub fn type_id(&self) -> MessageType {
+ match self {
+ &Message::Init(ref msg) => msg.type_id(),
+ &Message::Error(ref msg) => msg.type_id(),
+ &Message::Ping(ref msg) => msg.type_id(),
+ &Message::Pong(ref msg) => msg.type_id(),
+ &Message::OpenChannel(ref msg) => msg.type_id(),
+ &Message::AcceptChannel(ref msg) => msg.type_id(),
+ &Message::FundingCreated(ref msg) => msg.type_id(),
+ &Message::FundingSigned(ref msg) => msg.type_id(),
+ &Message::FundingLocked(ref msg) => msg.type_id(),
+ &Message::Shutdown(ref msg) => msg.type_id(),
+ &Message::ClosingSigned(ref msg) => msg.type_id(),
+ &Message::UpdateAddHTLC(ref msg) => msg.type_id(),
+ &Message::UpdateFulfillHTLC(ref msg) => msg.type_id(),
+ &Message::UpdateFailHTLC(ref msg) => msg.type_id(),
+ &Message::UpdateFailMalformedHTLC(ref msg) => msg.type_id(),
+ &Message::CommitmentSigned(ref msg) => msg.type_id(),
+ &Message::RevokeAndACK(ref msg) => msg.type_id(),
+ &Message::UpdateFee(ref msg) => msg.type_id(),
+ &Message::ChannelReestablish(ref msg) => msg.type_id(),
+ &Message::AnnouncementSignatures(ref msg) => msg.type_id(),
+ &Message::ChannelAnnouncement(ref msg) => msg.type_id(),
+ &Message::NodeAnnouncement(ref msg) => msg.type_id(),
+ &Message::ChannelUpdate(ref msg) => msg.type_id(),
+ &Message::Unknown(type_id) => type_id,
+ }
+ }
+}
+
+impl MessageType {
+ /// Returns whether the message type is even, indicating both endpoints must support it.
+ pub fn is_even(&self) -> bool {
+ (self.0 & 1) == 0
+ }
+}
+
+impl ::std::fmt::Display for MessageType {
+ fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
+ write!(f, "{}", self.0)
+ }
+}
+
+/// Reads a message from the data buffer consisting of a 2-byte big-endian type and a
+/// variable-length payload conforming to the type.
+///
+/// # Errors
+///
+/// Returns an error if the message payload code not be decoded as the specified type.
+pub fn read<R: ::std::io::Read>(buffer: &mut R) -> Result<Message, msgs::DecodeError> {
+ let message_type = <u16 as Readable<R>>::read(buffer)?;
+ match message_type {
+ msgs::Init::TYPE => {
+ Ok(Message::Init(Readable::read(buffer)?))
+ },
+ msgs::ErrorMessage::TYPE => {
+ Ok(Message::Error(Readable::read(buffer)?))
+ },
+ msgs::Ping::TYPE => {
+ Ok(Message::Ping(Readable::read(buffer)?))
+ },
+ msgs::Pong::TYPE => {
+ Ok(Message::Pong(Readable::read(buffer)?))
+ },
+ msgs::OpenChannel::TYPE => {
+ Ok(Message::OpenChannel(Readable::read(buffer)?))
+ },
+ msgs::AcceptChannel::TYPE => {
+ Ok(Message::AcceptChannel(Readable::read(buffer)?))
+ },
+ msgs::FundingCreated::TYPE => {
+ Ok(Message::FundingCreated(Readable::read(buffer)?))
+ },
+ msgs::FundingSigned::TYPE => {
+ Ok(Message::FundingSigned(Readable::read(buffer)?))
+ },
+ msgs::FundingLocked::TYPE => {
+ Ok(Message::FundingLocked(Readable::read(buffer)?))
+ },
+ msgs::Shutdown::TYPE => {
+ Ok(Message::Shutdown(Readable::read(buffer)?))
+ },
+ msgs::ClosingSigned::TYPE => {
+ Ok(Message::ClosingSigned(Readable::read(buffer)?))
+ },
+ msgs::UpdateAddHTLC::TYPE => {
+ Ok(Message::UpdateAddHTLC(Readable::read(buffer)?))
+ },
+ msgs::UpdateFulfillHTLC::TYPE => {
+ Ok(Message::UpdateFulfillHTLC(Readable::read(buffer)?))
+ },
+ msgs::UpdateFailHTLC::TYPE => {
+ Ok(Message::UpdateFailHTLC(Readable::read(buffer)?))
+ },
+ msgs::UpdateFailMalformedHTLC::TYPE => {
+ Ok(Message::UpdateFailMalformedHTLC(Readable::read(buffer)?))
+ },
+ msgs::CommitmentSigned::TYPE => {
+ Ok(Message::CommitmentSigned(Readable::read(buffer)?))
+ },
+ msgs::RevokeAndACK::TYPE => {
+ Ok(Message::RevokeAndACK(Readable::read(buffer)?))
+ },
+ msgs::UpdateFee::TYPE => {
+ Ok(Message::UpdateFee(Readable::read(buffer)?))
+ },
+ msgs::ChannelReestablish::TYPE => {
+ Ok(Message::ChannelReestablish(Readable::read(buffer)?))
+ },
+ msgs::AnnouncementSignatures::TYPE => {
+ Ok(Message::AnnouncementSignatures(Readable::read(buffer)?))
+ },
+ msgs::ChannelAnnouncement::TYPE => {
+ Ok(Message::ChannelAnnouncement(Readable::read(buffer)?))
+ },
+ msgs::NodeAnnouncement::TYPE => {
+ Ok(Message::NodeAnnouncement(Readable::read(buffer)?))
+ },
+ msgs::ChannelUpdate::TYPE => {
+ Ok(Message::ChannelUpdate(Readable::read(buffer)?))
+ },
+ _ => {
+ Ok(Message::Unknown(MessageType(message_type)))
+ },
+ }
+}
+
+/// Writes a message to the data buffer encoded as a 2-byte big-endian type and a variable-length
+/// payload.
+///
+/// # Errors
+///
+/// Returns an I/O error if the write could not be completed.
+pub fn write<M: Encode + Writeable, W: Writer>(message: &M, buffer: &mut W) -> Result<(), ::std::io::Error> {
+ M::TYPE.write(buffer)?;
+ message.write(buffer)
+}
+
+/// Defines a type-identified encoding for sending messages over the wire.
+///
+/// Messages implementing this trait specify a type and must be [`Writeable`] to use with [`write`].
+///
+/// [`Writeable`]: ../../util/ser/trait.Writeable.html
+/// [`write`]: fn.write.html
+pub trait Encode {
+ /// The type identifying the message payload.
+ const TYPE: u16;
+
+ /// Returns the type identifying the message payload. Convenience method for accessing
+ /// [`TYPE`](TYPE).
+ fn type_id(&self) -> MessageType {
+ MessageType(Self::TYPE)
+ }
+}
+
+impl Encode for msgs::Init {
+ const TYPE: u16 = 16;
+}
+
+impl Encode for msgs::ErrorMessage {
+ const TYPE: u16 = 17;
+}
+
+impl Encode for msgs::Ping {
+ const TYPE: u16 = 18;
+}
+
+impl Encode for msgs::Pong {
+ const TYPE: u16 = 19;
+}
+
+impl Encode for msgs::OpenChannel {
+ const TYPE: u16 = 32;
+}
+
+impl Encode for msgs::AcceptChannel {
+ const TYPE: u16 = 33;
+}
+
+impl Encode for msgs::FundingCreated {
+ const TYPE: u16 = 34;
+}
+
+impl Encode for msgs::FundingSigned {
+ const TYPE: u16 = 35;
+}
+
+impl Encode for msgs::FundingLocked {
+ const TYPE: u16 = 36;
+}
+
+impl Encode for msgs::Shutdown {
+ const TYPE: u16 = 38;
+}
+
+impl Encode for msgs::ClosingSigned {
+ const TYPE: u16 = 39;
+}
+
+impl Encode for msgs::UpdateAddHTLC {
+ const TYPE: u16 = 128;
+}
+
+impl Encode for msgs::UpdateFulfillHTLC {
+ const TYPE: u16 = 130;
+}
+
+impl Encode for msgs::UpdateFailHTLC {
+ const TYPE: u16 = 131;
+}
+
+impl Encode for msgs::UpdateFailMalformedHTLC {
+ const TYPE: u16 = 135;
+}
+
+impl Encode for msgs::CommitmentSigned {
+ const TYPE: u16 = 132;
+}
+
+impl Encode for msgs::RevokeAndACK {
+ const TYPE: u16 = 133;
+}
+
+impl Encode for msgs::UpdateFee {
+ const TYPE: u16 = 134;
+}
+
+impl Encode for msgs::ChannelReestablish {
+ const TYPE: u16 = 136;
+}
+
+impl Encode for msgs::AnnouncementSignatures {
+ const TYPE: u16 = 259;
+}
+
+impl Encode for msgs::ChannelAnnouncement {
+ const TYPE: u16 = 256;
+}
+
+impl Encode for msgs::NodeAnnouncement {
+ const TYPE: u16 = 257;
+}
+
+impl Encode for msgs::ChannelUpdate {
+ const TYPE: u16 = 258;
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use util::byte_utils;
+
+ // Big-endian wire encoding of Pong message (type = 19, byteslen = 2).
+ const ENCODED_PONG: [u8; 6] = [0u8, 19u8, 0u8, 2u8, 0u8, 0u8];
+
+ #[test]
+ fn read_empty_buffer() {
+ let buffer = [];
+ let mut reader = ::std::io::Cursor::new(buffer);
+ assert!(read(&mut reader).is_err());
+ }
+
+ #[test]
+ fn read_incomplete_type() {
+ let buffer = &ENCODED_PONG[..1];
+ let mut reader = ::std::io::Cursor::new(buffer);
+ assert!(read(&mut reader).is_err());
+ }
+
+ #[test]
+ fn read_empty_payload() {
+ let buffer = &ENCODED_PONG[..2];
+ let mut reader = ::std::io::Cursor::new(buffer);
+ assert!(read(&mut reader).is_err());
+ }
+
+ #[test]
+ fn read_invalid_message() {
+ let buffer = &ENCODED_PONG[..4];
+ let mut reader = ::std::io::Cursor::new(buffer);
+ assert!(read(&mut reader).is_err());
+ }
+
+ #[test]
+ fn read_known_message() {
+ let buffer = &ENCODED_PONG[..];
+ let mut reader = ::std::io::Cursor::new(buffer);
+ let message = read(&mut reader).unwrap();
+ match message {
+ Message::Pong(_) => (),
+ _ => panic!("Expected pong message; found message type: {}", message.type_id()),
+ }
+ }
+
+ #[test]
+ fn read_unknown_message() {
+ let buffer = &byte_utils::be16_to_array(::std::u16::MAX);
+ let mut reader = ::std::io::Cursor::new(buffer);
+ let message = read(&mut reader).unwrap();
+ match message {
+ Message::Unknown(MessageType(::std::u16::MAX)) => (),
+ _ => panic!("Expected message type {}; found: {}", ::std::u16::MAX, message.type_id()),
+ }
+ }
+
+ #[test]
+ fn write_message_with_type() {
+ let message = msgs::Pong { byteslen: 2u16 };
+ let mut buffer = Vec::new();
+ assert!(write(&message, &mut buffer).is_ok());
+
+ let type_length = ::std::mem::size_of::<u16>();
+ let (type_bytes, payload_bytes) = buffer.split_at(type_length);
+ assert_eq!(byte_utils::slice_to_be16(type_bytes), msgs::Pong::TYPE);
+ assert_eq!(payload_bytes, &ENCODED_PONG[type_length..]);
+ }
+
+ #[test]
+ fn read_message_encoded_with_write() {
+ let message = msgs::Pong { byteslen: 2u16 };
+ let mut buffer = Vec::new();
+ assert!(write(&message, &mut buffer).is_ok());
+
+ let mut reader = ::std::io::Cursor::new(buffer);
+ let decoded_message = read(&mut reader).unwrap();
+ match decoded_message {
+ Message::Pong(msgs::Pong { byteslen: 2u16 }) => (),
+ Message::Pong(msgs::Pong { byteslen }) => {
+ panic!("Expected byteslen {}; found: {}", message.byteslen, byteslen);
+ },
+ _ => panic!("Expected pong message; found message type: {}", decoded_message.type_id()),
+ }
+ }
+
+ #[test]
+ fn is_even_message_type() {
+ let message = Message::Unknown(MessageType(42));
+ assert!(message.type_id().is_even());
+ }
+
+ #[test]
+ fn is_odd_message_type() {
+ let message = Message::Unknown(MessageType(43));
+ assert!(!message.type_id().is_even());
+ }
+}
// option. This file may not be copied, modified, or distributed
// except according to those terms.
+use std::io;
+
#[cfg(not(feature = "fuzztarget"))]
mod real_chacha {
use std::cmp;
}
}
+ const BLOCK_SIZE: usize = 64;
+
#[derive(Clone,Copy)]
struct ChaChaState {
a: u32x4,
#[derive(Copy)]
pub struct ChaCha20 {
state : ChaChaState,
- output : [u8; 64],
+ output : [u8; BLOCK_SIZE],
offset : usize,
}
assert!(key.len() == 16 || key.len() == 32);
assert!(nonce.len() == 8 || nonce.len() == 12);
- ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; 64], offset: 64 }
+ ChaCha20{ state: ChaCha20::expand(key, nonce), output: [0u8; BLOCK_SIZE], offset: 64 }
}
fn expand(key: &[u8], nonce: &[u8]) -> ChaChaState {
}
}
- // put the the next 64 keystream bytes into self.output
+ // put the the next BLOCK_SIZE keystream bytes into self.output
fn update(&mut self) {
let mut state = self.state;
while i < len {
// If there is no keystream available in the output buffer,
// generate the next block.
- if self.offset == 64 {
+ if self.offset == BLOCK_SIZE {
self.update();
}
// Process the min(available keystream, remaining input length).
- let count = cmp::min(64 - self.offset, len - i);
+ let count = cmp::min(BLOCK_SIZE - self.offset, len - i);
// explicitly assert lengths to avoid bounds checks:
assert!(output.len() >= i + count);
assert!(input.len() >= i + count);
self.offset += count;
}
}
+
+ pub fn process_in_place(&mut self, input_output: &mut [u8]) {
+ let len = input_output.len();
+ let mut i = 0;
+ while i < len {
+ // If there is no keystream available in the output buffer,
+ // generate the next block.
+ if self.offset == BLOCK_SIZE {
+ self.update();
+ }
+
+ // Process the min(available keystream, remaining input length).
+ let count = cmp::min(BLOCK_SIZE - self.offset, len - i);
+ // explicitly assert lengths to avoid bounds checks:
+ assert!(input_output.len() >= i + count);
+ assert!(self.output.len() >= self.offset + count);
+ for j in 0..count {
+ input_output[i + j] ^= self.output[self.offset + j];
+ }
+ i += count;
+ self.offset += count;
+ }
+ }
}
}
#[cfg(not(feature = "fuzztarget"))]
pub fn process(&mut self, input: &[u8], output: &mut [u8]) {
output.copy_from_slice(input);
}
+
+ pub fn process_in_place(&mut self, _input_output: &mut [u8]) {}
}
}
#[cfg(feature = "fuzztarget")]
pub use self::fuzzy_chacha::ChaCha20;
+pub(crate) struct ChaChaReader<'a, R: io::Read> {
+ pub chacha: &'a mut ChaCha20,
+ pub read: R,
+}
+impl<'a, R: io::Read> io::Read for ChaChaReader<'a, R> {
+ fn read(&mut self, dest: &mut [u8]) -> Result<usize, io::Error> {
+ let res = self.read.read(dest)?;
+ if res > 0 {
+ self.chacha.process_in_place(&mut dest[0..res]);
+ }
+ Ok(res)
+ }
+}
+
#[cfg(test)]
mod test {
use std::iter::repeat;
use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
use std::cmp;
-use std::sync::Mutex;
+use std::sync::{Mutex, Arc};
use bitcoin::blockdata::transaction::Transaction;
use secp256k1;
use secp256k1::key::{SecretKey, PublicKey};
use secp256k1::{Secp256k1, Signature};
+use util::ser::{Writeable, Writer, Readable};
+use std::io::Error;
+use ln::msgs::DecodeError;
/// Enforces some rules on ChannelKeys calls. Eventually we will probably want to expose a variant
/// of this which would essentially be what you'd want to run on a hardware wallet.
+#[derive(Clone)]
pub struct EnforcingChannelKeys {
pub inner: InMemoryChannelKeys,
- commitment_number_obscure_and_last: Mutex<(Option<u64>, u64)>,
+ commitment_number_obscure_and_last: Arc<Mutex<(Option<u64>, u64)>>,
}
impl EnforcingChannelKeys {
pub fn new(inner: InMemoryChannelKeys) -> Self {
Self {
inner,
- commitment_number_obscure_and_last: Mutex::new((None, 0)),
+ commitment_number_obscure_and_last: Arc::new(Mutex::new((None, 0))),
}
}
}
impl EnforcingChannelKeys {
fn check_keys<T: secp256k1::Signing + secp256k1::Verification>(&self, secp_ctx: &Secp256k1<T>,
keys: &TxCreationKeys) {
- let revocation_base = PublicKey::from_secret_key(secp_ctx, &self.inner.revocation_base_key);
- let payment_base = PublicKey::from_secret_key(secp_ctx, &self.inner.payment_base_key);
- let htlc_base = PublicKey::from_secret_key(secp_ctx, &self.inner.htlc_base_key);
+ let revocation_base = PublicKey::from_secret_key(secp_ctx, &self.inner.revocation_base_key());
+ let payment_base = PublicKey::from_secret_key(secp_ctx, &self.inner.payment_base_key());
+ let htlc_base = PublicKey::from_secret_key(secp_ctx, &self.inner.htlc_base_key());
let remote_points = self.inner.remote_channel_pubkeys.as_ref().unwrap();
fn delayed_payment_base_key(&self) -> &SecretKey { self.inner.delayed_payment_base_key() }
fn htlc_base_key(&self) -> &SecretKey { self.inner.htlc_base_key() }
fn commitment_seed(&self) -> &[u8; 32] { self.inner.commitment_seed() }
+ fn pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys { self.inner.pubkeys() }
fn sign_remote_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
if commitment_tx.input.len() != 1 { panic!("lightning commitment transactions have a single input"); }
}
}
+impl Writeable for EnforcingChannelKeys {
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), Error> {
+ self.inner.write(writer)?;
+ let (obscure, last) = *self.commitment_number_obscure_and_last.lock().unwrap();
+ obscure.write(writer)?;
+ last.write(writer)?;
+ Ok(())
+ }
+}
-impl_writeable!(EnforcingChannelKeys, 0, {
- inner,
- commitment_number_obscure_and_last
-});
+impl<R: ::std::io::Read> Readable<R> for EnforcingChannelKeys {
+ fn read(reader: &mut R) -> Result<Self, DecodeError> {
+ let inner = Readable::read(reader)?;
+ let obscure_and_last = Readable::read(reader)?;
+ Ok(EnforcingChannelKeys {
+ inner: inner,
+ commitment_number_obscure_and_last: Arc::new(Mutex::new(obscure_and_last))
+ })
+ }
+}
use std::collections::HashMap;
use std::hash::Hash;
use std::sync::Mutex;
+use std::cmp;
use secp256k1::Signature;
use secp256k1::key::{PublicKey, SecretKey};
}
}
-struct VecWriter(Vec<u8>);
+pub(crate) struct VecWriter(pub Vec<u8>);
impl Writer for VecWriter {
fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
self.0.extend_from_slice(buf);
}
}
+/// Writer that only tracks the amount of data written - useful if you need to calculate the length
+/// of some data when serialized but don't yet need the full data.
+pub(crate) struct LengthCalculatingWriter(pub usize);
+impl Writer for LengthCalculatingWriter {
+ #[inline]
+ fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
+ self.0 += buf.len();
+ Ok(())
+ }
+ #[inline]
+ fn size_hint(&mut self, _size: usize) {}
+}
+
+/// Essentially std::io::Take but a bit simpler and with a method to walk the underlying stream
+/// forward to ensure we always consume exactly the fixed length specified.
+pub(crate) struct FixedLengthReader<R: Read> {
+ read: R,
+ bytes_read: u64,
+ total_bytes: u64,
+}
+impl<R: Read> FixedLengthReader<R> {
+ pub fn new(read: R, total_bytes: u64) -> Self {
+ Self { read, bytes_read: 0, total_bytes }
+ }
+
+ pub fn bytes_remain(&mut self) -> bool {
+ self.bytes_read != self.total_bytes
+ }
+
+ pub fn eat_remaining(&mut self) -> Result<(), DecodeError> {
+ ::std::io::copy(self, &mut ::std::io::sink()).unwrap();
+ if self.bytes_read != self.total_bytes {
+ Err(DecodeError::ShortRead)
+ } else {
+ Ok(())
+ }
+ }
+}
+impl<R: Read> Read for FixedLengthReader<R> {
+ fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
+ if self.total_bytes == self.bytes_read {
+ Ok(0)
+ } else {
+ let read_len = cmp::min(dest.len() as u64, self.total_bytes - self.bytes_read);
+ match self.read.read(&mut dest[0..(read_len as usize)]) {
+ Ok(v) => {
+ self.bytes_read += v as u64;
+ Ok(v)
+ },
+ Err(e) => Err(e),
+ }
+ }
+ }
+}
+
+/// A Read which tracks whether any bytes have been read at all. This allows us to distinguish
+/// between "EOF reached before we started" and "EOF reached mid-read".
+pub(crate) struct ReadTrackingReader<R: Read> {
+ read: R,
+ pub have_read: bool,
+}
+impl<R: Read> ReadTrackingReader<R> {
+ pub fn new(read: R) -> Self {
+ Self { read, have_read: false }
+ }
+}
+impl<R: Read> Read for ReadTrackingReader<R> {
+ fn read(&mut self, dest: &mut [u8]) -> Result<usize, ::std::io::Error> {
+ match self.read.read(dest) {
+ Ok(0) => Ok(0),
+ Ok(len) => {
+ self.have_read = true;
+ Ok(len)
+ },
+ Err(e) => Err(e),
+ }
+ }
+}
+
/// A trait that various rust-lightning types implement allowing them to be written out to a Writer
pub trait Writeable {
/// Writes self out to the given Writer
}
}
+/// Lightning TLV uses a custom variable-length integer called BigSize. It is similar to Bitcoin's
+/// variable-length integers except that it is serialized in big-endian instead of little-endian.
+///
+/// Like Bitcoin's variable-length integer, it exhibits ambiguity in that certain values can be
+/// encoded in several different ways, which we must check for at deserialization-time. Thus, if
+/// you're looking for an example of a variable-length integer to use for your own project, move
+/// along, this is a rather poor design.
+pub(crate) struct BigSize(pub u64);
+impl Writeable for BigSize {
+ #[inline]
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ match self.0 {
+ 0...0xFC => {
+ (self.0 as u8).write(writer)
+ },
+ 0xFD...0xFFFF => {
+ 0xFDu8.write(writer)?;
+ (self.0 as u16).write(writer)
+ },
+ 0x10000...0xFFFFFFFF => {
+ 0xFEu8.write(writer)?;
+ (self.0 as u32).write(writer)
+ },
+ _ => {
+ 0xFFu8.write(writer)?;
+ (self.0 as u64).write(writer)
+ },
+ }
+ }
+}
+impl<R: Read> Readable<R> for BigSize {
+ #[inline]
+ fn read(reader: &mut R) -> Result<BigSize, DecodeError> {
+ let n: u8 = Readable::read(reader)?;
+ match n {
+ 0xFF => {
+ let x: u64 = Readable::read(reader)?;
+ if x < 0x100000000 {
+ Err(DecodeError::InvalidValue)
+ } else {
+ Ok(BigSize(x))
+ }
+ }
+ 0xFE => {
+ let x: u32 = Readable::read(reader)?;
+ if x < 0x10000 {
+ Err(DecodeError::InvalidValue)
+ } else {
+ Ok(BigSize(x as u64))
+ }
+ }
+ 0xFD => {
+ let x: u16 = Readable::read(reader)?;
+ if x < 0xFD {
+ Err(DecodeError::InvalidValue)
+ } else {
+ Ok(BigSize(x as u64))
+ }
+ }
+ n => Ok(BigSize(n as u64))
+ }
+ }
+}
+
+/// In TLV we occasionally send fields which only consist of, or potentially end with, a
+/// variable-length integer which is simply truncated by skipping high zero bytes. This type
+/// encapsulates such integers implementing Readable/Writeable for them.
+#[cfg_attr(test, derive(PartialEq, Debug))]
+pub(crate) struct HighZeroBytesDroppedVarInt<T>(pub T);
+
macro_rules! impl_writeable_primitive {
($val_type:ty, $meth_write:ident, $len: expr, $meth_read:ident) => {
impl Writeable for $val_type {
writer.write_all(&$meth_write(*self))
}
}
+ impl Writeable for HighZeroBytesDroppedVarInt<$val_type> {
+ #[inline]
+ fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
+ // Skip any full leading 0 bytes when writing (in BE):
+ writer.write_all(&$meth_write(self.0)[(self.0.leading_zeros()/8) as usize..$len])
+ }
+ }
impl<R: Read> Readable<R> for $val_type {
#[inline]
fn read(reader: &mut R) -> Result<$val_type, DecodeError> {
Ok($meth_read(&buf))
}
}
+ impl<R: Read> Readable<R> for HighZeroBytesDroppedVarInt<$val_type> {
+ #[inline]
+ fn read(reader: &mut R) -> Result<HighZeroBytesDroppedVarInt<$val_type>, DecodeError> {
+ // We need to accept short reads (read_len == 0) as "EOF" and handle them as simply
+ // the high bytes being dropped. To do so, we start reading into the middle of buf
+ // and then convert the appropriate number of bytes with extra high bytes out of
+ // buf.
+ let mut buf = [0; $len*2];
+ let mut read_len = reader.read(&mut buf[$len..])?;
+ let mut total_read_len = read_len;
+ while read_len != 0 && total_read_len != $len {
+ read_len = reader.read(&mut buf[($len + total_read_len)..])?;
+ total_read_len += read_len;
+ }
+ if total_read_len == 0 || buf[$len] != 0 {
+ let first_byte = $len - ($len - total_read_len);
+ Ok(HighZeroBytesDroppedVarInt($meth_read(&buf[first_byte..first_byte + $len])))
+ } else {
+ // If the encoding had extra zero bytes, return a failure even though we know
+ // what they meant (as the TLV test vectors require this)
+ Err(DecodeError::InvalidValue)
+ }
+ }
+ }
}
}
+macro_rules! encode_tlv {
+ ($stream: expr, {$(($type: expr, $field: expr)),*}) => { {
+ use util::ser::{BigSize, LengthCalculatingWriter};
+ $(
+ BigSize($type).write($stream)?;
+ let mut len_calc = LengthCalculatingWriter(0);
+ $field.write(&mut len_calc)?;
+ BigSize(len_calc.0 as u64).write($stream)?;
+ $field.write($stream)?;
+ )*
+ } }
+}
+
+macro_rules! encode_varint_length_prefixed_tlv {
+ ($stream: expr, {$(($type: expr, $field: expr)),*}) => { {
+ use util::ser::{BigSize, LengthCalculatingWriter};
+ let mut len = LengthCalculatingWriter(0);
+ {
+ $(
+ BigSize($type).write(&mut len)?;
+ let mut field_len = LengthCalculatingWriter(0);
+ $field.write(&mut field_len)?;
+ BigSize(field_len.0 as u64).write(&mut len)?;
+ len.0 += field_len.0;
+ )*
+ }
+
+ BigSize(len.0 as u64).write($stream)?;
+ encode_tlv!($stream, {
+ $(($type, $field)),*
+ });
+ } }
+}
+
+macro_rules! decode_tlv {
+ ($stream: expr, {$(($reqtype: expr, $reqfield: ident)),*}, {$(($type: expr, $field: ident)),*}) => { {
+ use ln::msgs::DecodeError;
+ let mut last_seen_type: Option<u64> = None;
+ 'tlv_read: loop {
+ use util::ser;
+
+ // First decode the type of this TLV:
+ let typ: ser::BigSize = {
+ // We track whether any bytes were read during the consensus_decode call to
+ // determine whether we should break or return ShortRead if we get an
+ // UnexpectedEof. This should in every case be largely cosmetic, but its nice to
+ // pass the TLV test vectors exactly, which requre this distinction.
+ let mut tracking_reader = ser::ReadTrackingReader::new($stream);
+ match ser::Readable::read(&mut tracking_reader) {
+ Err(DecodeError::ShortRead) => {
+ if !tracking_reader.have_read {
+ break 'tlv_read
+ } else {
+ Err(DecodeError::ShortRead)?
+ }
+ },
+ Err(e) => Err(e)?,
+ Ok(t) => t,
+ }
+ };
+
+ // Types must be unique and monotonically increasing:
+ match last_seen_type {
+ Some(t) if typ.0 <= t => {
+ Err(DecodeError::InvalidValue)?
+ },
+ _ => {},
+ }
+ // As we read types, make sure we hit every required type:
+ $(if (last_seen_type.is_none() || last_seen_type.unwrap() < $reqtype) && typ.0 > $reqtype {
+ Err(DecodeError::InvalidValue)?
+ })*
+ last_seen_type = Some(typ.0);
+
+ // Finally, read the length and value itself:
+ let length: ser::BigSize = Readable::read($stream)?;
+ let mut s = ser::FixedLengthReader::new($stream, length.0);
+ match typ.0 {
+ $($reqtype => {
+ $reqfield = ser::Readable::read(&mut s)?;
+ if s.bytes_remain() {
+ s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
+ Err(DecodeError::InvalidValue)?
+ }
+ },)*
+ $($type => {
+ $field = Some(ser::Readable::read(&mut s)?);
+ if s.bytes_remain() {
+ s.eat_remaining()?; // Return ShortRead if there's actually not enough bytes
+ Err(DecodeError::InvalidValue)?
+ }
+ },)*
+ x if x % 2 == 0 => {
+ Err(DecodeError::UnknownRequiredFeature)?
+ },
+ _ => {},
+ }
+ s.eat_remaining()?;
+ }
+ // Make sure we got to each required type after we've read every TLV:
+ $(if last_seen_type.is_none() || last_seen_type.unwrap() < $reqtype {
+ Err(DecodeError::InvalidValue)?
+ })*
+ } }
+}
+
macro_rules! impl_writeable {
($st:ident, $len: expr, {$($field:ident),*}) => {
impl ::util::ser::Writeable for $st {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use std::io::{Cursor, Read};
+ use ln::msgs::DecodeError;
+ use util::ser::{Readable, Writeable, HighZeroBytesDroppedVarInt, VecWriter};
+ use secp256k1::PublicKey;
+
+ // The BOLT TLV test cases don't include any tests which use our "required-value" logic since
+ // the encoding layer in the BOLTs has no such concept, though it makes our macros easier to
+ // work with so they're baked into the decoder. Thus, we have a few additional tests below
+ fn tlv_reader(s: &[u8]) -> Result<(u64, u32, Option<u32>), DecodeError> {
+ let mut s = Cursor::new(s);
+ let mut a: u64 = 0;
+ let mut b: u32 = 0;
+ let mut c: Option<u32> = None;
+ decode_tlv!(&mut s, {(2, a), (3, b)}, {(4, c)});
+ Ok((a, b, c))
+ }
+
+ #[test]
+ fn tlv_v_short_read() {
+ // We only expect a u32 for type 3 (which we are given), but the L says its 8 bytes.
+ if let Err(DecodeError::ShortRead) = tlv_reader(&::hex::decode(
+ concat!("0100", "0208deadbeef1badbeef", "0308deadbeef")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ }
+
+ #[test]
+ fn tlv_types_out_of_order() {
+ if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+ concat!("0100", "0304deadbeef", "0208deadbeef1badbeef")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ // ...even if its some field we don't understand
+ if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+ concat!("0208deadbeef1badbeef", "0100", "0304deadbeef")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ }
+
+ #[test]
+ fn tlv_req_type_missing_or_extra() {
+ // It's also bad if they included even fields we don't understand
+ if let Err(DecodeError::UnknownRequiredFeature) = tlv_reader(&::hex::decode(
+ concat!("0100", "0208deadbeef1badbeef", "0304deadbeef", "0600")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ // ... or if they're missing fields we need
+ if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+ concat!("0100", "0208deadbeef1badbeef")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ // ... even if that field is even
+ if let Err(DecodeError::InvalidValue) = tlv_reader(&::hex::decode(
+ concat!("0304deadbeef", "0500")
+ ).unwrap()[..]) {
+ } else { panic!(); }
+ }
+
+ #[test]
+ fn tlv_simple_good_cases() {
+ assert_eq!(tlv_reader(&::hex::decode(
+ concat!("0208deadbeef1badbeef", "03041bad1dea")
+ ).unwrap()[..]).unwrap(),
+ (0xdeadbeef1badbeef, 0x1bad1dea, None));
+ assert_eq!(tlv_reader(&::hex::decode(
+ concat!("0208deadbeef1badbeef", "03041bad1dea", "040401020304")
+ ).unwrap()[..]).unwrap(),
+ (0xdeadbeef1badbeef, 0x1bad1dea, Some(0x01020304)));
+ }
+
+ impl<R: Read> Readable<R> for (PublicKey, u64, u64) {
+ #[inline]
+ fn read(reader: &mut R) -> Result<(PublicKey, u64, u64), DecodeError> {
+ Ok((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?))
+ }
+ }
+
+ // BOLT TLV test cases
+ fn tlv_reader_n1(s: &[u8]) -> Result<(Option<HighZeroBytesDroppedVarInt<u64>>, Option<u64>, Option<(PublicKey, u64, u64)>, Option<u16>), DecodeError> {
+ let mut s = Cursor::new(s);
+ let mut tlv1: Option<HighZeroBytesDroppedVarInt<u64>> = None;
+ let mut tlv2: Option<u64> = None;
+ let mut tlv3: Option<(PublicKey, u64, u64)> = None;
+ let mut tlv4: Option<u16> = None;
+ decode_tlv!(&mut s, {}, {(1, tlv1), (2, tlv2), (3, tlv3), (254, tlv4)});
+ Ok((tlv1, tlv2, tlv3, tlv4))
+ }
+
+ #[test]
+ fn bolt_tlv_bogus_stream() {
+ macro_rules! do_test {
+ ($stream: expr, $reason: ident) => {
+ if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+ } else { panic!(); }
+ }
+ }
+
+ // TLVs from the BOLT test cases which should not decode as either n1 or n2
+ do_test!(concat!("fd01"), ShortRead);
+ do_test!(concat!("fd0001", "00"), InvalidValue);
+ do_test!(concat!("fd0101"), ShortRead);
+ do_test!(concat!("0f", "fd"), ShortRead);
+ do_test!(concat!("0f", "fd26"), ShortRead);
+ do_test!(concat!("0f", "fd2602"), ShortRead);
+ do_test!(concat!("0f", "fd0001", "00"), InvalidValue);
+ do_test!(concat!("0f", "fd0201", "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), ShortRead);
+
+ do_test!(concat!("12", "00"), UnknownRequiredFeature);
+ do_test!(concat!("fd0102", "00"), UnknownRequiredFeature);
+ do_test!(concat!("fe01000002", "00"), UnknownRequiredFeature);
+ do_test!(concat!("ff0100000000000002", "00"), UnknownRequiredFeature);
+ }
+
+ #[test]
+ fn bolt_tlv_bogus_n1_stream() {
+ macro_rules! do_test {
+ ($stream: expr, $reason: ident) => {
+ if let Err(DecodeError::$reason) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+ } else { panic!(); }
+ }
+ }
+
+ // TLVs from the BOLT test cases which should not decode as n1
+ do_test!(concat!("01", "09", "ffffffffffffffffff"), InvalidValue);
+ do_test!(concat!("01", "01", "00"), InvalidValue);
+ do_test!(concat!("01", "02", "0001"), InvalidValue);
+ do_test!(concat!("01", "03", "000100"), InvalidValue);
+ do_test!(concat!("01", "04", "00010000"), InvalidValue);
+ do_test!(concat!("01", "05", "0001000000"), InvalidValue);
+ do_test!(concat!("01", "06", "000100000000"), InvalidValue);
+ do_test!(concat!("01", "07", "00010000000000"), InvalidValue);
+ do_test!(concat!("01", "08", "0001000000000000"), InvalidValue);
+ do_test!(concat!("02", "07", "01010101010101"), ShortRead);
+ do_test!(concat!("02", "09", "010101010101010101"), InvalidValue);
+ do_test!(concat!("03", "21", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb"), ShortRead);
+ do_test!(concat!("03", "29", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001"), ShortRead);
+ do_test!(concat!("03", "30", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb000000000000000100000000000001"), ShortRead);
+ do_test!(concat!("03", "31", "043da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002"), InvalidValue);
+ do_test!(concat!("03", "32", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb0000000000000001000000000000000001"), InvalidValue);
+ do_test!(concat!("fd00fe", "00"), ShortRead);
+ do_test!(concat!("fd00fe", "01", "01"), ShortRead);
+ do_test!(concat!("fd00fe", "03", "010101"), InvalidValue);
+ do_test!(concat!("00", "00"), UnknownRequiredFeature);
+
+ do_test!(concat!("02", "08", "0000000000000226", "01", "01", "2a"), InvalidValue);
+ do_test!(concat!("02", "08", "0000000000000231", "02", "08", "0000000000000451"), InvalidValue);
+ do_test!(concat!("1f", "00", "0f", "01", "2a"), InvalidValue);
+ do_test!(concat!("1f", "00", "1f", "01", "2a"), InvalidValue);
+
+ // The last BOLT test modified to not require creating a new decoder for one trivial test.
+ do_test!(concat!("ffffffffffffffffff", "00", "01", "00"), InvalidValue);
+ }
+
+ #[test]
+ fn bolt_tlv_valid_n1_stream() {
+ macro_rules! do_test {
+ ($stream: expr, $tlv1: expr, $tlv2: expr, $tlv3: expr, $tlv4: expr) => {
+ if let Ok((tlv1, tlv2, tlv3, tlv4)) = tlv_reader_n1(&::hex::decode($stream).unwrap()[..]) {
+ assert_eq!(tlv1.map(|v| v.0), $tlv1);
+ assert_eq!(tlv2, $tlv2);
+ assert_eq!(tlv3, $tlv3);
+ assert_eq!(tlv4, $tlv4);
+ } else { panic!(); }
+ }
+ }
+
+ do_test!(concat!(""), None, None, None, None);
+ do_test!(concat!("21", "00"), None, None, None, None);
+ do_test!(concat!("fd0201", "00"), None, None, None, None);
+ do_test!(concat!("fd00fd", "00"), None, None, None, None);
+ do_test!(concat!("fd00ff", "00"), None, None, None, None);
+ do_test!(concat!("fe02000001", "00"), None, None, None, None);
+ do_test!(concat!("ff0200000000000001", "00"), None, None, None, None);
+
+ do_test!(concat!("01", "00"), Some(0), None, None, None);
+ do_test!(concat!("01", "01", "01"), Some(1), None, None, None);
+ do_test!(concat!("01", "02", "0100"), Some(256), None, None, None);
+ do_test!(concat!("01", "03", "010000"), Some(65536), None, None, None);
+ do_test!(concat!("01", "04", "01000000"), Some(16777216), None, None, None);
+ do_test!(concat!("01", "05", "0100000000"), Some(4294967296), None, None, None);
+ do_test!(concat!("01", "06", "010000000000"), Some(1099511627776), None, None, None);
+ do_test!(concat!("01", "07", "01000000000000"), Some(281474976710656), None, None, None);
+ do_test!(concat!("01", "08", "0100000000000000"), Some(72057594037927936), None, None, None);
+ do_test!(concat!("02", "08", "0000000000000226"), None, Some((0 << 30) | (0 << 5) | (550 << 0)), None, None);
+ do_test!(concat!("03", "31", "023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb00000000000000010000000000000002"),
+ None, None, Some((
+ PublicKey::from_slice(&::hex::decode("023da092f6980e58d2c037173180e9a465476026ee50f96695963e8efe436f54eb").unwrap()[..]).unwrap(), 1, 2)),
+ None);
+ do_test!(concat!("fd00fe", "02", "0226"), None, None, None, Some(550));
+ }
+
+ fn do_simple_test_tlv_write() -> Result<(), ::std::io::Error> {
+ let mut stream = VecWriter(Vec::new());
+
+ stream.0.clear();
+ encode_varint_length_prefixed_tlv!(&mut stream, { (1, 1u8) });
+ assert_eq!(stream.0, ::hex::decode("03010101").unwrap());
+
+ stream.0.clear();
+ encode_varint_length_prefixed_tlv!(&mut stream, { (4, 0xabcdu16) });
+ assert_eq!(stream.0, ::hex::decode("040402abcd").unwrap());
+
+ stream.0.clear();
+ encode_varint_length_prefixed_tlv!(&mut stream, { (0xff, 0xabcdu16) });
+ assert_eq!(stream.0, ::hex::decode("06fd00ff02abcd").unwrap());
+
+ stream.0.clear();
+ encode_varint_length_prefixed_tlv!(&mut stream, { (0, 1u64), (0xff, HighZeroBytesDroppedVarInt(0u64)) });
+ assert_eq!(stream.0, ::hex::decode("0e00080000000000000001fd00ff00").unwrap());
+
+ Ok(())
+ }
+
+ #[test]
+ fn simple_test_tlv_write() {
+ do_simple_test_tlv_write().unwrap();
+ }
+}
use util::enforcing_trait_impls::EnforcingChannelKeys;
use util::events;
use util::logger::{Logger, Level, Record};
-use util::ser::{ReadableArgs, Writer};
+use util::ser::ReadableArgs;
+use util::ser::Writer;
use bitcoin::blockdata::transaction::Transaction;
use bitcoin::blockdata::script::Script;
}
pub struct TestChannelMonitor {
- pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor)>>,
- pub simple_monitor: channelmonitor::SimpleManyChannelMonitor<OutPoint>,
+ pub added_monitors: Mutex<Vec<(OutPoint, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>>,
+ pub simple_monitor: channelmonitor::SimpleManyChannelMonitor<OutPoint, EnforcingChannelKeys>,
pub update_ret: Mutex<Result<(), channelmonitor::ChannelMonitorUpdateErr>>,
}
impl TestChannelMonitor {
}
}
}
-impl channelmonitor::ManyChannelMonitor for TestChannelMonitor {
- fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
+impl channelmonitor::ManyChannelMonitor<EnforcingChannelKeys> for TestChannelMonitor {
+ fn add_update_monitor(&self, funding_txo: OutPoint, monitor: channelmonitor::ChannelMonitor<EnforcingChannelKeys>) -> Result<(), channelmonitor::ChannelMonitorUpdateErr> {
// At every point where we get a monitor update, we should be able to send a useful monitor
// to a watchtower and disk...
let mut w = TestVecWriter(Vec::new());
monitor.write_for_disk(&mut w).unwrap();
- assert!(<(Sha256dHash, channelmonitor::ChannelMonitor)>::read(
+ assert!(<(Sha256dHash, channelmonitor::ChannelMonitor<EnforcingChannelKeys>)>::read(
&mut ::std::io::Cursor::new(&w.0), Arc::new(TestLogger::new())).unwrap().1 == monitor);
w.0.clear();
monitor.write_for_watchtower(&mut w).unwrap(); // This at least shouldn't crash...
fn get_next_node_announcements(&self, _starting_point: Option<&PublicKey>, _batch_amount: u8) -> Vec<msgs::NodeAnnouncement> {
Vec::new()
}
+ fn should_request_full_sync(&self, _node_id: &PublicKey) -> bool {
+ true
+ }
}
pub struct TestLogger {