From e0647610dbe8fb6d7692209772dfece2352f2acd Mon Sep 17 00:00:00 2001 From: Sosthene Date: Wed, 11 Dec 2024 23:27:52 +0100 Subject: [PATCH] get_state_for_commitments_root mut and immut version --- src/process.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/process.rs b/src/process.rs index 0e8cf9b..5b3fc95 100644 --- a/src/process.rs +++ b/src/process.rs @@ -335,7 +335,25 @@ impl Process { } } - pub fn get_state_for_commitments_root(&mut self, merkle_root: [u8; 32]) -> anyhow::Result<&mut ProcessState> { + pub fn get_state_for_commitments_root(&self, merkle_root: &str) -> anyhow::Result<&ProcessState> { + if self.get_number_of_states() == 0 { + // This should never happen, but we better get rid of it now + return Err(anyhow::Error::msg("process is empty".to_owned())); + } + + for p in self.get_latest_concurrent_states()? { + if p.is_empty() { + continue; + } + if merkle_root == p.merkle_root.as_str() { + return Ok(p); + } + } + + return Err(anyhow::Error::msg("No state for this merkle root")); + } + + pub fn get_state_for_commitments_root_mut(&mut self, merkle_root: &str) -> anyhow::Result<&mut ProcessState> { if self.get_number_of_states() == 0 { // This should never happen, but we better get rid of it now return Err(anyhow::Error::msg("process is empty".to_owned())); @@ -345,8 +363,7 @@ impl Process { if p.is_empty() { continue; } - let root = ::create_merkle_tree(&p.pcd_commitment).unwrap().root().unwrap(); - if merkle_root == root { + if merkle_root == p.merkle_root.as_str() { return Ok(p); } }