Skip to content

Commit

Permalink
Merge branch 'develop' into blockfrost-crate-initial
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob authored Apr 16, 2024
2 parents 224d133 + efe99fd commit 41bc64c
Show file tree
Hide file tree
Showing 87 changed files with 2,393 additions and 1,069 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- uses: actions/setup-node@v1
- uses: actions/setup-node@v4
with:
node-version: '12.18.1'
node-version: 'lts/iron'
- name: Cache node modules
uses: actions/cache@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12.18.1
lts/iron
6 changes: 3 additions & 3 deletions chain/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cml-chain"
version = "5.1.0"
version = "5.2.0"
edition = "2018"
authors = ["dcSpark"]
license = "MIT"
Expand All @@ -14,8 +14,8 @@ keywords = ["cardano"]
crate-type = ["cdylib", "rlib"]

[dependencies]
cml-core = { "path" = "../../core/rust", version = "5.1.0" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.1.0" }
cml-core = { "path" = "../../core/rust", version = "5.2.0" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.2.0" }
cbor_event = "2.2.0"
linked-hash-map = "0.5.3"
derivative = "2.2.0"
Expand Down
75 changes: 63 additions & 12 deletions core/rust/src/metadata.rs → chain/rust/src/auxdata/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use crate::error::{DeserializeError, DeserializeFailure};

use crate::serialization::{fit_sz, Deserialize, LenEncoding, Serialize, StringEncoding};
use crate::Int;
use crate::json::metadatums::{
decode_metadatum_to_json_value, encode_json_value_to_metadatum, MetadataJsonSchema,
};
use cbor_event::{de::Deserializer, se::Serializer};
use cml_core::{
error::{DeserializeError, DeserializeFailure},
serialization::{fit_sz, Deserialize, LenEncoding, Serialize, StringEncoding},
Int,
};
use derivative::Derivative;

use std::io::{BufRead, Seek, Write};
Expand Down Expand Up @@ -149,13 +153,10 @@ impl Deserialize for Metadata {

/// Handles the extremely rare (2 total instances on mainnet) edge-case of in
/// previous generations allowing duplicate metadatum keys.
#[derive(
Clone, Debug, Default, serde::Deserialize, serde::Serialize, schemars::JsonSchema, Derivative,
)]
#[derive(Clone, Debug, Default, Derivative)]
#[derivative(Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct MetadatumMap {
pub entries: Vec<(TransactionMetadatum, TransactionMetadatum)>,
#[serde(skip)]
#[derivative(
PartialEq = "ignore",
Ord = "ignore",
Expand Down Expand Up @@ -273,7 +274,7 @@ impl Deserialize for MetadatumMap {
}
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, Derivative)]
#[derive(Clone, Debug, Derivative)]
#[derivative(
Eq,
PartialEq,
Expand All @@ -291,7 +292,6 @@ pub enum TransactionMetadatum {
PartialOrd = "ignore",
Hash = "ignore"
)]
#[serde(skip)]
elements_encoding: LenEncoding,
},
Int(Int),
Expand All @@ -303,7 +303,6 @@ pub enum TransactionMetadatum {
PartialOrd = "ignore",
Hash = "ignore"
)]
#[serde(skip)]
bytes_encoding: StringEncoding,
},
Text {
Expand All @@ -314,7 +313,6 @@ pub enum TransactionMetadatum {
PartialOrd = "ignore",
Hash = "ignore"
)]
#[serde(skip)]
text_encoding: StringEncoding,
},
}
Expand Down Expand Up @@ -401,6 +399,51 @@ impl TransactionMetadatum {
}
}

impl serde::Serialize for TransactionMetadatum {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let json_value = decode_metadatum_to_json_value(self, MetadataJsonSchema::DetailedSchema)
.expect("DetailedSchema can represent everything");
serde_json::Value::from(json_value).serialize(serializer)
}
}

impl<'de> serde::de::Deserialize<'de> for TransactionMetadatum {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'de>,
{
let serde_json_value =
<serde_json::Value as serde::de::Deserialize>::deserialize(deserializer)?;
let json_value = crate::json::json_serialize::Value::from(serde_json_value);
encode_json_value_to_metadatum(json_value.clone(), MetadataJsonSchema::DetailedSchema)
.map_err(|_e| {
serde::de::Error::invalid_value(
(&json_value).into(),
&"invalid tx metadatum (cardano-node JSON format)",
)
})
}
}

