1
+ use crate :: tree:: { hash, Digest , KeyDirectoryTree , Proof , RedisKDTree } ;
1
2
use anyhow:: { Context , Result } ;
2
3
use async_trait:: async_trait;
3
4
use ed25519:: Signature ;
4
5
use ed25519_dalek:: { Signer , SigningKey } ;
5
- use jmt:: { storage:: { TreeReader , TreeWriter } , KeyHash } ;
6
- // use indexed_merkle_tree::{
7
- // node::Node,
8
- // tree::{IndexedMerkleTree, Proof},
9
- // Hash,
10
- // };
11
- use crate :: tree:: { hash, Digest , KeyDirectoryTree , Proof } ;
6
+ use jmt:: KeyHash ;
12
7
use std:: { self , str:: FromStr , sync:: Arc } ;
13
8
use tokio:: {
14
9
sync:: {
@@ -24,7 +19,6 @@ use crate::error::DataAvailabilityError;
24
19
25
20
use crate :: {
26
21
cfg:: Config ,
27
- circuits:: BatchMerkleProofCircuit ,
28
22
common:: { AccountSource , Hashchain , HashchainEntry , Operation } ,
29
23
consts:: { CHANNEL_BUFFER_SIZE , DA_RETRY_COUNT , DA_RETRY_INTERVAL } ,
30
24
da:: { DataAvailabilityLayer , FinalizedEpoch } ,
@@ -34,10 +28,7 @@ use crate::{
34
28
webserver:: { OperationInput , WebServer } ,
35
29
} ;
36
30
37
- pub struct Sequencer < ' a , S >
38
- where
39
- S : ' a + TreeReader + TreeWriter ,
40
- {
31
+ pub struct Sequencer < ' a > {
41
32
pub db : Arc < dyn Database > ,
42
33
pub da : Arc < dyn DataAvailabilityLayer > ,
43
34
pub ws : WebServer ,
@@ -52,17 +43,14 @@ where
52
43
// [`pending_operations`] is a buffer for operations that have not yet been
53
44
// posted to the DA layer.
54
45
pending_operations : Arc < Mutex < Vec < Operation > > > ,
55
- tree : Arc < Mutex < KeyDirectoryTree < ' a , S > > > ,
46
+ tree : Arc < Mutex < RedisKDTree < ' a > > > ,
56
47
57
48
epoch_buffer_tx : Arc < Sender < FinalizedEpoch > > ,
58
49
epoch_buffer_rx : Arc < Mutex < Receiver < FinalizedEpoch > > > ,
59
50
}
60
51
61
52
#[ async_trait]
62
- impl < ' a , S > NodeType for Sequencer < ' a , S >
63
- where
64
- S : ' a + TreeReader + TreeWriter ,
65
- {
53
+ impl < ' a > NodeType for Sequencer < ' a > {
66
54
async fn start ( self : Arc < Self > ) -> Result < ( ) > {
67
55
self . da . start ( ) . await . context ( "Failed to start DA layer" ) ?;
68
56
@@ -95,13 +83,15 @@ impl Sequencer {
95
83
96
84
let start_height = cfg. celestia_config . unwrap_or_default ( ) . start_height ;
97
85
86
+ let tree = KeyDirectoryTree :: new ( & db. clone ( ) ) ;
87
+
98
88
Ok ( Sequencer {
99
89
db,
100
90
da,
101
91
ws : WebServer :: new ( ws) ,
102
92
key,
103
93
start_height,
104
- tree : Arc :: new ( Mutex :: new ( IndexedMerkleTree :: new_with_size ( 1024 ) . unwrap ( ) ) ) ,
94
+ tree,
105
95
pending_operations : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ,
106
96
epoch_buffer_tx : Arc :: new ( tx) ,
107
97
epoch_buffer_rx : Arc :: new ( Mutex :: new ( rx) ) ,
@@ -309,22 +299,22 @@ impl Sequencer {
309
299
. context ( "Failed to set new epoch" ) ?;
310
300
// add the commitment for the operations ran since the last epoch
311
301
self . db
312
- . add_commitment ( & epoch, & current_commitment)
302
+ . set_commitment ( & epoch, & current_commitment)
313
303
. context ( "Failed to add commitment for new epoch" ) ?;
314
304
315
- let batch_circuit =
316
- BatchMerkleProofCircuit :: new ( & prev_commitment, & current_commitment, proofs)
317
- . context ( "Failed to create BatchMerkleProofCircuit" ) ?;
318
- let ( proof, verifying_key) = batch_circuit
319
- . create_and_verify_snark ( )
320
- . context ( "Failed to create and verify snark" ) ?;
305
+ // let batch_circuit =
306
+ // BatchMerkleProofCircuit::new(&prev_commitment, ¤t_commitment, proofs)
307
+ // .context("Failed to create BatchMerkleProofCircuit")?;
308
+ // let (proof, verifying_key) = batch_circuit
309
+ // .create_and_verify_snark()
310
+ // .context("Failed to create and verify snark")?;
321
311
322
312
let epoch_json = FinalizedEpoch {
323
313
height : epoch,
324
314
prev_commitment,
325
315
current_commitment,
326
- proof : proof. into ( ) ,
327
- verifying_key : verifying_key. into ( ) ,
316
+ // proof: proof.into(),
317
+ // verifying_key: verifying_key.into(),
328
318
signature : None ,
329
319
} ;
330
320
@@ -377,25 +367,23 @@ impl Sequencer {
377
367
let new_chain_entry = HashchainEntry :: new ( operation. clone ( ) , previous_hash) ;
378
368
current_chain. push ( new_chain_entry. clone ( ) ) ;
379
369
380
- let updated_node = Node :: new_leaf (
381
- node. is_left_sibling ( ) ,
382
- hashed_id,
383
- new_chain_entry. hash ,
384
- node. get_next ( ) ,
385
- ) ;
370
+ // let updated_node = Node::new_leaf(
371
+ // node.is_left_sibling(),
372
+ // hashed_id,
373
+ // new_chain_entry.hash,
374
+ // node.get_next(),
375
+ // );
386
376
387
377
debug ! ( "updating hashchain for user id {}" , id. clone( ) ) ;
388
- self . tree . insert ( KeyHash :: with ( hashed_id) , )
378
+ let proof = self . tree . update ( KeyHash :: with ( hashed_id) , current_chain ) ? ;
389
379
self . db
390
380
. update_hashchain ( operation, & current_chain)
391
381
. context ( format ! (
392
382
"Failed to update hashchain for operation {:?}" ,
393
383
operation
394
384
) ) ?;
395
385
396
- tree. update_node ( index, updated_node)
397
- . map ( Proof :: Update )
398
- . context ( "Failed to update node in tree" )
386
+ proof
399
387
}
400
388
Operation :: CreateAccount { id, value, source } => {
401
389
// validation of account source
@@ -420,10 +408,11 @@ impl Sequencer {
420
408
}
421
409
422
410
debug ! ( "creating new hashchain for user id {}" , id. clone( ) ) ;
423
- let new_chain = vec ! [ HashchainEntry :: new( operation. clone( ) , Node :: HEAD ) ] ;
411
+ let chain = Hashchain :: new ( id. clone ( ) ) ;
412
+ chain. create_account ( value. into ( ) , * source) ;
424
413
425
414
self . db
426
- . update_hashchain ( operation, & new_chain )
415
+ . update_hashchain ( operation, & chain )
427
416
. context ( format ! (
428
417
"Failed to create hashchain for operation {:?}" ,
429
418
operation
@@ -432,11 +421,7 @@ impl Sequencer {
432
421
let mut tree = self . tree . lock ( ) . await ;
433
422
let hashed_id = hash ( id. as_bytes ( ) ) ;
434
423
435
- let mut node =
436
- Node :: new_leaf ( true , hashed_id, new_chain. first ( ) . unwrap ( ) . hash , Node :: TAIL ) ;
437
- tree. insert_node ( & mut node)
438
- . map ( Proof :: Insert )
439
- . context ( "Failed to insert node into tree" )
424
+ tree. insert ( KeyHash :: with ( hashed_id) , chain)
440
425
}
441
426
}
442
427
}
@@ -479,7 +464,7 @@ mod tests {
479
464
}
480
465
481
466
// Helper function to create a test Sequencer instance
482
- async fn create_test_sequencer ( ) -> Arc < Sequencer > {
467
+ async fn create_test_sequencer ( ) -> Arc < Sequencer < ' static > > {
483
468
let ( da_layer, _rx, _brx) = InMemoryDataAvailabilityLayer :: new ( 1 ) ;
484
469
let da_layer = Arc :: new ( da_layer) ;
485
470
let db = Arc :: new ( setup_db ( ) ) ;
@@ -578,7 +563,7 @@ mod tests {
578
563
assert_ne ! ( prev_commitment, new_commitment) ;
579
564
580
565
let hashchain = sequencer. db . get_hashchain ( id. as_str ( ) ) ;
581
- let value = hashchain. unwrap ( ) . first ( ) . unwrap ( ) . operation . value ( ) ;
566
+ let value = hashchain. unwrap ( ) . get ( 0 ) . operation . value ( ) ;
582
567
assert_eq ! ( value, "test" ) ;
583
568
584
569
teardown_db ( & db) ;
@@ -696,7 +681,7 @@ mod tests {
696
681
697
682
let hashchain = sequencer. db . get_hashchain ( "user@example.com" ) . unwrap ( ) ;
698
683
assert_eq ! ( hashchain. len( ) , 2 ) ;
699
- assert_eq ! ( hashchain[ 1 ] . operation. value( ) , "new_value" ) ;
684
+ assert_eq ! ( hashchain. get ( 1 ) . operation. value( ) , "new_value" ) ;
700
685
}
701
686
702
687
#[ tokio:: test]
@@ -724,7 +709,10 @@ mod tests {
724
709
725
710
let hashchain = sequencer. db . get_hashchain ( "user@example.com" ) . unwrap ( ) ;
726
711
assert_eq ! ( hashchain. len( ) , 2 ) ;
727
- assert ! ( matches!( hashchain[ 1 ] . operation, Operation :: Revoke { .. } ) ) ;
712
+ assert ! ( matches!(
713
+ hashchain. get( 1 ) . operation,
714
+ Operation :: Revoke { .. }
715
+ ) ) ;
728
716
}
729
717
730
718
#[ tokio:: test]
0 commit comments