Skip to content
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(CIP15): Add CIP-0015 parser #124

Merged
merged 5 commits into from
May 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/filters/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use serde::Deserialize;

use crate::{
model::{
CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord, NativeWitnessRecord,
OutputAssetRecord, PlutusDatumRecord, PlutusRedeemerRecord, PlutusWitnessRecord,
VKeyWitnessRecord,
CIP15AssetRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord,
NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, PlutusRedeemerRecord,
PlutusWitnessRecord, VKeyWitnessRecord,
},
pipelining::{new_inter_stage_channel, FilterProvider, PartialBootstrapResult, StageReceiver},
Error,
Expand Down Expand Up @@ -227,6 +227,14 @@ fn build_fingerprint(event: &Event, seed: u32) -> Result<String, Error> {
.append_optional(&event.context.tx_hash)?
.append_slice(policy)?
.append_slice(asset)?,
EventData::CIP15Asset(CIP15AssetRecord {
voting_key, nonce, ..
}) => b
.with_slot(&event.context.slot)
.with_prefix("cip15")
.append_optional(&event.context.tx_hash)?
.append_slice(voting_key)?
.append_to_string(nonce)?,
};

b.build()
Expand Down
56 changes: 56 additions & 0 deletions src/mapper/cip15.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use super::EventWriter;
use crate::model::CIP15AssetRecord;
use crate::Error;
use serde_json::Value as JsonValue;

use pallas::ledger::primitives::alonzo::Metadatum;

fn extract_json_property<'a>(json: &'a JsonValue, key: &str) -> Result<&'a JsonValue, Error> {
let result = json
.as_object()
.ok_or_else(|| Error::from("invalid metadatum object for CIP15"))?
.get(key)
.ok_or_else(|| Error::from("required key not found for CIP15"))?;

Ok(result)
}

fn extract_json_string_property(json: &JsonValue, key: &str) -> Result<String, Error> {
let result = extract_json_property(json, key)?
.as_str()
.ok_or_else(|| Error::from("invalid value type for CIP15"))?
.to_string();

Ok(result)
}

fn extract_json_int_property(json: &JsonValue, key: &str) -> Result<i64, Error> {
let result = extract_json_property(json, key)?
.as_i64()
.ok_or_else(|| Error::from("invalid value type for CIP15"))?;

Ok(result)
}

impl EventWriter {
fn to_cip15_asset_record(&self, content: &Metadatum) -> Result<CIP15AssetRecord, Error> {
let raw_json = self.to_metadatum_json(content)?;

Ok(CIP15AssetRecord {
voting_key: extract_json_string_property(&raw_json, "1")?,
stake_pub: extract_json_string_property(&raw_json, "2")?,
reward_address: extract_json_string_property(&raw_json, "3")?,
nonce: extract_json_int_property(&raw_json, "4")?,
raw_json,
})
}

pub(crate) fn crawl_metadata_label_61284(&self, content: &Metadatum) -> Result<(), Error> {
match self.to_cip15_asset_record(content) {
Ok(record) => self.append_from(record)?,
Err(err) => log::warn!("error parsing CIP15: {:?}", err),
}

Ok(())
}
}
1 change: 1 addition & 0 deletions src/mapper/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod byron;
mod cip15;
mod cip25;
mod collect;
mod map;
Expand Down
6 changes: 4 additions & 2 deletions src/mapper/shelley.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ impl EventWriter {
let record = self.to_metadata_record(label, content)?;
self.append_from(record)?;

if u64::from(label) == 721u64 {
self.crawl_metadata_label_721(content)?
match u64::from(label) {
721u64 => self.crawl_metadata_label_721(content)?,
61284u64 => self.crawl_metadata_label_61284(content)?,
_ => (),
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ impl From<CIP25AssetRecord> for EventData {
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct CIP15AssetRecord {
pub voting_key: String,
pub stake_pub: String,
pub reward_address: String,
pub nonce: i64,
pub raw_json: JsonValue,
}

impl From<CIP15AssetRecord> for EventData {
fn from(x: CIP15AssetRecord) -> Self {
EventData::CIP15Asset(x)
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct TxInputRecord {
pub tx_id: String,
Expand Down Expand Up @@ -273,6 +288,9 @@ pub enum EventData {
#[serde(rename = "cip25_asset")]
CIP25Asset(CIP25AssetRecord),

#[serde(rename = "cip15_asset")]
CIP15Asset(CIP15AssetRecord),

Mint(MintRecord),
Collateral {
tx_id: String,
Expand Down
22 changes: 19 additions & 3 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crossterm::style::{Attribute, Color, Stylize};

use crate::{
model::{
BlockRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord,
NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord, PlutusRedeemerRecord,
PlutusWitnessRecord, TransactionRecord, TxInputRecord, TxOutputRecord, VKeyWitnessRecord,
BlockRecord, CIP15AssetRecord, CIP25AssetRecord, Event, EventData, MetadataRecord,
MintRecord, NativeWitnessRecord, OutputAssetRecord, PlutusDatumRecord,
PlutusRedeemerRecord, PlutusWitnessRecord, TransactionRecord, TxInputRecord,
TxOutputRecord, VKeyWitnessRecord,
},
utils::Utils,
};
Expand Down Expand Up @@ -322,6 +323,21 @@ impl LogLine {
source,
max_width,
},
EventData::CIP15Asset(CIP15AssetRecord {
voting_key,
stake_pub,
..
}) => LogLine {
prefix: "CIP15",
color: Color::DarkYellow,
content: format!(
"{{ voting key: {}, stake pub: {} }}",
voting_key,
stake_pub,
),
source,
max_width,
},
}
}
}
Expand Down