impl schemars::JsonSchema for TransactionMetadatum {
fn schema_name() -> String {
String::from("TransactionMetadatum")
}

fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
schemars::schema::Schema::from(schemars::schema::SchemaObject::new_ref(
"TransactionMetadatum".to_owned(),
))
}

fn is_referenceable() -> bool {
true
}
}

impl Serialize for TransactionMetadatum {
fn serialize<'se, W: Write>(
&self,
Expand Down Expand Up @@ -556,4 +599,12 @@ mod tests {
let output_bytes = decode_arbitrary_bytes_from_metadatum(&metadata).expect("decode failed");
assert_eq!(input_bytes, output_bytes);
}

#[test]
fn metadatum_default_json() {
let json_str = "{\"map\":[{\"k\":{\"list\":[{\"map\":[{\"k\":{\"int\":5},\"v\":{\"int\":-7}},{\"k\":{\"string\":\"hello\"},\"v\":{\"string\":\"world\"}}]},{\"bytes\":\"ff00ff00\"}]},\"v\":{\"int\":5}}]}";
let metadatum: TransactionMetadatum = serde_json::from_str(json_str).unwrap();
let roundtrip_str = serde_json::to_string(&metadatum).unwrap();
assert_eq!(json_str, roundtrip_str);
}
}
3 changes: 2 additions & 1 deletion chain/rust/src/auxdata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// https://github.com/dcSpark/cddl-codegen

pub mod cbor_encodings;
pub mod metadata;
pub mod serialization;
pub mod utils;

use crate::plutus::{PlutusV1Script, PlutusV2Script, PlutusV3Script};
use crate::transaction::NativeScript;
use cbor_encodings::{ConwayFormatAuxDataEncoding, ShelleyMaFormatAuxDataEncoding};

