From 81390296a3ca2e2ec09a2937e687c27462cafdae Mon Sep 17 00:00:00 2001 From: Matthias Benkort <5680256+KtorZ@users.noreply.github.com> Date: Sun, 11 Aug 2024 01:08:45 +0200 Subject: [PATCH] fix(primitives): expose hidden struct fields in Conway (#501) * Provide slightly better error when failing to decode Plutus data. * Expose GovActionId, Constitution & Anchor inner fields --- pallas-primitives/src/alonzo/model.rs | 6 ++-- pallas-primitives/src/conway/model.rs | 42 +++++++++++++++++++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/pallas-primitives/src/alonzo/model.rs b/pallas-primitives/src/alonzo/model.rs index a4c92e2..c39567e 100644 --- a/pallas-primitives/src/alonzo/model.rs +++ b/pallas-primitives/src/alonzo/model.rs @@ -1057,9 +1057,9 @@ impl<'b, C> minicbor::decode::Decode<'b, C> for PlutusData { Ok(Self::Array(d.decode_with(ctx)?)) } - _ => Err(minicbor::decode::Error::message( - "bad cbor data type for plutus data", - )), + any => Err(minicbor::decode::Error::message(format!( + "bad cbor data type ({any:?}) for plutus data" + ))), } } } diff --git a/pallas-primitives/src/conway/model.rs b/pallas-primitives/src/conway/model.rs index b7f7bdb..66c175d 100644 --- a/pallas-primitives/src/conway/model.rs +++ b/pallas-primitives/src/conway/model.rs @@ -1032,13 +1032,19 @@ impl minicbor::encode::Encode for GovAction { } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct Constitution(Anchor, Nullable); +pub struct Constitution { + pub anchor: Anchor, + pub guardrail_script: Nullable, +} impl<'b, C> minicbor::Decode<'b, C> for Constitution { fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result { d.array()?; - Ok(Self(d.decode_with(ctx)?, d.decode_with(ctx)?)) + Ok(Self { + anchor: d.decode_with(ctx)?, + guardrail_script: d.decode_with(ctx)?, + }) } } @@ -1050,8 +1056,8 @@ impl minicbor::Encode for Constitution { ) -> Result<(), minicbor::encode::Error> { e.array(2)?; - e.encode_with(&self.0, ctx)?; - e.encode_with(&self.1, ctx)?; + e.encode_with(&self.anchor, ctx)?; + e.encode_with(&self.guardrail_script, ctx)?; Ok(()) } @@ -1128,13 +1134,19 @@ impl minicbor::encode::Encode for Voter { } #[derive(Serialize, Deserialize, Debug, PartialEq, PartialOrd, Eq, Ord, Clone)] -pub struct Anchor(String, Hash<32>); +pub struct Anchor { + pub url: String, + pub content_hash: Hash<32>, +} impl<'b, C> minicbor::Decode<'b, C> for Anchor { fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result { d.array()?; - Ok(Self(d.decode_with(ctx)?, d.decode_with(ctx)?)) + Ok(Self { + url: d.decode_with(ctx)?, + content_hash: d.decode_with(ctx)?, + }) } } @@ -1146,21 +1158,27 @@ impl minicbor::Encode for Anchor { ) -> Result<(), minicbor::encode::Error> { e.array(2)?; - e.encode_with(&self.0, ctx)?; - e.encode_with(self.1, ctx)?; + e.encode_with(&self.url, ctx)?; + e.encode_with(self.content_hash, ctx)?; Ok(()) } } #[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)] -pub struct GovActionId(Hash<32>, u32); +pub struct GovActionId { + pub transaction_id: Hash<32>, + pub action_index: u32, +} impl<'b, C> minicbor::Decode<'b, C> for GovActionId { fn decode(d: &mut minicbor::Decoder<'b>, ctx: &mut C) -> Result { d.array()?; - Ok(Self(d.decode_with(ctx)?, d.decode_with(ctx)?)) + Ok(Self { + transaction_id: d.decode_with(ctx)?, + action_index: d.decode_with(ctx)?, + }) } } @@ -1172,8 +1190,8 @@ impl minicbor::Encode for GovActionId { ) -> Result<(), minicbor::encode::Error> { e.array(2)?; - e.encode_with(self.0, ctx)?; - e.encode_with(self.1, ctx)?; + e.encode_with(self.transaction_id, ctx)?; + e.encode_with(self.action_index, ctx)?; Ok(()) }