From 822809183ed4ac21a871d6f460fa67bd71364d11 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Mon, 15 Apr 2024 18:31:46 -0300 Subject: [PATCH 1/2] fix(configs): parse into rational numbers --- pallas-configs/Cargo.toml | 1 + pallas-configs/src/shelley.rs | 73 ++++++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/pallas-configs/Cargo.toml b/pallas-configs/Cargo.toml index aa60b149..9f1f3217 100644 --- a/pallas-configs/Cargo.toml +++ b/pallas-configs/Cargo.toml @@ -20,6 +20,7 @@ serde = { version = "1.0.136", optional = true, features = ["derive"] } serde_json = { version = "1.0.79", optional = true } base64 = "0.22.0" serde_with = "3.7.0" +num-rational = "0.4.1" [features] json = ["serde", "serde_json"] diff --git a/pallas-configs/src/shelley.rs b/pallas-configs/src/shelley.rs index 03350483..1b348143 100644 --- a/pallas-configs/src/shelley.rs +++ b/pallas-configs/src/shelley.rs @@ -1,5 +1,24 @@ -use serde::Deserialize; -use std::collections::HashMap; +use num_rational::BigRational; +use pallas_crypto::hash::Hash; +use pallas_primitives::conway::RationalNumber; +use serde::{Deserialize, Deserializer}; +use std::{collections::HashMap, str::FromStr}; + +fn deserialize_rational<'de, D>( + deserializer: D, +) -> Result +where + D: Deserializer<'de>, +{ + let s = f32::deserialize(deserializer)?; + let r = BigRational::from_float(s).unwrap(); + let r = pallas_primitives::alonzo::RationalNumber { + numerator: r.numer().try_into().unwrap(), + denominator: r.denom().try_into().unwrap(), + }; + + Ok(r) +} #[derive(Debug, Deserialize)] #[serde(rename_all = "camelCase")] @@ -21,10 +40,37 @@ impl From for pallas_primitives::alonzo::ProtocolVersion { } } -#[derive(Debug, Deserialize)] +#[derive(Debug, Deserialize, Clone)] +pub enum NonceVariant { + NeutralNonce, + Nonce, +} + +impl From for pallas_primitives::alonzo::NonceVariant { + fn from(value: NonceVariant) -> Self { + match value { + NonceVariant::NeutralNonce => Self::NeutralNonce, + NonceVariant::Nonce => Self::Nonce, + } + } +} + +#[derive(Debug, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct ExtraEntropy { - pub tag: Option, + pub tag: NonceVariant, + pub hash: Option, +} + +impl From for pallas_primitives::alonzo::Nonce { + fn from(value: ExtraEntropy) -> Self { + Self { + variant: value.tag.into(), + hash: value + .hash + .map(|x| Hash::<32>::from_str(&x).expect("invalid nonce hash value")), + } + } } #[derive(Debug, Deserialize)] @@ -42,13 +88,20 @@ pub struct ProtocolParams { pub pool_deposit: u64, pub n_opt: u32, pub min_pool_cost: u64, + pub e_max: u32, + pub extra_entropy: ExtraEntropy, + + #[serde(deserialize_with = "deserialize_rational")] + pub decentralisation_param: RationalNumber, + + #[serde(deserialize_with = "deserialize_rational")] + pub rho: pallas_primitives::alonzo::RationalNumber, + + #[serde(deserialize_with = "deserialize_rational")] + pub tau: pallas_primitives::alonzo::RationalNumber, - pub decentralisation_param: Option, - pub e_max: Option, - pub extra_entropy: Option, - pub rho: Option, - pub tau: Option, - pub a0: Option, + #[serde(deserialize_with = "deserialize_rational")] + pub a0: pallas_primitives::alonzo::RationalNumber, } #[derive(Debug, Deserialize)] From 2dfff3f369c91d6d03f81f881af9854e493d4639 Mon Sep 17 00:00:00 2001 From: Santiago Carmuega Date: Mon, 15 Apr 2024 18:39:34 -0300 Subject: [PATCH 2/2] improve error handling --- pallas-configs/src/shelley.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pallas-configs/src/shelley.rs b/pallas-configs/src/shelley.rs index 1b348143..8609d930 100644 --- a/pallas-configs/src/shelley.rs +++ b/pallas-configs/src/shelley.rs @@ -11,10 +11,12 @@ where D: Deserializer<'de>, { let s = f32::deserialize(deserializer)?; - let r = BigRational::from_float(s).unwrap(); + let r = BigRational::from_float(s) + .ok_or(serde::de::Error::custom("can't turn float into rational"))?; + let r = pallas_primitives::alonzo::RationalNumber { - numerator: r.numer().try_into().unwrap(), - denominator: r.denom().try_into().unwrap(), + numerator: r.numer().try_into().map_err(serde::de::Error::custom)?, + denominator: r.denom().try_into().map_err(serde::de::Error::custom)?, }; Ok(r)