ledger maintains append only bundle MMR as well

This commit is contained in:
David Rusu 2025-03-08 21:30:40 +04:00
parent 62312074cc
commit a85b306baa
2 changed files with 21 additions and 3 deletions

View File

@ -1,7 +1,9 @@
use crate::{
crust::{NoteCommitment, Nullifier},
ds::indexed::{BatchUpdateProof, NullifierTree},
ds::mmr::{MMRProof, MMR},
crust::{tx::LedgerUpdate, Bundle, NoteCommitment, Nullifier},
ds::{
indexed::{BatchUpdateProof, NullifierTree},
mmr::{MMRProof, MMR},
},
};
use serde::{Deserialize, Serialize};
@ -9,12 +11,14 @@ use serde::{Deserialize, Serialize};
pub struct Ledger {
cm_root: [u8; 32],
nf_root: [u8; 32],
bundles_root: [u8; 32],
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LedgerWitness {
pub commitments: MMR,
pub nf_root: [u8; 32],
pub bundles: MMR,
}
impl LedgerWitness {
@ -22,6 +26,7 @@ impl LedgerWitness {
Ledger {
cm_root: self.commitments.commit(),
nf_root: self.nf_root,
bundles_root: self.bundles.commit(),
}
}
@ -33,6 +38,10 @@ impl LedgerWitness {
self.commitments.push(&cm.0);
}
pub fn add_bundle(&mut self, bundle_root: [u8; 32]) {
self.bundles.push(&bundle_root);
}
pub fn assert_nfs_update(&mut self, nullifiers: &[Nullifier], proof: &BatchUpdateProof) {
// update the nullifer root with the nullifier inserted into the tree
self.nf_root = proof.verify(nullifiers, self.nf_root);
@ -43,6 +52,7 @@ impl LedgerWitness {
pub struct LedgerState {
pub commitments: MMR,
pub nullifiers: NullifierTree,
pub bundles: MMR,
}
impl LedgerState {
@ -50,6 +60,7 @@ impl LedgerState {
LedgerWitness {
commitments: self.commitments.clone(),
nf_root: self.nf_root(),
bundles: self.bundles.clone(),
}
}
@ -65,4 +76,9 @@ impl LedgerState {
pub fn add_nullifiers(&mut self, nfs: Vec<Nullifier>) -> BatchUpdateProof {
self.nullifiers.insert_batch(nfs)
}
pub fn add_bundle(&mut self, bundle_root: [u8; 32]) -> (MMR, MMRProof) {
let proof = self.bundles.push(&bundle_root);
(self.bundles.clone(), proof)
}
}

View File

@ -59,6 +59,8 @@ fn main() {
nullifiers.extend(ledger_update.inputs);
}
ledger.add_bundle(bundle.root.0);
}
// TODO: sort outside and check