pub use cml_core::metadata::*;
pub use metadata::*;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub enum AuxiliaryData {
Expand Down
2 changes: 1 addition & 1 deletion chain/rust/src/auxdata/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cml_core::metadata::Metadata;
use crate::auxdata::metadata::Metadata;

use crate::{
plutus::{PlutusV1Script, PlutusV2Script},
Expand Down
4 changes: 1 addition & 3 deletions chain/rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,16 +1877,14 @@ mod tests {
use std::collections::BTreeMap;
use std::ops::Deref;

use cml_core::metadata::{
Metadata, MetadatumMap, TransactionMetadatum, TransactionMetadatumLabel,
};
use cml_core::Int;
use cml_crypto::{
Bip32PrivateKey, Bip32PublicKey, DatumHash, Deserialize, PrivateKey, RawBytesEncoding,
TransactionHash,
};

use crate::address::{Address, BaseAddress, EnterpriseAddress, Pointer, PointerAddress};
use crate::auxdata::{Metadata, MetadatumMap, TransactionMetadatum, TransactionMetadatumLabel};
use crate::builders::witness_builder::{PartialPlutusWitness, PlutusScriptWitness};
use crate::builders::{
input_builder::SingleInputBuilder, mint_builder::SingleMintBuilder,
Expand Down
48 changes: 24 additions & 24 deletions chain/rust/src/byron/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl Deserialize for AddressContent {
.map_err(|e: DeserializeError| e.annotate("addr_attributes"))?;
let addr_type = (|| -> Result<_, DeserializeError> {
let initial_position = raw.as_mut_ref().stream_position().unwrap();
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let public_key_value = raw.unsigned_integer()?;
if public_key_value != 0 {
return Err(DeserializeFailure::FixedValueMismatch {
Expand All @@ -207,15 +207,15 @@ impl Deserialize for AddressContent {
.into());
}
Ok(())
})(raw)
{
})(raw);
match deser_variant {
Ok(()) => return Ok(ByronAddrType::PublicKey),
Err(_) => raw
.as_mut_ref()
.seek(SeekFrom::Start(initial_position))
.unwrap(),
};
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let script_value = raw.unsigned_integer()?;
if script_value != 1 {
return Err(DeserializeFailure::FixedValueMismatch {
Expand All @@ -225,15 +225,15 @@ impl Deserialize for AddressContent {
.into());
}
Ok(())
})(raw)
{
})(raw);
match deser_variant {
Ok(()) => return Ok(ByronAddrType::Script),
Err(_) => raw
.as_mut_ref()
.seek(SeekFrom::Start(initial_position))
.unwrap(),
};
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let redeem_value = raw.unsigned_integer()?;
if redeem_value != 2 {
return Err(DeserializeFailure::FixedValueMismatch {
Expand All @@ -243,8 +243,8 @@ impl Deserialize for AddressContent {
.into());
}
Ok(())
})(raw)
{
})(raw);
match deser_variant {
Ok(()) => return Ok(ByronAddrType::Redeem),
Err(_) => raw
.as_mut_ref()
Expand Down Expand Up @@ -419,7 +419,7 @@ impl Deserialize for SpendingData {
let len = raw.array()?;
let _read_len = CBORReadLen::from(len);
let initial_position = raw.as_mut_ref().stream_position().unwrap();
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
(|| -> Result<_, DeserializeError> {
let tag_value = raw.unsigned_integer()?;
if tag_value != 0 {
Expand Down Expand Up @@ -448,15 +448,15 @@ impl Deserialize for SpendingData {
},
}
Ok(Self::SpendingDataPubKey(pubkey))
})(raw)
{
})(raw);
match deser_variant {
Ok(variant) => return Ok(variant),
Err(_) => raw
.as_mut_ref()
.seek(SeekFrom::Start(initial_position))
.unwrap(),
};
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
(|| -> Result<_, DeserializeError> {
let tag_value = raw.unsigned_integer()?;
if tag_value != 1 {
Expand Down Expand Up @@ -485,15 +485,15 @@ impl Deserialize for SpendingData {
},
}
Ok(Self::SpendingDataScript(script))
})(raw)
{
})(raw);
match deser_variant {
Ok(variant) => return Ok(variant),
Err(_) => raw
.as_mut_ref()
.seek(SeekFrom::Start(initial_position))
.unwrap(),
};
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
(|| -> Result<_, DeserializeError> {
let tag_value = raw.unsigned_integer()?;
if tag_value != 2 {
Expand Down Expand Up @@ -522,8 +522,8 @@ impl Deserialize for SpendingData {
},
}
Ok(Self::SpendingDataRedeem(redeem))
})(raw)
{
})(raw);
match deser_variant {
Ok(variant) => return Ok(variant),
Err(_) => raw
.as_mut_ref()
Expand Down Expand Up @@ -572,7 +572,7 @@ impl Deserialize for StakeDistribution {
let len = raw.array()?;
let _read_len = CBORReadLen::from(len);
let initial_position = raw.as_mut_ref().stream_position().unwrap();
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
(|| -> Result<_, DeserializeError> {
let tag_value = raw.unsigned_integer()?;
if tag_value != 0 {
Expand Down Expand Up @@ -601,15 +601,15 @@ impl Deserialize for StakeDistribution {
},
}
Ok(Self::SingleKey(stakeholder_id))
})(raw)
{
})(raw);
match deser_variant {
Ok(variant) => return Ok(variant),
Err(_) => raw
.as_mut_ref()
.seek(SeekFrom::Start(initial_position))
.unwrap(),
};
match (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let deser_variant = (|raw: &mut Deserializer<_>| -> Result<_, DeserializeError> {
let bootstrap_era_distr_value = raw.unsigned_integer()?;
if bootstrap_era_distr_value != 1 {
return Err(DeserializeFailure::FixedValueMismatch {
Expand All @@ -619,8 +619,8 @@ impl Deserialize for StakeDistribution {
.into());
}
Ok(())
})(raw)
{
})(raw);
match deser_variant {
Ok(()) => return Ok(StakeDistribution::BootstrapEra),
Err(_) => raw
.as_mut_ref()
Expand Down
Loading

0 comments on commit 41bc64c

Please sign in to comment.