mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
style(p2p): get rid of expects (#164)
* style(p2p): get rid of expects * test: add test
This commit is contained in:
@ -687,23 +687,26 @@ impl Encodable for DisconnectReason {
|
||||
|
||||
impl Decodable for DisconnectReason {
|
||||
fn decode(buf: &mut &[u8]) -> Result<Self, DecodeError> {
|
||||
let first = *buf.first().expect("disconnect reason should have at least 1 byte");
|
||||
buf.advance(1);
|
||||
if buf.len() < 3 {
|
||||
return Err(DecodeError::Custom("disconnect reason should have 3 bytes"))
|
||||
}
|
||||
|
||||
let first = buf[0];
|
||||
if first != 0x01 {
|
||||
return Err(DecodeError::Custom("invalid disconnect reason - invalid snappy header"))
|
||||
}
|
||||
|
||||
let second = *buf.first().expect("disconnect reason should have at least 2 bytes");
|
||||
buf.advance(1);
|
||||
let second = buf[1];
|
||||
if second != 0x00 {
|
||||
// TODO: make sure this error message is correct
|
||||
return Err(DecodeError::Custom("invalid disconnect reason - invalid snappy header"))
|
||||
}
|
||||
|
||||
let reason = *buf.first().expect("disconnect reason should have 3 bytes");
|
||||
buf.advance(1);
|
||||
DisconnectReason::try_from(reason)
|
||||
.map_err(|_| DecodeError::Custom("unknown disconnect reason"))
|
||||
let reason = buf[2];
|
||||
let reason = DisconnectReason::try_from(reason)
|
||||
.map_err(|_| DecodeError::Custom("unknown disconnect reason"))?;
|
||||
buf.advance(3);
|
||||
Ok(reason)
|
||||
}
|
||||
}
|
||||
|
||||
@ -952,6 +955,11 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reason_too_short() {
|
||||
assert!(DisconnectReason::decode(&mut &[0u8][..]).is_err())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disconnect_encoding_length() {
|
||||
let all_reasons = vec![
|
||||
|
||||
Reference in New Issue
Block a user