From 9632ef24e3ed2e15509475f1378231990ff8b8dc Mon Sep 17 00:00:00 2001 From: Sosthene Date: Fri, 4 Oct 2024 09:22:58 +0200 Subject: [PATCH] Update Process api --- src/process.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-) diff --git a/src/process.rs b/src/process.rs index b785313..f837c37 100644 --- a/src/process.rs +++ b/src/process.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use serde_json::{Map, Value}; use sp_client::{bitcoin::OutPoint, silentpayments::utils::SilentPaymentAddress}; -use crate::{crypto::AnkSharedSecretHash, prd::Prd, signature::Proof, MutexExt}; +use crate::{crypto::AnkSharedSecretHash, pcd::{AnkPcdHash, Pcd, RoleDefinition, ValidationRule}, prd::Prd, signature::Proof, MutexExt}; #[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)] pub struct ProcessState { @@ -48,11 +48,11 @@ impl Process { self.states.push(state); } - pub fn get_status_at(&self, index: usize) -> Option<&ProcessState> { + pub fn get_state_at(&self, index: usize) -> Option<&ProcessState> { self.states.get(index) } - pub fn get_status_at_mut(&mut self, index: usize) -> Option<&mut ProcessState> { + pub fn get_state_at_mut(&mut self, index: usize) -> Option<&mut ProcessState> { self.states.get_mut(index) } @@ -64,6 +64,78 @@ impl Process { self.states.last_mut() } + pub fn get_previous_state(&self, current_state: &ProcessState) -> Option<&ProcessState> { + // Find the index of the current state + let current_index = self.states.iter().position(|state| state == current_state)?; + + // Check if there is a previous state + if current_index > 0 { + // Create a new Process with the previous state + let previous_state = self.get_state_at(current_index-1).unwrap(); + Some(&previous_state) + } else { + None // No previous state exists + } + } + + /// 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 + pub fn get_latest_concurrent_states(&self) -> Vec<&ProcessState> { + let mut states = vec![]; + let latest_state = self.get_latest_state(); + + if latest_state.is_none() { + return states; + } + + let latest_state_outpoint = latest_state.unwrap().commited_in; + + // We iterate backwards until we find a state that has a different commited_in + for state in self.states.iter().rev() { + if state.commited_in != latest_state_outpoint { + break; + } + states.push(state); + } + + states + } + + pub fn get_latest_concurrent_states_mut(&mut self) -> Vec<&mut ProcessState> { + let mut states = vec![]; + let latest_state = self.get_latest_state(); + + if latest_state.is_none() { + return states; + } + + let latest_state_outpoint = latest_state.unwrap().commited_in; + + // We iterate backwards until we find a state that has a different commited_in + for state in self.states.iter_mut().rev() { + if state.commited_in != latest_state_outpoint { + break; + } + states.push(state); + } + + states + } + + pub fn remove_latest_concurrent_states(&mut self) -> Vec { + if self.states.is_empty() { + return vec![]; + } + + let latest_state = self.get_latest_state().unwrap(); + let latest_outpoint = latest_state.commited_in; + + let pos = self.states.iter().position(|s| s.commited_in == latest_outpoint).unwrap(); + + self.states.split_off(pos) + } + pub fn insert_impending_request(&mut self, request: Prd) { self.impending_requests.push(request); } @@ -75,6 +147,18 @@ impl Process { pub fn get_impending_requests_mut(&mut self) -> Vec<&mut Prd> { self.impending_requests.iter_mut().collect() } + + pub fn get_state_index(&self, state: &ProcessState) -> Option { + // Get the commited_in value of the provided state + let target_commited_in = state.commited_in; + + // Find the index of the first state with the same commited_in value + self.states.iter().position(|s| s.commited_in == target_commited_in) + } + + pub fn get_number_of_states(&self) -> usize { + self.states.len() + } } pub static CACHEDPROCESSES: OnceLock>> = OnceLock::new();