node: Move over to binary protocol

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier
2022-09-13 10:16:05 +02:00
parent b3d3077ccc
commit 9d3030a093
8 changed files with 386 additions and 74 deletions
+6 -1
View File
@@ -3,6 +3,7 @@ use std::net;
use git_url::Url;
use crate::collections::HashSet;
use crate::git;
use crate::identity::{Id, PublicKey};
use crate::protocol::message::{Address, Envelope, Message};
@@ -88,7 +89,11 @@ impl Default for Config {
remote_tracking: RemoteTracking::default(),
relay: true,
listen: vec![],
git_url: Url::default(),
git_url: Url {
scheme: git::url::Scheme::File,
path: "/dev/null".to_owned().into(),
..Url::default()
},
}
}
}
+83 -4
View File
@@ -12,7 +12,7 @@ use crate::storage;
use crate::storage::refs::SignedRefs;
/// Message envelope. All messages sent over the network are wrapped in this type.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Envelope {
/// Network magic constant. Used to differentiate networks.
pub magic: u32,
@@ -47,6 +47,26 @@ pub enum Address {
},
}
impl wire::Encode for Envelope {
fn encode<W: std::io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
let mut n = 0;
n += self.magic.encode(writer)?;
n += self.msg.encode(writer)?;
Ok(n)
}
}
impl wire::Decode for Envelope {
fn decode<R: std::io::Read + ?Sized>(reader: &mut R) -> Result<Self, wire::Error> {
let magic = u32::decode(reader)?;
let msg = Message::decode(reader)?;
Ok(Self { magic, msg })
}
}
impl wire::Encode for Address {
fn encode<W: std::io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, std::io::Error> {
let mut n = 0;
@@ -84,6 +104,13 @@ impl wire::Decode for Address {
Ok(Self::Ip { ip, port })
}
2 => {
let octets: [u8; 16] = wire::Decode::decode(reader)?;
let ip = net::IpAddr::from(net::Ipv6Addr::from(octets));
let port = u16::decode(reader)?;
Ok(Self::Ip { ip, port })
}
_ => {
todo!();
}
@@ -108,6 +135,7 @@ pub struct NodeAnnouncement {
impl NodeAnnouncement {
/// Verify a signature on this message.
pub fn verify(&self, signature: &crypto::Signature) -> bool {
// TODO: Use binary serialization.
let msg = serde_json::to_vec(self).unwrap();
self.id.verify(signature, &msg).is_ok()
}
@@ -115,7 +143,7 @@ impl NodeAnnouncement {
/// Message payload.
/// These are the messages peers send to each other.
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum Message {
/// Say hello to a peer. This is the first message sent to a peer after connection.
Hello {
@@ -295,9 +323,60 @@ impl wire::Decode for Message {
Ok(Self::RefsUpdate { id, signer, refs })
}
_ => {
todo!();
n => {
todo!("Mesage type {} is not yet implemented", n);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck_macros::quickcheck;
use crate::decoder::Decoder;
use crate::protocol::wire::{self, Encode};
#[quickcheck]
fn prop_message_encode_decode(message: Message) {
assert_eq!(
wire::deserialize::<Message>(&wire::serialize(&message)).unwrap(),
message
);
}
#[quickcheck]
fn prop_envelope_encode_decode(envelope: Envelope) {
assert_eq!(
wire::deserialize::<Envelope>(&wire::serialize(&envelope)).unwrap(),
envelope
);
}
#[test]
fn prop_envelope_decoder() {
fn property(items: Vec<Envelope>) {
let mut decoder = Decoder::<Envelope>::new(8);
for item in &items {
item.encode(&mut decoder).unwrap();
}
for item in items {
assert_eq!(decoder.next().unwrap().unwrap(), item);
}
}
quickcheck::QuickCheck::new()
.gen(quickcheck::Gen::new(16))
.quickcheck(property as fn(items: Vec<Envelope>));
}
#[quickcheck]
fn prop_addr(addr: Address) {
assert_eq!(
wire::deserialize::<Address>(&wire::serialize(&addr)).unwrap(),
addr
);
}
}
+156 -17
View File
@@ -23,6 +23,17 @@ pub enum Error {
InvalidSize { expected: usize, actual: usize },
#[error(transparent)]
InvalidRefName(#[from] fmt::Error),
#[error("invalid git url `{url}`: {error}")]
InvalidGitUrl {
url: String,
error: git::url::parse::Error,
},
}
impl Error {
pub fn is_eof(&self) -> bool {
matches!(self, Self::Io(err) if err.kind() == io::ErrorKind::UnexpectedEof)
}
}
pub trait Encode {
@@ -45,6 +56,13 @@ pub fn serialize<T: Encode + ?Sized>(data: &T) -> Vec<u8> {
buffer
}
/// Decode an object from a vector.
pub fn deserialize<T: Decode>(data: &[u8]) -> Result<T, Error> {
let mut cursor = io::Cursor::new(data);
T::decode(&mut cursor)
}
impl Encode for u8 {
fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
writer.write_u8(*self)?;
@@ -79,13 +97,14 @@ impl Encode for u64 {
impl Encode for usize {
fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
let u = (*self)
.try_into()
.map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?;
assert!(
*self <= u32::MAX as usize,
"Cannot encode sizes larger than {}",
u32::MAX
);
writer.write_u32::<NetworkEndian>(*self as u32)?;
writer.write_u32::<NetworkEndian>(u)?;
Ok(mem::size_of_val(&u))
Ok(mem::size_of::<u32>())
}
}
@@ -134,14 +153,18 @@ impl Encode for net::IpAddr {
}
}
impl Encode for str {
impl Encode for &str {
fn encode<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<usize, io::Error> {
let mut n = 0;
assert!(self.len() <= u8::MAX as usize);
n += self.len().encode(writer)?;
n += self.as_bytes().encode(writer)?;
let n = (self.len() as u8).encode(writer)?;
let bytes = self.as_bytes();
Ok(n)
// Nb. Don't use the [`Encode`] instance here for &[u8], because we are prefixing the
// length ourselves.
writer.write_all(bytes)?;
Ok(n + bytes.len())
}
}
@@ -276,7 +299,7 @@ impl Decode for u64 {
impl Decode for usize {
fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
let size: usize = u64::decode(reader)?
let size: usize = u32::decode(reader)?
.try_into()
.map_err(|_| io::Error::from(io::ErrorKind::InvalidInput))?;
@@ -311,8 +334,8 @@ where
impl Decode for String {
fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
let len = usize::decode(reader)?;
let mut bytes = vec![0; len];
let len = u8::decode(reader)?;
let mut bytes = vec![0; len as usize];
reader.read_exact(&mut bytes)?;
@@ -324,10 +347,11 @@ impl Decode for String {
impl Decode for git::Url {
fn decode<R: io::Read + ?Sized>(reader: &mut R) -> Result<Self, Error> {
let string = String::decode(reader)?;
let url = String::decode(reader)?;
let url = Self::from_bytes(url.as_bytes())
.map_err(|error| Error::InvalidGitUrl { url, error })?;
Self::from_bytes(string.as_bytes())
.map_err(|e| Error::Io(io::Error::new(io::ErrorKind::InvalidInput, e.to_string())))
Ok(url)
}
}
@@ -346,3 +370,118 @@ impl Decode for Digest {
Ok(Self::from(bytes))
}
}
#[cfg(test)]
mod tests {
use super::*;
use quickcheck_macros::quickcheck;
use crate::crypto::Unverified;
use crate::storage::refs::SignedRefs;
use crate::test::arbitrary;
#[quickcheck]
fn prop_u8(input: u8) {
assert_eq!(deserialize::<u8>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_u16(input: u16) {
assert_eq!(deserialize::<u16>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_u32(input: u32) {
assert_eq!(deserialize::<u32>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_u64(input: u64) {
assert_eq!(deserialize::<u64>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_usize(input: usize) -> quickcheck::TestResult {
if input > u32::MAX as usize {
return quickcheck::TestResult::discard();
}
assert_eq!(deserialize::<usize>(&serialize(&input)).unwrap(), input);
quickcheck::TestResult::passed()
}
#[quickcheck]
fn prop_string(input: String) -> quickcheck::TestResult {
if input.len() > u8::MAX as usize {
return quickcheck::TestResult::discard();
}
assert_eq!(deserialize::<String>(&serialize(&input)).unwrap(), input);
quickcheck::TestResult::passed()
}
#[quickcheck]
fn prop_pubkey(input: PublicKey) {
assert_eq!(deserialize::<PublicKey>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_id(input: Id) {
assert_eq!(deserialize::<Id>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_digest(input: Digest) {
assert_eq!(deserialize::<Digest>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_refs(input: Refs) {
assert_eq!(deserialize::<Refs>(&serialize(&input)).unwrap(), input);
}
#[quickcheck]
fn prop_signature(input: arbitrary::ByteArray<64>) {
let signature = Signature::from(input.into_inner());
assert_eq!(
deserialize::<Signature>(&serialize(&signature)).unwrap(),
signature
);
}
#[quickcheck]
fn prop_oid(input: arbitrary::ByteArray<20>) {
let oid = git::Oid::try_from(input.into_inner().as_slice()).unwrap();
assert_eq!(deserialize::<git::Oid>(&serialize(&oid)).unwrap(), oid);
}
#[quickcheck]
fn prop_signed_refs(input: SignedRefs<Unverified>) {
assert_eq!(
deserialize::<SignedRefs<Unverified>>(&serialize(&input)).unwrap(),
input
);
}
#[test]
fn test_string() {
assert_eq!(
serialize(&String::from("hello")),
vec![5, b'h', b'e', b'l', b'l', b'o']
);
}
#[test]
fn test_git_url() {
let url = git::Url {
scheme: git::url::Scheme::Https,
path: "/git".to_owned().into(),
host: Some("seed.radicle.xyz".to_owned()),
port: Some(8888),
..git::Url::default()
};
assert_eq!(deserialize::<git::Url>(&serialize(&url)).unwrap(), url);
}
}