node: Service -> Protocol
Signed-off-by: Alexis Sellier <alexis@radicle.xyz>
This commit is contained in:
parent
1017a2bc69
commit
15e8a96948
|
|
@ -7,8 +7,8 @@ use nakamoto_net::{LocalTime, Reactor};
|
|||
use crate::clock::RefClock;
|
||||
use crate::collections::HashMap;
|
||||
use crate::crypto::Signer;
|
||||
use crate::protocol;
|
||||
use crate::protocol::wire::Wire;
|
||||
use crate::service;
|
||||
use crate::service::wire::Wire;
|
||||
use crate::storage::git::Storage;
|
||||
use crate::transport::Transport;
|
||||
|
||||
|
|
@ -17,19 +17,19 @@ pub mod handle;
|
|||
/// Client configuration.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
/// Client protocol configuration.
|
||||
pub protocol: protocol::Config,
|
||||
/// Client service configuration.
|
||||
pub service: service::Config,
|
||||
/// Client listen addresses.
|
||||
pub listen: Vec<net::SocketAddr>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
/// Create a new configuration for the given network.
|
||||
pub fn new(network: protocol::Network) -> Self {
|
||||
pub fn new(network: service::Network) -> Self {
|
||||
Self {
|
||||
protocol: protocol::Config {
|
||||
service: service::Config {
|
||||
network,
|
||||
..protocol::Config::default()
|
||||
..service::Config::default()
|
||||
},
|
||||
..Self::default()
|
||||
}
|
||||
|
|
@ -39,7 +39,7 @@ impl Config {
|
|||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
protocol: protocol::Config::default(),
|
||||
service: service::Config::default(),
|
||||
listen: vec![([0, 0, 0, 0], 0).into()],
|
||||
}
|
||||
}
|
||||
|
|
@ -50,8 +50,8 @@ pub struct Client<R: Reactor, G: Signer> {
|
|||
storage: Storage,
|
||||
signer: G,
|
||||
|
||||
handle: chan::Sender<protocol::Command>,
|
||||
commands: chan::Receiver<protocol::Command>,
|
||||
handle: chan::Sender<service::Command>,
|
||||
commands: chan::Receiver<service::Command>,
|
||||
shutdown: chan::Sender<()>,
|
||||
listening: chan::Receiver<net::SocketAddr>,
|
||||
events: Events,
|
||||
|
|
@ -59,7 +59,7 @@ pub struct Client<R: Reactor, G: Signer> {
|
|||
|
||||
impl<R: Reactor, G: Signer> Client<R, G> {
|
||||
pub fn new<P: AsRef<Path>>(path: P, signer: G) -> Result<Self, nakamoto_net::error::Error> {
|
||||
let (handle, commands) = chan::unbounded::<protocol::Command>();
|
||||
let (handle, commands) = chan::unbounded::<service::Command>();
|
||||
let (shutdown, shutdown_recv) = chan::bounded(1);
|
||||
let (listening_send, listening) = chan::bounded(1);
|
||||
let reactor = R::new(shutdown_recv, listening_send)?;
|
||||
|
|
@ -79,7 +79,7 @@ impl<R: Reactor, G: Signer> Client<R, G> {
|
|||
}
|
||||
|
||||
pub fn run(mut self, config: Config) -> Result<(), nakamoto_net::error::Error> {
|
||||
let network = config.protocol.network;
|
||||
let network = config.service.network;
|
||||
let rng = fastrand::Rng::new();
|
||||
let time = LocalTime::now();
|
||||
let storage = self.storage;
|
||||
|
|
@ -88,8 +88,8 @@ impl<R: Reactor, G: Signer> Client<R, G> {
|
|||
|
||||
log::info!("Initializing client ({:?})..", network);
|
||||
|
||||
let protocol = protocol::Protocol::new(
|
||||
config.protocol,
|
||||
let service = service::Service::new(
|
||||
config.service,
|
||||
RefClock::from(time),
|
||||
storage,
|
||||
addresses,
|
||||
|
|
@ -98,7 +98,7 @@ impl<R: Reactor, G: Signer> Client<R, G> {
|
|||
);
|
||||
self.reactor.run(
|
||||
&config.listen,
|
||||
Transport::new(Wire::new(protocol)),
|
||||
Transport::new(Wire::new(service)),
|
||||
self.events,
|
||||
self.commands,
|
||||
)?;
|
||||
|
|
@ -119,8 +119,8 @@ impl<R: Reactor, G: Signer> Client<R, G> {
|
|||
|
||||
pub struct Events {}
|
||||
|
||||
impl nakamoto_net::Publisher<protocol::Event> for Events {
|
||||
fn publish(&mut self, e: protocol::Event) {
|
||||
impl nakamoto_net::Publisher<service::Event> for Events {
|
||||
fn publish(&mut self, e: service::Event) {
|
||||
log::info!("Received event {:?}", e);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ use nakamoto_net::Waker;
|
|||
use thiserror::Error;
|
||||
|
||||
use crate::identity::Id;
|
||||
use crate::protocol;
|
||||
use crate::protocol::{CommandError, FetchLookup};
|
||||
use crate::service;
|
||||
use crate::service::{CommandError, FetchLookup};
|
||||
|
||||
/// An error resulting from a handle method.
|
||||
#[derive(Error, Debug)]
|
||||
|
|
@ -47,7 +47,7 @@ impl<T> From<chan::SendError<T>> for Error {
|
|||
}
|
||||
|
||||
pub struct Handle<W: Waker> {
|
||||
pub(crate) commands: chan::Sender<protocol::Command>,
|
||||
pub(crate) commands: chan::Sender<service::Command>,
|
||||
pub(crate) shutdown: chan::Sender<()>,
|
||||
pub(crate) listening: chan::Receiver<net::SocketAddr>,
|
||||
pub(crate) waker: W,
|
||||
|
|
@ -57,31 +57,31 @@ impl<W: Waker> traits::Handle for Handle<W> {
|
|||
/// Retrieve or update the given project from the network.
|
||||
fn fetch(&self, id: Id) -> Result<FetchLookup, Error> {
|
||||
let (sender, receiver) = chan::bounded(1);
|
||||
self.commands.send(protocol::Command::Fetch(id, sender))?;
|
||||
self.commands.send(service::Command::Fetch(id, sender))?;
|
||||
receiver.recv().map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Start tracking the given project. Doesn't do anything if the project is already tracked.
|
||||
fn track(&self, id: Id) -> Result<bool, Error> {
|
||||
let (sender, receiver) = chan::bounded(1);
|
||||
self.commands.send(protocol::Command::Track(id, sender))?;
|
||||
self.commands.send(service::Command::Track(id, sender))?;
|
||||
receiver.recv().map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Untrack the given project and delete it from storage.
|
||||
fn untrack(&self, id: Id) -> Result<bool, Error> {
|
||||
let (sender, receiver) = chan::bounded(1);
|
||||
self.commands.send(protocol::Command::Untrack(id, sender))?;
|
||||
self.commands.send(service::Command::Untrack(id, sender))?;
|
||||
receiver.recv().map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Notify the client that a project has been updated.
|
||||
fn updated(&self, id: Id) -> Result<(), Error> {
|
||||
self.command(protocol::Command::AnnounceRefs(id))
|
||||
self.command(service::Command::AnnounceRefs(id))
|
||||
}
|
||||
|
||||
/// Send a command to the command channel, and wake up the event loop.
|
||||
fn command(&self, cmd: protocol::Command) -> Result<(), Error> {
|
||||
fn command(&self, cmd: service::Command) -> Result<(), Error> {
|
||||
self.commands.send(cmd)?;
|
||||
self.waker.wake()?;
|
||||
|
||||
|
|
@ -111,7 +111,7 @@ pub mod traits {
|
|||
/// Notify the client that a project has been updated.
|
||||
fn updated(&self, id: Id) -> Result<(), Error>;
|
||||
/// Send a command to the command channel, and wake up the event loop.
|
||||
fn command(&self, cmd: protocol::Command) -> Result<(), Error>;
|
||||
fn command(&self, cmd: service::Command) -> Result<(), Error>;
|
||||
/// Ask the client to shutdown.
|
||||
fn shutdown(self) -> Result<(), Error>;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ use std::{fs, io, net};
|
|||
use crate::client;
|
||||
use crate::client::handle::traits::Handle;
|
||||
use crate::identity::Id;
|
||||
use crate::protocol::FetchLookup;
|
||||
use crate::protocol::FetchResult;
|
||||
use crate::service::FetchLookup;
|
||||
use crate::service::FetchResult;
|
||||
|
||||
/// Default name for control socket file.
|
||||
pub const DEFAULT_SOCKET_NAME: &str = "radicle.sock";
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
use std::io;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use crate::protocol::message::Envelope;
|
||||
use crate::protocol::wire;
|
||||
use crate::service::message::Envelope;
|
||||
use crate::service::wire;
|
||||
|
||||
/// Message stream decoder.
|
||||
///
|
||||
|
|
|
|||
|
|
@ -14,9 +14,9 @@ mod git;
|
|||
mod hash;
|
||||
mod identity;
|
||||
mod logger;
|
||||
mod protocol;
|
||||
mod rad;
|
||||
mod serde_ext;
|
||||
mod service;
|
||||
mod storage;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
|
|
|||
|
|
@ -24,14 +24,14 @@ use crate::clock::RefClock;
|
|||
use crate::collections::{HashMap, HashSet};
|
||||
use crate::crypto;
|
||||
use crate::identity::{Id, Project};
|
||||
use crate::protocol::config::ProjectTracking;
|
||||
use crate::protocol::message::{NodeAnnouncement, RefsAnnouncement};
|
||||
use crate::protocol::peer::{Peer, PeerError, PeerState};
|
||||
use crate::service::config::ProjectTracking;
|
||||
use crate::service::message::{NodeAnnouncement, RefsAnnouncement};
|
||||
use crate::service::peer::{Peer, PeerError, PeerState};
|
||||
use crate::storage;
|
||||
use crate::storage::{Inventory, ReadRepository, RefUpdate, WriteRepository, WriteStorage};
|
||||
|
||||
pub use crate::protocol::config::{Config, Network};
|
||||
pub use crate::protocol::message::{Envelope, Message};
|
||||
pub use crate::service::config::{Config, Network};
|
||||
pub use crate::service::message::{Envelope, Message};
|
||||
|
||||
use self::filter::Filter;
|
||||
use self::message::{InventoryAnnouncement, NodeFeatures};
|
||||
|
|
@ -68,7 +68,7 @@ pub enum Io {
|
|||
Event(Event),
|
||||
}
|
||||
|
||||
/// A protocol event.
|
||||
/// A service event.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Event {
|
||||
RefsFetched {
|
||||
|
|
@ -121,7 +121,7 @@ pub enum FetchResult {
|
|||
},
|
||||
}
|
||||
|
||||
/// Commands sent to the protocol by the operator.
|
||||
/// Commands sent to the service by the operator.
|
||||
#[derive(Debug)]
|
||||
pub enum Command {
|
||||
AnnounceRefs(Id),
|
||||
|
|
@ -136,26 +136,26 @@ pub enum Command {
|
|||
pub enum CommandError {}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Protocol<S, T, G> {
|
||||
pub struct Service<S, T, G> {
|
||||
/// Peers currently or recently connected.
|
||||
peers: Peers,
|
||||
/// Protocol state that isn't peer-specific.
|
||||
/// Service state that isn't peer-specific.
|
||||
context: Context<S, T, G>,
|
||||
/// Whether our local inventory no long represents what we have announced to the network.
|
||||
out_of_sync: bool,
|
||||
/// Last time the protocol was idle.
|
||||
/// Last time the service was idle.
|
||||
last_idle: LocalTime,
|
||||
/// Last time the protocol synced.
|
||||
/// Last time the service synced.
|
||||
last_sync: LocalTime,
|
||||
/// Last time the protocol routing table was pruned.
|
||||
/// Last time the service routing table was pruned.
|
||||
last_prune: LocalTime,
|
||||
/// Last time the protocol announced its inventory.
|
||||
/// Last time the service announced its inventory.
|
||||
last_announce: LocalTime,
|
||||
/// Time when the protocol was initialized.
|
||||
/// Time when the service was initialized.
|
||||
start_time: LocalTime,
|
||||
}
|
||||
|
||||
impl<'r, T: WriteStorage<'r>, S: address_book::Store, G: crypto::Signer> Protocol<S, T, G> {
|
||||
impl<'r, T: WriteStorage<'r>, S: address_book::Store, G: crypto::Signer> Service<S, T, G> {
|
||||
pub fn new(
|
||||
config: Config,
|
||||
clock: RefClock,
|
||||
|
|
@ -264,12 +264,12 @@ impl<'r, T: WriteStorage<'r>, S: address_book::Store, G: crypto::Signer> Protoco
|
|||
&self.context.signer
|
||||
}
|
||||
|
||||
/// Get the local protocol time.
|
||||
/// Get the local service time.
|
||||
pub fn local_time(&self) -> LocalTime {
|
||||
self.context.clock.local_time()
|
||||
}
|
||||
|
||||
/// Get protocol configuration.
|
||||
/// Get service configuration.
|
||||
pub fn config(&self) -> &Config {
|
||||
&self.context.config
|
||||
}
|
||||
|
|
@ -589,7 +589,7 @@ impl<'r, T: WriteStorage<'r>, S: address_book::Store, G: crypto::Signer> Protoco
|
|||
}
|
||||
}
|
||||
|
||||
impl<S, T, G> Deref for Protocol<S, T, G> {
|
||||
impl<S, T, G> Deref for Service<S, T, G> {
|
||||
type Target = Context<S, T, G>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
|
|
@ -597,7 +597,7 @@ impl<S, T, G> Deref for Protocol<S, T, G> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S, T, G> DerefMut for Protocol<S, T, G> {
|
||||
impl<S, T, G> DerefMut for Service<S, T, G> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.context
|
||||
}
|
||||
|
|
@ -633,7 +633,7 @@ impl fmt::Display for DisconnectReason {
|
|||
}
|
||||
}
|
||||
|
||||
impl<S, T, G> Iterator for Protocol<S, T, G> {
|
||||
impl<S, T, G> Iterator for Service<S, T, G> {
|
||||
type Item = Io;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
|
@ -650,10 +650,10 @@ pub struct Lookup {
|
|||
pub remote: Vec<NodeId>,
|
||||
}
|
||||
|
||||
/// Global protocol state used across peers.
|
||||
/// Global service state used across peers.
|
||||
#[derive(Debug)]
|
||||
pub struct Context<S, T, G> {
|
||||
/// Protocol configuration.
|
||||
/// Service configuration.
|
||||
config: Config,
|
||||
/// Our cryptographic signer and key.
|
||||
signer: G,
|
||||
|
|
@ -5,7 +5,7 @@ use git_url::Url;
|
|||
use crate::collections::HashSet;
|
||||
use crate::git;
|
||||
use crate::identity::{Id, PublicKey};
|
||||
use crate::protocol::message::{Address, Envelope, Message};
|
||||
use crate::service::message::{Address, Envelope, Message};
|
||||
|
||||
/// Peer-to-peer network.
|
||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
|
|
@ -60,7 +60,7 @@ pub enum RemoteTracking {
|
|||
Allowed(HashSet<PublicKey>),
|
||||
}
|
||||
|
||||
/// Protocol configuration.
|
||||
/// Service configuration.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
/// Peers to connect to on startup.
|
||||
|
|
@ -4,7 +4,7 @@ use std::ops::{Deref, DerefMut};
|
|||
use bloomy::BloomFilter;
|
||||
|
||||
use crate::identity::Id;
|
||||
use crate::protocol::wire;
|
||||
use crate::service::wire;
|
||||
|
||||
/// Size in bytes of subscription bloom filter.
|
||||
pub const FILTER_SIZE: usize = 1024 * 16;
|
||||
|
|
@ -5,9 +5,9 @@ use byteorder::{NetworkEndian, ReadBytesExt};
|
|||
use crate::crypto;
|
||||
use crate::git;
|
||||
use crate::identity::Id;
|
||||
use crate::protocol::filter::Filter;
|
||||
use crate::protocol::wire;
|
||||
use crate::protocol::{NodeId, Timestamp, PROTOCOL_VERSION};
|
||||
use crate::service::filter::Filter;
|
||||
use crate::service::wire;
|
||||
use crate::service::{NodeId, Timestamp, PROTOCOL_VERSION};
|
||||
use crate::storage::refs::Refs;
|
||||
|
||||
/// Message envelope. All messages sent over the network are wrapped in this type.
|
||||
|
|
@ -607,7 +607,7 @@ mod tests {
|
|||
|
||||
use crate::crypto::Signer;
|
||||
use crate::decoder::Decoder;
|
||||
use crate::protocol::wire::{self, Encode};
|
||||
use crate::service::wire::{self, Encode};
|
||||
use crate::test::crypto::MockSigner;
|
||||
|
||||
#[quickcheck]
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::protocol::message::*;
|
||||
use crate::protocol::*;
|
||||
use crate::service::message::*;
|
||||
use crate::service::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
|
|
@ -16,7 +16,7 @@ use crate::git;
|
|||
use crate::git::fmt;
|
||||
use crate::hash::Digest;
|
||||
use crate::identity::Id;
|
||||
use crate::protocol;
|
||||
use crate::service;
|
||||
use crate::storage::refs::Refs;
|
||||
use crate::storage::WriteStorage;
|
||||
|
||||
|
|
@ -389,11 +389,11 @@ impl Decode for Digest {
|
|||
#[derive(Debug)]
|
||||
pub struct Wire<S, T, G> {
|
||||
inboxes: HashMap<IpAddr, Decoder>,
|
||||
inner: protocol::Protocol<S, T, G>,
|
||||
inner: service::Service<S, T, G>,
|
||||
}
|
||||
|
||||
impl<S, T, G> Wire<S, T, G> {
|
||||
pub fn new(inner: protocol::Protocol<S, T, G>) -> Self {
|
||||
pub fn new(inner: service::Service<S, T, G>) -> Self {
|
||||
Self {
|
||||
inboxes: HashMap::new(),
|
||||
inner,
|
||||
|
|
@ -419,7 +419,7 @@ where
|
|||
self.inner.wake()
|
||||
}
|
||||
|
||||
pub fn command(&mut self, cmd: protocol::Command) {
|
||||
pub fn command(&mut self, cmd: service::Command) {
|
||||
self.inner.command(cmd)
|
||||
}
|
||||
|
||||
|
|
@ -440,7 +440,7 @@ where
|
|||
pub fn disconnected(
|
||||
&mut self,
|
||||
addr: &std::net::SocketAddr,
|
||||
reason: nakamoto::DisconnectReason<protocol::DisconnectReason>,
|
||||
reason: nakamoto::DisconnectReason<service::DisconnectReason>,
|
||||
) {
|
||||
self.inboxes.remove(&addr.ip());
|
||||
self.inner.disconnected(addr, reason)
|
||||
|
|
@ -472,11 +472,11 @@ where
|
|||
}
|
||||
|
||||
impl<S, T, G> Iterator for Wire<S, T, G> {
|
||||
type Item = nakamoto::Io<protocol::Event, protocol::DisconnectReason>;
|
||||
type Item = nakamoto::Io<service::Event, service::DisconnectReason>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.inner.next() {
|
||||
Some(protocol::Io::Write(addr, msgs)) => {
|
||||
Some(service::Io::Write(addr, msgs)) => {
|
||||
let mut buf = Vec::new();
|
||||
for msg in msgs {
|
||||
log::debug!("Write {:?} to {}", &msg, addr.ip());
|
||||
|
|
@ -486,10 +486,10 @@ impl<S, T, G> Iterator for Wire<S, T, G> {
|
|||
}
|
||||
Some(nakamoto::Io::Write(addr, buf))
|
||||
}
|
||||
Some(protocol::Io::Event(e)) => Some(nakamoto::Io::Event(e)),
|
||||
Some(protocol::Io::Connect(a)) => Some(nakamoto::Io::Connect(a)),
|
||||
Some(protocol::Io::Disconnect(a, r)) => Some(nakamoto::Io::Disconnect(a, r)),
|
||||
Some(protocol::Io::Wakeup(d)) => Some(nakamoto::Io::Wakeup(d)),
|
||||
Some(service::Io::Event(e)) => Some(nakamoto::Io::Event(e)),
|
||||
Some(service::Io::Connect(a)) => Some(nakamoto::Io::Connect(a)),
|
||||
Some(service::Io::Disconnect(a, r)) => Some(nakamoto::Io::Disconnect(a, r)),
|
||||
Some(service::Io::Wakeup(d)) => Some(nakamoto::Io::Wakeup(d)),
|
||||
|
||||
None => None,
|
||||
}
|
||||
|
|
@ -497,7 +497,7 @@ impl<S, T, G> Iterator for Wire<S, T, G> {
|
|||
}
|
||||
|
||||
impl<S, T, G> Deref for Wire<S, T, G> {
|
||||
type Target = protocol::Protocol<S, T, G>;
|
||||
type Target = service::Service<S, T, G>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
|
|
@ -15,7 +15,7 @@ use crate::crypto;
|
|||
use crate::crypto::{PublicKey, Signature, Signer, Unverified, Verified};
|
||||
use crate::git;
|
||||
use crate::git::Oid;
|
||||
use crate::protocol::wire;
|
||||
use crate::service::wire;
|
||||
use crate::storage;
|
||||
use crate::storage::{ReadRepository, RemoteId, WriteRepository};
|
||||
|
||||
|
|
|
|||
|
|
@ -14,12 +14,12 @@ use crate::crypto::{PublicKey, SecretKey};
|
|||
use crate::git;
|
||||
use crate::hash;
|
||||
use crate::identity::{Delegate, Did, Doc, Id, Project};
|
||||
use crate::protocol::filter::{Filter, FILTER_SIZE};
|
||||
use crate::protocol::message::{
|
||||
use crate::service::filter::{Filter, FILTER_SIZE};
|
||||
use crate::service::message::{
|
||||
Address, Envelope, InventoryAnnouncement, Message, MessageType, NodeAnnouncement,
|
||||
RefsAnnouncement, Subscribe,
|
||||
};
|
||||
use crate::protocol::{NodeId, Timestamp};
|
||||
use crate::service::{NodeId, Timestamp};
|
||||
use crate::storage;
|
||||
use crate::storage::refs::{Refs, SignedRefs};
|
||||
use crate::test::storage::MockStorage;
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use std::sync::{Arc, Mutex};
|
|||
use crate::client::handle::traits;
|
||||
use crate::client::handle::Error;
|
||||
use crate::identity::Id;
|
||||
use crate::protocol;
|
||||
use crate::protocol::FetchLookup;
|
||||
use crate::service;
|
||||
use crate::service::FetchLookup;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Handle {
|
||||
|
|
@ -30,7 +30,7 @@ impl traits::Handle for Handle {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn command(&self, _cmd: protocol::Command) -> Result<(), Error> {
|
||||
fn command(&self, _cmd: service::Command) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,22 +7,22 @@ use log::*;
|
|||
use crate::address_book::{KnownAddress, Source};
|
||||
use crate::clock::RefClock;
|
||||
use crate::collections::HashMap;
|
||||
use crate::protocol;
|
||||
use crate::protocol::config::*;
|
||||
use crate::protocol::message::*;
|
||||
use crate::protocol::*;
|
||||
use crate::service;
|
||||
use crate::service::config::*;
|
||||
use crate::service::message::*;
|
||||
use crate::service::*;
|
||||
use crate::storage::WriteStorage;
|
||||
use crate::test::crypto::MockSigner;
|
||||
use crate::test::simulator;
|
||||
use crate::{Link, LocalTime};
|
||||
|
||||
/// Protocol instantiation used for testing.
|
||||
pub type Protocol<S> = protocol::Protocol<HashMap<net::IpAddr, KnownAddress>, S, MockSigner>;
|
||||
/// Service instantiation used for testing.
|
||||
pub type Service<S> = service::Service<HashMap<net::IpAddr, KnownAddress>, S, MockSigner>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Peer<S> {
|
||||
pub name: &'static str,
|
||||
pub protocol: Protocol<S>,
|
||||
pub service: Service<S>,
|
||||
pub ip: net::IpAddr,
|
||||
pub rng: fastrand::Rng,
|
||||
pub local_time: LocalTime,
|
||||
|
|
@ -45,16 +45,16 @@ where
|
|||
}
|
||||
|
||||
impl<S> Deref for Peer<S> {
|
||||
type Target = Protocol<S>;
|
||||
type Target = Service<S>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.protocol
|
||||
&self.service
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> DerefMut for Peer<S> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.protocol
|
||||
&mut self.service
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -91,13 +91,13 @@ where
|
|||
let local_time = LocalTime::now();
|
||||
let clock = RefClock::from(local_time);
|
||||
let signer = MockSigner::new(&mut rng);
|
||||
let protocol = Protocol::new(config, clock, storage, addrs, signer, rng.clone());
|
||||
let service = Service::new(config, clock, storage, addrs, signer, rng.clone());
|
||||
let ip = ip.into();
|
||||
let local_addr = net::SocketAddr::new(ip, rng.u16(..));
|
||||
|
||||
Self {
|
||||
name,
|
||||
protocol,
|
||||
service,
|
||||
ip,
|
||||
local_addr,
|
||||
rng,
|
||||
|
|
@ -111,12 +111,12 @@ where
|
|||
info!("{}: Initializing: address = {}", self.name, self.ip);
|
||||
|
||||
self.initialized = true;
|
||||
self.protocol.initialize(LocalTime::now());
|
||||
self.service.initialize(LocalTime::now());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn timestamp(&self) -> Timestamp {
|
||||
self.protocol.timestamp()
|
||||
self.service.timestamp()
|
||||
}
|
||||
|
||||
pub fn git_url(&self) -> Url {
|
||||
|
|
@ -124,11 +124,11 @@ where
|
|||
}
|
||||
|
||||
pub fn node_id(&self) -> NodeId {
|
||||
self.protocol.node_id()
|
||||
self.service.node_id()
|
||||
}
|
||||
|
||||
pub fn receive(&mut self, peer: &net::SocketAddr, msg: Message) {
|
||||
self.protocol
|
||||
self.service
|
||||
.received_message(peer, self.config().network.envelope(msg));
|
||||
}
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ where
|
|||
let git = Url::from_bytes(git.as_bytes()).unwrap();
|
||||
|
||||
self.initialize();
|
||||
self.protocol.connected(remote, &local, Link::Inbound);
|
||||
self.service.connected(remote, &local, Link::Inbound);
|
||||
self.receive(
|
||||
&remote,
|
||||
Message::init(
|
||||
|
|
@ -161,8 +161,8 @@ where
|
|||
let remote = simulator::Peer::<S>::addr(peer);
|
||||
|
||||
self.initialize();
|
||||
self.protocol.attempted(&remote);
|
||||
self.protocol
|
||||
self.service.attempted(&remote);
|
||||
self.service
|
||||
.connected(remote, &self.local_addr, Link::Outbound);
|
||||
|
||||
let mut msgs = self.messages(&remote);
|
||||
|
|
@ -187,8 +187,8 @@ where
|
|||
pub fn messages(&mut self, remote: &net::SocketAddr) -> impl Iterator<Item = Message> {
|
||||
let mut msgs = Vec::new();
|
||||
|
||||
self.protocol.outbox().retain(|o| match o {
|
||||
protocol::Io::Write(a, envelopes) if a == remote => {
|
||||
self.service.outbox().retain(|o| match o {
|
||||
service::Io::Write(a, envelopes) if a == remote => {
|
||||
msgs.extend(envelopes.iter().map(|e| e.msg.clone()));
|
||||
false
|
||||
}
|
||||
|
|
@ -206,6 +206,6 @@ where
|
|||
|
||||
/// Get a draining iterator over the peer's I/O outbox.
|
||||
pub fn outbox(&mut self) -> impl Iterator<Item = Io> + '_ {
|
||||
self.protocol.outbox().drain(..)
|
||||
self.service.outbox().drain(..)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@ use log::*;
|
|||
use nakamoto_net as nakamoto;
|
||||
use nakamoto_net::{Link, LocalDuration, LocalTime};
|
||||
|
||||
use crate::protocol::{DisconnectReason, Envelope, Event, Io};
|
||||
use crate::service::{DisconnectReason, Envelope, Event, Io};
|
||||
use crate::storage::WriteStorage;
|
||||
use crate::test::peer::Protocol;
|
||||
use crate::test::peer::Service;
|
||||
|
||||
/// Minimum latency between peers.
|
||||
pub const MIN_LATENCY: LocalDuration = LocalDuration::from_millis(1);
|
||||
|
|
@ -26,16 +26,16 @@ pub const MAX_EVENTS: usize = 2048;
|
|||
/// The simulator requires each peer to have a distinct IP address.
|
||||
type NodeId = std::net::IpAddr;
|
||||
|
||||
/// A simulated peer. Protocol instances have to be wrapped in this type to be simulated.
|
||||
pub trait Peer<S>: Deref<Target = Protocol<S>> + DerefMut<Target = Protocol<S>> + 'static {
|
||||
/// Initialize the peer. This should at minimum initialize the protocol with the
|
||||
/// A simulated peer. Service instances have to be wrapped in this type to be simulated.
|
||||
pub trait Peer<S>: Deref<Target = Service<S>> + DerefMut<Target = Service<S>> + 'static {
|
||||
/// Initialize the peer. This should at minimum initialize the service with the
|
||||
/// current time.
|
||||
fn init(&mut self);
|
||||
/// Get the peer address.
|
||||
fn addr(&self) -> net::SocketAddr;
|
||||
}
|
||||
|
||||
/// Simulated protocol input.
|
||||
/// Simulated service input.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Input {
|
||||
/// Connection attempt underway.
|
||||
|
|
@ -63,7 +63,7 @@ pub enum Input {
|
|||
Wake,
|
||||
}
|
||||
|
||||
/// A scheduled protocol input.
|
||||
/// A scheduled service input.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Scheduled {
|
||||
/// The node for which this input is scheduled.
|
||||
|
|
@ -349,8 +349,8 @@ impl<'r, S: WriteStorage<'r>> Simulation<S> {
|
|||
trace!(target: "sim", "{:05} {}", elapsed, next);
|
||||
} else {
|
||||
// TODO: This can be confusing, since this event may not actually be passed to
|
||||
// the protocol. It would be best to only log the events that are being sent
|
||||
// to the protocol, or to log when an input is being dropped.
|
||||
// the service. It would be best to only log the events that are being sent
|
||||
// to the service, or to log when an input is being dropped.
|
||||
info!(target: "sim", "{:05} {} ({})", elapsed, next, self.inbox.messages.len());
|
||||
}
|
||||
assert!(time >= self.time, "Time only moves forwards!");
|
||||
|
|
@ -415,7 +415,7 @@ impl<'r, S: WriteStorage<'r>> Simulation<S> {
|
|||
!self.is_done()
|
||||
}
|
||||
|
||||
/// Process a protocol output event from a node.
|
||||
/// Process a service output event from a node.
|
||||
pub fn schedule(&mut self, node: &NodeId, out: Io) {
|
||||
let node = *node;
|
||||
|
||||
|
|
@ -491,7 +491,7 @@ impl<'r, S: WriteStorage<'r>> Simulation<S> {
|
|||
if self.is_partitioned(node, remote.ip()) {
|
||||
log::info!(target: "sim", "{} -/-> {} (partitioned)", node, remote.ip());
|
||||
|
||||
// Sometimes, the protocol gets a failure input, other times it just hangs.
|
||||
// Sometimes, the service gets a failure input, other times it just hangs.
|
||||
if self.rng.bool() {
|
||||
self.inbox.insert(
|
||||
self.time + MIN_LATENCY,
|
||||
|
|
|
|||
|
|
@ -5,10 +5,10 @@ use crossbeam_channel as chan;
|
|||
use nakamoto_net as nakamoto;
|
||||
|
||||
use crate::collections::{HashMap, HashSet};
|
||||
use crate::protocol::config::*;
|
||||
use crate::protocol::message::*;
|
||||
use crate::protocol::peer::*;
|
||||
use crate::protocol::*;
|
||||
use crate::service::config::*;
|
||||
use crate::service::message::*;
|
||||
use crate::service::peer::*;
|
||||
use crate::service::*;
|
||||
use crate::storage::git::Storage;
|
||||
use crate::storage::ReadStorage;
|
||||
use crate::test::fixtures;
|
||||
|
|
@ -19,7 +19,7 @@ use crate::test::simulator;
|
|||
use crate::test::simulator::{Peer as _, Simulation};
|
||||
use crate::test::storage::MockStorage;
|
||||
use crate::{assert_matches, Link, LocalTime};
|
||||
use crate::{client, identity, protocol, rad, storage, test};
|
||||
use crate::{client, identity, rad, service, storage, test};
|
||||
|
||||
// NOTE
|
||||
//
|
||||
|
|
@ -39,7 +39,7 @@ fn test_outbound_connection() {
|
|||
alice.connect_to(&eve);
|
||||
|
||||
let peers = alice
|
||||
.protocol
|
||||
.service
|
||||
.peers()
|
||||
.negotiated()
|
||||
.map(|(ip, _)| *ip)
|
||||
|
|
@ -59,7 +59,7 @@ fn test_inbound_connection() {
|
|||
alice.connect_from(&eve);
|
||||
|
||||
let peers = alice
|
||||
.protocol
|
||||
.service
|
||||
.peers()
|
||||
.negotiated()
|
||||
.map(|(ip, _)| *ip)
|
||||
|
|
@ -396,8 +396,8 @@ fn test_push_and_pull() {
|
|||
let mut eve = Peer::new("eve", [9, 9, 9, 9], storage_eve);
|
||||
|
||||
// Alice and Bob connect to Eve.
|
||||
alice.command(protocol::Command::Connect(eve.addr()));
|
||||
bob.command(protocol::Command::Connect(eve.addr()));
|
||||
alice.command(service::Command::Connect(eve.addr()));
|
||||
bob.command(service::Command::Connect(eve.addr()));
|
||||
|
||||
let mut sim = Simulation::new(
|
||||
LocalTime::now(),
|
||||
|
|
@ -422,11 +422,11 @@ fn test_push_and_pull() {
|
|||
|
||||
// Bob tracks Alice's project.
|
||||
let (sender, _) = chan::bounded(1);
|
||||
bob.command(protocol::Command::Track(proj_id.clone(), sender));
|
||||
bob.command(service::Command::Track(proj_id.clone(), sender));
|
||||
|
||||
// Eve tracks Alice's project.
|
||||
let (sender, _) = chan::bounded(1);
|
||||
eve.command(protocol::Command::Track(proj_id.clone(), sender));
|
||||
eve.command(service::Command::Track(proj_id.clone(), sender));
|
||||
|
||||
// Neither of them have it in the beginning.
|
||||
assert!(eve.get(&proj_id).unwrap().is_none());
|
||||
|
|
@ -435,7 +435,7 @@ fn test_push_and_pull() {
|
|||
// Alice announces her refs.
|
||||
// We now expect Eve to fetch Alice's project from Alice.
|
||||
// Then we expect Bob to fetch Alice's project from Eve.
|
||||
alice.command(protocol::Command::AnnounceRefs(proj_id.clone()));
|
||||
alice.command(service::Command::AnnounceRefs(proj_id.clone()));
|
||||
sim.run_while([&mut alice, &mut bob, &mut eve], |s| !s.is_settled());
|
||||
|
||||
assert!(eve
|
||||
|
|
@ -450,7 +450,7 @@ fn test_push_and_pull() {
|
|||
.is_some());
|
||||
assert_matches!(
|
||||
sim.events(&bob.ip).next(),
|
||||
Some(protocol::Event::RefsFetched { from, .. })
|
||||
Some(service::Event::RefsFetched { from, .. })
|
||||
if from == eve.git_url(),
|
||||
"Bob fetched from Eve"
|
||||
);
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use nakamoto_net::{Io, Link};
|
|||
use crate::address_book;
|
||||
use crate::collections::HashMap;
|
||||
use crate::crypto;
|
||||
use crate::protocol::wire::Wire;
|
||||
use crate::protocol::{Command, DisconnectReason, Event, Protocol};
|
||||
use crate::service::wire::Wire;
|
||||
use crate::service::{Command, DisconnectReason, Event, Service};
|
||||
use crate::storage::WriteStorage;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -93,7 +93,7 @@ impl<S, T, G> Iterator for Transport<S, T, G> {
|
|||
}
|
||||
|
||||
impl<S, T, G> Deref for Transport<S, T, G> {
|
||||
type Target = Protocol<S, T, G>;
|
||||
type Target = Service<S, T, G>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.inner
|
||||
|
|
|
|||
Loading…
Reference in New Issue