Skip to content

Commit

Permalink
Functions added for comparing dataJson (#7)
Browse files Browse the repository at this point in the history
* [fix]: parsing fixed

* added doc, removed print statement and unused functions

* added panic for the case when len of declared class is non-zero but class hash is zero

* adding clone and eq traits to structs

* functions added to compare class declarations and contract updates

* made function public for use

* updated comparing functions

* updated datajson update to handle primitive as well

* removed unorderEq for class declaration
  • Loading branch information
Mohiiit authored May 26, 2024
1 parent 6899b38 commit ac23e15
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
1 change: 0 additions & 1 deletion crates/types/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ pub fn parse_state_diffs(data: &[BigUint]) -> DataJson {
}
let info_word = &data[i];


let (class_flag, nonce, number_of_storage_updates) = extract_bits(&info_word);

let new_class_hash = if class_flag {
Expand Down
92 changes: 87 additions & 5 deletions crates/types/src/state_diffs.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use num_bigint::BigUint;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::HashSet;
use std::hash::{Hash, Hasher};

Check warning on line 4 in crates/types/src/state_diffs.rs

View workflow job for this annotation

GitHub Actions / build-and-test

unused import: `Hasher`

Check warning on line 4 in crates/types/src/state_diffs.rs

View workflow job for this annotation

GitHub Actions / build-and-test

unused import: `Hasher`

Check warning on line 4 in crates/types/src/state_diffs.rs

View workflow job for this annotation

GitHub Actions / build-and-test

unused import: `Hasher`


#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct DataJson {
pub state_update_size: u64,
pub state_update: Vec<ContractUpdate>,
pub class_declaration_size: u64,
pub class_declaration: Vec<ClassDeclaration>,
}

// Define the data structures
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ContractUpdate {
#[serde(serialize_with = "serialize_biguint")]
pub address: BigUint,
Expand All @@ -30,7 +30,7 @@ pub struct StorageUpdate {
pub value: BigUint,
}

#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub struct ClassDeclaration {
#[serde(serialize_with = "serialize_biguint")]
pub class_hash: BigUint,
Expand Down Expand Up @@ -59,3 +59,85 @@ where
None => serializer.serialize_none(),
}
}

// Trait for unordered equality
pub trait UnorderedEq {
fn unordered_eq(&self, other: &Self) -> bool;
}

// Implement UnorderedEq for DataJson
impl UnorderedEq for DataJson {
fn unordered_eq(&self, other: &Self) -> bool {
self.state_update_size == other.state_update_size
&& self.class_declaration_size == other.class_declaration_size
&& self.state_update.unordered_eq(&other.state_update)
&& self
.class_declaration
.unordered_eq(&other.class_declaration)
}
}

// Implement UnorderedEq for Vec<ContractUpdate>
impl UnorderedEq for Vec<ContractUpdate> {
fn unordered_eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}

let mut self_sorted = self.clone();
let mut other_sorted = other.clone();

self_sorted.sort_by_key(|update| update.address.clone());
other_sorted.sort_by_key(|update| update.address.clone());

for (self_update, other_update) in self_sorted.iter().zip(other_sorted.iter()) {
if !self_update.unordered_eq(other_update) {
return false;
}
}

true
}
}

// Implement UnorderedEq for Vec<ClassDeclaration>
impl UnorderedEq for Vec<ClassDeclaration> {
fn unordered_eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}

let set_self: HashSet<_> = self.iter().collect();
let set_other: HashSet<_> = other.iter().collect();

set_self == set_other
}
}

// Implement UnorderedEq for Vec<StorageUpdate>
impl UnorderedEq for Vec<StorageUpdate> {
fn unordered_eq(&self, other: &Self) -> bool {
if self.len() != other.len() {
return false;
}

let mut self_sorted = self.clone();
let mut other_sorted = other.clone();

self_sorted.sort_by_key(|update| update.key.clone());
other_sorted.sort_by_key(|update| update.key.clone());

self_sorted == other_sorted
}
}

// Implement UnorderedEq for ContractUpdate
impl UnorderedEq for ContractUpdate {
fn unordered_eq(&self, other: &Self) -> bool {
self.address == other.address
&& self.nonce == other.nonce
&& self.number_of_storage_updates == other.number_of_storage_updates
&& self.new_class_hash == other.new_class_hash
&& self.storage_updates.unordered_eq(&other.storage_updates)
}
}

0 comments on commit ac23e15

Please sign in to comment.