Skip to content

Commit 287dd72

Browse files
Pass a problem report to Decode::decode as a context (#789)
1 parent 820c6b7 commit 287dd72

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

rust/catalyst-contest/src/contest_ballot/ballot.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ impl ContestBallot {
3939
"Document must be Contest Ballot type"
4040
);
4141

42-
let report = ProblemReport::new("Contest Ballot");
42+
let mut report = ProblemReport::new("Contest Ballot");
4343

44-
let payload = payload(doc, &report);
44+
let payload = payload(doc, &mut report);
4545
if let Some(payload) = &payload {
4646
check_proof(payload, &report);
4747
}
@@ -66,7 +66,7 @@ impl ContestBallot {
6666
/// Returns a decoded contest ballot payload.
6767
pub fn payload(
6868
doc: &CatalystSignedDocument,
69-
report: &ProblemReport,
69+
report: &mut ProblemReport,
7070
) -> Option<ContentBallotPayload> {
7171
let Ok(bytes) = doc.decoded_content() else {
7272
report.functional_validation(
@@ -77,9 +77,7 @@ pub fn payload(
7777
};
7878

7979
let mut decoder = minicbor::Decoder::new(&bytes);
80-
// TODO: Pass a problem report in the decode context. See the issue for more details:
81-
// https://github.com/input-output-hk/catalyst-libs/issues/775
82-
let Ok(payload) = ContentBallotPayload::decode(&mut decoder, &mut ()) else {
80+
let Ok(payload) = ContentBallotPayload::decode(&mut decoder, report) else {
8381
report.functional_validation(
8482
"Invalid document content: unable to decode CBOR",
8583
"Cannot get a document content during Contest Ballot document validation.",

rust/catalyst-contest/src/contest_ballot/payload.rs

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::collections::BTreeMap;
44

5+
use catalyst_signed_doc::problem_report::ProblemReport;
56
use cbork_utils::{decode_context::DecodeCtx, map::Map};
67
use minicbor::{Decode, Decoder, Encode, Encoder, encode::Write};
78

@@ -34,13 +35,15 @@ pub struct ContentBallotPayload {
3435
pub voter_choices: Option<EncryptedChoices>,
3536
}
3637

37-
impl Decode<'_, ()> for ContentBallotPayload {
38+
impl Decode<'_, ProblemReport> for ContentBallotPayload {
3839
fn decode(
3940
d: &mut Decoder<'_>,
40-
ctx: &mut (),
41+
report: &mut ProblemReport,
4142
) -> Result<Self, minicbor::decode::Error> {
4243
use minicbor::data::Type;
4344

45+
let context = "Content ballot payload decoding";
46+
4447
let map = Map::decode(d, &mut DecodeCtx::Deterministic)?;
4548

4649
let mut choices = BTreeMap::new();
@@ -55,36 +58,56 @@ impl Decode<'_, ()> for ContentBallotPayload {
5558
match key_decoder.datatype()? {
5659
Type::U8 | Type::U16 | Type::U32 | Type::U64 => {
5760
let key = key_decoder.u64()?;
58-
let val = Choices::decode(&mut value_decoder, ctx)?;
59-
choices.insert(key, val);
61+
match Choices::decode(&mut value_decoder, &mut ()) {
62+
Ok(val) => {
63+
choices.insert(key, val);
64+
},
65+
Err(e) => {
66+
report.other(
67+
&format!("Unable to decode choices for {key} key: {e:?}"),
68+
context,
69+
);
70+
},
71+
}
6072
},
6173
Type::String => {
6274
match key_decoder.str()? {
6375
"column-proof" => {
64-
return Err(minicbor::decode::Error::message(
76+
report.other(
6577
"column-proof is a placeholder and shouldn't be used",
66-
));
78+
context,
79+
);
6780
},
6881
"matrix-proof" => {
69-
return Err(minicbor::decode::Error::message(
82+
report.other(
7083
"matrix-proof is a placeholder and shouldn't be used",
71-
));
84+
context,
85+
);
7286
},
7387
"voter-choices" => {
74-
voter_choices =
75-
Some(EncryptedChoices::decode(&mut value_decoder, ctx)?);
88+
match EncryptedChoices::decode(&mut value_decoder, &mut ()) {
89+
Ok(v) => voter_choices = Some(v),
90+
Err(e) => {
91+
report.other(
92+
&format!("Unable to decode encrypted choices: {e:?}"),
93+
context,
94+
);
95+
},
96+
}
7697
},
7798
key => {
78-
return Err(minicbor::decode::Error::message(format!(
79-
"Unexpected content ballot payload key value: {key:?}"
80-
)));
99+
report.other(
100+
&format!("Unexpected content ballot payload key value: {key:?}"),
101+
context,
102+
);
81103
},
82104
}
83105
},
84106
t => {
85-
return Err(minicbor::decode::Error::message(format!(
86-
"Unexpected content ballot payload key type: {t:?}"
87-
)));
107+
report.other(
108+
&format!("Unexpected content ballot payload key type: {t:?}"),
109+
context,
110+
);
88111
},
89112
}
90113
}
@@ -160,7 +183,10 @@ mod tests {
160183
original
161184
.encode(&mut Encoder::new(&mut buffer), &mut ())
162185
.unwrap();
163-
let decoded = ContentBallotPayload::decode(&mut Decoder::new(&buffer), &mut ()).unwrap();
186+
let mut report = ProblemReport::new("test");
187+
let decoded =
188+
ContentBallotPayload::decode(&mut Decoder::new(&buffer), &mut report).unwrap();
164189
assert_eq!(original, decoded);
190+
assert!(!report.is_problematic());
165191
}
166192
}

rust/catalyst-contest/src/contest_ballot/rule.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ impl CatalystSignedDocumentValidationRule for ContestBallotRule {
1616
doc: &CatalystSignedDocument,
1717
provider: &dyn Provider,
1818
) -> anyhow::Result<bool> {
19-
let payload = payload(doc, doc.report());
19+
let mut report = doc.report().clone();
20+
let payload = payload(doc, &mut report);
2021
if let Some(payload) = &payload {
2122
check_proof(payload, doc.report());
2223
}

0 commit comments

Comments
 (0)