Handle prd updates for a state we already know about but don't have the keys

This commit is contained in:
Sosthene 2025-11-19 21:21:58 +01:00
parent d80832ca99
commit 42355506bb

View File

@ -847,27 +847,47 @@ fn handle_prd(
PrdType::Update => { PrdType::Update => {
// Compute the merkle tree root for the proposed new state to see if we already know about it // Compute the merkle tree root for the proposed new state to see if we already know about it
let update_merkle_root = prd.pcd_commitments.create_merkle_tree()?.root().ok_or(AnyhowError::msg("Invalid merkle tree"))?; let update_merkle_root = prd.pcd_commitments.create_merkle_tree()?.root().ok_or(AnyhowError::msg("Invalid merkle tree"))?;
if relevant_process.get_state_for_id(&update_merkle_root).is_ok() { let updated_state: &ProcessState = if let Ok(existing_state) = relevant_process.get_state_for_id_mut(&update_merkle_root) {
// We already know about that state // We already know about that state, if we also have the keys we can just stop here
return Err(AnyhowError::msg("Received update for a state we already know")); if !existing_state.keys.is_empty() {
} // We check that the keys are the same, just in case
if existing_state.keys == prd.keys {
debug!("Received update for a state we already know and have the same keys");
return Ok(ApiReturn::default());
} else {
// We merge the keys that we have with the ones that we receive
debug!("Received update for a state we already know and have different keys, merging them");
for (key, value) in prd.keys.iter() {
if !existing_state.keys.contains_key(key) { // We don't want to override existing keys
existing_state.keys.insert(key.clone(), value.clone());
}
}
}
} else {
// We don't have any keys for this state, we can just update it
existing_state.keys = prd.keys.clone();
}
existing_state
} else {
let commited_in = relevant_process.get_process_tip()?;
let commited_in = relevant_process.get_process_tip()?; let new_state = ProcessState {
commited_in,
pcd_commitment: prd.pcd_commitments,
state_id: update_merkle_root.clone(),
keys: prd.keys,
roles: prd.roles,
public_data: prd.public_data,
..Default::default()
};
let new_state = ProcessState { relevant_process.insert_concurrent_state(new_state)?;
commited_in,
pcd_commitment: prd.pcd_commitments, relevant_process.get_state_for_id(&new_state.state_id).expect("New state should be inserted")
state_id: update_merkle_root.clone(),
keys: prd.keys,
roles: prd.roles,
public_data: prd.public_data,
..Default::default()
}; };
// Compute the diffs // Compute the diffs
let diffs = create_diffs(&lock_local_device()?, &relevant_process, &new_state, members_list)?; let diffs = create_diffs(&lock_local_device()?, &relevant_process, &updated_state, members_list)?;
relevant_process.insert_concurrent_state(new_state)?;
let updated_process = UpdatedProcess { let updated_process = UpdatedProcess {
process_id: outpoint, process_id: outpoint,