Update update_process
This commit is contained in:
parent
166c7c993f
commit
f789b30372
58
src/api.rs
58
src/api.rs
@ -1314,7 +1314,7 @@ pub fn create_new_process(
|
|||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn update_process(
|
pub fn update_process(
|
||||||
init_commitment: String,
|
init_commitment: String,
|
||||||
new_state: String,
|
new_state_str: String,
|
||||||
) -> ApiResult<ApiReturn> {
|
) -> ApiResult<ApiReturn> {
|
||||||
let outpoint = OutPoint::from_str(&init_commitment)?;
|
let outpoint = OutPoint::from_str(&init_commitment)?;
|
||||||
|
|
||||||
@ -1322,46 +1322,29 @@ pub fn update_process(
|
|||||||
let process = processes.get_mut(&outpoint)
|
let process = processes.get_mut(&outpoint)
|
||||||
.ok_or(ApiError::new("Unknown process".to_owned()))?;
|
.ok_or(ApiError::new("Unknown process".to_owned()))?;
|
||||||
|
|
||||||
let last_state = process.get_latest_commited_state()
|
let prev_state = process.get_latest_commited_state()
|
||||||
.ok_or(ApiError::new("Process must have at least one state already commited".to_owned()))?;
|
.ok_or(ApiError::new("Process must have at least one state already commited".to_owned()))?;
|
||||||
|
|
||||||
let last_state_commitments = &last_state.pcd_commitment;
|
let last_state_commitments = &prev_state.pcd_commitment;
|
||||||
|
|
||||||
let new_state_val = Value::from_str(&new_state)?;
|
let clear_new_state = Value::from_str(&new_state_str)?.to_value_object()?;
|
||||||
|
|
||||||
// We hash all the new values
|
let new_state = ProcessState::new(prev_state.commited_in, clear_new_state.clone())?;
|
||||||
let pcd_commitment = Value::Object(new_state_val.hash_all_fields(outpoint)?);
|
|
||||||
let new_state_merkle_root = <Value as Pcd>::create_merkle_tree(&pcd_commitment)?.root().unwrap();
|
|
||||||
|
|
||||||
// We compare the new state with the previous one
|
// We compare the new state with the previous one
|
||||||
let last_state_merkle_root = <Value as Pcd>::create_merkle_tree(last_state_commitments)?.root().unwrap();
|
let last_state_merkle_root = &prev_state.merkle_root;
|
||||||
|
|
||||||
if last_state_merkle_root == new_state_merkle_root {
|
if *last_state_merkle_root == new_state.merkle_root {
|
||||||
return Err(ApiError::new("new proposed state is identical to the previous commited state".to_owned()));
|
return Err(ApiError::new("new proposed state is identical to the previous commited state".to_owned()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// We create the encrypted pcd
|
// We check that we don't have already a similar concurrent state
|
||||||
let mut fields2keys = Map::new();
|
let concurrent_processes = process.get_latest_concurrent_states()?;
|
||||||
let mut fields2cipher = Map::new();
|
if concurrent_processes.iter().any(|p| p.merkle_root == new_state.merkle_root) {
|
||||||
let fields_to_encrypt: Vec<String> = new_state_val
|
return Err(ApiError::new("New state already known".to_owned()));
|
||||||
.as_object()
|
}
|
||||||
.unwrap()
|
|
||||||
.keys()
|
|
||||||
.map(|k| k.clone())
|
|
||||||
.collect();
|
|
||||||
new_state_val.encrypt_fields(&fields_to_encrypt, &mut fields2keys, &mut fields2cipher);
|
|
||||||
|
|
||||||
let encrypted_pcd = Value::Object(fields2cipher);
|
let diffs = create_diffs(&process, &new_state)?;
|
||||||
|
|
||||||
let device = lock_local_device()?;
|
|
||||||
|
|
||||||
let new_state = ProcessState {
|
|
||||||
commited_in: outpoint,
|
|
||||||
pcd_commitment: pcd_commitment,
|
|
||||||
encrypted_pcd: encrypted_pcd,
|
|
||||||
keys: fields2keys,
|
|
||||||
validation_tokens: vec![]
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add the new state to the process
|
// Add the new state to the process
|
||||||
process.insert_concurrent_state(new_state.clone())?;
|
process.insert_concurrent_state(new_state.clone())?;
|
||||||
@ -1369,7 +1352,7 @@ pub fn update_process(
|
|||||||
let updated_process = UpdatedProcess {
|
let updated_process = UpdatedProcess {
|
||||||
commitment_tx: outpoint,
|
commitment_tx: outpoint,
|
||||||
current_process: process.clone(),
|
current_process: process.clone(),
|
||||||
new_state: Some(new_state),
|
new_diffs: diffs,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -1391,19 +1374,10 @@ pub fn create_update_message(
|
|||||||
let process = processes.get_mut(&outpoint)
|
let process = processes.get_mut(&outpoint)
|
||||||
.ok_or(ApiError::new("Unknown process".to_owned()))?;
|
.ok_or(ApiError::new("Unknown process".to_owned()))?;
|
||||||
|
|
||||||
let mut merkle_root_bin = [0u8; 32];
|
let update_state = process.get_state_for_commitments_root(&merkle_root_hex)?;
|
||||||
let merkle_root_vec = Vec::from_hex(&merkle_root_hex)?;
|
|
||||||
|
|
||||||
if merkle_root_vec.len() != 32 {
|
|
||||||
return Err(ApiError::new("merkle root must be 32B long".to_owned()));
|
|
||||||
}
|
|
||||||
|
|
||||||
merkle_root_bin.copy_from_slice(&merkle_root_vec);
|
|
||||||
|
|
||||||
let update_state = process.get_state_for_commitments_root(merkle_root_bin)?;
|
|
||||||
|
|
||||||
// We must have at least the key for the roles field, otherwise we don't know who to send the message to
|
// We must have at least the key for the roles field, otherwise we don't know who to send the message to
|
||||||
let clear_state = update_state.decrypt_pcd()?.to_value_object()?;
|
let clear_state = update_state.decrypt_pcd()?;
|
||||||
|
|
||||||
let roles = Value::Object(clear_state).extract_roles()?;
|
let roles = Value::Object(clear_state).extract_roles()?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user