Better handling of process updates

This commit is contained in:
NicolasCantu 2025-01-24 12:42:38 +01:00 committed by Nicolas Cantu
parent 010afc9f45
commit 90146df13b

View File

@ -159,11 +159,18 @@ fn handle_existing_commitment(outpoint: OutPoint, commit_msg: CommitMessage) ->
.get_mut(&outpoint)
.ok_or(Error::msg(format!("Commitment not found: {}", outpoint)))?;
if commit_msg.validation_tokens.is_empty() {
register_new_state(process, commit_msg)
} else {
match register_new_state(process, &commit_msg) {
Ok(new_state_id) => log::debug!("Registering new state for process {} with state id {}", outpoint, new_state_id),
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);
// If the validation succeed, we return a new tip
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(())
// }
// Register a new state when validation tokens are empty
fn register_new_state(commitment: &mut Process, commit_msg: CommitMessage) -> Result<OutPoint> {
let concurrent_states = commitment.get_latest_concurrent_states()?;
// Register a new state
fn register_new_state(process: &mut Process, commit_msg: &CommitMessage) -> Result<String> {
let concurrent_states = process.get_latest_concurrent_states()?;
let (empty_state, actual_states) = concurrent_states.split_last().unwrap();
let current_outpoint = empty_state.commited_in;
@ -227,21 +234,21 @@ fn register_new_state(commitment: &mut Process, commit_msg: CommitMessage) -> Re
.iter()
.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
let roles_only_map = json!({ "roles": serde_json::to_value(&commit_msg.roles)? });
let new_state = ProcessState {
commited_in: current_outpoint,
pcd_commitment: commit_msg.pcd_commitment,
pcd_commitment: commit_msg.pcd_commitment.clone(),
encrypted_pcd: roles_only_map,
state_id: new_state_id,
state_id: new_state_id.clone(),
..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