Refactor ProcessState is_valid()

This commit is contained in:
Sosthene 2024-11-29 09:07:48 +01:00
parent 391ca02186
commit eda2dd687d

View File

@ -87,28 +87,13 @@ impl ProcessState {
return Err(anyhow::anyhow!("State is identical to the previous state"));
}
println!("modified fields: {:?}", modified_fields);
// Compute the hash of the new state
let new_state_hash = AnkPcdHash::from_value(&self.encrypted_pcd);
let mut fields2plains = Map::new();
let fields2commit = self.pcd_commitment.as_object().ok_or(anyhow::Error::msg("pcd_commitment is not an object"))?;
let merkle_root = Value::Object(fields2commit.clone()).create_merkle_tree()?.root().unwrap();
self.encrypted_pcd
.decrypt_fields(&self.keys, &mut fields2plains)?;
.decrypt_fields(&fields2commit, &self.keys, &mut fields2plains)?;
let mut roles2rules: HashMap<String, RoleDefinition> = HashMap::new();
if let Some(roles) = fields2plains.get("roles") {
if let Some(roles_map) = roles.as_object() {
for (role, conditions) in roles_map {
let role_def = serde_json::from_value::<RoleDefinition>(conditions.clone())?;
roles2rules.insert(role.to_string(), role_def);
}
} else {
return Err(anyhow::anyhow!("Roles is not an object"));
}
} else {
return Err(anyhow::anyhow!("Missing roles in the encrypted pcd"));
}
let roles2rules = Value::Object(fields2plains).extract_roles()?;
// Check if each modified field satisfies at least one applicable rule across all roles
let all_fields_validated = modified_fields.iter().all(|field| {
@ -136,10 +121,10 @@ impl ProcessState {
role_def.validation_rules.iter().any(|rule| {
rule.is_satisfied(
field,
new_state_hash.clone(),
merkle_root,
&self.validation_tokens,
&role_def.members,
)
).is_ok()
})
})
});