Skip to content

Commit 368d9bd

Browse files
author
Antoine Riard
committed
Implement serialize/deserialize for Router.
Extend route_test to check if serialize/deserialize of NetworkMap works. Add PartialEq traits on some Router's structs. Modify also UnsignedNodeAnnouncement serialization
1 parent 301f91e commit 368d9bd

File tree

9 files changed

+456
-166
lines changed

9 files changed

+456
-166
lines changed

fuzz/fuzz_targets/router_target.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ pub fn do_test(data: &[u8]) {
125125
match <($MsgType)>::read(&mut reader) {
126126
Ok(msg) => msg,
127127
Err(e) => match e {
128-
msgs::DecodeError::UnknownVersion => return,
128+
msgs::DecodeError::UnknownVersion { .. } => return,
129129
msgs::DecodeError::UnknownRequiredFeature => return,
130-
msgs::DecodeError::InvalidValue => return,
130+
msgs::DecodeError::InvalidValue { .. } => return,
131131
msgs::DecodeError::ExtraAddressesPerType => return,
132132
msgs::DecodeError::BadLengthDescriptor => return,
133133
msgs::DecodeError::ShortRead => panic!("We picked the length..."),

src/ln/channel.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,7 @@ impl<R: ::std::io::Read> Readable<R> for InboundHTLCRemovalReason {
34403440
0 => InboundHTLCRemovalReason::FailRelay(Readable::read(reader)?),
34413441
1 => InboundHTLCRemovalReason::FailMalformed((Readable::read(reader)?, Readable::read(reader)?)),
34423442
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
3443-
_ => return Err(DecodeError::InvalidValue),
3443+
_ => return Err(DecodeError::InvalidValue { byte: None }),
34443444
})
34453445
}
34463446
}
@@ -3686,7 +3686,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
36863686
2 => InboundHTLCState::AwaitingAnnouncedRemoteRevoke(Readable::read(reader)?),
36873687
3 => InboundHTLCState::Committed,
36883688
4 => InboundHTLCState::LocalRemoved(Readable::read(reader)?),
3689-
_ => return Err(DecodeError::InvalidValue),
3689+
_ => return Err(DecodeError::InvalidValue { byte: None }),
36903690
},
36913691
});
36923692
}
@@ -3695,7 +3695,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
36953695
match <u8 as Readable<R>>::read(reader)? {
36963696
0 => None,
36973697
1 => Some(Readable::read(reader)?),
3698-
_ => return Err(DecodeError::InvalidValue),
3698+
_ => return Err(DecodeError::InvalidValue { byte: None }),
36993699
}
37003700
} }
37013701

@@ -3715,7 +3715,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37153715
2 => OutboundHTLCState::RemoteRemoved,
37163716
3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove,
37173717
4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke,
3718-
_ => return Err(DecodeError::InvalidValue),
3718+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37193719
},
37203720
});
37213721
}
@@ -3740,7 +3740,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37403740
htlc_id: Readable::read(reader)?,
37413741
err_packet: Readable::read(reader)?,
37423742
},
3743-
_ => return Err(DecodeError::InvalidValue),
3743+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37443744
});
37453745
}
37463746

@@ -3751,7 +3751,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37513751
0 => None,
37523752
1 => Some(RAACommitmentOrder::CommitmentFirst),
37533753
2 => Some(RAACommitmentOrder::RevokeAndACKFirst),
3754-
_ => return Err(DecodeError::InvalidValue),
3754+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37553755
};
37563756

37573757
let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
@@ -3779,14 +3779,14 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37793779
for _ in 0..last_local_commitment_txn_count {
37803780
last_local_commitment_txn.push(match Transaction::consensus_decode(reader.by_ref()) {
37813781
Ok(tx) => tx,
3782-
Err(_) => return Err(DecodeError::InvalidValue),
3782+
Err(_) => return Err(DecodeError::InvalidValue { byte: None }),
37833783
});
37843784
}
37853785

37863786
let last_sent_closing_fee = match <u8 as Readable<R>>::read(reader)? {
37873787
0 => None,
37883788
1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
3789-
_ => return Err(DecodeError::InvalidValue),
3789+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37903790
};
37913791

37923792
let funding_tx_confirmed_in = read_option!();
@@ -3820,7 +3820,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
38203820
// We drop the ChannelMonitor's last block connected hash cause we don't actually bother
38213821
// doing full block connection operations on the internal CHannelMonitor copies
38223822
if monitor_last_block != last_block_connected {
3823-
return Err(DecodeError::InvalidValue);
3823+
return Err(DecodeError::InvalidValue { byte: None });
38243824
}
38253825

