Skip to content

Commit

Permalink
Merge pull request AleoNet#4 from niklaslong/circ
Browse files Browse the repository at this point in the history
Implement `Writing` on `Router`
  • Loading branch information
niklaslong authored Mar 14, 2023
2 parents e04d825 + aed60cd commit 45bfa1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
9 changes: 1 addition & 8 deletions node/router/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use snarkos_node_messages::{
MessageCodec,
MessageTrait,
};
use snarkos_node_tcp::{ConnectionSide, Tcp, P2P};
use snarkos_node_tcp::ConnectionSide;
use snarkvm::prelude::{error, Address, Header, Network};

use anyhow::{bail, Result};
Expand All @@ -36,13 +36,6 @@ use tokio::net::TcpStream;
use tokio_stream::StreamExt;
use tokio_util::codec::Framed;

impl<N: Network> P2P for Router<N> {
/// Returns a reference to the TCP instance.
fn tcp(&self) -> &Tcp {
&self.tcp
}
}

impl<N: Network> Router<N> {
/// Performs the handshake protocol.
pub async fn handshake<'a>(
Expand Down
26 changes: 24 additions & 2 deletions node/router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ mod routing;
pub use routing::*;

use snarkos_account::Account;
use snarkos_node_messages::NodeType;
use snarkos_node_tcp::{Config, Tcp};
use snarkos_node_messages::{Message, MessageCodec, NodeType};
use snarkos_node_tcp::{protocols::Writing, Config, ConnectionSide, Tcp, P2P};
use snarkvm::prelude::{Address, Network, PrivateKey, ViewKey};

use anyhow::{bail, Result};
Expand Down Expand Up @@ -94,6 +94,28 @@ pub struct InnerRouter<N: Network> {
is_dev: bool,
}

// Implement some of the Tcp traits at this level to allow propagating messages through the router
// in the BFT. Note: these traits are also implemented at the node level.
impl<N: Network> P2P for Router<N> {
/// Returns a reference to the TCP instance.
fn tcp(&self) -> &Tcp {
&self.tcp
}
}

// Only Writing is included here, since Reading and Handshake depend on node-level objects.
#[async_trait]
impl<N: Network> Writing for Router<N> {
type Codec = MessageCodec<N>;
type Message = Message<N>;

/// Creates an [`Encoder`] used to write the outbound messages to the target stream.
/// The `side` parameter indicates the connection side **from the node's perspective**.
fn codec(&self, _addr: SocketAddr, _side: ConnectionSide) -> Self::Codec {
Default::default()
}
}

impl<N: Network> Router<N> {
/// The maximum number of candidate peers permitted to be stored in the node.
const MAXIMUM_CANDIDATE_PEERS: usize = 10_000;
Expand Down

0 comments on commit 45bfa1d

Please sign in to comment.