|
|
@@ -17,37 +17,6 @@ use crate::serial::{serialize, Decodable, Encodable, VarInt};
|
|
|
|
|
|
const MAGIC_BYTES: [u8; 4] = [0xd9, 0xef, 0xb6, 0x7d];
|
|
|
|
|
|
-pub type Ciphertext = Vec<u8>;
|
|
|
-pub type CiphertextHash = [u8; 32];
|
|
|
-
|
|
|
-// Packets and Message because Rust doesn't allow value
|
|
|
-// aliasing from ADL type enums (which Message uses).
|
|
|
-#[derive(IntoPrimitive, TryFromPrimitive, Copy, Clone, PartialEq, Eq, Hash, Debug)]
|
|
|
-#[repr(u8)]
|
|
|
-pub enum PacketType {
|
|
|
- Ping = 1,
|
|
|
- Pong = 2,
|
|
|
- GetAddrs = 3,
|
|
|
- Addrs = 4,
|
|
|
- Inv = 5,
|
|
|
- GetSlabs = 6,
|
|
|
- Slab = 7,
|
|
|
- Version = 8,
|
|
|
- Verack = 9,
|
|
|
-}
|
|
|
-
|
|
|
-pub enum Message {
|
|
|
- Ping(PingMessage),
|
|
|
- Pong(PongMessage),
|
|
|
- GetAddrs(GetAddrsMessage),
|
|
|
- Addrs(AddrsMessage),
|
|
|
- Inv(InvMessage),
|
|
|
- GetSlabs(GetSlabsMessage),
|
|
|
- Slab(SlabMessage),
|
|
|
- Version(VersionMessage),
|
|
|
- Verack(VerackMessage),
|
|
|
-}
|
|
|
-
|
|
|
pub struct PingMessage {
|
|
|
pub nonce: u32,
|
|
|
}
|
|
|
@@ -58,20 +27,6 @@ pub struct PongMessage {
|
|
|
|
|
|
pub struct GetAddrsMessage {}
|
|
|
|
|
|
-pub struct GetSlabsMessage {
|
|
|
- pub slabs_hash: Vec<[u8; 32]>,
|
|
|
-}
|
|
|
-
|
|
|
-#[derive(Clone)]
|
|
|
-pub struct SlabMessage {
|
|
|
- pub nonce: [u8; 12],
|
|
|
- pub ciphertext: Ciphertext,
|
|
|
-}
|
|
|
-
|
|
|
-pub struct InvMessage {
|
|
|
- pub slabs_hash: Vec<[u8; 32]>,
|
|
|
-}
|
|
|
-
|
|
|
pub struct AddrsMessage {
|
|
|
pub addrs: Vec<SocketAddr>,
|
|
|
}
|
|
|
@@ -112,56 +67,6 @@ impl Decodable for PongMessage {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Encodable for GetSlabsMessage {
|
|
|
- fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
|
|
|
- let mut len = 0;
|
|
|
- len += self.slabs_hash.encode(&mut s)?;
|
|
|
- Ok(len)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-impl Decodable for GetSlabsMessage {
|
|
|
- fn decode<D: io::Read>(mut d: D) -> Result<Self> {
|
|
|
- Ok(Self {
|
|
|
- slabs_hash: Decodable::decode(&mut d)?,
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-impl Encodable for SlabMessage {
|
|
|
- fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
|
|
|
- let mut len = 0;
|
|
|
- len += self.nonce.encode(&mut s)?;
|
|
|
- len += self.ciphertext.encode(&mut s)?;
|
|
|
- Ok(len)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-impl Decodable for SlabMessage {
|
|
|
- fn decode<D: io::Read>(mut d: D) -> Result<Self> {
|
|
|
- Ok(Self {
|
|
|
- nonce: Decodable::decode(&mut d)?,
|
|
|
- ciphertext: Decodable::decode(&mut d)?,
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-impl Encodable for InvMessage {
|
|
|
- fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
|
|
|
- let mut len = 0;
|
|
|
- len += self.slabs_hash.encode(&mut s)?;
|
|
|
- Ok(len)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-impl Decodable for InvMessage {
|
|
|
- fn decode<D: io::Read>(mut d: D) -> Result<Self> {
|
|
|
- Ok(Self {
|
|
|
- slabs_hash: Decodable::decode(&mut d)?,
|
|
|
- })
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
impl Encodable for GetAddrsMessage {
|
|
|
fn encode<S: io::Write>(&self, mut _s: S) -> Result<usize> {
|
|
|
let len = 0;
|
|
|
@@ -215,137 +120,10 @@ impl Decodable for VerackMessage {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Message {
|
|
|
- pub fn packet_type(&self) -> PacketType {
|
|
|
- match self {
|
|
|
- Message::Ping(_message) => PacketType::Ping,
|
|
|
- Message::Pong(_message) => PacketType::Pong,
|
|
|
- Message::GetAddrs(_message) => PacketType::GetAddrs,
|
|
|
- Message::Addrs(_message) => PacketType::Addrs,
|
|
|
- Message::Inv(_message) => PacketType::Inv,
|
|
|
- Message::GetSlabs(_message) => PacketType::GetSlabs,
|
|
|
- Message::Slab(_message) => PacketType::Slab,
|
|
|
- Message::Version(_message) => PacketType::Version,
|
|
|
- Message::Verack(_message) => PacketType::Verack,
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub fn pack(&self) -> Result<Packet> {
|
|
|
- match self {
|
|
|
- Message::Ping(message) => {
|
|
|
- let mut payload = Vec::new();
|
|
|
- message.encode(&mut payload)?;
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Ping,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Pong(message) => {
|
|
|
- let mut payload = Vec::new();
|
|
|
- message.encode(&mut payload)?;
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Pong,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::GetAddrs(message) => {
|
|
|
- let mut payload = Vec::new();
|
|
|
- message.encode(&mut payload)?;
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::GetAddrs,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Addrs(message) => {
|
|
|
- let mut payload = Vec::new();
|
|
|
- message.encode(Cursor::new(&mut payload))?;
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Addrs,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Inv(message) => {
|
|
|
- let payload = serialize(message);
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Inv,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::GetSlabs(message) => {
|
|
|
- let payload = serialize(message);
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::GetSlabs,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Slab(message) => {
|
|
|
- let payload = serialize(message);
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Slab,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Version(message) => {
|
|
|
- let payload = serialize(message);
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Version,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- Message::Verack(message) => {
|
|
|
- let payload = serialize(message);
|
|
|
- Ok(Packet {
|
|
|
- command: PacketType::Verack,
|
|
|
- command2: String::from(self.name()),
|
|
|
- payload,
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub fn unpack(packet: Packet) -> Result<Self> {
|
|
|
- let cursor = Cursor::new(packet.payload.clone());
|
|
|
- match packet.command {
|
|
|
- PacketType::Ping => Ok(Self::Ping(PingMessage::decode(cursor)?)),
|
|
|
- PacketType::Pong => Ok(Self::Pong(PongMessage::decode(cursor)?)),
|
|
|
- PacketType::GetAddrs => Ok(Self::GetAddrs(GetAddrsMessage::decode(cursor)?)),
|
|
|
- PacketType::Addrs => Ok(Self::Addrs(AddrsMessage::decode(cursor)?)),
|
|
|
- PacketType::Inv => Ok(Self::Inv(InvMessage::decode(cursor)?)),
|
|
|
- PacketType::GetSlabs => Ok(Self::GetSlabs(GetSlabsMessage::decode(cursor)?)),
|
|
|
- PacketType::Slab => Ok(Self::Slab(SlabMessage::decode(cursor)?)),
|
|
|
- PacketType::Version => Ok(Self::Version(VersionMessage::decode(cursor)?)),
|
|
|
- PacketType::Verack => Ok(Self::Verack(VerackMessage::decode(cursor)?)),
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub fn name(&self) -> &'static str {
|
|
|
- match self {
|
|
|
- Message::Ping(_) => "ping",
|
|
|
- Message::Pong(_) => "pong",
|
|
|
- Message::GetAddrs(_) => "getaddr",
|
|
|
- Message::Addrs(_) => "addr",
|
|
|
- Message::Inv(_) => "inv",
|
|
|
- Message::GetSlabs(_) => "GetSlabs",
|
|
|
- Message::Slab(_) => "Slab",
|
|
|
- Message::Version(_) => "version",
|
|
|
- Message::Verack(_) => "verack",
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
// Packets are the base type read from the network
|
|
|
// These are converted to messages and passed to event loop
|
|
|
pub struct Packet {
|
|
|
- pub command: PacketType,
|
|
|
- pub command2: String,
|
|
|
+ pub command: String,
|
|
|
pub payload: Vec<u8>,
|
|
|
}
|
|
|
|
|
|
@@ -361,9 +139,6 @@ pub async fn read_packet<R: AsyncRead + Unpin>(stream: &mut R) -> Result<Packet>
|
|
|
}
|
|
|
|
|
|
// The type of the message
|
|
|
- //let command = AsyncReadExt::read_u8(stream).await?;
|
|
|
- //debug!(target: "net", "read command: {}", command);
|
|
|
- //let command = PacketType::try_from(command).map_err(|_| Error::MalformedPacket)?;
|
|
|
let command_len = VarInt::decode_async(stream).await?.0 as usize;
|
|
|
let mut command = vec![0u8; command_len];
|
|
|
if command_len > 0 {
|
|
|
@@ -381,7 +156,7 @@ pub async fn read_packet<R: AsyncRead + Unpin>(stream: &mut R) -> Result<Packet>
|
|
|
}
|
|
|
debug!(target: "net", "read payload {} bytes", payload_len);
|
|
|
|
|
|
- Ok(Packet { command: PacketType::Verack, command2: command, payload })
|
|
|
+ Ok(Packet { command: command, payload })
|
|
|
}
|
|
|
|
|
|
pub async fn send_packet<W: AsyncWrite + Unpin>(stream: &mut W, packet: Packet) -> Result<()> {
|
|
|
@@ -389,15 +164,12 @@ pub async fn send_packet<W: AsyncWrite + Unpin>(stream: &mut W, packet: Packet)
|
|
|
stream.write_all(&MAGIC_BYTES).await?;
|
|
|
debug!(target: "net", "sent magic...");
|
|
|
|
|
|
- //AsyncWriteExt::write_u8(stream, packet.command as u8).await?;
|
|
|
- //debug!(target: "net", "sent command: {}", packet.command as u8);
|
|
|
-
|
|
|
- VarInt(packet.command2.len() as u64)
|
|
|
+ VarInt(packet.command.len() as u64)
|
|
|
.encode_async(stream)
|
|
|
.await?;
|
|
|
- assert!(!packet.command2.is_empty());
|
|
|
- stream.write_all(&packet.command2.as_bytes()).await?;
|
|
|
- debug!(target: "net", "sent command: {}", packet.command2);
|
|
|
+ assert!(!packet.command.is_empty());
|
|
|
+ stream.write_all(&packet.command.as_bytes()).await?;
|
|
|
+ debug!(target: "net", "sent command: {}", packet.command);
|
|
|
|
|
|
assert_eq!(std::mem::size_of::<usize>(), std::mem::size_of::<u64>());
|
|
|
VarInt(packet.payload.len() as u64)
|
|
|
@@ -412,81 +184,3 @@ pub async fn send_packet<W: AsyncWrite + Unpin>(stream: &mut W, packet: Packet)
|
|
|
Ok(())
|
|
|
}
|
|
|
|
|
|
-pub async fn receive_message<R: AsyncRead + Unpin>(stream: &mut R) -> Result<Message> {
|
|
|
- let packet = read_packet(stream).await?;
|
|
|
- debug!(target: "net", "unpacking packet: {:?}", packet.command);
|
|
|
- let message = Message::unpack(packet)?;
|
|
|
- debug!(target: "net", "received Message::{}", message.name());
|
|
|
- Ok(message)
|
|
|
-}
|
|
|
-
|
|
|
-pub async fn send_message<W: AsyncWrite + Unpin>(stream: &mut W, message: Message) -> Result<()> {
|
|
|
- debug!(target: "net", "sending Message::{}", message.name());
|
|
|
- let packet = message.pack()?;
|
|
|
- send_packet(stream, packet).await
|
|
|
-}
|
|
|
-
|
|
|
-pub async fn sleep(seconds: u64) {
|
|
|
- Timer::after(Duration::from_secs(seconds)).await;
|
|
|
-}
|
|
|
-
|
|
|
-// Used for ping pong loop timer
|
|
|
-pub struct InactivityTimer {
|
|
|
- reset_sender: async_channel::Sender<()>,
|
|
|
- timeout_receiver: async_channel::Receiver<()>,
|
|
|
- task: smol::Task<()>,
|
|
|
-}
|
|
|
-
|
|
|
-impl InactivityTimer {
|
|
|
- pub fn new(executor: Arc<Executor<'_>>) -> Self {
|
|
|
- let (reset_sender, reset_receiver) = async_channel::bounded::<()>(1);
|
|
|
- let (timeout_sender, timeout_receiver) = async_channel::bounded::<()>(1);
|
|
|
-
|
|
|
- let task = executor.spawn(async {
|
|
|
- match Self::_start(reset_receiver, timeout_sender).await {
|
|
|
- Ok(()) => {}
|
|
|
- Err(err) => error!("InactivityTimer fatal error {}", err),
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- Self {
|
|
|
- reset_sender,
|
|
|
- timeout_receiver,
|
|
|
- task,
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub async fn stop(self) {
|
|
|
- self.task.cancel().await;
|
|
|
- }
|
|
|
-
|
|
|
- // This loop basically waits for 10 secs. If it doesn't
|
|
|
- // receive a signal that something happened then it will
|
|
|
- // send a timeout signal. This will wakeup the main event loop
|
|
|
- // and the connection will be dropped.
|
|
|
- async fn _start(
|
|
|
- reset_rx: async_channel::Receiver<()>,
|
|
|
- timeout_sx: async_channel::Sender<()>,
|
|
|
- ) -> Result<()> {
|
|
|
- loop {
|
|
|
- let is_awake = futures::select! {
|
|
|
- _ = reset_rx.recv().fuse() => true,
|
|
|
- _ = sleep(10).fuse() => false
|
|
|
- };
|
|
|
-
|
|
|
- if !is_awake {
|
|
|
- warn!("InactivityTimer timeout");
|
|
|
- timeout_sx.send(()).await?;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- pub async fn reset(&self) -> Result<()> {
|
|
|
- self.reset_sender.send(()).await?;
|
|
|
- Ok(())
|
|
|
- }
|
|
|
-
|
|
|
- pub async fn wait_for_wakeup(&self) -> Result<()> {
|
|
|
- Ok(self.timeout_receiver.recv().await?)
|
|
|
- }
|
|
|
-}
|