Skip to content

Commit

Permalink
fix structs that marked as "set" in cddl
Browse files Browse the repository at this point in the history
  • Loading branch information
lisicky committed Jun 22, 2024
1 parent 5591034 commit 27a7902
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 62 deletions.
2 changes: 1 addition & 1 deletion rust/src/builders/certificates_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl CertificatesBuilder {

pub fn build(&self) -> Certificates {
let certs = self.certs.iter().map(|(c, _)| c.clone()).collect();
Certificates(certs)
Certificates::from_vec(certs)
}

//return only ref inputs that are script refs with added size
Expand Down
2 changes: 1 addition & 1 deletion rust/src/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ impl TransactionBuilder {
)]
pub fn set_certs(&mut self, certs: &Certificates) -> Result<(), JsError> {
let mut builder = CertificatesBuilder::new();
for cert in &certs.0 {
for cert in &certs.certs {
builder.add(cert)?;
}

Expand Down
2 changes: 1 addition & 1 deletion rust/src/builders/voting_proposal_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ impl VotingProposalBuilder {
for (voter, _) in &self.proposals {
proposals.push(voter.clone());
}
VotingProposals(proposals)
VotingProposals::from_vec(proposals)
}
}
42 changes: 35 additions & 7 deletions rust/src/protocol_types/certificates/certificates_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,59 @@ use crate::*;
#[derive(
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema,
)]
pub struct Certificates(pub(crate) Vec<Certificate>);
pub struct Certificates {
pub(crate) certs: Vec<Certificate>,
pub(crate) dedup: BTreeSet<Certificate>
}

impl_to_from!(Certificates);

impl NoneOrEmpty for Certificates {
fn is_none_or_empty(&self) -> bool {
self.0.is_empty()
self.certs.is_empty()
}
}

#[wasm_bindgen]
impl Certificates {
pub fn new() -> Self {
Self(Vec::new())
Self {
certs: Vec::new(),
dedup: BTreeSet::new(),
}
}

pub fn len(&self) -> usize {
self.0.len()
self.certs.len()
}

pub fn get(&self, index: usize) -> Certificate {
self.0[index].clone()
self.certs[index].clone()
}

/// Add a new `Certificate` to the set.
/// Returns `true` if the element was not already present in the set.
pub fn add(&mut self, elem: &Certificate) -> bool {
if self.dedup.insert(elem.clone()) {
self.certs.push(elem.clone());
true
} else {
false
}
}

pub fn add(&mut self, elem: &Certificate) {
self.0.push(elem.clone());
pub(crate) fn add_move(&mut self, elem: Certificate) {
if self.dedup.insert(elem.clone()) {
self.certs.push(elem);
}
}


pub(crate) fn from_vec(certs_vec: Vec<Certificate>) -> Self {
let mut certs = Self::new();
for cert in certs_vec {
certs.add_move(cert);
}
certs
}
}
45 changes: 38 additions & 7 deletions rust/src/protocol_types/governance/proposals/voting_proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,62 @@ use crate::*;
JsonSchema,
)]
#[wasm_bindgen]
pub struct VotingProposals(pub(crate) Vec<VotingProposal>);
pub struct VotingProposals {
pub(crate) proposals: Vec<VotingProposal>,
pub(crate) dedup: BTreeSet<VotingProposal>,
}

impl_to_from!(VotingProposals);

impl NoneOrEmpty for VotingProposals {
fn is_none_or_empty(&self) -> bool {
self.0.is_empty()
self.proposals.is_empty()
}
}

