@@ -13,7 +13,14 @@ use std::fs::File;
1313use std:: io:: Write ;
1414use std:: path:: Path ;
1515use std:: sync:: { Arc , RwLock } ;
16+ use cardano_serialization_lib:: { Block , Transaction , TransactionWitnessSet } ;
17+ use cardano_serialization_lib:: utils:: BigNum ;
18+ use futures:: executor:: block_on;
19+ use futures_util:: StreamExt ;
20+ use serde:: { Deserialize , Serialize } ;
21+ use pharos:: { Channel , Observable } ;
1622use tokio:: task:: JoinHandle ;
23+ use crate :: InMemoryNode ;
1724
1825pub type BlockNo = u32 ;
1926pub type Address = String ;
@@ -24,7 +31,14 @@ pub struct SharedInMemoryDbSync {
2431 pub ( crate ) update_thread : JoinHandle < ( ) > ,
2532 // Allowing for now since there is no usage yet in explorer service
2633 #[ allow( dead_code) ]
27- pub ( crate ) db_sync : Arc < RwLock < InMemoryDbSync > > ,
34+
35+ /// thread safe `InMemoryDbSync`. It has inner struct db_sync with rw lock guard and handle to update
36+ /// thread which listen to `InMemoryNode` mock block updates
37+ pub struct SharedInMemoryDbSync {
38+ pub ( crate ) update_thread : JoinHandle < ( ) > ,
39+ // Allowing for now since there is no usage yet in explorer service
40+ #[ allow( dead_code) ]
41+ pub ( crate ) db_sync : Arc < RwLock < InMemoryDbSync > >
2842}
2943
3044impl Drop for SharedInMemoryDbSync {
@@ -33,6 +47,7 @@ impl Drop for SharedInMemoryDbSync {
3347 }
3448}
3549
50+
3651/// Mock of real cardano db sync. At this moment we only stores transactions metadata
3752/// as the only purpose of existance for this struct is to provide catalyst voting registrations
3853/// Struct can be persisted and restored from json file using `serde_json`.
@@ -41,8 +56,9 @@ pub struct InMemoryDbSync {
4156 pub ( crate ) transactions : HashMap < BlockNo , Vec < Transaction > > ,
4257 pub ( crate ) blocks : Vec < Block > ,
4358 stakes : HashMap < Address , BigNum > ,
44- settings : Settings ,
59+ settings : Settings
4560}
61+
4662impl Debug for InMemoryDbSync {
4763 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
4864 write ! ( f, "{:?}" , self . settings)
@@ -73,18 +89,18 @@ impl InMemoryDbSync {
7389 let shared_db_sync = Arc :: new ( RwLock :: new ( self ) ) ;
7490 let db_sync = shared_db_sync. clone ( ) ;
7591
76- let handle = tokio:: spawn ( async move {
77- loop {
92+ let handle = tokio:: spawn ( async move {
93+ loop {
7894 let block = observer. next ( ) . await ;
7995 if let Some ( block) = block {
8096 db_sync. write ( ) . unwrap ( ) . on_block_propagation ( & block) ;
8197 }
8298 }
8399 } ) ;
84100
85- SharedInMemoryDbSync {
101+ SharedInMemoryDbSync {
86102 update_thread : handle,
87- db_sync : shared_db_sync,
103+ db_sync : shared_db_sync
88104 }
89105 }
90106
0 commit comments