From 3545bed2d81caae8d45297e1fd25c2227a045625 Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Wed, 15 May 2024 19:17:58 -0700 Subject: [PATCH] Simplify encode --- protocol/src/serde.rs | 244 +++++++++++++++--------------------------- 1 file changed, 87 insertions(+), 157 deletions(-) diff --git a/protocol/src/serde.rs b/protocol/src/serde.rs index d5a76ca..fe9f715 100644 --- a/protocol/src/serde.rs +++ b/protocol/src/serde.rs @@ -211,15 +211,22 @@ pub fn serialize(msg: NetworkMessage) -> Result, Error> { /// Deserialize v2 message into NetworkMessage. pub fn deserialize(buffer: &[u8]) -> Result { 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), @@ -240,165 +247,88 @@ pub fn deserialize(buffer: &[u8]) -> Result { } } // 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), } }