Skip to content

Commit

Permalink
Merge branch 'develop' into docs-update-again
Browse files Browse the repository at this point in the history
  • Loading branch information
rooooooooob authored May 21, 2024
2 parents e1793c8 + 255500b commit 9f645c0
Show file tree
Hide file tree
Showing 145 changed files with 7,340 additions and 4,058 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
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ members = [
# exclude old crate structure to avoid error in it
exclude = [
"rust",
"rust/json-gen"
"rust/json-gen",
"tools/metadata-cddl-checker",
"tools/plutus-datum-codegen"
]

[profile.release]
Expand Down
1 change: 1 addition & 0 deletions build-and-test.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
set -eu
. test.sh && npm run rust:build-nodejs
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.3.1"
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.3.1" }
cml-crypto = { "path" = "../../crypto/rust", version = "5.3.1" }
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
14 changes: 7 additions & 7 deletions chain/rust/src/block/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,13 @@ impl Deserialize for HeaderBody {
(|| -> Result<_, DeserializeError> {
let (block_number, block_number_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("block_number"))?;
let (slot, slot_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("slot"))?;
let (prev_hash, prev_hash_encoding) = (|| -> Result<_, DeserializeError> {
Ok(match raw.cbor_type()? != cbor_event::Type::Special {
Expand Down Expand Up @@ -444,8 +444,8 @@ impl Deserialize for HeaderBody {
.map_err(|e: DeserializeError| e.annotate("vrf_result"))?;
let (block_body_size, block_body_size_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("block_body_size"))?;
let (block_body_hash, block_body_hash_encoding) = raw
.bytes_sz()
Expand Down Expand Up @@ -600,13 +600,13 @@ impl DeserializeEmbeddedGroup for OperationalCert {
.map_err(|e: DeserializeError| e.annotate("hot_vkey"))?;
let (sequence_number, sequence_number_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("sequence_number"))?;
let (kes_period, kes_period_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("kes_period"))?;
let (sigma, sigma_encoding) = raw
.bytes_sz()
Expand Down Expand Up @@ -716,13 +716,13 @@ impl DeserializeEmbeddedGroup for ProtocolVersion {
(|| -> Result<_, DeserializeError> {
let (major, major_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("major"))?;
let (minor, minor_encoding) = raw
.unsigned_integer_sz()
.map(|(x, enc)| (x, Some(enc)))
.map_err(Into::<DeserializeError>::into)
.map(|(x, enc)| (x, Some(enc)))
.map_err(|e: DeserializeError| e.annotate("minor"))?;
Ok(ProtocolVersion {
major,
Expand Down
6 changes: 3 additions & 3 deletions chain/rust/src/builders/certificate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::witness_builder::{NativeScriptWitnessInfo, RequiredWitnessSet};

use crate::{
certs::{Certificate, StakeCredential},
transaction::RequiredSigners,
RequiredSigners,
};

use cml_crypto::{Ed25519KeyHash, ScriptHash};
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn cert_required_wits(cert: &Certificate, required_witnesses: &mut RequiredW
required_witnesses.add_from_credential(cert.stake_credential.clone());
}
Certificate::PoolRegistration(cert) => {
for owner in &cert.pool_params.pool_owners {
for owner in cert.pool_params.pool_owners.as_ref() {
required_witnesses.add_vkey_key_hash(*owner);
}
required_witnesses.add_vkey_key_hash(cert.pool_params.operator);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn add_cert_vkeys(
}
},
Certificate::PoolRegistration(cert) => {
for owner in &cert.pool_params.pool_owners {
for owner in cert.pool_params.pool_owners.as_ref() {
vkeys.insert(*owner);
}
vkeys.insert(cert.pool_params.operator);
Expand Down
5 changes: 3 additions & 2 deletions chain/rust/src/builders/input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::{
certs::StakeCredential,
crypto::hash::hash_plutus_data,
plutus::PlutusData,
transaction::{RequiredSigners, TransactionInput, TransactionOutput},
NativeScript,
transaction::{TransactionInput, TransactionOutput},
NativeScript, RequiredSigners,
};

#[derive(Debug, thiserror::Error)]
Expand Down Expand Up @@ -140,6 +140,7 @@ impl SingleInputBuilder {
) -> Result<InputBuilderResult, InputBuilderError> {
let mut required_wits = RequiredWitnessSet::default();
required_signers
.as_ref()
.iter()
.for_each(|required_signer| required_wits.add_vkey_key_hash(*required_signer));
input_required_wits(&self.utxo_info, &mut required_wits);
Expand Down
2 changes: 1 addition & 1 deletion chain/rust/src/builders/mint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::witness_builder::{NativeScriptWitnessInfo, RequiredWitnessSet};

use cml_core::ordered_hash_map::OrderedHashMap;

use crate::{assets::AssetName, transaction::RequiredSigners, NativeScript, PolicyId};
use crate::{assets::AssetName, NativeScript, PolicyId, RequiredSigners};

#[derive(Clone)]
pub struct MintBuilderResult {
Expand Down
Loading

0 comments on commit 9f645c0

Please sign in to comment.