From d2a3147120b727cac4f3be2ad157decc99f98122 Mon Sep 17 00:00:00 2001 From: NicolasCantu Date: Wed, 26 Mar 2025 12:36:06 +0100 Subject: [PATCH] Refactor handle_obliteration --- src/process.rs | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/process.rs b/src/process.rs index a317e6e..4b261fb 100644 --- a/src/process.rs +++ b/src/process.rs @@ -74,33 +74,28 @@ impl ProcessState { } /// This is a simplified and streamlined validation for obliteration state - fn handle_obliteration(&self, members_list: &OutPointMemberMap) -> anyhow::Result<()> { - // We need an Apophis role - if let Some(apophis) = self.roles.get(SpecialRoles::APOPHIS.to_string().as_str()) { - // Apophis should have only one rule - if apophis.validation_rules.len() != 1 { return Err(anyhow::Error::msg("Should have only one rule")); }; - let obliteration_rule = apophis.validation_rules.get(0).unwrap(); + fn handle_obliteration(&self, apophis: &RoleDefinition, members_list: &OutPointMemberMap) -> anyhow::Result<()> { + // Apophis should have only one rule + if apophis.validation_rules.len() != 1 { return Err(anyhow::Error::msg("Should have only one rule")); }; + let obliteration_rule = apophis.validation_rules.get(0).unwrap(); - let empty_field = ""; - // This rule should have only one empty string as field - if obliteration_rule.fields.len() != 1 { return Err(anyhow::Error::msg("Should have only one field")); }; - if obliteration_rule.fields.get(0).unwrap() != empty_field { return Err(anyhow::Error::msg("Field should be empty")); }; + let empty_field = ""; + // This rule should have only one empty string as field + if obliteration_rule.fields.len() != 1 { return Err(anyhow::Error::msg("Should have only one field")); }; + if obliteration_rule.fields.get(0).unwrap() != empty_field { return Err(anyhow::Error::msg("Field should be empty")); }; - let members: Vec<&Member> = apophis.members.iter() - .filter_map(|outpoint| { - members_list.0.get(outpoint) - }) - .collect(); + let members: Vec<&Member> = apophis.members.iter() + .filter_map(|outpoint| { + members_list.0.get(outpoint) + }) + .collect(); - if apophis.validation_rules.iter().all( - |r| r.is_satisfied(empty_field, [0u8; 32], &self.validation_tokens, &members).is_ok() - ) { - Ok(()) - } else { - Err(anyhow::Error::msg("Apophis is not satisfied")) - } + if apophis.validation_rules.iter().all( + |r| r.is_satisfied(empty_field, [0u8; 32], &self.validation_tokens, &members).is_ok() + ) { + Ok(()) } else { - Err(anyhow::Error::msg("Missing an apophis role")) + Err(anyhow::Error::msg("Apophis is not satisfied")) } }