Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
refactor!: rename Node to Instance and NodeConfig to Config
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `Node` and `NodeConfig` are part of the public API.
  • Loading branch information
madadam authored and dirvine committed Oct 19, 2020
1 parent ed29db1 commit d8d6314
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 40 deletions.
10 changes: 5 additions & 5 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use hex_fmt::HexFmt;
use log::{info, LevelFilter};
use sn_routing::{
event::{Connected, Event},
EventStream, Node, NodeConfig, TransportConfig,
Config, EventStream, Instance, TransportConfig,
};
use std::{
collections::HashSet,
Expand Down Expand Up @@ -190,12 +190,12 @@ async fn start_node(

info!("Node #{} starting...", index);

let config = NodeConfig {
let config = Config {
first,
transport_config,
..Default::default()
};
let (node, event_stream) = Node::new(config)
let (node, event_stream) = Instance::new(config)
.await
.expect("Failed to instantiate a Node");

Expand All @@ -209,7 +209,7 @@ async fn start_node(
}

// Runs the nodes event loop. Blocks until terminated.
async fn run_node(index: usize, mut node: Node, mut event_stream: EventStream) {
async fn run_node(index: usize, mut node: Instance, mut event_stream: EventStream) {
tokio::spawn(async move {
while let Some(event) = event_stream.next().await {
if !handle_event(index, &mut node, event).await {
Expand All @@ -220,7 +220,7 @@ async fn run_node(index: usize, mut node: Node, mut event_stream: EventStream) {
}

// Handles the event emitted by the node.
async fn handle_event(index: usize, node: &mut Node, event: Event) -> bool {
async fn handle_event(index: usize, node: &mut Instance, event: Event) -> bool {
match event {
Event::Connected(Connected::First) => {
let contact_info = node
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/node/executor.rs → src/instance/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use super::Command;
use super::{Command, Stage};
use crate::{
cancellation::{cancellable, CancellationHandle, CancellationToken},
event::Event,
messages::Message,
node::stage::Stage,
};
use bytes::Bytes;
use qp2p::{IncomingConnections, IncomingMessages, Message as QuicP2pMsg};
Expand Down
14 changes: 7 additions & 7 deletions src/node/mod.rs → src/instance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ use std::{net::SocketAddr, sync::Arc};
use tokio::sync::mpsc;
use xor_name::{Prefix, XorName};

/// Node configuration.
pub struct NodeConfig {
/// Instance configuration.
pub struct Config {
/// If true, configures the node to start a new network instead of joining an existing one.
pub first: bool,
/// The `Keypair` of the node or `None` for randomly generated one.
Expand All @@ -51,7 +51,7 @@ pub struct NodeConfig {
pub network_params: NetworkParams,
}

impl Default for NodeConfig {
impl Default for Config {
fn default() -> Self {
Self {
first: false,
Expand All @@ -63,24 +63,24 @@ impl Default for NodeConfig {
}

/// Interface for sending and receiving messages to and from other nodes, in the role of a full
/// sn_routing node.
/// routing node.
///
/// A node is a part of the network that can route messages and be a member of a section or group
/// location. Its methods can be used to send requests and responses as either an individual
/// `Node` or as a part of a section or group location. Their `src` argument indicates that
/// role, and can be any [`SrcLocation`](enum.SrcLocation.html).
pub struct Node {
pub struct Instance {
stage: Arc<Stage>,
_executor: Executor,
}

impl Node {
impl Instance {
////////////////////////////////////////////////////////////////////////////
// Public API
////////////////////////////////////////////////////////////////////////////

/// Create new node using the given config.
pub async fn new(config: NodeConfig) -> Result<(Self, EventStream)> {
pub async fn new(config: Config) -> Result<(Self, EventStream)> {
let mut rng = rng::new();
let keypair = config
.keypair
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ extern crate serde;
// ############################################################################
pub use self::{
error::{Error, Result},
instance::{Config, EventStream, Instance},
location::{DstLocation, SrcLocation},
network_params::NetworkParams,
node::{EventStream, Node, NodeConfig},
section::{SectionProofChain, MIN_AGE},
};
pub use qp2p::Config as TransportConfig;
Expand All @@ -96,12 +96,12 @@ mod cancellation;
mod consensus;
mod delivery_group;
mod error;
mod instance;
mod location;
mod message_filter;
mod messages;
mod network;
mod network_params;
mod node;
mod peer;
mod relocation;
mod section;
Expand Down
14 changes: 7 additions & 7 deletions tests/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use futures::future::join_all;
use sn_routing::{
event::{Connected, Event},
rng::MainRng,
Node,
Instance,
};
use utils::*;
use xor_name::XorName;
Expand All @@ -23,7 +23,7 @@ use xor_name::XorName;
async fn test_genesis_node() -> Result<()> {
let keypair = Keypair::generate(&mut MainRng::default());
let pub_key = keypair.public;
let (node, mut event_stream) = TestNodeBuilder::new(None)
let (node, mut event_stream) = InstanceBuilder::new(None)
.first()
.keypair(keypair)
.create()
Expand All @@ -41,7 +41,7 @@ async fn test_genesis_node() -> Result<()> {

#[tokio::test]
async fn test_node_bootstrapping() -> Result<()> {
let (genesis_node, mut event_stream) = TestNodeBuilder::new(None).first().create().await?;
let (genesis_node, mut event_stream) = InstanceBuilder::new(None).first().create().await?;

// spawn genesis node events listener
let genesis_handler = tokio::spawn(async move {
Expand All @@ -55,7 +55,7 @@ async fn test_node_bootstrapping() -> Result<()> {

// bootstrap a second node with genesis
let genesis_contact = genesis_node.our_connection_info()?;
let (node1, mut event_stream) = TestNodeBuilder::new(None)
let (node1, mut event_stream) = InstanceBuilder::new(None)
.with_contact(genesis_contact)
.create()
.await?;
Expand All @@ -75,7 +75,7 @@ async fn test_node_bootstrapping() -> Result<()> {
#[tokio::test]
async fn test_section_bootstrapping() -> Result<()> {
let num_of_nodes = 7;
let (genesis_node, mut event_stream) = TestNodeBuilder::new(None)
let (genesis_node, mut event_stream) = InstanceBuilder::new(None)
.elder_size(num_of_nodes)
.first()
.create()
Expand Down Expand Up @@ -107,14 +107,14 @@ async fn test_section_bootstrapping() -> Result<()> {
let mut nodes_joining_tasks = Vec::with_capacity(num_of_nodes);
for _ in 0..num_of_nodes {
nodes_joining_tasks.push(async {
let (node, mut event_stream) = TestNodeBuilder::new(None)
let (node, mut event_stream) = InstanceBuilder::new(None)
.with_contact(genesis_contact)
.create()
.await?;

assert_next_event!(event_stream, Event::Connected(Connected::First));

Ok::<Node, Error>(node)
Ok::<Instance, Error>(node)
});
}

Expand Down
6 changes: 3 additions & 3 deletions tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async fn test_messages_client_node() -> Result<()> {
let msg = b"hello!";
let response = b"good bye!";

let (node, mut event_stream) = TestNodeBuilder::new(None).first().create().await?;
let (node, mut event_stream) = InstanceBuilder::new(None).first().create().await?;

// spawn node events listener
let node_handler = tokio::spawn(async move {
Expand Down Expand Up @@ -65,7 +65,7 @@ async fn test_messages_between_nodes() -> Result<()> {
let msg = b"hello!";
let response = b"good bye!";

let (node1, mut event_stream) = TestNodeBuilder::new(None).first().create().await?;
let (node1, mut event_stream) = InstanceBuilder::new(None).first().create().await?;
let node1_contact = node1.our_connection_info()?;
let node1_name = node1.name().await;

Expand All @@ -84,7 +84,7 @@ async fn test_messages_between_nodes() -> Result<()> {
});

// start a second node which sends a message to the first node
let (node2, mut event_stream) = TestNodeBuilder::new(None)
let (node2, mut event_stream) = InstanceBuilder::new(None)
.with_contact(node1_contact)
.create()
.await?;
Expand Down
26 changes: 12 additions & 14 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use futures::future;
use itertools::Itertools;
use sn_routing::{
event::{Connected, Event},
log_ident, EventStream, NetworkParams, Node, NodeConfig, TransportConfig, MIN_AGE,
log_ident, Config, EventStream, Instance, NetworkParams, TransportConfig, MIN_AGE,
};
use std::{
collections::{BTreeSet, HashSet},
Expand All @@ -29,14 +29,12 @@ use std::{

static LOG_INIT: Once = Once::new();

// ----- TestNode and builder -----

pub struct TestNodeBuilder {
config: NodeConfig,
pub struct InstanceBuilder {
config: Config,
}

impl<'a> TestNodeBuilder {
pub fn new(config: Option<NodeConfig>) -> Self {
impl<'a> InstanceBuilder {
pub fn new(config: Option<Config>) -> Self {
// We initialise the logger but only once for all tests
LOG_INIT.call_once(|| {
env_logger::builder()
Expand All @@ -57,7 +55,7 @@ impl<'a> TestNodeBuilder {
.init()
});

let config = config.unwrap_or_else(NodeConfig::default);
let config = config.unwrap_or_else(Config::default);

Self { config }
}
Expand Down Expand Up @@ -89,7 +87,7 @@ impl<'a> TestNodeBuilder {
self
}

pub async fn create(self) -> Result<(Node, EventStream)> {
pub async fn create(self) -> Result<(Instance, EventStream)> {
// make sure we set 127.0.0.1 as the IP if was not set
let config = if self.config.transport_config.ip.is_none() {
let mut config = self.config;
Expand All @@ -99,7 +97,7 @@ impl<'a> TestNodeBuilder {
self.config
};

Ok(Node::new(config).await?)
Ok(Instance::new(config).await?)
}
}

Expand All @@ -124,11 +122,11 @@ macro_rules! assert_next_event {
pub async fn create_connected_nodes(
count: usize,
network_params: NetworkParams,
) -> Result<Vec<(Node, EventStream)>> {
) -> Result<Vec<(Instance, EventStream)>> {
let mut nodes = vec![];

// Create the first node
let (node, mut event_stream) = TestNodeBuilder::new(None)
let (node, mut event_stream) = InstanceBuilder::new(None)
.first()
.network_params(network_params)
.create()
Expand All @@ -142,7 +140,7 @@ pub async fn create_connected_nodes(

// Create the other nodes bootstrapping off the first node.
let other_nodes = (1..count).map(|_| async {
let (node, mut event_stream) = TestNodeBuilder::new(None)
let (node, mut event_stream) = InstanceBuilder::new(None)
.network_params(network_params)
.with_contact(bootstrap_contact)
.create()
Expand Down Expand Up @@ -183,7 +181,7 @@ pub async fn create_connected_nodes(
Ok(nodes)
}

pub async fn verify_invariants_for_node(node: &Node, elder_size: usize) -> Result<()> {
pub async fn verify_invariants_for_node(node: &Instance, elder_size: usize) -> Result<()> {
let our_name = node.name().await;
assert!(node.matches_our_prefix(&our_name).await?);

Expand Down

0 comments on commit d8d6314

Please sign in to comment.