diff --git a/dump.rdb b/dump.rdb new file mode 100644 index 00000000..a8ceea3d Binary files /dev/null and b/dump.rdb differ diff --git a/src/reducers/mod.rs b/src/reducers/mod.rs index 678712e9..aa79d96f 100644 --- a/src/reducers/mod.rs +++ b/src/reducers/mod.rs @@ -11,17 +11,12 @@ pub mod pool_by_stake; pub mod utxo_by_address; mod worker; -#[cfg(feature = "unstable")] pub mod address_by_txo; -#[cfg(feature = "unstable")] +pub mod plutus_script_by_hash; pub mod total_transactions_count; -#[cfg(feature = "unstable")] pub mod total_transactions_count_by_contract_addresses; -#[cfg(feature = "unstable")] pub mod transactions_count_by_contract_address; -#[cfg(feature = "unstable")] pub mod transactions_count_by_contract_address_by_epoch; -#[cfg(feature = "unstable")] pub mod transactions_count_by_epoch; #[derive(Deserialize)] @@ -31,19 +26,21 @@ pub enum Config { PointByTx(point_by_tx::Config), PoolByStake(pool_by_stake::Config), - #[cfg(feature = "unstable")] + AddressByTxo(address_by_txo::Config), - #[cfg(feature = "unstable")] + + PlutusScriptByHash(plutus_script_by_hash::Config), + TotalTransactionsCount(total_transactions_count::Config), - #[cfg(feature = "unstable")] + TransactionsCountByEpoch(transactions_count_by_epoch::Config), - #[cfg(feature = "unstable")] + TransactionsCountByContractAddress(transactions_count_by_contract_address::Config), - #[cfg(feature = "unstable")] + TransactionsCountByContractAddressByEpoch( transactions_count_by_contract_address_by_epoch::Config, ), - #[cfg(feature = "unstable")] + TotalTransactionsCountByContractAddresses( total_transactions_count_by_contract_addresses::Config, ), @@ -56,17 +53,19 @@ impl Config { Config::PointByTx(c) => c.plugin(), Config::PoolByStake(c) => c.plugin(), - #[cfg(feature = "unstable")] + Config::AddressByTxo(c) => c.plugin(chain), - #[cfg(feature = "unstable")] + + Config::PlutusScriptByHash(c) => c.plugin(chain), + Config::TotalTransactionsCount(c) => c.plugin(), - #[cfg(feature = "unstable")] + Config::TransactionsCountByEpoch(c) => c.plugin(chain), - #[cfg(feature = "unstable")] + Config::TransactionsCountByContractAddress(c) => c.plugin(chain), - #[cfg(feature = "unstable")] + Config::TransactionsCountByContractAddressByEpoch(c) => c.plugin(chain), - #[cfg(feature = "unstable")] + Config::TotalTransactionsCountByContractAddresses(c) => c.plugin(), } } @@ -106,19 +105,21 @@ pub enum Reducer { PointByTx(point_by_tx::Reducer), PoolByStake(pool_by_stake::Reducer), - #[cfg(feature = "unstable")] + AddressByTxo(address_by_txo::Reducer), - #[cfg(feature = "unstable")] + + PlutusScriptByHash(plutus_script_by_hash::Reducer), + TotalTransactionsCount(total_transactions_count::Reducer), - #[cfg(feature = "unstable")] + TransactionsCountByEpoch(transactions_count_by_epoch::Reducer), - #[cfg(feature = "unstable")] + TransactionsCountByContractAddress(transactions_count_by_contract_address::Reducer), - #[cfg(feature = "unstable")] + TransactionsCountByContractAddressByEpoch( transactions_count_by_contract_address_by_epoch::Reducer, ), - #[cfg(feature = "unstable")] + TotalTransactionsCountByContractAddresses( total_transactions_count_by_contract_addresses::Reducer, ), @@ -135,17 +136,19 @@ impl Reducer { Reducer::PointByTx(x) => x.reduce_block(block, output), Reducer::PoolByStake(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + Reducer::AddressByTxo(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + + Reducer::PlutusScriptByHash(x) => x.reduce_block(block, output), + Reducer::TotalTransactionsCount(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + Reducer::TransactionsCountByEpoch(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + Reducer::TransactionsCountByContractAddress(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + Reducer::TransactionsCountByContractAddressByEpoch(x) => x.reduce_block(block, output), - #[cfg(feature = "unstable")] + Reducer::TotalTransactionsCountByContractAddresses(x) => x.reduce_block(block, output), } } diff --git a/src/reducers/plutus_script_by_hash.rs b/src/reducers/plutus_script_by_hash.rs new file mode 100644 index 00000000..92390651 --- /dev/null +++ b/src/reducers/plutus_script_by_hash.rs @@ -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, + pub filter: Option>, +} + +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); + let member = hex::encode::>(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 { + 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) + } +} diff --git a/testdrive/daemon.toml b/testdrive/daemon.toml index 14ed6dd6..fb6f8632 100644 --- a/testdrive/daemon.toml +++ b/testdrive/daemon.toml @@ -14,13 +14,16 @@ key_prefix = "c2" type = "PoolByStake" key_prefix = "c3" +[[reducers]] +type = "PlutusScriptByHash" +key_prefix = "c4" + [storage] type = "Redis" -connection_params = "redis://redis:6379" +connection_params = "redis://localhost:6379" [intersect] -type = "Point" -value = [57867490, "c491c5006192de2c55a95fb3544f60b96bd1665accaf2dfa2ab12fc7191f016b"] +type = "Tip" [chain] type = "Mainnet"