Introduce foreigner network (#25)
* support network identity for instance * introduce foreign network foreign network allow a node serving as one public node. other nodes can connect to this node to discover peers and exchange route info.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
use std::fmt::Debug;
|
||||
|
||||
use rkyv::{Archive, Deserialize, Serialize};
|
||||
use tokio_util::bytes::Bytes;
|
||||
|
||||
use crate::common::rkyv_util::{decode_from_bytes, encode_to_bytes};
|
||||
use crate::common::{
|
||||
global_ctx::NetworkIdentity,
|
||||
rkyv_util::{decode_from_bytes, encode_to_bytes},
|
||||
};
|
||||
|
||||
const MAGIC: u32 = 0xd1e1a5e1;
|
||||
const VERSION: u32 = 1;
|
||||
@@ -44,6 +49,43 @@ impl From<&ArchivedUUID> for UUID {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Archive, Deserialize, Serialize)]
|
||||
#[archive(compare(PartialEq), check_bytes)]
|
||||
// Derives can be passed through to the generated type:
|
||||
pub struct NetworkIdentityForPacket(Vec<u8>);
|
||||
|
||||
impl From<NetworkIdentity> for NetworkIdentityForPacket {
|
||||
fn from(network: NetworkIdentity) -> Self {
|
||||
Self(bincode::serialize(&network).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<NetworkIdentityForPacket> for NetworkIdentity {
|
||||
fn from(network: NetworkIdentityForPacket) -> Self {
|
||||
bincode::deserialize(&network.0).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&ArchivedNetworkIdentityForPacket> for NetworkIdentity {
|
||||
fn from(network: &ArchivedNetworkIdentityForPacket) -> Self {
|
||||
NetworkIdentityForPacket(network.0.to_vec()).into()
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for NetworkIdentityForPacket {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let network: NetworkIdentity = bincode::deserialize(&self.0).unwrap();
|
||||
write!(f, "{:?}", network)
|
||||
}
|
||||
}
|
||||
|
||||
impl Debug for ArchivedNetworkIdentityForPacket {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let network: NetworkIdentity = bincode::deserialize(&self.0).unwrap();
|
||||
write!(f, "{:?}", network)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Archive, Deserialize, Serialize, Debug)]
|
||||
#[archive(compare(PartialEq), check_bytes)]
|
||||
// Derives can be passed through to the generated type:
|
||||
@@ -53,7 +95,7 @@ pub struct HandShake {
|
||||
pub my_peer_id: UUID,
|
||||
pub version: u32,
|
||||
pub features: Vec<String>,
|
||||
// pub interfaces: Vec<String>,
|
||||
pub network_identity: NetworkIdentityForPacket,
|
||||
}
|
||||
|
||||
#[derive(Archive, Deserialize, Serialize, Debug)]
|
||||
@@ -116,7 +158,7 @@ impl From<Packet> for Bytes {
|
||||
}
|
||||
|
||||
impl Packet {
|
||||
pub fn new_handshake(from_peer: uuid::Uuid) -> Self {
|
||||
pub fn new_handshake(from_peer: uuid::Uuid, network: &NetworkIdentity) -> Self {
|
||||
Packet {
|
||||
from_peer: from_peer.into(),
|
||||
to_peer: None,
|
||||
@@ -125,6 +167,7 @@ impl Packet {
|
||||
my_peer_id: from_peer.into(),
|
||||
version: VERSION,
|
||||
features: Vec::new(),
|
||||
network_identity: network.clone().into(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user