Skip to content

Commit 2b8588d

Browse files
committed
update decoding test
1 parent e59adee commit 2b8588d

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

rust/catalyst-voting/src/txs/v1/decoding.rs

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,70 @@ mod tests {
265265
}
266266

267267
#[proptest]
268-
#[allow(clippy::indexing_slicing)]
269268
fn tx_to_bytes_from_bytes_test(t1: Tx) {
270269
let bytes = t1.to_bytes();
271270

272-
// verify correctness serializing tx size field
273-
let size = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
271+
let mut reader = bytes.as_slice();
272+
273+
let size = read_be_u32(&mut reader).unwrap();
274274
assert_eq!(size as usize, bytes.len() - 4);
275275

276+
let padding_tag = read_be_u8(&mut reader).unwrap();
277+
assert_eq!(padding_tag, PADDING_TAG);
278+
279+
let fragment_tag = read_be_u8(&mut reader).unwrap();
280+
assert_eq!(fragment_tag, FRAGMENT_TAG);
281+
282+
let vote_plan_id = read_array(&mut reader).unwrap();
283+
assert_eq!(vote_plan_id, t1.vote_plan_id);
284+
285+
let proposal_index = read_be_u8(&mut reader).unwrap();
286+
assert_eq!(proposal_index, t1.proposal_index);
287+
288+
let vote_tag = read_be_u8(&mut reader).unwrap();
289+
assert!(vote_tag == PUBLIC_VOTE_TAG || vote_tag == PRIVATE_VOTE_TAG);
290+
match vote_tag {
291+
PUBLIC_VOTE_TAG => {
292+
let vote = read_be_u8(&mut reader).unwrap();
293+
assert_eq!(VotePayload::Public(vote), t1.vote);
294+
},
295+
PRIVATE_VOTE_TAG => {
296+
let size = read_be_u8(&mut reader).unwrap();
297+
let vote = EncryptedVote::from_bytes(&mut reader, size.into()).unwrap();
298+
let size = read_be_u8(&mut reader).unwrap();
299+
let proof = VoterProof::from_bytes(&mut reader, size.into()).unwrap();
300+
assert_eq!(VotePayload::Private(vote, proof), t1.vote);
301+
},
302+
_ => {},
303+
}
304+
305+
let block_date = read_be_u64(&mut reader).unwrap();
306+
assert_eq!(block_date, 0);
307+
308+
let inputs_amount = read_be_u8(&mut reader).unwrap();
309+
assert_eq!(inputs_amount, NUMBER_OF_INPUTS);
310+
311+
let outputs_amount = read_be_u8(&mut reader).unwrap();
312+
assert_eq!(outputs_amount, NUMBER_OF_OUTPUTS);
313+
314+
let input_tag = read_be_u8(&mut reader).unwrap();
315+
assert_eq!(input_tag, INPUT_TAG);
316+
317+
let value = read_be_u64(&mut reader).unwrap();
318+
assert_eq!(value, 0);
319+
320+
let public_key = read_array(&mut reader).unwrap();
321+
assert_eq!(PublicKey::from_bytes(&public_key).unwrap(), t1.public_key);
322+
323+
let witness_tag = read_be_u8(&mut reader).unwrap();
324+
assert_eq!(witness_tag, WITNESS_TAG);
325+
326+
let nonce = read_be_u32(&mut reader).unwrap();
327+
assert_eq!(nonce, 0);
328+
329+
let signature = read_array(&mut reader).unwrap();
330+
assert_eq!(Signature::from_bytes(&signature), t1.signature);
331+
276332
let t2 = Tx::from_bytes(&mut bytes.as_slice()).unwrap();
277333
assert_eq!(t1, t2);
278334
}

rust/catalyst-voting/src/txs/v1/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//! &users_private_key,
2525
//! )
2626
//! .unwrap();
27+
//! public_tx.verify_signature().unwrap();
2728
//!
2829
//! let private_tx = Tx::new_private_with_default_rng(
2930
//! vote_plan_id,
@@ -34,6 +35,8 @@
3435
//! &users_private_key,
3536
//! )
3637
//! .unwrap();
38+
//! private_tx.verify_signature().unwrap();
39+
//! private_tx.verify_proof(&election_public_key).unwrap();
3740
//! ```
3841
3942
mod decoding;

0 commit comments

Comments
 (0)