Skip to content

Commit

Permalink
D-parameter and permissioned candidates transactions offchain
Browse files Browse the repository at this point in the history
  • Loading branch information
LGLO authored Nov 12, 2024
1 parent 56ad7cf commit 651c851
Show file tree
Hide file tree
Showing 12 changed files with 1,033 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ on how to create these new data sources see `node/src/main_chain_follower.rs` fi
* `partner-chains-cli` does not use `pc-contracts-cli` in `prepare-configuration` wizard, it uses `partner-chains-cardano-offchain` crate instead.
* Update cardano-node to 10.1.2
* Bumped `partner-chains-smart-contracts` version to v6.2.2
* Added d-parameter and permissioned-candidates transaction building (not wired in)

## Removed

Expand Down
21 changes: 20 additions & 1 deletion primitives/plutus-data/src/d_param.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{DataDecodingError, DecodingResult, PlutusDataExtensions};
use cardano_serialization_lib::PlutusData;
use cardano_serialization_lib::{PlutusData, PlutusList};

#[derive(Clone, Debug, PartialEq)]
pub enum DParamDatum {
Expand All @@ -24,6 +24,13 @@ impl From<DParamDatum> for sidechain_domain::DParameter {
}
}

pub fn d_parameter_to_plutus_data(d_param: &sidechain_domain::DParameter) -> PlutusData {
let mut list = PlutusList::new();
list.add(&PlutusData::new_integer(&d_param.num_permissioned_candidates.into()));
list.add(&PlutusData::new_integer(&d_param.num_registered_candidates.into()));
PlutusData::new_list(&list)
}

/// Parses plutus data schema that was used before datum versioning was added. Kept for backwards compatibility.
fn decode_legacy_d_parameter_datum(datum: PlutusData) -> DecodingResult<DParamDatum> {
let d_parameter = datum
Expand Down Expand Up @@ -62,4 +69,16 @@ mod tests {

assert_eq!(DParamDatum::try_from(plutus_data).unwrap(), expected_datum)
}

#[test]
fn domain_d_param_to_csl() {
let d_param = sidechain_domain::DParameter {
num_permissioned_candidates: 1,
num_registered_candidates: 2,
};

let expected_plutus_data = test_plutus_data!({"list": [{"int": 1}, {"int": 2}]});

assert_eq!(d_parameter_to_plutus_data(&d_param), expected_plutus_data)
}
}
55 changes: 55 additions & 0 deletions primitives/plutus-data/src/permissioned_candidates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ impl TryFrom<PlutusData> for PermissionedCandidateDatums {
}
}

pub fn permissioned_candidates_to_plutus_data(
candidates: &[PermissionedCandidateData],
) -> PlutusData {
let mut list = PlutusList::new();
for candidate in candidates {
let mut candidate_datum = PlutusList::new();
candidate_datum.add(&PlutusData::new_bytes(candidate.sidechain_public_key.0.clone()));
candidate_datum.add(&PlutusData::new_bytes(candidate.aura_public_key.0.clone()));
candidate_datum.add(&PlutusData::new_bytes(candidate.grandpa_public_key.0.clone()));
list.add(&PlutusData::new_list(&candidate_datum));
}
PlutusData::new_list(&list)
}

/// Parses plutus data schema that was used before datum versioning was added. Kept for backwards compatibility.
fn decode_legacy_permissioned_candidates_datums(
datum: PlutusData,
Expand Down Expand Up @@ -112,4 +126,45 @@ mod tests {

assert_eq!(PermissionedCandidateDatums::try_from(plutus_data).unwrap(), expected_datum)
}

#[test]
fn permissioned_candidates_to_datum() {
let input = vec![
PermissionedCandidateData {
sidechain_public_key: SidechainPublicKey(
hex!("cb6df9de1efca7a3998a8ead4e02159d5fa99c3e0d4fd6432667390bb4726854").into(),
),
aura_public_key: AuraPublicKey(
hex!("bf20afa1c1a72af3341fa7a447e3f9eada9f3d054a7408fb9e49ad4d6e6559ec").into(),
),
grandpa_public_key: GrandpaPublicKey(
hex!("9042a40b0b1baa9adcead024432a923eac706be5e1a89d7f2f2d58bfa8f3c26d").into(),
),
},
PermissionedCandidateData {
sidechain_public_key: SidechainPublicKey(
hex!("79c3b7fc0b7697b9414cb87adcb37317d1cab32818ae18c0e97ad76395d1fdcf").into(),
),
aura_public_key: AuraPublicKey(
hex!("56d1da82e56e4cb35b13de25f69a3e9db917f3e13d6f786321f4b0a9dc153b19").into(),
),
grandpa_public_key: GrandpaPublicKey(
hex!("7392f3ea668aa2be7997d82c07bcfbec3ee4a9a4e01e3216d92b8f0d0a086c32").into(),
),
},
];
let expected_plutus_data = test_plutus_data!({"list": [
{"list": [
{"bytes": "cb6df9de1efca7a3998a8ead4e02159d5fa99c3e0d4fd6432667390bb4726854"},
{"bytes": "bf20afa1c1a72af3341fa7a447e3f9eada9f3d054a7408fb9e49ad4d6e6559ec"},
{"bytes": "9042a40b0b1baa9adcead024432a923eac706be5e1a89d7f2f2d58bfa8f3c26d"}
]},
{"list": [
{"bytes": "79c3b7fc0b7697b9414cb87adcb37317d1cab32818ae18c0e97ad76395d1fdcf"},
{"bytes": "56d1da82e56e4cb35b13de25f69a3e9db917f3e13d6f786321f4b0a9dc153b19"},
{"bytes": "7392f3ea668aa2be7997d82c07bcfbec3ee4a9a4e01e3216d92b8f0d0a086c32"}
]}
]});
assert_eq!(permissioned_candidates_to_plutus_data(&input), expected_plutus_data)
}
}
1 change: 1 addition & 0 deletions toolkit/offchain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ minicbor = { workspace = true }
ogmios-client = { workspace = true, features = ["jsonrpsee-client"] }
pallas-addresses = { workspace = true }
pallas-primitives = { workspace = true }
partner-chains-plutus-data = { workspace = true }
plutus = { workspace = true }
raw-scripts = { workspace = true }
serde = { workspace = true }
Expand Down
Loading

0 comments on commit 651c851

Please sign in to comment.