-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement 'block header CBOR by hash' reducer (#70)
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use pallas::ledger::traverse::MultiEraBlock; | ||
use serde::Deserialize; | ||
|
||
use crate::prelude::*; | ||
use crate::{crosscut, model}; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Config { | ||
pub key_prefix: Option<String>, | ||
pub filter: Option<crosscut::filters::Predicate>, | ||
} | ||
|
||
pub struct Reducer { | ||
config: Config, | ||
policy: crosscut::policies::RuntimePolicy, | ||
} | ||
|
||
impl Reducer { | ||
pub fn reduce_block<'b>( | ||
&mut self, | ||
block: &'b MultiEraBlock<'b>, | ||
ctx: &model::BlockContext, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
if filter_matches_block!(self, block, ctx) { | ||
let value = block | ||
.header() | ||
.cbor() | ||
.to_vec(); | ||
|
||
let crdt = model::CRDTCommand::any_write_wins( | ||
self.config.key_prefix.as_deref(), | ||
block.hash(), | ||
value | ||
); | ||
|
||
output.send(gasket::messaging::Message::from(crdt))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn plugin(self, policy: &crosscut::policies::RuntimePolicy) -> super::Reducer { | ||
let reducer = Reducer { | ||
config: self, | ||
policy: policy.clone(), | ||
}; | ||
|
||
super::Reducer::BlockHeaderByHash(reducer) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters