@@ -164,7 +164,8 @@ impl From<DBMembershipProof> for MembershipProof<Fr254> {
164164pub 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}
205206impl 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
0 commit comments