38263826
Ok(Channel {

src/ln/channelmanager.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,7 +2632,7 @@ impl<R: ::std::io::Read> Readable<R> for PendingForwardHTLCInfo {
26322632
let onion_packet = match <u8 as Readable<R>>::read(reader)? {
26332633
0 => None,
26342634
1 => Some(msgs::OnionPacket::read(reader)?),
2635-
_ => return Err(DecodeError::InvalidValue),
2635+
_ => return Err(DecodeError::InvalidValue { byte: None }),
26362636
};
26372637
Ok(PendingForwardHTLCInfo {
26382638
onion_packet,
@@ -2666,7 +2666,7 @@ impl<R: ::std::io::Read> Readable<R> for HTLCFailureMsg {
26662666
match <u8 as Readable<R>>::read(reader)? {
26672667
0 => Ok(HTLCFailureMsg::Relay(Readable::read(reader)?)),
26682668
1 => Ok(HTLCFailureMsg::Malformed(Readable::read(reader)?)),
2669-
_ => Err(DecodeError::InvalidValue),
2669+
_ => Err(DecodeError::InvalidValue { byte: None }),
26702670
}
26712671
}
26722672
}
@@ -2692,7 +2692,7 @@ impl<R: ::std::io::Read> Readable<R> for PendingHTLCStatus {
26922692
match <u8 as Readable<R>>::read(reader)? {
26932693
0 => Ok(PendingHTLCStatus::Forward(Readable::read(reader)?)),
26942694
1 => Ok(PendingHTLCStatus::Fail(Readable::read(reader)?)),
2695-
_ => Err(DecodeError::InvalidValue),
2695+
_ => Err(DecodeError::InvalidValue { byte: None }),
26962696
}
26972697
}
26982698
}
@@ -2730,7 +2730,7 @@ impl<R: ::std::io::Read> Readable<R> for HTLCSource {
27302730
session_priv: Readable::read(reader)?,
27312731
first_hop_htlc_msat: Readable::read(reader)?,
27322732
}),
2733-
_ => Err(DecodeError::InvalidValue),
2733+
_ => Err(DecodeError::InvalidValue { byte: None }),
27342734
}
27352735
}
27362736
}
@@ -2760,7 +2760,7 @@ impl<R: ::std::io::Read> Readable<R> for HTLCFailReason {
27602760
failure_code: Readable::read(reader)?,
27612761
data: Readable::read(reader)?,
27622762
}),
2763-
_ => Err(DecodeError::InvalidValue),
2763+
_ => Err(DecodeError::InvalidValue { byte: None }),
27642764
}
27652765
}
27662766
}
@@ -2796,7 +2796,7 @@ impl<R: ::std::io::Read> Readable<R> for HTLCForwardInfo {
27962796
htlc_id: Readable::read(reader)?,
27972797
err_packet: Readable::read(reader)?,
27982798
}),
2799-
_ => Err(DecodeError::InvalidValue),
2799+
_ => Err(DecodeError::InvalidValue { byte: None }),
28002800
}
28012801
}
28022802
}
@@ -2928,10 +2928,10 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
29282928
for _ in 0..channel_count {
29292929
let mut channel: Channel = ReadableArgs::read(reader, args.logger.clone())?;
29302930
if channel.last_block_connected != last_block_hash {
2931-
return Err(DecodeError::InvalidValue);
2931+
return Err(DecodeError::InvalidValue { byte: None });
29322932
}
29332933

2934-
let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue)?;
2934+
let funding_txo = channel.channel_monitor().get_funding_txo().ok_or(DecodeError::InvalidValue { byte: None })?;
29352935
funding_txo_set.insert(funding_txo.clone());
29362936
if let Some(monitor) = args.channel_monitors.get(&funding_txo) {
29372937
if channel.get_cur_local_commitment_transaction_number() != monitor.get_cur_local_commitment_number() ||
@@ -2947,7 +2947,7 @@ impl<'a, R : ::std::io::Read> ReadableArgs<R, ChannelManagerReadArgs<'a>> for (S
29472947
by_id.insert(channel.channel_id(), channel);
29482948
}
29492949
} else {
2950-
return Err(DecodeError::InvalidValue);
2950+
return Err(DecodeError::InvalidValue { byte: None });
29512951
}
29522952
}
29532953

