Better handling of process updates

This commit is contained in:
NicolasCantu 2025-01-24 12:42:38 +01:00
parent 3ea25e542a
commit c3aba61be2

View File

@ -159,11 +159,18 @@ fn handle_existing_commitment(outpoint: OutPoint, commit_msg: CommitMessage) ->
.get_mut(&outpoint) .get_mut(&outpoint)
.ok_or(Error::msg(format!("Commitment not found: {}", outpoint)))?; .ok_or(Error::msg(format!("Commitment not found: {}", outpoint)))?;
if commit_msg.validation_tokens.is_empty() { match register_new_state(process, &commit_msg) {
register_new_state(process, commit_msg) Ok(new_state_id) => log::debug!("Registering new state for process {} with state id {}", outpoint, new_state_id),
} else { Err(existing_state_id) => log::debug!("State {} already exists", existing_state_id)
}
if commit_msg.validation_tokens.len() > 0 {
log::debug!("Received commit_msg with {} validation tokens for process {}", commit_msg.validation_tokens.len(), outpoint); log::debug!("Received commit_msg with {} validation tokens for process {}", commit_msg.validation_tokens.len(), outpoint);
// If the validation succeed, we return a new tip
process_validation(process, commit_msg) process_validation(process, commit_msg)
} else {
// We just return the current outpoint
process.get_process_tip()
} }
} }
@ -214,9 +221,9 @@ pub fn dump_cached_processes() -> Result<(), anyhow::Error> {
// Ok(()) // Ok(())
// } // }
// Register a new state when validation tokens are empty // Register a new state
fn register_new_state(commitment: &mut Process, commit_msg: CommitMessage) -> Result<OutPoint> { fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Result<String> {
let concurrent_states = commitment.get_latest_concurrent_states()?; let concurrent_states = process.get_latest_concurrent_states()?;
let (empty_state, actual_states) = concurrent_states.split_last().unwrap(); let (empty_state, actual_states) = concurrent_states.split_last().unwrap();
let current_outpoint = empty_state.commited_in; let current_outpoint = empty_state.commited_in;
@ -227,21 +234,21 @@ fn register_new_state(commitment: &mut Process, commit_msg: CommitMessage) -> Re
.iter() .iter()
.any(|state| state.state_id == new_state_id) .any(|state| state.state_id == new_state_id)
{ {
return Err(Error::msg("Proposed state already exists")); return Err(Error::msg(new_state_id));
} }
// Add the new state // Add the new state
let roles_only_map = json!({ "roles": serde_json::to_value(&commit_msg.roles)? }); let roles_only_map = json!({ "roles": serde_json::to_value(&commit_msg.roles)? });
let new_state = ProcessState { let new_state = ProcessState {
commited_in: current_outpoint, commited_in: current_outpoint,
pcd_commitment: commit_msg.pcd_commitment, pcd_commitment: commit_msg.pcd_commitment.clone(),
encrypted_pcd: roles_only_map, encrypted_pcd: roles_only_map,
state_id: new_state_id, state_id: new_state_id.clone(),
..Default::default() ..Default::default()
}; };
commitment.insert_concurrent_state(new_state)?; process.insert_concurrent_state(new_state)?;
Ok(current_outpoint) Ok(new_state_id)
} }
// Process validation for a state with validation tokens // Process validation for a state with validation tokens