From 623e725361416c40577df02079c4c041e455b987 Mon Sep 17 00:00:00 2001 From: MicaiahReid Date: Wed, 2 Aug 2023 15:57:03 -0400 Subject: [PATCH] use implement trait enum --- .../chainhook-cli/src/service/tests/mod.rs | 3 ++ .../src/chainhooks/stacks/mod.rs | 32 ++++++------------- .../chainhook-sdk/src/chainhooks/tests/mod.rs | 15 ++++++--- .../chainhook-sdk/src/chainhooks/types.rs | 9 ++++++ 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/components/chainhook-cli/src/service/tests/mod.rs b/components/chainhook-cli/src/service/tests/mod.rs index 3faea3fad..8fbe52f46 100644 --- a/components/chainhook-cli/src/service/tests/mod.rs +++ b/components/chainhook-cli/src/service/tests/mod.rs @@ -300,6 +300,9 @@ async fn it_handles_stacks_predicates_with_network(network: &str) { #[test_case(json!({"scope":"block_height", "between": [100,102]}); "with scope block_height between match")] #[test_case(json!({"scope":"contract_deployment", "deployer": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM"}); "with scope contract_deployment type deployer")] #[test_case(json!({"scope":"contract_deployment", "deployer": "*"}); "with scope contract_deployment type deployer wildcard")] +#[test_case(json!({"scope":"contract_deployment", "implement_trait": "sip09"}); "with scope contract_deployment type implement_trait sip09")] +#[test_case(json!({"scope":"contract_deployment", "implement_trait": "sip10"}); "with scope contract_deployment type implement_trait sip10")] +#[test_case(json!({"scope":"contract_deployment", "implement_trait": "*"}); "with scope contract_deployment type implement_trait and wildcard trait")] #[test_case(json!({"scope":"contract_call","contract_identifier": "SP000000000000000000002Q6VF78.pox","method": "stack-stx"}); "with scope contract_call")] #[test_case(json!({"scope":"print_event","contract_identifier": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.monkey-sip09","contains": "vault"}); "with scope print_event both fields")] #[test_case(json!({"scope":"print_event","contract_identifier": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.monkey-sip09", "contains": "*"}); "with scope print_event wildcard conatins")] diff --git a/components/chainhook-sdk/src/chainhooks/stacks/mod.rs b/components/chainhook-sdk/src/chainhooks/stacks/mod.rs index 5da291d0d..50cdec4f6 100644 --- a/components/chainhook-sdk/src/chainhooks/stacks/mod.rs +++ b/components/chainhook-sdk/src/chainhooks/stacks/mod.rs @@ -298,8 +298,10 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>( } _ => false, }, - StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip09) => { - match &transaction.metadata.kind { + StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait( + stacks_trait, + )) => match stacks_trait { + _ => match &transaction.metadata.kind { StacksTransactionKind::ContractDeployment(_actual_deployment) => { ctx.try_log(|logger| { slog::warn!( @@ -310,22 +312,8 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>( false } _ => false, - } - } - StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip10) => { - match &transaction.metadata.kind { - StacksTransactionKind::ContractDeployment(_actual_deployment) => { - ctx.try_log(|logger| { - slog::warn!( - logger, - "StacksContractDeploymentPredicate::Trait uninmplemented" - ) - }); - false - } - _ => false, - } - } + }, + }, StacksPredicate::ContractCall(expected_contract_call) => match &transaction.metadata.kind { StacksTransactionKind::ContractCall(actual_contract_call) => { actual_contract_call @@ -391,13 +379,13 @@ pub fn evaluate_stacks_predicate_on_transaction<'a>( || expected_event.contract_identifier == "*" { if expected_event.contains == "*" { - return true; - } + return true; + } let value = format!("{}", expect_decoded_clarity_value(&actual.hex_value)); if value.contains(&expected_event.contains) { - return true; - } + return true; + } } } } diff --git a/components/chainhook-sdk/src/chainhooks/tests/mod.rs b/components/chainhook-sdk/src/chainhooks/tests/mod.rs index 5ea9b88ad..52189abc2 100644 --- a/components/chainhook-sdk/src/chainhooks/tests/mod.rs +++ b/components/chainhook-sdk/src/chainhooks/tests/mod.rs @@ -4,7 +4,7 @@ use self::fixtures::get_all_event_types; use super::{ stacks::{evaluate_stacks_chainhooks_on_chain_event, StacksTriggerChainhook, handle_stacks_hook_action, StacksChainhookOccurrence}, - types::{StacksChainhookSpecification, StacksPrintEventBasedPredicate, StacksNftEventBasedPredicate, StacksFtEventBasedPredicate,StacksContractCallBasedPredicate,StacksContractDeploymentPredicate, ExactMatchingRule, FileHook}, + types::{StacksChainhookSpecification, StacksPrintEventBasedPredicate, StacksNftEventBasedPredicate, StacksFtEventBasedPredicate,StacksContractCallBasedPredicate,StacksContractDeploymentPredicate, ExactMatchingRule, FileHook, StacksTrait}, }; use crate::{chainhooks::{types::{HookAction, StacksPredicate, StacksStxEventBasedPredicate,}, tests::fixtures::{get_expected_occurrence, get_test_event_by_type}}, utils::AbstractStacksBlock}; use crate::utils::Context; @@ -309,14 +309,19 @@ fn test_stacks_predicates(blocks_with_events: Vec>, "Deployer predicate does not match non-matching deployer" )] #[test_case( - StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip09), + StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Sip09)), 0; - "ImplementSip09 predicate returns no values" + "ImplementSip predicate returns no values for Sip09" )] #[test_case( - StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementSip10), + StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Sip10)), 0; - "ImplementSip10 predicate returns no values" + "ImplementSip predicate returns no values for Sip10" +)] +#[test_case( + StacksPredicate::ContractDeployment(StacksContractDeploymentPredicate::ImplementTrait(StacksTrait::Any)), + 0; + "ImplementSip predicate returns no values for Any" )] fn test_stacks_predicate_contract_deploy(predicate: StacksPredicate, expected_applies: u64) { // Prepare block diff --git a/components/chainhook-sdk/src/chainhooks/types.rs b/components/chainhook-sdk/src/chainhooks/types.rs index 868feeaeb..4bc62073a 100644 --- a/components/chainhook-sdk/src/chainhooks/types.rs +++ b/components/chainhook-sdk/src/chainhooks/types.rs @@ -763,6 +763,15 @@ pub struct StacksContractCallBasedPredicate { // #[serde(tag = "type", content = "rule")] pub enum StacksContractDeploymentPredicate { Deployer(String), + ImplementTrait(StacksTrait), +} +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum StacksTrait { + Sip09, + Sip10, + #[serde(rename = "*")] + Any, } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]