more breaking everything

This commit is contained in:
John Smith 2023-02-08 21:43:55 -05:00
parent 764b629714
commit 1f62d3836c
7 changed files with 80 additions and 29 deletions

View File

@ -10,6 +10,33 @@ use rkyv::{Archive as RkyvArchive, Deserialize as RkyvDeserialize, Serialize as
/// Cryptography version fourcc code
pub type CryptoKind = FourCC;
#[derive(
Clone,
Copy,
Debug,
Serialize,
Deserialize,
PartialOrd,
Ord,
PartialEq,
Eq,
Hash,
RkyvArchive,
RkyvSerialize,
RkyvDeserialize,
)]
#[archive_attr(repr(C), derive(CheckBytes))]
pub struct KeyPair {
pub key: PublicKey,
pub secret: SecretKey,
}
impl KeyPair {
pub fn new(key: PublicKey, secret: SecretKey) -> Self {
Self { key, secret }
}
}
#[derive(
Clone,
Copy,

View File

@ -70,10 +70,8 @@ pub(super) struct RoutingTableUnlockedInner {
config: VeilidConfig,
network_manager: NetworkManager,
/// The current node's public DHT key
node_id: PublicKey,
/// The current node's DHT key secret
node_id_secret: SecretKey,
/// The current node's public DHT keys and secrets
node_id_keypairs: BTreeMap<CryptoKind, KeyPair>,
/// Buckets to kick on our next kick task
kick_queue: Mutex<BTreeSet<usize>>,
/// Background process for computing statistics
@ -107,8 +105,19 @@ impl RoutingTable {
RoutingTableUnlockedInner {
config: config.clone(),
network_manager,
node_id: c.network.node_id.unwrap(),
node_id_secret: c.network.node_id_secret.unwrap(),
node_id_keypairs: c
.network
.routing_table
.node_ids
.iter()
.map(|(k, v)| {
(
*k,
KeyPair::new(v.node_id.unwrap(), v.node_id_secret.unwrap()),
)
})
.collect(),
kick_queue: Mutex::new(BTreeSet::default()),
rolling_transfers_task: TickTask::new(ROLLING_TRANSFERS_INTERVAL_SECS),
kick_buckets_task: TickTask::new(1),
@ -136,6 +145,9 @@ impl RoutingTable {
pub fn network_manager(&self) -> NetworkManager {
self.unlocked_inner.network_manager.clone()
}
pub fn crypto(&self) -> Crypto {
self.network_manager().crypto()
}
pub fn rpc_processor(&self) -> RPCProcessor {
self.network_manager().rpc_processor()
}
@ -149,12 +161,16 @@ impl RoutingTable {
f(&*self.unlocked_inner.config.get())
}
pub fn node_id(&self) -> PublicKey {
self.unlocked_inner.node_id
pub fn node_id(&self, kind: CryptoKind) -> PublicKey {
self.unlocked_inner.node_id_keypairs.get(&kind).unwrap().key
}
pub fn node_id_secret(&self) -> SecretKey {
self.unlocked_inner.node_id_secret
pub fn node_id_secret(&self, kind: CryptoKind) -> SecretKey {
self.unlocked_inner
.node_id_keypairs
.get(&kind)
.unwrap()
.secret
}
/////////////////////////////////////
@ -453,8 +469,10 @@ impl RoutingTable {
self.inner.write().purge_last_connections();
}
fn find_bucket_index(&self, node_id: PublicKey) -> usize {
distance(&node_id, &self.unlocked_inner.node_id)
fn find_bucket_index(&self, node_id: TypedKey) -> usize {
let crypto = self.crypto().get(node_id.kind).unwrap();
.distance(&node_id, &self.unlocked_inner.node_id)
.first_nonzero_bit()
.unwrap()
}

View File

@ -16,7 +16,7 @@ pub struct RouteHopData {
#[derive(Clone, Debug)]
pub enum RouteNode {
/// Route node is optimized, no contact method information as this node id has been seen before
NodeId(NodeId),
NodeId(TypedKey),
/// Route node with full contact method information to ensure the peer is reachable
PeerInfo(PeerInfo),
}
@ -57,14 +57,14 @@ pub enum PrivateRouteHops {
#[derive(Clone, Debug)]
pub struct PrivateRoute {
/// The public key used for the entire route
pub public_key: PublicKey,
pub public_key: TypedKey,
pub hop_count: u8,
pub hops: PrivateRouteHops,
}
impl PrivateRoute {
/// Empty private route is the form used when receiving the last hop
pub fn new_empty(public_key: PublicKey) -> Self {
pub fn new_empty(public_key: TypedKey) -> Self {
Self {
public_key,
hop_count: 0,
@ -72,7 +72,7 @@ impl PrivateRoute {
}
}
/// Stub route is the form used when no privacy is required, but you need to specify the destination for a safety route
pub fn new_stub(public_key: PublicKey, node: RouteNode) -> Self {
pub fn new_stub(public_key: TypedKey, node: RouteNode) -> Self {
Self {
public_key,
hop_count: 1,

View File

@ -52,15 +52,18 @@ impl RoutingTableInner {
pub fn network_manager(&self) -> NetworkManager {
self.unlocked_inner.network_manager.clone()
}
pub fn crypto(&self) -> Crypto {
self.network_manager().crypto()
}
pub fn rpc_processor(&self) -> RPCProcessor {
self.network_manager().rpc_processor()
}
pub fn node_id(&self) -> PublicKey {
pub fn node_id(&self, kind: CryptoKind) -> PublicKey {
self.unlocked_inner.node_id
}
pub fn node_id_secret(&self) -> SecretKey {
pub fn node_id_secret(&self, kind: CryptoKind) -> SecretKey {
self.unlocked_inner.node_id_secret
}
@ -654,7 +657,7 @@ impl RoutingTableInner {
&mut self,
outer_self: RoutingTable,
routing_domain: RoutingDomain,
node_id: PublicKey,
node_ids: Vec<TypedKey>,
signed_node_info: SignedNodeInfo,
allow_invalid: bool,
) -> Option<NodeRef> {

View File

@ -34,6 +34,7 @@ pub fn encode_signal_info(
pub fn decode_signal_info(
reader: &veilid_capnp::operation_signal::Reader,
crypto: Crypto,
) -> Result<SignalInfo, RPCError> {
Ok(
match reader
@ -52,7 +53,7 @@ pub fn decode_signal_info(
let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol(
"invalid peer info in hole punch signal info",
))?;
let peer_info = decode_peer_info(&pi_reader)?;
let peer_info = decode_peer_info(&pi_reader, crypto)?;
SignalInfo::HolePunch { receipt, peer_info }
}
@ -68,7 +69,7 @@ pub fn decode_signal_info(
let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol(
"invalid peer info in reverse connect signal info",
))?;
let peer_info = decode_peer_info(&pi_reader)?;
let peer_info = decode_peer_info(&pi_reader, crypto)?;
SignalInfo::ReverseConnect { receipt, peer_info }
}

View File

@ -5,6 +5,7 @@ pub fn encode_value_data(
builder: &mut veilid_capnp::value_data::Builder,
) -> Result<(), RPCError> {
builder.set_data(&value_data.data);
builder.set_schema(u32::from_be_bytes(value_data.schema.0));
builder.set_seq(value_data.seq);
Ok(())
}
@ -12,5 +13,6 @@ pub fn encode_value_data(
pub fn decode_value_data(reader: &veilid_capnp::value_data::Reader) -> Result<ValueData, RPCError> {
let data = reader.get_data().map_err(RPCError::protocol)?.to_vec();
let seq = reader.get_seq();
Ok(ValueData { data, seq })
let schema = FourCC::from(reader.get_schema().to_be_bytes());
Ok(ValueData { data, schema, seq })
}

View File

@ -332,20 +332,20 @@ pub struct VeilidState {
)]
#[archive_attr(repr(C), derive(CheckBytes))]
pub struct ValueData {
pub data: Vec<u8>,
pub schema: ValueSchema,
pub seq: u32,
pub schema: ValueSchema,
pub data: Vec<u8>,
}
impl ValueData {
pub fn new(data: Vec<u8>, schema: ValueSchema) -> Self {
pub fn new(schema: ValueSchema, data: Vec<u8>) -> Self {
Self {
data,
schema,
seq: 0,
schema,
data,
}
}
pub fn new_with_seq(data: Vec<u8>, schema: ValueSchema, seq: u32) -> Self {
Self { data, schema, seq }
pub fn new_with_seq(seq: u32, schema: ValueSchema, data: Vec<u8>) -> Self {
Self { seq, schema, data }
}
pub fn change(&mut self, data: Vec<u8>) {
self.data = data;