Skip to content

Commit

Permalink
feat: added deserialize config
Browse files Browse the repository at this point in the history
  • Loading branch information
paulobressan committed Aug 21, 2023
1 parent 1dbc1f2 commit 629efdf
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions src/filters/match_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gasket::framework::*;
use pallas::network::miniprotocols::Point;
use serde::Deserialize;
use serde::{Deserialize, Deserializer};
use tracing::error;

use crate::framework::*;
Expand Down Expand Up @@ -30,7 +30,7 @@ gasket::impl_splitter!(|_worker: Worker, stage: Stage, unit: ChainEvent| => {
let out = match unit {
ChainEvent::Apply(point, record) => match record {
Record::ParsedTx(tx) => {
if stage.predicate.tx_match(point, tx) {
if stage.predicate.tx_match(point, tx)? {
Ok(Some(unit.to_owned()))
} else {
Ok(None)
Expand All @@ -49,33 +49,59 @@ gasket::impl_splitter!(|_worker: Worker, stage: Stage, unit: ChainEvent| => {
out
});

#[derive(Deserialize, Clone)]
#[derive(Deserialize, Clone, Debug)]
pub struct AddressPattern {
pub exact_hex: Option<String>,
pub exact_bech32: Option<String>,
pub payment_hex: Option<String>,
pub payment_bech32: Option<String>,
pub stake_hex: Option<String>,
pub stake_bech32: Option<String>,
pub exact: Option<AddressValue>,
pub payment: Option<AddressValue>,
pub stake: Option<AddressValue>,
pub is_script: Option<bool>,
}
#[derive(Clone, Debug)]
pub struct AddressValue {
value: String,

Check warning on line 61 in src/filters/match_pattern.rs

View workflow job for this annotation

GitHub Actions / Test Suite

field `value` is never read

Check warning on line 61 in src/filters/match_pattern.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

field `value` is never read
kind: AddressKind,
}
#[derive(Clone, Debug)]
pub enum AddressKind {
Hex,
Bech32,
}

impl<'de> Deserialize<'de> for AddressValue {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
let mut address_value = AddressValue {
value: value.clone(),
kind: AddressKind::Hex,
};

if bech32::decode(&value).is_ok() {
address_value.kind = AddressKind::Bech32;
}

Ok(address_value)
}
}

#[derive(Deserialize, Clone)]
#[derive(Deserialize, Clone, Debug)]
pub struct BlockPattern {
pub slot_before: Option<u64>,
pub slot_after: Option<u64>,
}

#[derive(Deserialize, Clone)]
#[derive(Deserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Predicate {
Block(BlockPattern),
}

impl Predicate {
fn tx_match(&self, point: &Point, _: &ParsedTx) -> bool {
fn tx_match(&self, point: &Point, parsed_tx: &ParsedTx) -> Result<bool, WorkerError> {

Check warning on line 102 in src/filters/match_pattern.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused variable: `parsed_tx`

Check warning on line 102 in src/filters/match_pattern.rs

View workflow job for this annotation

GitHub Actions / Check (ubuntu-latest, stable)

unused variable: `parsed_tx`
match self {
Predicate::Block(block_pattern) => self.slot_match(point, block_pattern),
Predicate::Block(block_pattern) => Ok(self.slot_match(point, block_pattern)),
}
}

Expand Down

0 comments on commit 629efdf

Please sign in to comment.