Skip to content

Commit 415ff9e

Browse files
committed
add public tx test
1 parent 626ecc5 commit 415ff9e

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

rust/vote-tx-v2/src/public_tx/choice.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use minicbor::{Decode, Encode};
44

55
/// A public voting choice struct.
6-
pub struct Choice(u64);
6+
#[derive(Debug, Clone, PartialEq)]
7+
pub struct Choice(pub u64);
78

89
impl Decode<'_, ()> for Choice {
910
fn decode(d: &mut minicbor::Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {

rust/vote-tx-v2/src/public_tx/mod.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33
44
mod choice;
55
mod proof;
6-
mod prop_id;
76

87
use std::ops::{Deref, DerefMut};
98

109
pub use choice::Choice;
1110
use minicbor::{Decode, Encode};
1211
pub use proof::Proof;
13-
pub use prop_id::PropId;
1412

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;
1617

1718
/// A public vote tx struct.
19+
#[derive(Debug, Clone, PartialEq)]
1820
pub struct PublicTx<VoteDataT>(GeneralizedTx<Choice, Proof, PropId, VoteDataT>)
1921
where VoteDataT: for<'a> Cbor<'a>;
2022

@@ -54,3 +56,34 @@ where VoteDataT: for<'a> Cbor<'a>
5456
self.0.encode(e, ctx)
5557
}
5658
}
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+
}

rust/vote-tx-v2/src/public_tx/proof.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use minicbor::{Decode, Encode};
44

55
/// A public voting proof struct, CBOR `undefined`.
6+
#[derive(Debug, Clone, PartialEq)]
67
pub struct Proof;
78

89
impl Decode<'_, ()> for Proof {

rust/vote-tx-v2/src/public_tx/prop_id.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)