Skip to content

Commit

Permalink
add GeneralisedTx struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Nov 11, 2024
1 parent db7eaa9 commit 2e29621
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
47 changes: 32 additions & 15 deletions rust/vote-tx-v2/src/decoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use minicbor::{
Decode, Decoder, Encode, Encoder,
};

use crate::{Choice, Proof, PropId, TxBody, Uuid, Vote, VoterData};
use crate::{Choice, GeneralisedTx, Proof, PropId, TxBody, Uuid, Vote, VoterData};

/// UUID CBOR tag <https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml/>.
const CBOR_UUID_TAG: u64 = 37;
Expand All @@ -16,18 +16,34 @@ const VOTE_LEN: u64 = 3;
/// `TxBody` array struct length
const TX_BODY_LEN: u64 = 3;

impl Decode<'_, ()> for TxBody {
/// `GeneralisedTx` array struct length
const GENERALISED_TX_LEN: u64 = 1;

impl Decode<'_, ()> for GeneralisedTx {
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
let Some(TX_BODY_LEN) = d.array()? else {
return Err(minicbor::decode::Error::message(format!(
"must be a defined sized array with {TX_BODY_LEN} entries"
)));
};
d.array()?;
let tx_body = TxBody::decode(d, &mut ())?;
Ok(Self { tx_body })
}
}

impl Encode<()> for GeneralisedTx {
fn encode<W: minicbor::encode::Write>(
&self, e: &mut Encoder<W>, (): &mut (),
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.array(GENERALISED_TX_LEN)?;
self.tx_body.encode(e, &mut ())?;
Ok(())
}
}

impl Decode<'_, ()> for TxBody {
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
d.array()?;
let vote_type = Uuid::decode(d, &mut ())?;
let votes = Vec::<Vote>::decode(d, &mut ())?;
let voter_data = VoterData::decode(d, &mut ())?;
Ok(TxBody {
Ok(Self {
vote_type,
votes,
voter_data,
Expand Down Expand Up @@ -99,12 +115,7 @@ impl Encode<()> for Uuid {

impl Decode<'_, ()> for Vote {
fn decode(d: &mut Decoder<'_>, (): &mut ()) -> Result<Self, minicbor::decode::Error> {
let Some(VOTE_LEN) = d.array()? else {
return Err(minicbor::decode::Error::message(format!(
"must be a defined sized array with {VOTE_LEN} entries"
)));
};

d.array()?;
let choices = Vec::<Choice>::decode(d, &mut ())?;
if choices.is_empty() {
return Err(minicbor::decode::Error::message(
Expand All @@ -113,7 +124,6 @@ impl Decode<'_, ()> for Vote {
}
let proof = Proof::decode(d, &mut ())?;
let prop_id = PropId::decode(d, &mut ())?;

Ok(Self {
choices,
proof,
Expand Down Expand Up @@ -219,6 +229,13 @@ mod tests {
use super::*;
use crate::Cbor;

#[proptest]
fn generalised_tx_from_bytes_to_bytes_test(generalised_tx: GeneralisedTx) {
let bytes = generalised_tx.to_bytes().unwrap();
let decoded = GeneralisedTx::from_bytes(&bytes).unwrap();
assert_eq!(generalised_tx, decoded);
}

#[proptest]
fn tx_body_from_bytes_to_bytes_test(tx_body: TxBody) {
let bytes = tx_body.to_bytes().unwrap();
Expand Down
19 changes: 17 additions & 2 deletions rust/vote-tx-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ use minicbor::{Decode, Decoder, Encode, Encoder};

mod decoding;

/// A geeneralised tx struct.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeneralisedTx {
/// `tx-body`
tx_body: TxBody,
}

/// A tx body struct.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxBody {
Expand Down Expand Up @@ -83,8 +90,16 @@ mod arbitrary_impl {
sample::size_range,
};

use super::{Choice, Proof, PropId, Uuid, Vote, VoterData};
use crate::TxBody;
use super::{Choice, GeneralisedTx, Proof, PropId, TxBody, Uuid, Vote, VoterData};

impl Arbitrary for GeneralisedTx {
type Parameters = ();
type Strategy = BoxedStrategy<Self>;

fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
any::<TxBody>().prop_map(|tx_body| Self { tx_body }).boxed()
}
}

impl Arbitrary for TxBody {
type Parameters = ();
Expand Down

0 comments on commit 2e29621

Please sign in to comment.