Skip to content

Commit

Permalink
refactor: revisit the match filter
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Dec 31, 2023
1 parent 74216bf commit c022c81
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 241 deletions.
238 changes: 0 additions & 238 deletions src/filters/match_pattern.rs

This file was deleted.

64 changes: 64 additions & 0 deletions src/filters/match_tx/address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#[derive(Deserialize, Clone, Debug)]

Check failure on line 1 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find derive macro `Deserialize` in this scope

Check failure on line 1 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find derive macro `Deserialize` in this scope

Check failure on line 1 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find derive macro `Deserialize` in this scope

Check failure on line 1 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find derive macro `Deserialize` in this scope
pub enum AddressPatternValue {
ExactHex(String),
ExactBech32(String),
PaymentHex(String),
PaymentBech32(String),
StakeHex(String),
StakeBech32(String),
}

#[derive(Deserialize, Clone, Debug)]

Check failure on line 11 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find derive macro `Deserialize` in this scope

Check failure on line 11 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find derive macro `Deserialize` in this scope

Check failure on line 11 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find derive macro `Deserialize` in this scope

Check failure on line 11 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find derive macro `Deserialize` in this scope
pub struct AddressPattern {
pub value: AddressPatternValue,
pub is_script: Option<bool>,
}

impl AddressPattern {
fn address_match(&self, address: &Address) -> Result<bool, WorkerError> {

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find type `Address` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find type `WorkerError` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find type `Address` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find type `WorkerError` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `Address` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `WorkerError` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find type `Address` in this scope

Check failure on line 18 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find type `WorkerError` in this scope
match address {
Address::Byron(addr) => match &self.value {

Check failure on line 20 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

failed to resolve: use of undeclared type `Address`

Check failure on line 20 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

failed to resolve: use of undeclared type `Address`

Check failure on line 20 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared type `Address`

Check failure on line 20 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

failed to resolve: use of undeclared type `Address`
AddressPatternValue::ExactHex(exact_hex) => Ok(addr.to_hex().eq(exact_hex)),
AddressPatternValue::PaymentHex(payment_hex) => Ok(addr.to_hex().eq(payment_hex)),
_ => Ok(false),
},
Address::Shelley(addr) => match &self.value {

Check failure on line 25 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

failed to resolve: use of undeclared type `Address`

Check failure on line 25 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

failed to resolve: use of undeclared type `Address`

Check failure on line 25 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared type `Address`

Check failure on line 25 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

failed to resolve: use of undeclared type `Address`
AddressPatternValue::ExactHex(exact_hex) => Ok(addr.to_hex().eq(exact_hex)),
AddressPatternValue::ExactBech32(exact_bech32) => {
Ok(addr.to_bech32().or_panic()?.eq(exact_bech32))
}
AddressPatternValue::PaymentHex(payment_hex) => {
Ok(addr.payment().to_hex().eq(payment_hex))
}
AddressPatternValue::PaymentBech32(payment_bech32) => {
Ok(addr.payment().to_bech32().or_panic()?.eq(payment_bech32))
}
AddressPatternValue::StakeHex(stake_hex) => {
if addr.delegation().as_hash().is_none() {
return Ok(false);
}

let stake_address: StakeAddress = addr.clone().try_into().or_panic()?;

Check failure on line 41 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find type `StakeAddress` in this scope

Check failure on line 41 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find type `StakeAddress` in this scope

Check failure on line 41 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `StakeAddress` in this scope

Check failure on line 41 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find type `StakeAddress` in this scope
Ok(stake_address.to_hex().eq(stake_hex))
}
AddressPatternValue::StakeBech32(stake_bech32) => {
if addr.delegation().as_hash().is_none() {
return Ok(false);
}

let stake_address: StakeAddress = addr.clone().try_into().or_panic()?;

Check failure on line 49 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find type `StakeAddress` in this scope

Check failure on line 49 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find type `StakeAddress` in this scope

Check failure on line 49 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `StakeAddress` in this scope

Check failure on line 49 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find type `StakeAddress` in this scope
Ok(stake_address.to_bech32().or_panic()?.eq(stake_bech32))
}
},
Address::Stake(stake_address) => match &self.value {

Check failure on line 53 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

failed to resolve: use of undeclared type `Address`

Check failure on line 53 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

failed to resolve: use of undeclared type `Address`

Check failure on line 53 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared type `Address`

Check failure on line 53 in src/filters/match_tx/address.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

failed to resolve: use of undeclared type `Address`
AddressPatternValue::StakeHex(stake_hex) => {
Ok(stake_address.to_hex().eq(stake_hex))
}
AddressPatternValue::StakeBech32(stake_bech32) => {
Ok(stake_address.to_bech32().or_panic()?.eq(stake_bech32))
}
_ => Ok(false),
},
}
}
}
92 changes: 92 additions & 0 deletions src/filters/match_tx/eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use std::ops::{Add, AddAssign};

