-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: script by hash reducer #25
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use pallas::crypto::hash::{Hash, Hasher}; | ||
use pallas::ledger::primitives::alonzo; | ||
use pallas::ledger::primitives::alonzo::PlutusScript; | ||
use serde::Deserialize; | ||
|
||
use crate::{crosscut, model}; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Config { | ||
pub key_prefix: Option<String>, | ||
pub filter: Option<Vec<String>>, | ||
} | ||
|
||
pub struct Reducer { | ||
config: Config, | ||
address_hrp: String, | ||
} | ||
|
||
impl Reducer { | ||
fn send_set_add( | ||
&mut self, | ||
slot: u64, | ||
hash: Hash<28>, | ||
cbor: PlutusScript, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
let key = format!("{}", hash); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it looks like you are ignoring a configuration key (prefix) |
||
let member = hex::encode::<Vec<u8>>(cbor.into()); | ||
|
||
let crdt = model::CRDTCommand::LastWriteWins(key, member, slot); | ||
|
||
output.send(gasket::messaging::Message::from(crdt))?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn reduce_alonzo_compatible_tx( | ||
&mut self, | ||
slot: u64, | ||
tx: &alonzo::TransactionWitnessSet, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
if let Some(plutus_scripts) = &tx.plutus_script { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your code is iterative but actually the way one should write this IMHO is that rather than returning OK at the end, one should rather map and collect all results and return them from this function. Now I am by far a Rust noob but perhaps Rust will take a first item out of all results or evaluate lazily and stop execution and return result on first error. @scarmuega do you agree? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it return result on first error if we use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting operator... this is what I thought but you confirmed it. Powerful and confusing operator. |
||
for scr in plutus_scripts.iter() { | ||
let hash = Hasher::<224>::hash_cbor(scr); | ||
|
||
self.send_set_add(slot, hash, scr.clone(), output)?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn reduce_block( | ||
&mut self, | ||
block: &model::MultiEraBlock, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
match block { | ||
model::MultiEraBlock::Byron(_) => Ok(()), | ||
model::MultiEraBlock::AlonzoCompatible(x) => { | ||
x.1.transaction_witness_sets.iter().try_for_each(|tx| { | ||
self.reduce_alonzo_compatible_tx(x.1.header.header_body.slot, tx, output) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn plugin(self, chain: &crosscut::ChainWellKnownInfo) -> super::Reducer { | ||
let reducer = Reducer { | ||
config: self, | ||
address_hrp: chain.address_hrp.clone(), | ||
}; | ||
|
||
super::Reducer::PlutusScriptByHash(reducer) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you need address_hrp for anything, probably copy and paste.