From 00327b2e463a81c280b0fdb20ca7e7756d252dfb Mon Sep 17 00:00:00 2001 From: Sosthene Date: Mon, 25 Nov 2024 23:24:05 +0100 Subject: [PATCH] Add get_state_for_commitment_root --- src/process.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/process.rs b/src/process.rs index d670679..a322252 100644 --- a/src/process.rs +++ b/src/process.rs @@ -256,6 +256,25 @@ impl Process { } } + pub fn get_state_for_commitments_root(&mut self, merkle_root: [u8; 32]) -> 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())); + } + + for p in self.get_latest_concurrent_states_mut()? { + if p.is_empty() { + continue; + } + let root = ::create_merkle_tree(&p.pcd_commitment).unwrap().root().unwrap(); + if merkle_root == root { + return Ok(p); + } + } + + return Err(anyhow::Error::msg("No state for this merkle root")); + } + /// This is useful when multiple unvalidated states are pending waiting for enough validations /// It returns the latest state and all the previous states that have the same commited_in /// It means that all the returned states are unvalidated and except for the one that get validated they will be pruned