Check warning on line 1 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

unused import: `AddAssign`

Check warning on line 1 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `AddAssign`

Check warning on line 1 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

unused import: `AddAssign`

use crate::framework::*;

use super::Predicate;

#[derive(Clone, Copy)]
enum MatchOutcome {
Positive,
Negative,
Uncertain,
}

impl Add for MatchOutcome {
type Output = Self;

fn add(self, rhs: Self) -> Self::Output {
match (self, rhs) {
// one positive is enough
(MatchOutcome::Positive, _) => MatchOutcome::Positive,
(_, MatchOutcome::Positive) => MatchOutcome::Positive,

// if there's any uncertainty, return uncertain
(_, MatchOutcome::Uncertain) => MatchOutcome::Uncertain,
(MatchOutcome::Uncertain, _) => MatchOutcome::Uncertain,

// default to negative
_ => MatchOutcome::Negative,
}
}
}

impl MatchOutcome {
fn fold_any_of(outcomes: impl Iterator<Item = Self>) -> Self {
let mut folded = MatchOutcome::Negative;

for item in outcomes {
if item == Self::Positive {
return Self::Positive;
}

folded = folded + item;
}

folded
}

fn fold_all_of(outcomes: impl Iterator<Item = Self>) -> Self {
for item in outcomes {
if item != Self::Positive {
return item;
}
}

Self::Positive
}
}

fn eval_tx(tx: &ParsedTx, predicate: Predicate) -> MatchOutcome {
match predicate {
Predicate::Block(block_pattern) => Ok(block_match(point, block_pattern)),

Check failure on line 61 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Lint Rust

cannot find function `block_match` in this scope

Check failure on line 61 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

cannot find function `block_match` in this scope

Check failure on line 61 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find function `block_match` in this scope

Check failure on line 61 in src/filters/match_tx/eval.rs

View workflow job for this annotation

GitHub Actions / Check (windows-latest, stable)

cannot find function `block_match` in this scope
Predicate::OutputAddress(address_pattern) => Ok(output_match(tx, address_pattern)?),
Predicate::WithdrawalAddress(address_pattern) => Ok(withdrawal_match(tx, address_pattern)?),
Predicate::CollateralAddress(address_pattern) => Ok(collateral_match(tx, address_pattern)?),
Predicate::Not(x) => Ok(!x.tx_match(point, tx)?),
Predicate::AnyOf(x) => {
let o = x.iter().map(|x| eval_tx(tx, x));
fold_any_of(o)
}
Predicate::AllOf(x) => {
let o = x.iter().map(|x| eval_tx(tx, x));
fold_all_of(o)
}
}
}

fn eval_block(block: &ParsedBlock, predicate: &Predicate) -> MatchOutcome {
let outcomes = block
.body
.unwrap_or_default()
.tx
.iter()
.map(|tx| eval_tx(tx, predicate));
}

pub fn eval(record: &Record, predicate: &Predicate) -> MatchOutcome {
match record {
Record::ParsedTx(x) => eval_tx(x, predicate),
Record::ParsedBlock(x) => eval_block(x, predicate),
_ => MatchOutcome::Uncertain,
}
}
Loading

0 comments on commit c022c81

Please sign in to comment.