From c45cf7b7b5ff6de217fb978761c661fe1b68d146 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Sun, 13 Feb 2022 21:33:37 -0300 Subject: [PATCH] chore: Improve code organization --- src/sinks/assert/checks.rs | 32 ++++++++++++++++ src/sinks/assert/mod.rs | 2 + src/sinks/assert/prelude.rs | 24 ++++++++++++ src/sinks/assert/run.rs | 75 +++---------------------------------- 4 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 src/sinks/assert/checks.rs create mode 100644 src/sinks/assert/prelude.rs diff --git a/src/sinks/assert/checks.rs b/src/sinks/assert/checks.rs new file mode 100644 index 00000000..35465454 --- /dev/null +++ b/src/sinks/assert/checks.rs @@ -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, + } +} diff --git a/src/sinks/assert/mod.rs b/src/sinks/assert/mod.rs index 0a447c1d..d74c0b67 100644 --- a/src/sinks/assert/mod.rs +++ b/src/sinks/assert/mod.rs @@ -1,3 +1,5 @@ +mod checks; +mod prelude; mod run; mod setup; diff --git a/src/sinks/assert/prelude.rs b/src/sinks/assert/prelude.rs new file mode 100644 index 00000000..7c17e1fa --- /dev/null +++ b/src/sinks/assert/prelude.rs @@ -0,0 +1,24 @@ +use crate::model::{BlockRecord, Event}; + +#[derive(Default, Debug)] +pub(crate) struct State { + pub current_event: Option, + pub previous_event: Option, + pub current_block: Option, + pub previous_block: Option, +} + +pub(crate) enum Outcome { + Pass, + Fail, + Unknown, +} + +impl From for Outcome { + fn from(other: bool) -> Self { + match other { + true => Outcome::Pass, + false => Outcome::Fail, + } + } +} diff --git a/src/sinks/assert/run.rs b/src/sinks/assert/run.rs index 3fce1c10..9df4923f 100644 --- a/src/sinks/assert/run.rs +++ b/src/sinks/assert/run.rs @@ -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, - previous_event: Option, - current_block: Option, - previous_block: Option, -} - -enum Outcome { - Pass, - Fail, - Unknown, -} - -impl From 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); @@ -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 { @@ -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(