#[wasm_bindgen]
impl VotingProposals {
pub fn new() -> Self {
Self(Vec::new())
Self {
proposals: Vec::new(),
dedup: BTreeSet::new(),
}
}

pub fn len(&self) -> usize {
self.0.len()
self.proposals.len()
}

pub fn get(&self, index: usize) -> VotingProposal {
self.0[index].clone()
self.proposals[index].clone()
}

/// Add a proposal to the set of proposals
/// Returns true if the proposal was added, false if it was already present
pub fn add(&mut self, proposal: &VotingProposal) -> bool {
if self.dedup.insert(proposal.clone()) {
self.proposals.push(proposal.clone());
true
} else {
false
}
}

pub(crate) fn add_move(&mut self, proposal: VotingProposal) {
if self.dedup.insert(proposal.clone()) {
self.proposals.push(proposal);
}
}

pub(crate) fn from_vec(proposals: Vec<VotingProposal>) -> Self {
let mut voting_proposals = VotingProposals::new();
for proposal in proposals {
voting_proposals.add_move(proposal);
}
voting_proposals
}

pub fn add(&mut self, proposal: &VotingProposal) {
self.0.push(proposal.clone());
pub(crate) fn contains(&self, proposal: &VotingProposal) -> bool {
self.dedup.contains(proposal)
}
}
10 changes: 3 additions & 7 deletions rust/src/serialization/certificates/certificates_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ impl Serialize for Certificates {
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
if self.0.is_empty() {
return Ok(serializer);
}
//TODO: uncomment this line when we conway ero will come
//serializer.write_tag(258)?;
let ordered_dedup = self.0.iter().collect::<BTreeSet<_>>();
serializer.write_array(cbor_event::Len::Len(self.0.len() as u64))?;
for element in ordered_dedup {
serializer.write_array(Len::Len(self.len() as u64))?;
for element in &self.certs {
element.serialize(serializer)?;
}
Ok(serializer)
Expand All @@ -38,6 +34,6 @@ impl Deserialize for Certificates {
Ok(())
})()
.map_err(|e| e.annotate("Certificates"))?;
Ok(Self(arr))
Ok(Self::from_vec(arr))
}
}
6 changes: 3 additions & 3 deletions rust/src/serialization/ed25519_key_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ impl Deserialize for Ed25519KeyHashes {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
skip_set_tag(raw)?;
let mut creds = Ed25519KeyHashes::new();
let mut counter = 0u64;
let mut total = 0u64;
(|| -> Result<_, DeserializeError> {
let len = raw.array()?;
while match len {
cbor_event::Len::Len(n) => counter < n,
cbor_event::Len::Len(n) => total < n,
cbor_event::Len::Indefinite => true,
} {
if is_break_tag(raw, "Ed25519KeyHashes")? {
break;
}
creds.add_move(Ed25519KeyHash::deserialize(raw)?);
counter += 1;
total += 1;
}
Ok(())
})()
Expand Down
11 changes: 4 additions & 7 deletions rust/src/serialization/governance/proposals/voting_proposals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ impl cbor_event::se::Serialize for VotingProposals {
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
if self.0.is_empty() {
return Ok(serializer);
}
//TODO: uncomment this line when we conway ero will come
//serializer.write_tag(258)?;
let ordered_dedup = self.0.iter().collect::<BTreeSet<_>>();
serializer.write_array(cbor_event::Len::Len(self.0.len() as u64))?;
for element in ordered_dedup {
serializer.write_array(cbor_event::Len::Len(self.len() as u64))?;
for element in &self.proposals {
element.serialize(serializer)?;
}
Ok(serializer)
Expand All @@ -22,6 +18,7 @@ impl cbor_event::se::Serialize for VotingProposals {

impl Deserialize for VotingProposals {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
skip_set_tag(raw)?;
let mut arr = Vec::new();
(|| -> Result<_, DeserializeError> {
skip_set_tag(raw)?;
Expand All @@ -38,6 +35,6 @@ impl Deserialize for VotingProposals {
Ok(())
})()
.map_err(|e| e.annotate("VotingProposals"))?;
Ok(Self(arr))
Ok(Self::from_vec(arr))
}
}
13 changes: 6 additions & 7 deletions rust/src/serialization/governance/voting_procedures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ impl cbor_event::se::Serialize for VotingProcedures {
&self,
serializer: &'se mut Serializer<W>,
) -> cbor_event::Result<&'se mut Serializer<W>> {
if self.is_none_or_empty() {
return Ok(serializer)
}

serializer.write_map(cbor_event::Len::Len(self.0.len() as u64))?;

for (voter, votes) in &self.0 {
if votes.is_empty() {
continue;
Expand All @@ -33,8 +28,9 @@ impl Deserialize for VotingProcedures {
let mut voter_to_vote = BTreeMap::new();
(|| -> Result<_, DeserializeError> {
let len = raw.map()?;
let mut total = 0;
while match len {
cbor_event::Len::Len(n) => voter_to_vote.len() < n as usize,
cbor_event::Len::Len(n) => total < n,
cbor_event::Len::Indefinite => true,
} {
if is_break_tag(raw, "voting_procedure map")? {
Expand All @@ -52,6 +48,7 @@ impl Deserialize for VotingProcedures {
)))
.into());
}
total += 1;
}
Ok(Self(voter_to_vote))
})()
Expand All @@ -65,8 +62,9 @@ fn deserialize_internal_map<R: BufRead + Seek>(
let mut gov_act_id_to_vote = BTreeMap::new();
(|| -> Result<_, DeserializeError> {
let len = raw.map()?;
let mut total = 0;
while match len {
cbor_event::Len::Len(n) => gov_act_id_to_vote.len() < n as usize,
cbor_event::Len::Len(n) => total < n,
cbor_event::Len::Indefinite => true,
} {
if is_break_tag(raw, "gov_act_id_to_vote map")? {
Expand All @@ -84,6 +82,7 @@ fn deserialize_internal_map<R: BufRead + Seek>(
)))
.into());
}
total += 1;
}
Ok(gov_act_id_to_vote)
})()
Expand Down
1 change: 1 addition & 0 deletions rust/src/serialization/plutus/plutus_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl PlutusScripts {

impl Deserialize for PlutusScripts {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
skip_set_tag(raw)?;
let mut arr = Vec::new();
(|| -> Result<_, DeserializeError> {
skip_set_tag(raw)?;
Expand Down
7 changes: 4 additions & 3 deletions rust/src/serialization/witnesses/vkeywitnesses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ impl cbor_event::se::Serialize for Vkeywitnesses {

impl Deserialize for Vkeywitnesses {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
skip_set_tag(raw)?;
let mut wits = Vkeywitnesses::new();
let mut counter = 0u64;
let mut total = 0u64;
(|| -> Result<_, DeserializeError> {
skip_set_tag(raw)?;
let len = raw.array()?;
while match len {
cbor_event::Len::Len(n) => counter < n,
cbor_event::Len::Len(n) => total < n,
cbor_event::Len::Indefinite => true,
} {
if raw.cbor_type()? == cbor_event::Type::Special {
assert_eq!(raw.special()?, cbor_event::Special::Break);
break;
}
wits.add_move(Vkeywitness::deserialize(raw)?);
counter += 1;
total += 1;
}
Ok(())
})()
Expand Down
2 changes: 1 addition & 1 deletion rust/src/tests/builders/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5727,7 +5727,7 @@ fn build_tx_with_certs_withdrawals_plutus_script_address() {
assert_eq!(final_tx_wits.plutus_scripts().unwrap().len(), 3);
assert_eq!(final_tx_wits.redeemers().unwrap().len(), 5);

let certs = final_tx_body.certs().unwrap().0;
let certs = final_tx_body.certs().unwrap().certs;
let withdraws = final_tx_body
.withdrawals()
.unwrap()
Expand Down
22 changes: 11 additions & 11 deletions rust/src/tests/builders/voting_proposal_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ fn voting_proposal_builder_all_proposals() {

let voting_proposals = tx.body().voting_proposals().unwrap();
assert_eq!(voting_proposals.len(), 7);
assert!(voting_proposals.0.contains(&hf_proposal));
assert!(voting_proposals.0.contains(&committee_proposal));
assert!(voting_proposals.0.contains(&constitution_proposal));
assert!(voting_proposals.0.contains(&no_conf_proposal));
assert!(voting_proposals.0.contains(&pp_update_proposal));
assert!(voting_proposals.0.contains(&withdrawal_proposal));
assert!(voting_proposals.0.contains(&info_proposal));
assert!(voting_proposals.contains(&hf_proposal));
assert!(voting_proposals.contains(&committee_proposal));
assert!(voting_proposals.contains(&constitution_proposal));
assert!(voting_proposals.contains(&no_conf_proposal));
assert!(voting_proposals.contains(&pp_update_proposal));
assert!(voting_proposals.contains(&withdrawal_proposal));
assert!(voting_proposals.contains(&info_proposal));

let mut total_out = total_tx_output_with_fee(&tx);
total_out = total_out.checked_add(&total_deposit).unwrap();
Expand Down Expand Up @@ -266,8 +266,8 @@ fn voting_proposal_builder_with_plutus_script_witness() {

let voting_proposals = tx.body().voting_proposals().unwrap();
assert_eq!(voting_proposals.len(), 2);
assert!(voting_proposals.0.contains(&hf_proposal));
assert!(voting_proposals.0.contains(&committee_proposal));
assert!(voting_proposals.contains(&hf_proposal));
assert!(voting_proposals.contains(&committee_proposal));

let mut total_out = total_tx_output_with_fee(&tx);
total_out = total_out.checked_add(&total_deposit).unwrap();
Expand Down Expand Up @@ -374,8 +374,8 @@ fn voting_proposal_builder_with_ref_plutus_script_witness() {

let voting_proposals = tx.body().voting_proposals().unwrap();
assert_eq!(voting_proposals.len(), 2);
assert!(voting_proposals.0.contains(&hf_proposal));
assert!(voting_proposals.0.contains(&committee_proposal));
assert!(voting_proposals.contains(&hf_proposal));
assert!(voting_proposals.contains(&committee_proposal));

let mut total_out = total_tx_output_with_fee(&tx);
total_out = total_out.checked_add(&total_deposit).unwrap();
Expand Down
Loading

0 comments on commit 27a7902

Please sign in to comment.