Update handle_prd

This commit is contained in:
Sosthene 2024-12-17 23:55:28 +01:00
parent d54981f66e
commit ee777bbcbc

View File

@ -860,8 +860,15 @@ fn handle_prd(
return Err(AnyhowError::msg("Received update for a state we already know"));
}
let commited_in = OutPoint::from_str(&prd.root_commitment)?;
// Extract the roles from the payload
let proposal_roles: HashMap<String, RoleDefinition> = serde_json::from_str(&prd.payload)?;
// TODO: check that the role in the prd has the right commitment
let new_state = ProcessState {
commited_in: OutPoint::from_str(&prd.root_commitment)?,
commited_in,
pcd_commitment: prd.pcd_commitments,
merkle_root: update_merkle_root.clone(),
keys: prd.keys,
@ -870,9 +877,18 @@ fn handle_prd(
// Compute the diffs
// At this point we don't have the encrypted values
// But it can still be useful to track diffs
let diffs = create_diffs(&relevant_process, &new_state)?;
// Take the roles from the last validated state
let mut roles = HashMap::new();
if let Some(last_state) = relevant_process.get_latest_commited_state() {
let decrypted_last_state = last_state.decrypt_pcd()?;
roles = Value::Object(decrypted_last_state).extract_roles()?;
} else {
// We don't have commited state yet, let's take the current roles
roles = proposal_roles;
}
relevant_process.insert_concurrent_state(new_state);
let updated_process = UpdatedProcess {
@ -903,6 +919,10 @@ fn handle_prd(
let updated_state = to_update.clone();
let clear_state = to_update.decrypt_pcd()?;
let roles = Value::Object(clear_state).extract_roles()?;
// We must return an update of the process
let updated_process = UpdatedProcess {
commitment_tx: OutPoint::from_str(&prd.root_commitment)?,
@ -911,8 +931,15 @@ fn handle_prd(
..Default::default()
};
let commit_msg = CommitMessage::new_update_commitment(
OutPoint::from_str(&prd.root_commitment)?,
updated_state.pcd_commitment,
roles
);
return Ok(ApiReturn {
updated_process: Some(updated_process),
commit_to_send: Some(commit_msg),
..Default::default()
});
}