Skip to content

Commit dcdaab9

Browse files
authored
Merge pull request #20 from EYBlockchain/kahina/storing-commitment-Mongodb
Kahina/storing commitment mongodb
2 parents 3c64402 + 0fa364b commit dcdaab9

7 files changed

Lines changed: 198 additions & 172 deletions

File tree

nightfall_client/src/driven/db/mongo.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ impl From<DBMembershipProof> for MembershipProof<Fr254> {
164164
pub struct CommitmentEntry {
165165
pub preimage: Preimage,
166166
pub status: CommitmentStatus,
167-
#[serde(serialize_with = "ark_se_hex", deserialize_with = "ark_de_hex")]
167+
168+
#[serde(rename = "_id",serialize_with = "ark_se_hex", deserialize_with = "ark_de_hex")]
168169
pub key: Fr254,
169170
#[serde(
170171
serialize_with = "ark_se_hex",
@@ -203,7 +204,8 @@ impl Commitment for CommitmentEntry {
203204
}
204205
}
205206
impl CommitmentEntryDB for CommitmentEntry {
206-
fn new(preimage: Preimage, key: Fr254, nullifier: Fr254, status: CommitmentStatus) -> Self {
207+
fn new(preimage: Preimage, nullifier: Fr254, status: CommitmentStatus) -> Self {
208+
let key = preimage.hash().expect("failed to hash preimage");
207209
Self {
208210
preimage,
209211
status,
@@ -328,7 +330,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
328330
}
329331

330332
async fn get_commitment(&self, k: &Fr254) -> Option<CommitmentEntry> {
331-
let filter = doc! { "key": hex::encode(k.into_bigint().to_bytes_le()) };
333+
let filter = doc! { "_id": hex::encode(k.into_bigint().to_bytes_le()) };
332334
self.database(DB)
333335
.collection::<CommitmentEntry>("commitments")
334336
.find_one(filter)
@@ -365,7 +367,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
365367
.into_iter()
366368
.map(|c| hex::encode(c.into_bigint().to_bytes_le()))
367369
.collect::<Vec<_>>();
368-
let filter = doc! { "key": { "$in": commitment_str }};
370+
let filter = doc! { "_id": { "$in": commitment_str }};
369371
let update = doc! {"$set": { "status": "PendingSpend" }};
370372
self.database(DB)
371373
.collection::<CommitmentEntry>("commitments")
@@ -380,7 +382,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
380382
.into_iter()
381383
.map(|c| hex::encode(c.into_bigint().to_bytes_le()))
382384
.collect::<Vec<_>>();
383-
let filter = doc! { "key": { "$in": commitment_str }};
385+
let filter = doc! { "_id": { "$in": commitment_str }};
384386
let update = doc! {"$set": { "status": "PendingCreation" }};
385387
self.database(DB)
386388
.collection::<CommitmentEntry>("commitments")
@@ -419,7 +421,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
419421
.collect::<Vec<_>>();
420422
let l1_hash = l1_hash.map(|h| h.encode_hex());
421423
let l2_blocknumber = l2_blocknumber.map(|b| b.encode_hex());
422-
let filter = doc! { "key": { "$in": commitment_str }};
424+
let filter = doc! { "_id": { "$in": commitment_str }};
423425
let update = doc! {"$set": { "status": "Unspent", "layer_1_transaction_hash": l1_hash, "layer_2_block_number": l2_blocknumber }};
424426
self.database(DB)
425427
.collection::<CommitmentEntry>("commitments")
@@ -431,7 +433,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
431433

432434
// we compute a nullifier for each spend commitment that we process.
433435
async fn add_nullifier(&self, key: &Fr254, nullifier: Fr254) -> Option<()> {
434-
let filter = doc! { "key": hex::encode(key.into_bigint().to_bytes_le())};
436+
let filter = doc! { "_id": hex::encode(key.into_bigint().to_bytes_le())};
435437
let update =
436438
doc! {"$set": { "nullifier": hex::encode(nullifier.into_bigint().to_bytes_le()) }};
437439

@@ -443,7 +445,10 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
443445
Some(())
444446
}
445447

446-
async fn store_commitment(&self, commitment: CommitmentEntry) -> Option<()> {
448+
async fn store_commitment(
449+
&self,
450+
commitment: CommitmentEntry
451+
) -> Option<()> {
447452
let result = self
448453
.database(DB)
449454
.collection::<CommitmentEntry>("commitments")
@@ -464,7 +469,7 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
464469
}
465470
}
466471

467-
/// function to store multiple commitments in the database, optionally ignoring duplicate key errors
472+
/// function to store multiple commitments in the database, optionally ignoring duplicate _id errors
468473
async fn store_commitments(
469474
&self,
470475
commitments: &[CommitmentEntry],
@@ -480,13 +485,13 @@ impl CommitmentDB<Fr254, CommitmentEntry> for Client {
480485
.await;
481486
match res {
482487
Ok(_) => Some(()),
483-
// unpack the Mongo error and check if it's a duplicate key error. If so, behave according to dup_key_check
488+
// unpack the Mongo error and check if it's a duplicate _id error. If so, behave according to dup_key_check
484489
Err(e) => {
485490
match e.kind.as_ref() {
486491
ErrorKind::Write(WriteError(write_error)) => {
487492
if write_error.code == 11000 && !dup_key_check {
488-
println!("Duplicate key error: {:?}", write_error);
489-
// duplicate key error but we don't care
493+
println!("Duplicate _id error: {:?}", write_error);
494+
// duplicate _id error but we don't care
490495
Some(())
491496
} else {
492497
None

nightfall_client/src/driven/event_handlers/nightfall_event.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,20 +323,12 @@ async fn process_propose_block_event<N: NightfallContract>(
323323
debug!("Commitment {} is not owned by us", commitment_hash);
324324
} else {
325325
info!("Received commitment owned by us, with hash {}", test_hash);
326-
// We can transfer to ourselves, so we need to check if we already have the commitment in our database
327-
let found_commitment = db.get_commitment(&commitment_hash).await;
328-
if found_commitment.is_some() {
329-
debug!("Commitment {} already in database", commitment_hash);
330-
// If we already have the commitment, we don't need to do anything
331-
continue;
332-
}
333326
// store our newly received commitment in our commitment db
334327
let nullifier = test_preimage
335328
.nullifier_hash(&nullifier_key)
336329
.map_err(|_| EventHandlerError::HashError)?;
337330
let commitment_entry = CommitmentEntry::new(
338331
test_preimage,
339-
commitment_hash,
340332
nullifier,
341333
CommitmentStatus::Unspent,
342334
);

nightfall_client/src/drivers/rest/client_nf_3.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ pub async fn handle_deposit<N: NightfallContract>(
398398
let commitment_hash = preimage_value.hash().expect("Could not hash commitment");
399399
let commitment_entry = CommitmentEntry::new(
400400
preimage_value,
401-
commitment_hash,
402401
nullifier,
403402
CommitmentStatus::PendingCreation,
404403
);
@@ -432,7 +431,6 @@ pub async fn handle_deposit<N: NightfallContract>(
432431

433432
let commitment_entry = CommitmentEntry::new(
434433
preimage_fee,
435-
commitment_hash,
436434
nullifier,
437435
CommitmentStatus::PendingCreation,
438436
);

nightfall_client/src/drivers/rest/client_operation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ where
8484
.expect("Nullifiers must be hashable");
8585
let commitment_entry = CommitmentEntry::new(
8686
commitment.get_preimage(),
87-
commitment_hash,
8887
nullifier,
8988
CommitmentStatus::PendingCreation,
9089
);

nightfall_client/src/ports/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where
4141
}
4242

4343
pub trait CommitmentEntryDB: Commitment {
44-
fn new(preimage: Preimage, key: Fr254, nullifier: Fr254, status: CommitmentStatus) -> Self;
44+
fn new(preimage: Preimage, nullifier: Fr254, status: CommitmentStatus) -> Self;
4545
fn get_status(&self) -> CommitmentStatus;
4646
}
4747

0 commit comments

Comments
 (0)