Skip to content

Commit

Permalink
Simplify encode
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed May 16, 2024
1 parent 31e93ec commit 3545bed
Showing 1 changed file with 87 additions and 157 deletions.
244 changes: 87 additions & 157 deletions protocol/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,22 @@ pub fn serialize(msg: NetworkMessage) -> Result<alloc::vec::Vec<u8>, Error> {
/// Deserialize v2 message into NetworkMessage.
pub fn deserialize(buffer: &[u8]) -> Result<NetworkMessage, Error> {
let short_id = buffer[0];
let mut payload_buffer = &buffer[1..];
match short_id {
// Zero-byte means the command is encoded in the next 12 bytes.
0u8 => {
// Next 12 bytes have encoded command.
let mut command_buffer = &buffer[1..13];
let command = CommandString::consensus_decode(&mut command_buffer)
.map_err(|_| Error::Deserialize)?;
let mut payload_buffer = &buffer[13..];
// Rest of buffer is payload.
payload_buffer = &buffer[13..];
// There are a handful of "known" messages which don't use a short ID, otherwise Unknown.
match command.as_ref() {
"version" => Ok(NetworkMessage::Version(
Decodable::consensus_decode(&mut payload_buffer)
.map_err(|_| Error::Deserialize)?,
)),
"verack" => Ok(NetworkMessage::Verack),
"sendheaders" => Ok(NetworkMessage::SendHeaders),
"getaddr" => Ok(NetworkMessage::GetAddr),
Expand All @@ -240,165 +247,88 @@ pub fn deserialize(buffer: &[u8]) -> Result<NetworkMessage, Error> {
}
}
// The following single byte IDs map to command short IDs.
1u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Addr(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
2u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Block(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
3u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::BlockTxn(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
4u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::CmpctBlock(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
5u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::FeeFilter(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
6u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::FilterAdd(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
1u8 => Ok(NetworkMessage::Addr(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
2u8 => Ok(NetworkMessage::Block(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
3u8 => Ok(NetworkMessage::BlockTxn(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
4u8 => Ok(NetworkMessage::CmpctBlock(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
5u8 => Ok(NetworkMessage::FeeFilter(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
6u8 => Ok(NetworkMessage::FilterAdd(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
7u8 => Ok(NetworkMessage::FilterClear),
8u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::FilterLoad(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
9u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetBlocks(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
10u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetBlockTxn(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
11u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetData(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
12u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
13u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Headers(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
14u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Inv(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
8u8 => Ok(NetworkMessage::FilterLoad(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
9u8 => Ok(NetworkMessage::GetBlocks(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
10u8 => Ok(NetworkMessage::GetBlockTxn(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
11u8 => Ok(NetworkMessage::GetData(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
12u8 => Ok(NetworkMessage::GetHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
13u8 => Ok(NetworkMessage::Headers(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
14u8 => Ok(NetworkMessage::Inv(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
15u8 => Ok(NetworkMessage::MemPool),
16u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::MerkleBlock(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
17u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::NotFound(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
18u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Ping(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
19u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Pong(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
20u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::SendCmpct(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
21u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::Tx(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
22u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetCFilters(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
23u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::CFilter(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
24u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetCFHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
25u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::CFHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
26u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::GetCFCheckpt(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
27u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::CFCheckpt(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
28u8 => {
let mut payload_buffer = &buffer[1..];
Ok(NetworkMessage::AddrV2(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
))
}
16u8 => Ok(NetworkMessage::MerkleBlock(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
17u8 => Ok(NetworkMessage::NotFound(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
18u8 => Ok(NetworkMessage::Ping(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
19u8 => Ok(NetworkMessage::Pong(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
20u8 => Ok(NetworkMessage::SendCmpct(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
21u8 => Ok(NetworkMessage::Tx(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
22u8 => Ok(NetworkMessage::GetCFilters(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
23u8 => Ok(NetworkMessage::CFilter(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
24u8 => Ok(NetworkMessage::GetCFHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
25u8 => Ok(NetworkMessage::CFHeaders(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
26u8 => Ok(NetworkMessage::GetCFCheckpt(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
27u8 => Ok(NetworkMessage::CFCheckpt(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),
28u8 => Ok(NetworkMessage::AddrV2(
Decodable::consensus_decode(&mut payload_buffer).map_err(|_| Error::Deserialize)?,
)),

// Unsupported short ID.
_ => Err(Error::Deserialize),
}
}

0 comments on commit 3545bed

Please sign in to comment.