From 23df7331c860bfc77e92168f51e3cd8b9f6f403e Mon Sep 17 00:00:00 2001 From: Giacomo Pasini <21265557+zeegomo@users.noreply.github.com> Date: Mon, 26 Aug 2024 19:29:49 +0200 Subject: [PATCH] Use efficient comm for balances (#38) * Use efficient comm for balances At the moment we're using merkle trees even though we don't need inclusion proofs, which means we're calculating more hashes than necessary and limiting the account set size to 256. This change is a temporary solution that chooses a more efficient way to calculate a commitment to the account balances, while we wait for something more scalable like a Verkle Tree. * remove leftover constants --- goas/atomic_asset_transfer/common/src/lib.rs | 24 ++++++++------------ 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/goas/atomic_asset_transfer/common/src/lib.rs b/goas/atomic_asset_transfer/common/src/lib.rs index e351dcd..59c1901 100644 --- a/goas/atomic_asset_transfer/common/src/lib.rs +++ b/goas/atomic_asset_transfer/common/src/lib.rs @@ -1,6 +1,6 @@ pub mod mmr; -use cl::{balance::Unit, merkle, NoteCommitment}; +use cl::{balance::Unit, NoteCommitment}; use ed25519_dalek::{ ed25519::{signature::SignerMut, SignatureBytes}, Signature, SigningKey, VerifyingKey, PUBLIC_KEY_LENGTH, @@ -12,11 +12,6 @@ use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::collections::BTreeMap; -// TODO: sparse merkle tree -pub const MAX_BALANCES: usize = 1 << 8; -pub const MAX_TXS: usize = 1 << 8; -pub const MAX_EVENTS: usize = 1 << 8; - // state of the zone #[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] pub struct StateCommitment(pub [u8; 32]); @@ -105,14 +100,15 @@ impl StateWitness { } pub fn balances_root(&self) -> [u8; 32] { - let balance_bytes = Vec::from_iter(self.balances.iter().map(|(owner, balance)| { - let mut bytes: Vec = vec![]; - bytes.extend(owner); - bytes.extend(balance.to_le_bytes()); - bytes - })); - let balance_merkle_leaves = cl::merkle::padded_leaves(&balance_bytes); - merkle::root::(balance_merkle_leaves) + let mut hasher = Sha256::new(); + hasher.update(b"NOMOS_BALANCES_ROOT"); + + for (k, v) in self.balances.iter() { + hasher.update(k); + hasher.update(&v.to_le_bytes()); + } + + hasher.finalize().into() } pub fn total_balance(&self) -> u64 {