Skip to content

Commit

Permalink
chore: Improve code organization
Browse files Browse the repository at this point in the history
  • Loading branch information
scarmuega committed Feb 14, 2022
1 parent 1446cc0 commit c45cf7b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 70 deletions.
32 changes: 32 additions & 0 deletions src/sinks/assert/checks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::prelude::*;

pub(crate) fn block_depth_doesnt_skip_numbers(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from((curr.number - prev.number) == 1),
_ => Outcome::Unknown,
}
}

pub(crate) fn block_slot_increases(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from(prev.slot < curr.slot),
_ => Outcome::Unknown,
}
}

pub(crate) fn event_timestamp_increases(state: &State) -> Outcome {
match (&state.previous_event, &state.current_event) {
(Some(prev), Some(curr)) => match (prev.context.timestamp, curr.context.timestamp) {
(Some(prev), Some(curr)) => Outcome::from(prev <= curr),
_ => Outcome::Unknown,
},
_ => Outcome::Unknown,
}
}

pub(crate) fn block_previous_hash_matches(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from(curr.previous_hash == prev.hash),
_ => Outcome::Unknown,
}
}
2 changes: 2 additions & 0 deletions src/sinks/assert/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod checks;
mod prelude;
mod run;
mod setup;

Expand Down
24 changes: 24 additions & 0 deletions src/sinks/assert/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::model::{BlockRecord, Event};

#[derive(Default, Debug)]
pub(crate) struct State {
pub current_event: Option<Event>,
pub previous_event: Option<Event>,
pub current_block: Option<BlockRecord>,
pub previous_block: Option<BlockRecord>,
}

pub(crate) enum Outcome {
Pass,
Fail,
Unknown,
}

impl From<bool> for Outcome {
fn from(other: bool) -> Self {
match other {
true => Outcome::Pass,
false => Outcome::Fail,
}
}
}
75 changes: 5 additions & 70 deletions src/sinks/assert/run.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
use std::sync::Arc;

use crate::{
model::{BlockRecord, Event, EventData},
model::{Event, EventData},
pipelining::StageReceiver,
utils::Utils,
Error,
};

use super::checks::*;
use super::prelude::*;
use super::Config;

#[derive(Default, Debug)]
struct State {
current_event: Option<Event>,
previous_event: Option<Event>,
current_block: Option<BlockRecord>,
previous_block: Option<BlockRecord>,
}

enum Outcome {
Pass,
Fail,
Unknown,
}

impl From<bool> for Outcome {
fn from(other: bool) -> Self {
match other {
true => Outcome::Pass,
false => Outcome::Fail,
}
}
}

macro_rules! execute_assertion {
($config:expr, $state:expr, $func:ident) => {
let outcome = $func($state);
Expand Down Expand Up @@ -60,48 +39,6 @@ macro_rules! execute_assertion {
};
}

fn block_depth_doesnt_skip_numbers(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from((curr.number - prev.number) == 1),
_ => Outcome::Unknown,
}
}

fn block_slot_increases(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from(prev.slot < curr.slot),
_ => Outcome::Unknown,
}
}

fn event_timestamp_increases(state: &State) -> Outcome {
match (&state.previous_event, &state.current_event) {
(Some(prev), Some(curr)) => match (prev.context.timestamp, curr.context.timestamp) {
(Some(prev), Some(curr)) => Outcome::from(prev <= curr),
_ => Outcome::Unknown,
},
_ => Outcome::Unknown,
}
}

fn block_previous_hash_matches(state: &State) -> Outcome {
match (&state.previous_block, &state.current_block) {
(Some(prev), Some(curr)) => Outcome::from(curr.previous_hash == prev.hash),
_ => Outcome::Unknown,
}
}

/*
fn cbor_decoding_is_isomorphic(state: &State) -> bool {
match state.latest_event {
Some(event) => match event.data {
EventData::Block(block) => {
}
}
}
}
*/
fn reduce_state(current: State, event: Event) -> State {
let state = match &event.data {
EventData::Block(r) => State {
Expand All @@ -112,13 +49,11 @@ fn reduce_state(current: State, event: Event) -> State {
_ => current,
};

let state = State {
State {
previous_event: state.current_event,
current_event: Some(event),
..state
};

state
}
}

pub fn assertion_loop(
Expand Down

0 comments on commit c45cf7b

Please sign in to comment.