src/ln/channelmonitor.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,15 +1956,15 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19561956
($key: expr) => {
19571957
match $key {
19581958
Ok(res) => res,
1959-
Err(_) => return Err(DecodeError::InvalidValue),
1959+
Err(_) => return Err(DecodeError::InvalidValue { byte: None }),
19601960
}
19611961
}
19621962
}
19631963
macro_rules! read_option { () => {
19641964
match <u8 as Readable<R>>::read(reader)? {
19651965
0 => None,
19661966
1 => Some(Readable::read(reader)?),
1967-
_ => return Err(DecodeError::InvalidValue),
1967+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19681968
}
19691969
} }
19701970

@@ -1986,12 +1986,12 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19861986
let prev_latest_per_commitment_point = match <u8 as Readable<R>>::read(reader)? {
19871987
0 => None,
19881988
1 => Some(Readable::read(reader)?),
1989-
_ => return Err(DecodeError::InvalidValue),
1989+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19901990
};
19911991
let latest_per_commitment_point = match <u8 as Readable<R>>::read(reader)? {
19921992
0 => None,
19931993
1 => Some(Readable::read(reader)?),
1994-
_ => return Err(DecodeError::InvalidValue),
1994+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19951995
};
19961996
// Technically this can fail and serialize fail a round-trip, but only for serialization of
19971997
// barely-init'd ChannelMonitors that we can't do anything with.
@@ -2015,7 +2015,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20152015
prev_remote_commitment_txid,
20162016
}
20172017
},
2018-
_ => return Err(DecodeError::InvalidValue),
2018+
_ => return Err(DecodeError::InvalidValue { byte: None }),
20192019
};
20202020

20212021
let their_htlc_base_key = Some(Readable::read(reader)?);
@@ -2071,7 +2071,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20712071
htlcs.push((read_htlc_in_commitment!(), read_option!().map(|o: HTLCSource| Box::new(o))));
20722072
}
20732073
if let Some(_) = remote_claimable_outpoints.insert(txid, htlcs) {
2074-
return Err(DecodeError::InvalidValue);
2074+
return Err(DecodeError::InvalidValue { byte: None });
20752075
}
20762076
}
20772077

@@ -2086,7 +2086,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20862086
outputs.push(Readable::read(reader)?);
20872087
}
20882088
if let Some(_) = remote_commitment_txn_on_chain.insert(txid, (commitment_number, outputs)) {
2089-
return Err(DecodeError::InvalidValue);
2089+
return Err(DecodeError::InvalidValue { byte: None });
20902090
}
20912091
}
20922092

@@ -2096,7 +2096,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20962096
let payment_hash: PaymentHash = Readable::read(reader)?;
20972097
let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
20982098
if let Some(_) = remote_hash_commitment_number.insert(payment_hash, commitment_number) {
2099-
return Err(DecodeError::InvalidValue);
2099+
return Err(DecodeError::InvalidValue { byte: None });
21002100
}
21012101
}
21022102

@@ -2107,13 +2107,13 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21072107
Ok(tx) => tx,
21082108
Err(e) => match e {
21092109
encode::Error::Io(ioe) => return Err(DecodeError::Io(ioe)),
2110-
_ => return Err(DecodeError::InvalidValue),
2110+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21112111
},
21122112
};
21132113

21142114
if tx.input.is_empty() {
21152115
// Ensure tx didn't hit the 0-input ambiguity case.
2116-
return Err(DecodeError::InvalidValue);
2116+
return Err(DecodeError::InvalidValue { byte: None });
21172117
}
21182118

21192119
let revocation_key = Readable::read(reader)?;
@@ -2129,7 +2129,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21292129
let sigs = match <u8 as Readable<R>>::read(reader)? {
21302130
0 => None,
21312131
1 => Some((Readable::read(reader)?, Readable::read(reader)?)),
2132-
_ => return Err(DecodeError::InvalidValue),
2132+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21332133
};
21342134
htlcs.push((htlc, sigs, read_option!()));
21352135
}
@@ -2148,15 +2148,15 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21482148
1 => {
21492149
Some(read_local_tx!())
21502150
},
2151-
_ => return Err(DecodeError::InvalidValue),
2151+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21522152
};
21532153

21542154
let current_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
21552155
0 => None,
21562156
1 => {
21572157
Some(read_local_tx!())
21582158
},
2159-
_ => return Err(DecodeError::InvalidValue),
2159+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21602160
};
21612161

21622162
let current_remote_commitment_number = <U48 as Readable<R>>::read(reader)?.0;
@@ -2167,7 +2167,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21672167
let preimage: PaymentPreimage = Readable::read(reader)?;
21682168
let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
21692169
if let Some(_) = payment_preimages.insert(hash, preimage) {
2170-
return Err(DecodeError::InvalidValue);
2170+
return Err(DecodeError::InvalidValue { byte: None });
21712171
}
21722172
}
21732173

0 commit comments

Comments
 (0)