From b75db6638bf0dc103d0252ec6d79e680b5132f1b Mon Sep 17 00:00:00 2001 From: Nick Johnson Date: Fri, 12 Apr 2024 11:24:58 -0700 Subject: [PATCH] Finish up read hopefully --- proxy/src/lib.rs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/proxy/src/lib.rs b/proxy/src/lib.rs index 264b0c1..50cdc77 100644 --- a/proxy/src/lib.rs +++ b/proxy/src/lib.rs @@ -3,6 +3,7 @@ //! The V1 and V2 p2p protocols have different header encodings, so a proxy has to do //! a little more work than just encrypt/decrypt. +use core::slice::SlicePattern; use std::fmt; use std::net::SocketAddr; @@ -16,7 +17,6 @@ use tokio::net::TcpStream; /// Default to local host on port 1324. pub const DEFAULT_PROXY: &str = "127.0.0.1:1324"; -/// Default to the signet network. const DEFAULT_MAGIC: Magic = Magic::BITCOIN; /// All V1 messages have a 24 byte header. const V1_HEADER_BYTES: usize = 24; @@ -25,6 +25,42 @@ const VERSION_COMMAND: [u8; 12] = [ 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x00, 0x00, ]; +/// A subset of commands are represented with a single byte +/// in V2 instead of the 12-byte ASCII encoding like V1. The +/// indexes of the commands in the list corresponds to their +/// ID in the protocol, but needs +1 since the zero indexed +/// is reserved to indicated a 12-bytes representation. +const V2_SHORTID_COMMANDS: &[&str] = &[ + "addr", + "block", + "blocktxn", + "cmpctblock", + "feefilter", + "filteradd", + "filterclear", + "filterload", + "getblocks", + "getblocktxn", + "getdata", + "getheaders", + "headers", + "inv", + "mempool", + "merkleblock", + "notfound", + "ping", + "pong", + "sendcmpct", + "tx", + "getcfilters", + "cfilter", + "getcfheaders", + "cfheaders", + "getcfcheckpt", + "cfcheckpt", + "addrv2", +]; + /// An error occured while establishing the proxy connection or during the main loop. #[derive(Debug)] pub enum Error { @@ -117,9 +153,13 @@ pub async fn read_v2( let packet_bytes = decrypter.decypt_len(length_bytes); let mut packet_bytes = vec![0u8; packet_bytes]; input.read_exact(&mut packet_bytes).await?; - let message = RawNetworkMessage::magic() - Ok(todo!()) + let type_index = if packet_bytes[0] == 0u8 { 13 } else { 1 }; + let mut payload = &packet_bytes[type_index..]; + + let message = RawNetworkMessage::consensus_decode(&mut payload).expect("raw network message"); + + Ok(message) } /// Write the network message to the output stream.