node: Send git url during handshake
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
parent
aa28e40f67
commit
1ff8f625ff
|
|
@ -14,6 +14,7 @@ colored = { version = "1.9.0" }
|
||||||
fastrand = { version = "1.8.0" }
|
fastrand = { version = "1.8.0" }
|
||||||
git-ref-format = { version = "0", features = ["serde", "macro"] }
|
git-ref-format = { version = "0", features = ["serde", "macro"] }
|
||||||
git2 = { version = "0.13" }
|
git2 = { version = "0.13" }
|
||||||
|
git-url = { version = "0.3.5", features = ["serde1"] }
|
||||||
multibase = { version = "0.9.1" }
|
multibase = { version = "0.9.1" }
|
||||||
log = { version = "0.4.17", features = ["std"] }
|
log = { version = "0.4.17", features = ["std"] }
|
||||||
once_cell = { version = "1.13" }
|
once_cell = { version = "1.13" }
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ use std::ops::{Deref, DerefMut};
|
||||||
use std::{collections::VecDeque, fmt, io, net, net::IpAddr};
|
use std::{collections::VecDeque, fmt, io, net, net::IpAddr};
|
||||||
|
|
||||||
use fastrand::Rng;
|
use fastrand::Rng;
|
||||||
|
use git_url::Url;
|
||||||
use log::*;
|
use log::*;
|
||||||
use nakamoto::{LocalDuration, LocalTime};
|
use nakamoto::{LocalDuration, LocalTime};
|
||||||
use nakamoto_net as nakamoto;
|
use nakamoto_net as nakamoto;
|
||||||
|
|
@ -56,7 +57,7 @@ pub struct Envelope {
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
pub enum Message {
|
pub enum Message {
|
||||||
/// Say hello to a peer. This is the first message sent to a peer after connection.
|
/// Say hello to a peer. This is the first message sent to a peer after connection.
|
||||||
Hello { version: u32 },
|
Hello { version: u32, git: Url },
|
||||||
/// Get node addresses from a peer.
|
/// Get node addresses from a peer.
|
||||||
GetAddrs,
|
GetAddrs,
|
||||||
/// Send node addresses to a peer. Sent in response to [`Message::GetAddrs`].
|
/// Send node addresses to a peer. Sent in response to [`Message::GetAddrs`].
|
||||||
|
|
@ -84,9 +85,10 @@ impl From<Message> for Envelope {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Message {
|
impl Message {
|
||||||
pub fn hello() -> Self {
|
pub fn hello(git: Url) -> Self {
|
||||||
Self::Hello {
|
Self::Hello {
|
||||||
version: PROTOCOL_VERSION,
|
version: PROTOCOL_VERSION,
|
||||||
|
git,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -152,6 +154,8 @@ pub struct Config {
|
||||||
pub remote_tracking: RemoteTracking,
|
pub remote_tracking: RemoteTracking,
|
||||||
/// Whether or not our node should relay inventories.
|
/// Whether or not our node should relay inventories.
|
||||||
pub relay: bool,
|
pub relay: bool,
|
||||||
|
/// Our Git URL for fetching projects.
|
||||||
|
pub git_url: Url,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
|
|
@ -161,6 +165,7 @@ impl Default for Config {
|
||||||
project_tracking: ProjectTracking::default(),
|
project_tracking: ProjectTracking::default(),
|
||||||
remote_tracking: RemoteTracking::default(),
|
remote_tracking: RemoteTracking::default(),
|
||||||
relay: true,
|
relay: true,
|
||||||
|
git_url: Url::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -471,13 +476,12 @@ where
|
||||||
// For inbound connections, we wait for the remote to say "Hello" first.
|
// For inbound connections, we wait for the remote to say "Hello" first.
|
||||||
// TODO: How should we deal with multiple peers connecting from the same IP address?
|
// TODO: How should we deal with multiple peers connecting from the same IP address?
|
||||||
if link.is_outbound() {
|
if link.is_outbound() {
|
||||||
let since = self.local_time();
|
let git = self.config.git_url.clone();
|
||||||
|
|
||||||
if let Some(peer) = self.peers.get_mut(&id) {
|
if let Some(peer) = self.peers.get_mut(&id) {
|
||||||
self.context
|
self.context
|
||||||
.write_all(peer.addr, [Message::hello(), Message::get_inventory([])]);
|
.write_all(peer.addr, [Message::hello(git), Message::get_inventory([])]);
|
||||||
|
|
||||||
peer.state = PeerState::Negotiated { since };
|
|
||||||
peer.attempts = 0;
|
peer.attempts = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -780,7 +784,7 @@ enum PeerState {
|
||||||
#[default]
|
#[default]
|
||||||
Initial,
|
Initial,
|
||||||
/// State after successful handshake.
|
/// State after successful handshake.
|
||||||
Negotiated { since: LocalTime },
|
Negotiated { since: LocalTime, git: Url },
|
||||||
/// When a peer is disconnected.
|
/// When a peer is disconnected.
|
||||||
Disconnected { since: LocalTime },
|
Disconnected { since: LocalTime },
|
||||||
}
|
}
|
||||||
|
|
@ -848,7 +852,7 @@ impl Peer {
|
||||||
debug!("Received {:?} from {}", &envelope.msg, self.id());
|
debug!("Received {:?} from {}", &envelope.msg, self.id());
|
||||||
|
|
||||||
match envelope.msg {
|
match envelope.msg {
|
||||||
Message::Hello { version } => {
|
Message::Hello { version, git } => {
|
||||||
if version != PROTOCOL_VERSION {
|
if version != PROTOCOL_VERSION {
|
||||||
return Err(PeerError::WrongVersion(version));
|
return Err(PeerError::WrongVersion(version));
|
||||||
}
|
}
|
||||||
|
|
@ -856,10 +860,12 @@ impl Peer {
|
||||||
// Nb. This is a very primitive handshake. Eventually we should have anyhow
|
// Nb. This is a very primitive handshake. Eventually we should have anyhow
|
||||||
// extra "acknowledgment" message sent when the `Hello` is well received.
|
// extra "acknowledgment" message sent when the `Hello` is well received.
|
||||||
if self.link.is_inbound() {
|
if self.link.is_inbound() {
|
||||||
ctx.write_all(self.addr, [Message::hello(), Message::get_inventory([])]);
|
let git = ctx.config.git_url.clone();
|
||||||
|
ctx.write_all(self.addr, [Message::hello(git), Message::get_inventory([])]);
|
||||||
}
|
}
|
||||||
self.state = PeerState::Negotiated {
|
self.state = PeerState::Negotiated {
|
||||||
since: ctx.clock.local_time(),
|
since: ctx.clock.local_time(),
|
||||||
|
git,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// TODO: Handle misbehavior.
|
// TODO: Handle misbehavior.
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use std::net;
|
use std::net;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use git_url::Url;
|
||||||
use log::*;
|
use log::*;
|
||||||
use nakamoto_net::simulator;
|
use nakamoto_net::simulator;
|
||||||
use nakamoto_net::Protocol as _;
|
use nakamoto_net::Protocol as _;
|
||||||
|
|
@ -120,10 +121,12 @@ where
|
||||||
|
|
||||||
pub fn connect_from(&mut self, remote: &net::SocketAddr) {
|
pub fn connect_from(&mut self, remote: &net::SocketAddr) {
|
||||||
let local = net::SocketAddr::new(self.ip, self.rng.u16(..));
|
let local = net::SocketAddr::new(self.ip, self.rng.u16(..));
|
||||||
|
let git = format!("file://{}.git", remote.ip());
|
||||||
|
let git = Url::from_bytes(git.as_bytes()).unwrap();
|
||||||
|
|
||||||
self.initialize();
|
self.initialize();
|
||||||
self.protocol.connected(*remote, &local, Link::Inbound);
|
self.protocol.connected(*remote, &local, Link::Inbound);
|
||||||
self.receive(remote, Message::hello());
|
self.receive(remote, Message::hello(git));
|
||||||
|
|
||||||
let mut msgs = self.messages(remote);
|
let mut msgs = self.messages(remote);
|
||||||
msgs.find(|m| matches!(m, Message::Hello { .. }))
|
msgs.find(|m| matches!(m, Message::Hello { .. }))
|
||||||
|
|
@ -144,7 +147,8 @@ where
|
||||||
msgs.find(|m| matches!(m, Message::GetInventory { .. }))
|
msgs.find(|m| matches!(m, Message::GetInventory { .. }))
|
||||||
.expect("`get-inventory` is sent");
|
.expect("`get-inventory` is sent");
|
||||||
|
|
||||||
self.receive(remote, Message::hello());
|
let git = self.config().git_url.clone();
|
||||||
|
self.receive(remote, Message::hello(git));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Drain outgoing messages sent from this peer to the remote address.
|
/// Drain outgoing messages sent from this peer to the remote address.
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue