Commit dfc43543 authored by Éloïs's avatar Éloïs
Browse files

[ref] dbs: DunpNodeIdV1Db must be indexed by pubkey

parent ce88f841
......@@ -26,24 +26,24 @@ pub struct DunpNodeIdV1Db([u8; 37]); // uuid ++ pubkey
impl DunpNodeIdV1Db {
pub fn new(uuid: u32, pubkey: PublicKey) -> Self {
let mut buffer = uninit_array![u8; 37];
let (uuid_buffer, pubkey_buffer) = buffer.as_out().split_at_out(4);
let (pubkey_buffer, uuid_buffer) = buffer.as_out().split_at_out(33);
uuid_buffer.copy_from_slice(&uuid.to_be_bytes()[..]);
pubkey_buffer.copy_from_slice(pubkey.as_ref());
uuid_buffer.copy_from_slice(&uuid.to_be_bytes()[..]);
Self(unsafe { std::mem::transmute(buffer) })
}
pub fn get_uuid(&self) -> u32 {
let mut buffer = uninit_array![u8; 4];
buffer.as_out().copy_from_slice(&self.0[..4]);
buffer.as_out().copy_from_slice(&self.0[33..]);
u32::from_be_bytes(unsafe { std::mem::transmute(buffer) })
}
pub fn get_pubkey(&self) -> PublicKey {
let mut buffer = uninit_array![u8; 33];
buffer.as_out().copy_from_slice(&self.0[4..]);
buffer.as_out().copy_from_slice(&self.0[..33]);
let bytes: [u8; 33] = unsafe { std::mem::transmute(buffer) };
PublicKey::try_from(&bytes[..]).unwrap_or_else(|_| unreachable!())
......@@ -93,3 +93,19 @@ impl ExplorableKey for DunpNodeIdV1Db {
Ok(self.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serde() {
let node_id = DunpNodeIdV1Db::new(42, PublicKey::default());
assert_eq!(node_id.get_uuid(), 42);
assert_eq!(node_id.get_pubkey(), PublicKey::default());
let mut node_id_ = DunpNodeIdV1Db([0u8; 37]);
node_id_.0[32] = 32;
node_id_.0[36] = 42;
assert_eq!(node_id_, node_id)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment