Skip to content

Commit

Permalink
Finish up read hopefully
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed Apr 12, 2024
1 parent 72a921b commit b75db66
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -117,9 +153,13 @@ pub async fn read_v2<T: AsyncRead + Unpin>(
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.
Expand Down

0 comments on commit b75db66

Please sign in to comment.