Skip to content

Commit e953453

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 a5bcd56 commit e953453

File tree

9 files changed

+457
-167
lines changed

9 files changed

+457
-167
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
@@ -3438,7 +3438,7 @@ impl<R: ::std::io::Read> Readable<R> for InboundHTLCRemovalReason {
34383438
0 => InboundHTLCRemovalReason::FailRelay(Readable::read(reader)?),
34393439
1 => InboundHTLCRemovalReason::FailMalformed((Readable::read(reader)?, Readable::read(reader)?)),
34403440
2 => InboundHTLCRemovalReason::Fulfill(Readable::read(reader)?),
3441-
_ => return Err(DecodeError::InvalidValue),
3441+
_ => return Err(DecodeError::InvalidValue { byte: None }),
34423442
})
34433443
}
34443444
}
@@ -3684,7 +3684,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
36843684
2 => InboundHTLCState::AwaitingAnnouncedRemoteRevoke(Readable::read(reader)?),
36853685
3 => InboundHTLCState::Committed,
36863686
4 => InboundHTLCState::LocalRemoved(Readable::read(reader)?),
3687-
_ => return Err(DecodeError::InvalidValue),
3687+
_ => return Err(DecodeError::InvalidValue { byte: None }),
36883688
},
36893689
});
36903690
}
@@ -3693,7 +3693,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
36933693
match <u8 as Readable<R>>::read(reader)? {
36943694
0 => None,
36953695
1 => Some(Readable::read(reader)?),
3696-
_ => return Err(DecodeError::InvalidValue),
3696+
_ => return Err(DecodeError::InvalidValue { byte: None }),
36973697
}
36983698
} }
36993699

@@ -3713,7 +3713,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37133713
2 => OutboundHTLCState::RemoteRemoved,
37143714
3 => OutboundHTLCState::AwaitingRemoteRevokeToRemove,
37153715
4 => OutboundHTLCState::AwaitingRemovedRemoteRevoke,
3716-
_ => return Err(DecodeError::InvalidValue),
3716+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37173717
},
37183718
});
37193719
}
@@ -3738,7 +3738,7 @@ impl<R : ::std::io::Read> ReadableArgs<R, Arc<Logger>> for Channel {
37383738
htlc_id: Readable::read(reader)?,
37393739
err_packet: Readable::read(reader)?,
37403740
},
3741-
_ => return Err(DecodeError::InvalidValue),
3741+
_ => return Err(DecodeError::InvalidValue { byte: None }),
37423742
});
37433743
}
37443744

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

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

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

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

38243824
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: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19051905
($key: expr) => {
19061906
match $key {
19071907
Ok(res) => res,
1908-
Err(_) => return Err(DecodeError::InvalidValue),
1908+
Err(_) => return Err(DecodeError::InvalidValue { byte: None }),
19091909
}
19101910
}
19111911
}
@@ -1928,12 +1928,12 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19281928
let prev_latest_per_commitment_point = match <u8 as Readable<R>>::read(reader)? {
19291929
0 => None,
19301930
1 => Some(Readable::read(reader)?),
1931-
_ => return Err(DecodeError::InvalidValue),
1931+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19321932
};
19331933
let latest_per_commitment_point = match <u8 as Readable<R>>::read(reader)? {
19341934
0 => None,
19351935
1 => Some(Readable::read(reader)?),
1936-
_ => return Err(DecodeError::InvalidValue),
1936+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19371937
};
19381938
// Technically this can fail and serialize fail a round-trip, but only for serialization of
19391939
// barely-init'd ChannelMonitors that we can't do anything with.
@@ -1945,12 +1945,12 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19451945
let current_remote_commitment_txid = match <u8 as Readable<R>>::read(reader)? {
19461946
0 => None,
19471947
1 => Some(Readable::read(reader)?),
1948-
_ => return Err(DecodeError::InvalidValue),
1948+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19491949
};
19501950
let prev_remote_commitment_txid = match <u8 as Readable<R>>::read(reader)? {
19511951
0 => None,
19521952
1 => Some(Readable::read(reader)?),
1953-
_ => return Err(DecodeError::InvalidValue),
1953+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19541954
};
19551955
Storage::Local {
19561956
revocation_base_key,
@@ -1965,7 +1965,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
19651965
prev_remote_commitment_txid,
19661966
}
19671967
},
1968-
_ => return Err(DecodeError::InvalidValue),
1968+
_ => return Err(DecodeError::InvalidValue { byte: None }),
19691969
};
19701970

