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
This commit is contained in:
Giacomo Pasini 2024-08-26 19:29:49 +02:00 committed by GitHub
parent da18cef987
commit 23df7331c8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 14 deletions

View File

@ -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<u8> = vec![];
bytes.extend(owner);
bytes.extend(balance.to_le_bytes());
bytes
}));
let balance_merkle_leaves = cl::merkle::padded_leaves(&balance_bytes);
merkle::root::<MAX_BALANCES>(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 {