From fe1e4c7d1a6535d469f01bf15215a8eeaf9e64ea Mon Sep 17 00:00:00 2001 From: bitful-pannul Date: Fri, 15 Dec 2023 20:26:50 -0300 Subject: [PATCH] state: do not hash process_ids for keys --- src/state.rs | 18 +++++++++++------- src/types.rs | 7 ------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/state.rs b/src/state.rs index c8897045..0ebc152c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -32,8 +32,8 @@ pub async fn load_state( let db = DB::open_default(state_path).unwrap(); let mut process_map: ProcessMap = HashMap::new(); - let kernel_id = KERNEL_PROCESS_ID.to_hash(); - match db.get(kernel_id) { + let kernel_id = process_to_vec(KERNEL_PROCESS_ID.clone()); + match db.get(&kernel_id) { Ok(Some(value)) => { process_map = bincode::deserialize::(&value).unwrap(); } @@ -47,7 +47,7 @@ pub async fn load_state( .await .unwrap(); - db.put(kernel_id, bincode::serialize(&process_map).unwrap()) + db.put(&kernel_id, bincode::serialize(&process_map).unwrap()) .unwrap(); } Err(e) => { @@ -145,8 +145,8 @@ async fn handle_request( let (ipc, bytes) = match action { StateAction::SetState(process_id) => { - let key = process_id.to_hash(); - // TODO consistency with to_stirngs + let key = process_to_vec(process_id); + let Some(ref payload) = payload else { return Err(StateError::BadBytes { action: "SetState".into(), @@ -157,7 +157,7 @@ async fn handle_request( (serde_json::to_vec(&StateResponse::SetState).unwrap(), None) } StateAction::GetState(process_id) => { - let key = process_id.to_hash(); + let key = process_to_vec(process_id.clone()); match db.get(key) { Ok(Some(value)) => ( serde_json::to_vec(&StateResponse::GetState).unwrap(), @@ -178,7 +178,7 @@ async fn handle_request( } } StateAction::DeleteState(process_id) => { - let key = process_id.to_hash(); + let key = process_to_vec(process_id); match db.delete(key) { Ok(_) => ( serde_json::to_vec(&StateResponse::DeleteState).unwrap(), @@ -623,6 +623,10 @@ fn make_error_message(our_name: String, km: &KernelMessage, error: StateError) - } } +fn process_to_vec(process: ProcessId) -> Vec { + process.to_string().as_bytes().to_vec() +} + impl From for VfsError { fn from(err: std::io::Error) -> Self { VfsError::IOError { diff --git a/src/types.rs b/src/types.rs index df18830c..be973cf8 100644 --- a/src/types.rs +++ b/src/types.rs @@ -153,13 +153,6 @@ impl ProcessId { pub fn publisher(&self) -> &str { &self.publisher_node } - pub fn to_hash(&self) -> [u8; 32] { - let mut hasher = blake3::Hasher::new(); - hasher.update(self.process_name.as_bytes()); - hasher.update(self.package_name.as_bytes()); - hasher.update(self.publisher_node.as_bytes()); - hasher.finalize().into() - } pub fn en_wit(&self) -> wit::ProcessId { wit::ProcessId { process_name: self.process_name.clone(),