Skip to content

Commit

Permalink
Clean up calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nyonson committed May 13, 2024
1 parent 17834ce commit 4f6878f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 2 additions & 2 deletions proxy/src/bin/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), bip324_proxy::Error> {
res = read_v1(&mut client_reader) => {
match res {
Ok(msg) => {
println!("Read {} message from client, writing to remote.", msg.cmd);
println!("Read {} message from client, writing to remote.", msg.command());
write_v2(&mut remote_writer, &mut encrypter, msg).await?;
},
Err(e) => {
Expand All @@ -94,7 +94,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), bip324_proxy::Error> {
res = read_v2(&mut remote_reader, &mut decrypter) => {
match res {
Ok(msg) => {
println!("Read {} message from remote, writing to client.", msg.cmd);
println!("Read {} message from remote, writing to client.", msg.command());
write_v1(&mut client_writer, msg).await?;
},
Err(e) => {
Expand Down
4 changes: 2 additions & 2 deletions proxy/src/bin/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), bip324_proxy::Error> {
res = read_v1(&mut client_reader) => {
match res {
Ok(msg) => {
println!("Read {} message from client, writing to remote.", msg.cmd);
println!("Read {} message from client, writing to remote.", msg.command());
write_v1(&mut remote_writer, msg).await?;
},
Err(e) => {
Expand All @@ -32,7 +32,7 @@ async fn proxy_conn(mut client: TcpStream) -> Result<(), bip324_proxy::Error> {
res = read_v1(&mut remote_reader) => {
match res {
Ok(msg) => {
println!("Read {} message from remote, writing to client.", msg.cmd);
println!("Read {} message from remote, writing to client.", msg.command());
write_v1(&mut client_writer, msg).await?;
},
Err(e) => {
Expand Down
12 changes: 7 additions & 5 deletions proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ use bip324::serde::{deserialize, serialize_with_alloc};
use bip324::ReceivedMessage;
use bip324::{PacketReader, PacketWriter};
use bitcoin::consensus::{Decodable, Encodable};
use bitcoin::hashes::sha256d;
use bitcoin::hashes::Hash;
use bitcoin::p2p::message::{NetworkMessage, RawNetworkMessage};
use bitcoin::p2p::{Address, Magic};
use hex::prelude::*;
Expand All @@ -38,6 +36,7 @@ const VERSION_COMMAND: [u8; 12] = [
pub enum Error {
WrongNetwork,
WrongCommand,
Serde,
Network(std::io::Error),
Cipher(bip324::Error),
}
Expand All @@ -49,6 +48,7 @@ impl fmt::Display for Error {
Error::Network(e) => write!(f, "Network error {}", e),
Error::WrongCommand => write!(f, "Recieved message with wrong command"),
Error::Cipher(e) => write!(f, "Cipher encryption/decrytion error {}", e),
Error::Serde => write!(f, "Unable to serialize command"),
}
}
}
Expand All @@ -60,6 +60,7 @@ impl std::error::Error for Error {
Error::WrongNetwork => None,
Error::WrongCommand => None,
Error::Cipher(e) => Some(e),
Error::Serde => None,
}
}
}
Expand Down Expand Up @@ -144,7 +145,7 @@ pub async fn read_v2<T: AsyncRead + Unpin>(
.message
.expect("not a decoy");

let message = deserialize(&contents)?;
let message = deserialize(&contents).map_err(|_| Error::Serde)?;
Ok(message)
}

Expand All @@ -155,7 +156,8 @@ pub async fn write_v1<T: AsyncWrite + Unpin>(
) -> Result<(), Error> {
let raw = RawNetworkMessage::new(DEFAULT_MAGIC, msg);
let mut buffer = vec![];
raw.consensus_encode(&mut buffer);
raw.consensus_encode(&mut buffer)
.map_err(|_| Error::Serde)?;

Ok(output.write_all(&buffer).await?)
}
Expand All @@ -166,7 +168,7 @@ pub async fn write_v2<T: AsyncWrite + Unpin>(
encrypter: &mut PacketWriter,
msg: NetworkMessage,
) -> Result<(), Error> {
let payload = serialize_with_alloc(msg)?;
let payload = serialize_with_alloc(msg).map_err(|_| Error::Serde)?;
let write_bytes = encrypter
.prepare_packet_with_alloc(&payload, None, false)
.expect("encryption");
Expand Down

0 comments on commit 4f6878f

Please sign in to comment.