node: Start sketching out node announcements
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
parent
ad1f25b3f5
commit
77ced78b31
|
|
@ -4,6 +4,8 @@ use ed25519_consensus as ed25519;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
pub use ed25519::Signature;
|
||||||
|
|
||||||
pub trait Signer {
|
pub trait Signer {
|
||||||
/// Return this signer's public/verification key.
|
/// Return this signer's public/verification key.
|
||||||
fn public_key(&self) -> &PublicKey;
|
fn public_key(&self) -> &PublicKey;
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::{collections::VecDeque, fmt, io, net, net::IpAddr};
|
use std::{collections::VecDeque, fmt, io, net, net::IpAddr};
|
||||||
|
|
||||||
use ed25519_consensus::VerificationKey;
|
|
||||||
use fastrand::Rng;
|
use fastrand::Rng;
|
||||||
use git_url::Url;
|
use git_url::Url;
|
||||||
use log::*;
|
use log::*;
|
||||||
|
|
@ -29,12 +28,28 @@ pub type Routing = HashMap<ProjId, HashSet<NodeId>>;
|
||||||
/// Seconds since epoch.
|
/// Seconds since epoch.
|
||||||
pub type Timestamp = u64;
|
pub type Timestamp = u64;
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
|
// TODO: We should check the length and charset when deserializing.
|
||||||
|
pub struct Hostname(String);
|
||||||
|
|
||||||
/// Peer public protocol address.
|
/// Peer public protocol address.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
pub enum Address {
|
pub enum Address {
|
||||||
Tor { key: VerificationKey, port: u16 },
|
/// Tor V3 onion address.
|
||||||
Tcp { addr: net::SocketAddr },
|
Onion {
|
||||||
Dns { host: String, port: u16 },
|
key: crypto::PublicKey,
|
||||||
|
port: u16,
|
||||||
|
checksum: u16,
|
||||||
|
version: u8,
|
||||||
|
},
|
||||||
|
Ip {
|
||||||
|
ip: net::IpAddr,
|
||||||
|
port: u16,
|
||||||
|
},
|
||||||
|
Hostname {
|
||||||
|
host: Hostname,
|
||||||
|
port: u16,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const NETWORK_MAGIC: u32 = 0x819b43d9;
|
pub const NETWORK_MAGIC: u32 = 0x819b43d9;
|
||||||
|
|
@ -64,6 +79,23 @@ pub struct Envelope {
|
||||||
pub msg: Message,
|
pub msg: Message,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Advertized node feature. Signals what services the node supports.
|
||||||
|
pub type NodeFeatures = [u8; 32];
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
|
||||||
|
pub struct NodeAnnouncement {
|
||||||
|
/// Node identifier.
|
||||||
|
id: NodeId,
|
||||||
|
/// Advertized features.
|
||||||
|
features: NodeFeatures,
|
||||||
|
/// Monotonic timestamp.
|
||||||
|
timestamp: Timestamp,
|
||||||
|
/// Non-unique alias. Must be valid UTF-8.
|
||||||
|
alias: [u8; 32],
|
||||||
|
/// Announced addresses.
|
||||||
|
addresses: Vec<Address>,
|
||||||
|
}
|
||||||
|
|
||||||
/// Message payload.
|
/// Message payload.
|
||||||
/// These are the messages peers send to each other.
|
/// These are the messages peers send to each other.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
|
@ -77,10 +109,12 @@ pub enum Message {
|
||||||
addrs: Vec<Address>,
|
addrs: Vec<Address>,
|
||||||
git: Url,
|
git: Url,
|
||||||
},
|
},
|
||||||
/// Get node addresses from a peer.
|
Node {
|
||||||
GetAddrs,
|
/// Signature over the announcement, by the node being announced.
|
||||||
/// Send node addresses to a peer. Sent in response to [`Message::GetAddrs`].
|
signature: crypto::Signature,
|
||||||
Addrs { addrs: Vec<net::SocketAddr> },
|
/// Unsigned node announcement.
|
||||||
|
announcement: NodeAnnouncement,
|
||||||
|
},
|
||||||
/// Get a peer's inventory.
|
/// Get a peer's inventory.
|
||||||
GetInventory { ids: Vec<ProjId> },
|
GetInventory { ids: Vec<ProjId> },
|
||||||
/// Send our inventory to a peer. Sent in response to [`Message::GetInventory`].
|
/// Send our inventory to a peer. Sent in response to [`Message::GetInventory`].
|
||||||
|
|
@ -1035,12 +1069,7 @@ impl Peer {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(PeerState::Negotiated { .. }, Message::GetAddrs) => {
|
(PeerState::Negotiated { .. }, Message::Node { .. }) => {
|
||||||
// TODO: Send peer addresses.
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
(PeerState::Negotiated { .. }, Message::Addrs { .. }) => {
|
|
||||||
// TODO: Update address book.
|
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
(PeerState::Negotiated { .. }, Message::Hello { .. }) => {
|
(PeerState::Negotiated { .. }, Message::Hello { .. }) => {
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,10 @@ where
|
||||||
Message::hello(
|
Message::hello(
|
||||||
peer.id(),
|
peer.id(),
|
||||||
self.local_time().as_secs(),
|
self.local_time().as_secs(),
|
||||||
vec![Address::Tcp { addr: remote }],
|
vec![Address::Ip {
|
||||||
|
ip: remote.ip(),
|
||||||
|
port: remote.port(),
|
||||||
|
}],
|
||||||
git,
|
git,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue