From d99d4f0067fad2895ce9fbf5ff3e8ca921e3408f Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Thu, 30 Jan 2025 16:06:35 +0100 Subject: [PATCH] Renamed some variables for clarity + minor fixes --- src/api.rs | 62 +++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/src/api.rs b/src/api.rs index 5255c84..2c1c4cf 100644 --- a/src/api.rs +++ b/src/api.rs @@ -106,12 +106,11 @@ pub struct UserDiff { #[tsify(into_wasm_abi)] #[allow(non_camel_case_types)] pub struct UpdatedProcess { - pub commitment_tx: OutPoint, + pub process_id: OutPoint, pub current_process: Process, pub up_to_date_roles: HashMap, - pub new_diffs: Vec, // All diffs should have the same new_state_merkle_root - pub modified_state: Option, // basically when we add/receive validation proofs for a state - // I think we should never have both new_state and modified_state + pub diffs: Vec, // All diffs should have the same state_id + pub validated_state: Option, // when we add/receive validation proofs for a state } #[derive(Debug, PartialEq, Tsify, Serialize, Deserialize, Default)] @@ -310,7 +309,7 @@ pub fn is_paired() -> ApiResult { } #[wasm_bindgen] -pub fn pair_device(commitment_tx: String, mut sp_addresses: Vec) -> ApiResult<()> { +pub fn pair_device(process_id: String, mut sp_addresses: Vec) -> ApiResult<()> { let mut local_device = lock_local_device()?; if local_device.get_pairing_commitment().is_some() { @@ -327,7 +326,7 @@ pub fn pair_device(commitment_tx: String, mut sp_addresses: Vec) -> ApiR } local_device.pair( - OutPoint::from_str(&commitment_tx)?, + OutPoint::from_str(&process_id)?, Member::new( sp_addresses .into_iter() @@ -644,17 +643,17 @@ fn process_transaction( let roles = if let Ok(roles) = new_state.encrypted_pcd.extract_roles() { roles } else { HashMap::new() }; let diffs = if let Ok(diffs) = create_diffs(process, new_state) { diffs } else { vec![] }; let updated_process = UpdatedProcess { - commitment_tx: outpoint, + process_id: outpoint, current_process: process.clone(), up_to_date_roles: roles, - new_diffs: diffs, + diffs: diffs, ..Default::default() }; let api_return = ApiReturn { updated_process: Some(updated_process), ..Default::default() }; - debug!("Found an update for process {:?}", api_return.updated_process.as_ref().unwrap().commitment_tx); + debug!("Found an update for process {:?}", api_return.updated_process.as_ref().unwrap().process_id); return Ok(api_return); } Err(e) => debug!("Failed to find process update: {}", e) @@ -706,7 +705,7 @@ fn confirm_prd(prd: &Prd, shared_secret: &AnkSharedSecretHash) -> AnyhowResult (), } - let outpoint = OutPoint::from_str(&prd.root_commitment)?; + let outpoint = OutPoint::from_str(&prd.process_id)?; let local_device = lock_local_device()?; let sender = local_device.to_member(); @@ -865,7 +864,7 @@ fn handle_prd( return handle_prd_connect(prd, secret); } - let outpoint = OutPoint::from_str(&prd.root_commitment)?; + let outpoint = OutPoint::from_str(&prd.process_id)?; let mut processes = lock_processes()?; let relevant_process = match processes.entry(outpoint) { @@ -885,7 +884,7 @@ fn handle_prd( return Err(AnyhowError::msg("Received update for a state we already know")); } - let commited_in = OutPoint::from_str(&prd.root_commitment)?; + let commited_in = relevant_process.get_process_tip()?; // Extract the roles from the payload let proposal_roles: HashMap = serde_json::from_str(&prd.payload)?; @@ -917,9 +916,9 @@ fn handle_prd( relevant_process.insert_concurrent_state(new_state)?; let updated_process = UpdatedProcess { - commitment_tx: outpoint, + process_id: outpoint, current_process: relevant_process.clone(), - new_diffs: diffs, + diffs, up_to_date_roles: roles, ..Default::default() }; @@ -966,9 +965,9 @@ fn handle_prd( let clear_state = to_update.decrypt_pcd()?; let roles = Value::Object(clear_state).extract_roles()?; - let modified_state = Some(to_update.state_id.clone()); + let validated_state = Some(to_update.state_id.clone()); let mut commit_msg = CommitMessage::new_update_commitment( - OutPoint::from_str(&prd.root_commitment)?, + OutPoint::from_str(&prd.process_id)?, updated_state.pcd_commitment, roles ); @@ -977,9 +976,9 @@ fn handle_prd( // We must return an update of the process let updated_process = UpdatedProcess { - commitment_tx: OutPoint::from_str(&prd.root_commitment)?, + process_id: OutPoint::from_str(&prd.process_id)?, current_process: relevant_process.clone(), - modified_state, + validated_state, ..Default::default() }; @@ -1008,11 +1007,11 @@ fn handle_decrypted_message( #[wasm_bindgen] /// Use the provided Map to update a state /// The map uses hash commitment as keys, as in storage -pub fn update_process_state(init_commitment: String, state_id: String, hash2values: String) -> ApiResult { +pub fn update_process_state(process_id: String, state_id: String, hash2values: String) -> ApiResult { let hash2values_map = serde_json::from_str::(&hash2values)?.to_value_object()?; // Get the process - let outpoint = OutPoint::from_str(&init_commitment)?; + let outpoint = OutPoint::from_str(&process_id)?; let mut processes = lock_processes()?; { @@ -1021,10 +1020,7 @@ pub fn update_process_state(init_commitment: String, state_id: String, hash2valu .ok_or(ApiError::new("Unknown process".to_owned()))?; // Get the state - let state = process.get_latest_concurrent_states_mut()? - .into_iter() - .find(|state| state.state_id == state_id) - .ok_or(ApiError::new("Unknown state".to_owned()))?; + let state = process.get_state_for_id_mut(&state_id)?; // Update each value // Check if there's already something @@ -1081,13 +1077,13 @@ pub fn update_process_state(init_commitment: String, state_id: String, hash2valu // If every value we can decrypt is valid, then we return the new state and diffs // We borrow it again immutably let process = processes.get(&outpoint).unwrap(); - let state = process.get_latest_concurrent_states()?.into_iter().find(|s| s.state_id == state_id).unwrap(); + let state = process.get_state_for_id(&state_id)?; let diffs = create_diffs(&process, &state)?; let udpated_process = UpdatedProcess { - commitment_tx: outpoint, + process_id: outpoint, current_process: process.clone(), - new_diffs: diffs, + diffs: diffs, ..Default::default() }; @@ -1278,10 +1274,10 @@ pub fn create_new_process( let commit_msg = CommitMessage::new_first_commitment(transaction, new_state.pcd_commitment, roles.clone()); let updated_process = UpdatedProcess { - commitment_tx: outpoint, + process_id: outpoint, current_process: process, up_to_date_roles: roles, - new_diffs: diffs, + diffs: diffs, ..Default::default() }; @@ -1339,10 +1335,10 @@ pub fn update_process( process.insert_concurrent_state(new_state.clone())?; let updated_process = UpdatedProcess { - commitment_tx: outpoint, + process_id: outpoint, current_process: process.clone(), up_to_date_roles: roles.clone(), - new_diffs: diffs, + diffs, ..Default::default() }; @@ -1536,9 +1532,9 @@ fn add_validation_token(init_commitment: String, merkle_root_hex: String, approv }; let updated_process = UpdatedProcess { - commitment_tx: OutPoint::from_str(&init_commitment)?, + process_id: OutPoint::from_str(&init_commitment)?, current_process: process.clone(), - modified_state: Some(merkle_root_hex), + validated_state: Some(merkle_root_hex), ..Default::default() };