Skip to content

Commit 92ff499

Browse files
authored
Merge pull request #191 from TheBlueMatt/2018-09-chanmon-ser-framework
Migrate ChannelMonitor writing/reading to new serialization framework
2 parents 66fbc66 + d2a6ca5 commit 92ff499

File tree

9 files changed

+371
-339
lines changed

9 files changed

+371
-339
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lightning"
3-
version = "0.0.4"
3+
version = "0.0.5"
44
authors = ["Matt Corallo"]
55
license = "Apache-2.0"
66
repository = "https://github.com/rust-bitcoin/rust-lightning/"
@@ -37,3 +37,6 @@ features = ["bitcoinconsensus"]
3737

3838
[dev-dependencies]
3939
hex = "0.3"
40+
41+
[profile.dev]
42+
opt-level = 1

fuzz/fuzz_targets/chanmon_deser_target.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,30 @@ extern crate lightning;
55

66
use lightning::ln::channelmonitor;
77
use lightning::util::reset_rng_state;
8+
use lightning::util::ser::{Readable, Writer};
9+
10+
use std::io::Cursor;
11+
12+
struct VecWriter(Vec<u8>);
13+
impl Writer for VecWriter {
14+
fn write_all(&mut self, buf: &[u8]) -> Result<(), ::std::io::Error> {
15+
self.0.extend_from_slice(buf);
16+
Ok(())
17+
}
18+
fn size_hint(&mut self, size: usize) {
19+
self.0.reserve_exact(size);
20+
}
21+
}
822

923
#[inline]
1024
pub fn do_test(data: &[u8]) {
1125
reset_rng_state();
12-
if let Some(monitor) = channelmonitor::ChannelMonitor::deserialize(data) {
13-
assert!(channelmonitor::ChannelMonitor::deserialize(&monitor.serialize_for_disk()[..]).unwrap() == monitor);
14-
monitor.serialize_for_watchtower();
26+
if let Ok(monitor) = channelmonitor::ChannelMonitor::read(&mut Cursor::new(data)) {
27+
let mut w = VecWriter(Vec::new());
28+
monitor.write_for_disk(&mut w).unwrap();
29+
assert!(channelmonitor::ChannelMonitor::read(&mut Cursor::new(&w.0)).unwrap() == monitor);
30+
w.0.clear();
31+
monitor.write_for_watchtower(&mut w).unwrap();
1532
}
1633
}
1734

fuzz/fuzz_targets/router_target.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,12 @@ pub fn do_test(data: &[u8]) {
125125
match <($MsgType)>::read(&mut reader) {
126126
Ok(msg) => msg,
127127
Err(e) => match e {
128-
msgs::DecodeError::UnknownRealmByte => return,
128+
msgs::DecodeError::UnknownVersion => return,
129129
msgs::DecodeError::UnknownRequiredFeature => return,
130-
msgs::DecodeError::BadPublicKey => return,
131-
msgs::DecodeError::BadSignature => return,
132-
msgs::DecodeError::BadText => return,
130+
msgs::DecodeError::InvalidValue => return,
133131
msgs::DecodeError::ExtraAddressesPerType => return,
134132
msgs::DecodeError::BadLengthDescriptor => return,
135133
msgs::DecodeError::ShortRead => panic!("We picked the length..."),
136-
msgs::DecodeError::InvalidValue => panic!("Should not happen with p2p message decoding"),
137134
msgs::DecodeError::Io(e) => panic!(format!("{}", e)),
138135
}
139136
}

src/ln/channelmanager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ impl ChannelManager {
806806
match msgs::OnionHopData::read(&mut Cursor::new(&decoded[..])) {
807807
Err(err) => {
808808
let error_code = match err {
809-
msgs::DecodeError::UnknownRealmByte => 0x4000 | 1,
809+
msgs::DecodeError::UnknownVersion => 0x4000 | 1, // unknown realm byte
810810
_ => 0x2000 | 2, // Should never happen
811811
};
812812
return_err!("Unable to decode our hop data", error_code, &[0;0]);

0 commit comments

Comments
 (0)