[bug] prd response doesn't trigger infinite responses loop
This commit is contained in:
parent
de5eb1b8cd
commit
49637d9b40
59
src/api.rs
59
src/api.rs
@ -905,38 +905,59 @@ fn handle_prd(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
PrdType::Response => {
|
PrdType::Response => {
|
||||||
let mut to_update = relevant_process
|
let update_state_id = prd.pcd_commitments.create_merkle_tree()?.root().ok_or(AnyhowError::msg("Invalid merkle tree"))?.to_lower_hex_string();
|
||||||
.get_latest_concurrent_states_mut()?
|
let mut to_update = relevant_process.get_state_for_id_mut(&update_state_id)?;
|
||||||
.into_iter()
|
|
||||||
.find(|r| {
|
|
||||||
r.pcd_commitment == prd.pcd_commitments
|
|
||||||
})
|
|
||||||
.ok_or(anyhow::Error::msg("Original request not found"))?;
|
|
||||||
|
|
||||||
to_update
|
let new_validations = prd.validation_tokens;
|
||||||
.validation_tokens
|
let mut to_add: Vec<&Proof> = vec![];
|
||||||
.extend(prd.validation_tokens);
|
|
||||||
|
for token in &new_validations {
|
||||||
|
let key = token.get_key();
|
||||||
|
|
||||||
|
// Check if the token key already exists
|
||||||
|
let already_exists = to_update
|
||||||
|
.validation_tokens
|
||||||
|
.iter()
|
||||||
|
.any(|existing_token| existing_token.get_key() == key);
|
||||||
|
|
||||||
|
if !already_exists {
|
||||||
|
debug!("Adding token with key {}", key);
|
||||||
|
to_add.push(token);
|
||||||
|
} else {
|
||||||
|
debug!("Token with key {} already exists, skipping", key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's no new proofs return early
|
||||||
|
if to_add.is_empty() {
|
||||||
|
return Err(AnyhowError::msg("No new validation tokens found in prd response"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// We add the new tokens and return all that was updated
|
||||||
|
to_update.validation_tokens.extend(to_add);
|
||||||
|
|
||||||
let updated_state = to_update.clone();
|
let updated_state = to_update.clone();
|
||||||
|
|
||||||
let clear_state = to_update.decrypt_pcd()?;
|
let clear_state = to_update.decrypt_pcd()?;
|
||||||
|
|
||||||
let roles = Value::Object(clear_state).extract_roles()?;
|
let roles = Value::Object(clear_state).extract_roles()?;
|
||||||
|
let modified_state = Some(to_update.state_id.clone());
|
||||||
|
let mut commit_msg = CommitMessage::new_update_commitment(
|
||||||
|
OutPoint::from_str(&prd.root_commitment)?,
|
||||||
|
updated_state.pcd_commitment,
|
||||||
|
roles
|
||||||
|
);
|
||||||
|
|
||||||
|
commit_msg.set_validation_tokens(updated_state.validation_tokens);
|
||||||
|
|
||||||
// We must return an update of the process
|
// We must return an update of the process
|
||||||
let updated_process = UpdatedProcess {
|
let updated_process = UpdatedProcess {
|
||||||
commitment_tx: OutPoint::from_str(&prd.root_commitment)?,
|
commitment_tx: OutPoint::from_str(&prd.root_commitment)?,
|
||||||
current_process: relevant_process.clone(),
|
current_process: relevant_process.clone(),
|
||||||
modified_state: Some(updated_state.state_id),
|
modified_state,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let commit_msg = CommitMessage::new_update_commitment(
|
|
||||||
OutPoint::from_str(&prd.root_commitment)?,
|
|
||||||
updated_state.pcd_commitment,
|
|
||||||
roles
|
|
||||||
);
|
|
||||||
|
|
||||||
return Ok(ApiReturn {
|
return Ok(ApiReturn {
|
||||||
updated_process: Some(updated_process),
|
updated_process: Some(updated_process),
|
||||||
commit_to_send: Some(commit_msg),
|
commit_to_send: Some(commit_msg),
|
||||||
@ -1359,7 +1380,7 @@ pub fn create_update_message(
|
|||||||
|
|
||||||
let full_prd = Prd::new_update(
|
let full_prd = Prd::new_update(
|
||||||
outpoint,
|
outpoint,
|
||||||
serde_json::to_string(&sender)?,
|
sender,
|
||||||
roles,
|
roles,
|
||||||
update_state.keys.clone(),
|
update_state.keys.clone(),
|
||||||
update_state.pcd_commitment.clone(),
|
update_state.pcd_commitment.clone(),
|
||||||
@ -1522,7 +1543,7 @@ pub fn create_response_prd(init_commitment: String, merkle_root_hex: String) ->
|
|||||||
|
|
||||||
let response_prd = Prd::new_response(
|
let response_prd = Prd::new_response(
|
||||||
outpoint,
|
outpoint,
|
||||||
serde_json::to_string(&sender)?,
|
sender,
|
||||||
vec![*proof],
|
vec![*proof],
|
||||||
update_state.pcd_commitment.clone(),
|
update_state.pcd_commitment.clone(),
|
||||||
);
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user