mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-16 08:10:40 +08:00
node: Implement Client
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
@@ -4,10 +4,34 @@ use git_url::Url;
|
||||
|
||||
use crate::collections::HashSet;
|
||||
use crate::identity::{ProjId, UserId};
|
||||
use crate::protocol::message::Address;
|
||||
use crate::protocol::message::{Address, Envelope, Message};
|
||||
|
||||
/// Peer-to-peer network.
|
||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum Network {
|
||||
#[default]
|
||||
Main,
|
||||
Test,
|
||||
}
|
||||
|
||||
impl Network {
|
||||
pub fn magic(&self) -> u32 {
|
||||
match self {
|
||||
Self::Main => 0x819b43d9,
|
||||
Self::Test => 0x717ebaf8,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn envelope(&self, msg: Message) -> Envelope {
|
||||
Envelope {
|
||||
magic: self.magic(),
|
||||
msg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Project tracking policy.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ProjectTracking {
|
||||
/// Track all projects we come across.
|
||||
All { blocked: HashSet<ProjId> },
|
||||
@@ -24,7 +48,7 @@ impl Default for ProjectTracking {
|
||||
}
|
||||
|
||||
/// Project remote tracking policy.
|
||||
#[derive(Debug, Default)]
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub enum RemoteTracking {
|
||||
/// Only track remotes of project delegates.
|
||||
#[default]
|
||||
@@ -36,11 +60,13 @@ pub enum RemoteTracking {
|
||||
}
|
||||
|
||||
/// Protocol configuration.
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
/// Peers to connect to on startup.
|
||||
/// Connections to these peers will be maintained.
|
||||
pub connect: Vec<net::SocketAddr>,
|
||||
/// Peer-to-peer network.
|
||||
pub network: Network,
|
||||
/// Project tracking policy.
|
||||
pub project_tracking: ProjectTracking,
|
||||
/// Project remote tracking policy.
|
||||
@@ -57,6 +83,7 @@ impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
connect: Vec::default(),
|
||||
network: Network::default(),
|
||||
project_tracking: ProjectTracking::default(),
|
||||
remote_tracking: RemoteTracking::default(),
|
||||
relay: true,
|
||||
|
||||
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::crypto;
|
||||
use crate::identity::ProjId;
|
||||
use crate::protocol::{Context, NodeId, Timestamp, NETWORK_MAGIC, PROTOCOL_VERSION};
|
||||
use crate::protocol::{Context, NodeId, Timestamp, PROTOCOL_VERSION};
|
||||
use crate::storage;
|
||||
|
||||
/// Message envelope. All messages sent over the network are wrapped in this type.
|
||||
@@ -98,15 +98,6 @@ pub enum Message {
|
||||
},
|
||||
}
|
||||
|
||||
impl From<Message> for Envelope {
|
||||
fn from(msg: Message) -> Self {
|
||||
Self {
|
||||
magic: NETWORK_MAGIC,
|
||||
msg,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn hello(id: NodeId, timestamp: Timestamp, addrs: Vec<Address>, git: Url) -> Self {
|
||||
Self::Hello {
|
||||
|
||||
@@ -104,7 +104,7 @@ impl Peer {
|
||||
T: storage::ReadStorage + storage::WriteStorage,
|
||||
G: crypto::Signer,
|
||||
{
|
||||
if envelope.magic != NETWORK_MAGIC {
|
||||
if envelope.magic != ctx.config.network.magic() {
|
||||
return Err(PeerError::WrongMagic(envelope.magic));
|
||||
}
|
||||
debug!("Received {:?} from {}", &envelope.msg, self.ip());
|
||||
|
||||
Reference in New Issue
Block a user