19711971
let their_htlc_base_key = Some(Readable::read(reader)?);
@@ -2018,7 +2018,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20182018
match <u8 as Readable<R>>::read(reader)? {
20192019
0 => None,
20202020
1 => Some(Readable::read(reader)?),
2021-
_ => return Err(DecodeError::InvalidValue),
2021+
_ => return Err(DecodeError::InvalidValue { byte: None }),
20222022
}
20232023
)
20242024
}
@@ -2040,7 +2040,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20402040
sources.push(read_htlc_source!());
20412041
}
20422042
if let Some(_) = remote_claimable_outpoints.insert(txid, (outputs, sources)) {
2043-
return Err(DecodeError::InvalidValue);
2043+
return Err(DecodeError::InvalidValue { byte: None });
20442044
}
20452045
}
20462046

@@ -2055,7 +2055,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20552055
outputs.push(Readable::read(reader)?);
20562056
}
20572057
if let Some(_) = remote_commitment_txn_on_chain.insert(txid, (commitment_number, outputs)) {
2058-
return Err(DecodeError::InvalidValue);
2058+
return Err(DecodeError::InvalidValue { byte: None });
20592059
}
20602060
}
20612061

@@ -2065,7 +2065,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20652065
let payment_hash: PaymentHash = Readable::read(reader)?;
20662066
let commitment_number = <U48 as Readable<R>>::read(reader)?.0;
20672067
if let Some(_) = remote_hash_commitment_number.insert(payment_hash, commitment_number) {
2068-
return Err(DecodeError::InvalidValue);
2068+
return Err(DecodeError::InvalidValue { byte: None });
20692069
}
20702070
}
20712071

@@ -2076,13 +2076,13 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
20762076
Ok(tx) => tx,
20772077
Err(e) => match e {
20782078
encode::Error::Io(ioe) => return Err(DecodeError::Io(ioe)),
2079-
_ => return Err(DecodeError::InvalidValue),
2079+
_ => return Err(DecodeError::InvalidValue { byte: None }),
20802080
},
20812081
};
20822082

20832083
if tx.input.is_empty() {
20842084
// Ensure tx didn't hit the 0-input ambiguity case.
2085-
return Err(DecodeError::InvalidValue);
2085+
return Err(DecodeError::InvalidValue { byte: None });
20862086
}
20872087

20882088
let revocation_key = Readable::read(reader)?;
@@ -2118,15 +2118,15 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21182118
1 => {
21192119
Some(read_local_tx!())
21202120
},
2121-
_ => return Err(DecodeError::InvalidValue),
2121+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21222122
};
21232123

21242124
let current_local_signed_commitment_tx = match <u8 as Readable<R>>::read(reader)? {
21252125
0 => None,
21262126
1 => {
21272127
Some(read_local_tx!())
21282128
},
2129-
_ => return Err(DecodeError::InvalidValue),
2129+
_ => return Err(DecodeError::InvalidValue { byte: None }),
21302130
};
21312131

21322132
let current_remote_commitment_number = <U48 as Readable<R>>::read(reader)?.0;
@@ -2137,7 +2137,7 @@ impl<R: ::std::io::Read> ReadableArgs<R, Arc<Logger>> for (Sha256dHash, ChannelM
21372137
let preimage: PaymentPreimage = Readable::read(reader)?;
21382138
let hash = PaymentHash(Sha256::hash(&preimage.0[..]).into_inner());
21392139
if let Some(_) = payment_preimages.insert(hash, preimage) {
2140-
return Err(DecodeError::InvalidValue);
2140+
return Err(DecodeError::InvalidValue { byte: None });
21412141
}
21422142
}
21432143

0 commit comments

Comments
 (0)