Skip to content

Commit

Permalink
restored partialeq instead of hashing
Browse files Browse the repository at this point in the history
  • Loading branch information
ogabrielides committed Jul 4, 2024
1 parent e1a0c56 commit ed7d1b6
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 181 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/rs-platform-version/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ license = "MIT"

[dependencies]
thiserror = { version = "1.0.59" }
sha2 = "0.10.5"
bincode = { version = "2.0.0-rc.3", features = ["serde"] }

[features]
Expand Down
47 changes: 12 additions & 35 deletions packages/rs-platform-version/src/version/fee/data_contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;

Expand All @@ -14,39 +13,17 @@ pub struct FeeDataContractValidationVersion {
pub document_type_unique_index_per_property_fee: u64,
}

impl FeeDataContractValidationVersion {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(&mut hasher, &self.document_type_base_fee.to_be_bytes());
Digest::update(&mut hasher, &self.document_type_size_fee.to_be_bytes());
Digest::update(
&mut hasher,
&self.document_type_per_property_fee.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.document_type_base_non_unique_index_fee.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self
.document_type_non_unique_index_per_property_fee
.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.document_type_base_unique_index_fee.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self
.document_type_unique_index_per_property_fee
.to_be_bytes(),
);

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
impl PartialEq for FeeDataContractValidationVersion {
fn eq(&self, other: &Self) -> bool {
self.document_type_base_fee == other.document_type_base_fee
&& self.document_type_size_fee == other.document_type_size_fee
&& self.document_type_per_property_fee == other.document_type_per_property_fee
&& self.document_type_base_non_unique_index_fee
== other.document_type_base_non_unique_index_fee
&& self.document_type_non_unique_index_per_property_fee
== other.document_type_non_unique_index_per_property_fee
&& self.document_type_base_unique_index_fee == other.document_type_base_unique_index_fee
&& self.document_type_unique_index_per_property_fee
== other.document_type_unique_index_per_property_fee
}
}
29 changes: 11 additions & 18 deletions packages/rs-platform-version/src/version/fee/hashing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;

#[derive(Clone, Debug, Encode, Decode, Default)]
pub struct FeeHashingVersion {
pub single_sha256_base: u64,
pub blake3_base: u64,
pub sha256_ripe_md160_base: u64,
pub sha256_per_block: u64,
pub blake3_per_block: u64,
}

impl FeeHashingVersion {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(&mut hasher, &self.single_sha256_base.to_be_bytes());
Digest::update(&mut hasher, &self.blake3_base.to_be_bytes());
Digest::update(&mut hasher, &self.sha256_ripe_md160_base.to_be_bytes());
Digest::update(&mut hasher, &self.sha256_per_block.to_be_bytes());
Digest::update(&mut hasher, &self.blake3_per_block.to_be_bytes());
pub sha256_per_block: u64,
pub sha256_ripe_md160_base: u64,
pub single_sha256_base: u64,

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
}
impl PartialEq for FeeHashingVersion {
fn eq(&self, other: &Self) -> bool {
self.blake3_base == other.blake3_base
&& self.blake3_per_block == other.blake3_per_block
&& self.sha256_per_block == other.sha256_per_block
&& self.sha256_ripe_md160_base == other.sha256_ripe_md160_base
&& self.single_sha256_base == other.single_sha256_base
}
}
28 changes: 6 additions & 22 deletions packages/rs-platform-version/src/version/fee/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::version::fee::signature::FeeSignatureVersion;
use crate::version::fee::state_transition_min_fees::StateTransitionMinFees;
use crate::version::fee::storage::FeeStorageVersion;
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

mod data_contract;
mod hashing;
Expand All @@ -27,26 +26,11 @@ pub struct FeeVersion {

impl PartialEq for FeeVersion {
fn eq(&self, other: &Self) -> bool {
self.to_hash() == other.to_hash()
}
}

impl FeeVersion {
fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(&mut hasher, &self.storage.to_hash().to_be_bytes());
Digest::update(&mut hasher, &self.signature.to_hash().to_be_bytes());
Digest::update(&mut hasher, &self.hashing.to_hash().to_be_bytes());
Digest::update(&mut hasher, &self.processing.to_hash().to_be_bytes());
Digest::update(&mut hasher, &self.data_contract.to_hash().to_be_bytes());
Digest::update(
&mut hasher,
&self.state_transition_min_fees.to_hash().to_be_bytes(),
);

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
self.storage == other.storage
&& self.signature == other.signature
&& self.hashing == other.hashing
&& self.processing == other.processing
&& self.data_contract == other.data_contract
&& self.state_transition_min_fees == other.state_transition_min_fees
}
}
44 changes: 12 additions & 32 deletions packages/rs-platform-version/src/version/fee/processing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;

Expand All @@ -13,36 +12,17 @@ pub struct FeeProcessingVersion {
pub validate_key_structure: u64,
}

impl FeeProcessingVersion {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(
&mut hasher,
&self.fetch_identity_balance_processing_cost.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.fetch_identity_revision_processing_cost.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self
.fetch_identity_balance_and_revision_processing_cost
.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.fetch_identity_cost_per_look_up_key_by_id.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.fetch_single_identity_key_processing_cost.to_be_bytes(),
);
Digest::update(&mut hasher, &self.validate_key_structure.to_be_bytes());

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
impl PartialEq for FeeProcessingVersion {
fn eq(&self, other: &Self) -> bool {
self.fetch_identity_balance_processing_cost == other.fetch_identity_balance_processing_cost
&& self.fetch_identity_revision_processing_cost
== other.fetch_identity_revision_processing_cost
&& self.fetch_identity_balance_and_revision_processing_cost
== other.fetch_identity_balance_and_revision_processing_cost
&& self.fetch_identity_cost_per_look_up_key_by_id
== other.fetch_identity_cost_per_look_up_key_by_id
&& self.fetch_single_identity_key_processing_cost
== other.fetch_single_identity_key_processing_cost
&& self.validate_key_structure == other.validate_key_structure
}
}
33 changes: 7 additions & 26 deletions packages/rs-platform-version/src/version/fee/signature/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;

Expand All @@ -12,30 +11,12 @@ pub struct FeeSignatureVersion {
pub verify_signature_eddsa25519_hash160: u64,
}

impl FeeSignatureVersion {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(
&mut hasher,
&self.verify_signature_ecdsa_secp256k1.to_be_bytes(),
);
Digest::update(&mut hasher, &self.verify_signature_bls12_381.to_be_bytes());
Digest::update(
&mut hasher,
&self.verify_signature_ecdsa_hash160.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.verify_signature_bip13_script_hash.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.verify_signature_eddsa25519_hash160.to_be_bytes(),
);

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
impl PartialEq for FeeSignatureVersion {
fn eq(&self, other: &Self) -> bool {
self.verify_signature_ecdsa_secp256k1 == other.verify_signature_ecdsa_secp256k1
&& self.verify_signature_bls12_381 == other.verify_signature_bls12_381
&& self.verify_signature_ecdsa_hash160 == other.verify_signature_ecdsa_hash160
&& self.verify_signature_bip13_script_hash == other.verify_signature_bip13_script_hash
&& self.verify_signature_eddsa25519_hash160 == other.verify_signature_eddsa25519_hash160
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;
#[derive(Clone, Debug, Encode, Decode, Default)]
Expand All @@ -12,22 +11,13 @@ pub struct StateTransitionMinFees {
pub contract_update: u64,
}

impl StateTransitionMinFees {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(&mut hasher, &self.credit_transfer.to_be_bytes());
Digest::update(&mut hasher, &self.credit_withdrawal.to_be_bytes());
Digest::update(&mut hasher, &self.identity_update.to_be_bytes());
Digest::update(
&mut hasher,
&self.document_batch_sub_transition.to_be_bytes(),
);
Digest::update(&mut hasher, &self.contract_create.to_be_bytes());
Digest::update(&mut hasher, &self.contract_update.to_be_bytes());

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
impl PartialEq for StateTransitionMinFees {
fn eq(&self, other: &Self) -> bool {
self.credit_transfer == other.credit_transfer
&& self.credit_withdrawal == other.credit_withdrawal
&& self.identity_update == other.identity_update
&& self.document_batch_sub_transition == other.document_batch_sub_transition
&& self.contract_create == other.contract_create
&& self.contract_update == other.contract_update
}
}
35 changes: 7 additions & 28 deletions packages/rs-platform-version/src/version/fee/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
extern crate sha2;

use bincode::{Decode, Encode};
use sha2::{Digest, Sha256};

pub mod v1;

Expand All @@ -14,30 +11,12 @@ pub struct FeeStorageVersion {
pub storage_seek_cost: u64,
}

impl FeeStorageVersion {
pub(crate) fn to_hash(&self) -> u64 {
let mut hasher = Sha256::new();
Digest::update(
&mut hasher,
&self.storage_disk_usage_credit_per_byte.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.storage_processing_credit_per_byte.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.storage_load_credit_per_byte.to_be_bytes(),
);
Digest::update(
&mut hasher,
&self.non_storage_load_credit_per_byte.to_be_bytes(),
);
Digest::update(&mut hasher, &self.storage_seek_cost.to_be_bytes());

let result = hasher.finalize();
// Use the first 8 bytes of the hash as the u64 representation
let hash_bytes: [u8; 8] = result[0..8].try_into().unwrap();
u64::from_be_bytes(hash_bytes)
impl PartialEq for FeeStorageVersion {
fn eq(&self, other: &Self) -> bool {
self.storage_disk_usage_credit_per_byte == other.storage_disk_usage_credit_per_byte
&& self.storage_processing_credit_per_byte == other.storage_processing_credit_per_byte
&& self.storage_load_credit_per_byte == other.storage_load_credit_per_byte
&& self.non_storage_load_credit_per_byte == other.non_storage_load_credit_per_byte
&& self.storage_seek_cost == other.storage_seek_cost
}
}

0 comments on commit ed7d1b6

Please sign in to comment.