|
3 | 3 |
|
4 | 4 | mod choice; |
5 | 5 | mod proof; |
6 | | -mod prop_id; |
7 | 6 |
|
8 | 7 | use std::ops::{Deref, DerefMut}; |
9 | 8 |
|
10 | 9 | pub use choice::Choice; |
11 | 10 | use minicbor::{Decode, Encode}; |
12 | 11 | pub use proof::Proof; |
13 | | -pub use prop_id::PropId; |
14 | 12 |
|
15 | | -use crate::{gen_tx::GeneralizedTx, Cbor}; |
| 13 | +use crate::{gen_tx::GeneralizedTx, uuid::Uuid, Cbor}; |
| 14 | + |
| 15 | +/// A public voting proposal id struct. |
| 16 | +pub type PropId = Uuid; |
16 | 17 |
|
17 | 18 | /// A public vote tx struct. |
| 19 | +#[derive(Debug, Clone, PartialEq)] |
18 | 20 | pub struct PublicTx<VoteDataT>(GeneralizedTx<Choice, Proof, PropId, VoteDataT>) |
19 | 21 | where VoteDataT: for<'a> Cbor<'a>; |
20 | 22 |
|
@@ -54,3 +56,34 @@ where VoteDataT: for<'a> Cbor<'a> |
54 | 56 | self.0.encode(e, ctx) |
55 | 57 | } |
56 | 58 | } |
| 59 | + |
| 60 | +#[cfg(test)] |
| 61 | +mod tests { |
| 62 | + use proptest::sample::size_range; |
| 63 | + use test_strategy::proptest; |
| 64 | + |
| 65 | + use super::*; |
| 66 | + use crate::{encoded_cbor::EncodedCbor, gen_tx::GeneralizedTxBuilder, uuid::Uuid}; |
| 67 | + |
| 68 | + #[proptest] |
| 69 | + fn public_tx_from_bytes_to_bytes_test( |
| 70 | + vote_type: Vec<u8>, voter_data: Vec<u8>, |
| 71 | + #[any(size_range(1..10_usize).lift())] choices: Vec<u64>, prop_id: Vec<u8>, |
| 72 | + ) { |
| 73 | + let gen_tx_builder = GeneralizedTxBuilder::<Choice, Proof, PropId, _>::new( |
| 74 | + Uuid(vote_type), |
| 75 | + EncodedCbor(voter_data), |
| 76 | + ); |
| 77 | + let choices = choices.into_iter().map(Choice).collect(); |
| 78 | + let gen_tx = gen_tx_builder |
| 79 | + .with_vote(choices, Proof, Uuid(prop_id)) |
| 80 | + .unwrap() |
| 81 | + .build() |
| 82 | + .unwrap(); |
| 83 | + let public_tx = PublicTx(gen_tx); |
| 84 | + |
| 85 | + let bytes = public_tx.to_bytes().unwrap(); |
| 86 | + let decoded = PublicTx::from_bytes(&bytes).unwrap(); |
| 87 | + assert_eq!(public_tx, decoded); |
| 88 | + } |
| 89 | +} |
0 commit comments