-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
- Loading branch information
There are no files selected for viewing
This file was deleted.
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
|
||
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
|
||
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
|
||
match address { | ||
Address::Byron(addr) => match &self.value { | ||
Check failure on line 20 in src/filters/match_tx/address.rs
|
||
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
|
||
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
|
||
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
|
||
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
|
||
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), | ||
}, | ||
} | ||
} | ||
} |
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
|
||
|
||
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
|
||
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, | ||
} | ||
} |