Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce Supply by Asset reducer
- Loading branch information
Showing
5 changed files
with
203 additions
and
4 deletions.
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 |
---|---|---|
|
@@ -2,5 +2,6 @@ mod args; | |
pub mod epochs; | ||
pub mod filters; | ||
pub mod policies; | ||
pub mod time; | ||
|
||
pub use args::*; |
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,72 @@ | ||
use super::ChainWellKnownInfo; | ||
|
||
#[inline] | ||
fn compute_linear_timestamp( | ||
known_slot: u64, | ||
known_time: u64, | ||
slot_length: u64, | ||
query_slot: u64, | ||
) -> u64 { | ||
known_time + (query_slot - known_slot) * slot_length | ||
} | ||
|
||
#[inline] | ||
fn compute_era_epoch(era_slot: u64, era_slot_length: u64, era_epoch_length: u64) -> (u64, u64) { | ||
let epoch = (era_slot * era_slot_length) / era_epoch_length; | ||
let reminder = era_slot % era_epoch_length; | ||
|
||
(epoch, reminder) | ||
} | ||
|
||
/// A naive, standalone implementation of a time provider | ||
/// | ||
/// This time provider doesn't require any external resources other than an | ||
/// initial config. It works by applying simple slot => wallclock conversion | ||
/// logic from a well-known configured point in the chain, assuming homogeneous | ||
/// slot length from that point forward. | ||
#[derive(Clone)] | ||
pub(crate) struct NaiveProvider { | ||
config: ChainWellKnownInfo, | ||
} | ||
|
||
impl NaiveProvider { | ||
pub fn new(config: ChainWellKnownInfo) -> Self { | ||
assert!( | ||
config.byron_epoch_length > 0, | ||
"byron epoch length needs to be greater than zero" | ||
); | ||
|
||
assert!( | ||
config.shelley_epoch_length > 0, | ||
"shelley epoch length needs to be greater than zero" | ||
); | ||
|
||
let (shelley_start_epoch, _) = compute_era_epoch( | ||
config.shelley_known_slot, | ||
config.byron_slot_length as u64, | ||
config.byron_epoch_length as u64, | ||
); | ||
|
||
NaiveProvider { config } | ||
} | ||
|
||
pub fn slot_to_wallclock(&self, slot: u64) -> u64 { | ||
let NaiveProvider { config, .. } = self; | ||
|
||
if slot < config.shelley_known_slot { | ||
compute_linear_timestamp( | ||
config.byron_known_slot, | ||
config.byron_known_time, | ||
config.byron_slot_length as u64, | ||
slot, | ||
) | ||
} else { | ||
compute_linear_timestamp( | ||
config.shelley_known_slot, | ||
config.shelley_known_time, | ||
config.shelley_slot_length as u64, | ||
slot, | ||
) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::str::FromStr; | ||
|
||
use gasket::error::AsWorkError; | ||
use pallas::crypto::hash::Hash; | ||
use pallas::ledger::traverse::Asset; | ||
use pallas::ledger::traverse::{MultiEraBlock, MultiEraTx}; | ||
use serde::Deserialize; | ||
|
||
use crate::{crosscut, model}; | ||
|
||
#[derive(Deserialize)] | ||
pub struct Config { | ||
pub key_prefix: Option<String>, | ||
pub policy_ids_hex: Option<Vec<String>>, | ||
} | ||
|
||
pub struct Reducer { | ||
config: Config, | ||
policy: crosscut::policies::RuntimePolicy, | ||
policy_ids: Option<Vec<Hash<28>>>, | ||
} | ||
|
||
impl Reducer { | ||
fn is_policy_id_accepted(&self, policy_id: &Hash<28>) -> bool { | ||
return match &self.policy_ids { | ||
Some(pids) => pids.contains(&policy_id), | ||
None => true, | ||
}; | ||
} | ||
|
||
fn process_asset( | ||
&mut self, | ||
policy: Hash<28>, | ||
asset: Vec<u8>, | ||
qty: i64, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
if !self.is_policy_id_accepted(&policy) { | ||
return Ok(()); | ||
} | ||
let prefix = self.config.key_prefix.as_deref(); | ||
let asset_id = &format!("{}{}", policy, hex::encode(asset)); | ||
|
||
let key = match &self.config.key_prefix { | ||
Some(prefix) => format!("{}.{}", prefix, asset_id), | ||
None => format!("{}.{}", "supply_by_asset".to_string(), asset_id), | ||
}; | ||
|
||
let crdt = model::CRDTCommand::PNCounter(key, qty); | ||
|
||
output.send(crdt.into()) | ||
} | ||
|
||
pub fn reduce_block<'b>( | ||
&mut self, | ||
block: &'b MultiEraBlock<'b>, | ||
ctx: &model::BlockContext, | ||
output: &mut super::OutputPort, | ||
) -> Result<(), gasket::error::Error> { | ||
for tx in block.txs().into_iter() { | ||
for (idx, txo) in ctx.find_consumed_txos(&tx, &self.policy).or_panic()? { | ||
for asset in txo.assets() { | ||
if let Asset::NativeAsset(policy, asset, qty) = asset { | ||
self.process_asset(policy,asset, -1 * qty as i64, output)?; | ||
} | ||
} | ||
} | ||
|
||
for (idx, txo) in tx.produces() { | ||
for asset in txo.assets() { | ||
if let Asset::NativeAsset(policy, asset, qty) = asset { | ||
self.process_asset(policy, asset, qty as i64, output)?; | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
impl Config { | ||
pub fn plugin(self, policy: &crosscut::policies::RuntimePolicy) -> super::Reducer { | ||
let policy_ids: Option<Vec<Hash<28>>> = match &self.policy_ids_hex { | ||
Some(pids) => { | ||
let ps = pids | ||
.iter() | ||
.map(|pid| Hash::<28>::from_str(pid).expect("invalid policy_id")) | ||
.collect(); | ||
|
||
Some(ps) | ||
} | ||
None => None, | ||
}; | ||
let reducer = Reducer { | ||
config: self, | ||
policy: policy.clone(), | ||
}; | ||
|
||
super::Reducer::UtxosByAsset(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