@@ -43,7 +43,9 @@ use tokio::{
4343} ;
4444use tungstenite:: { connect, Message } ;
4545use url:: Url ;
46+
4647pub const SHRED_CF : & str = "archived_shreds" ;
48+
4749pub struct SampleService {
4850 sample_indices : Vec < u64 > ,
4951 // peers: Vec<(Pubkey, SocketAddr)>,
@@ -59,63 +61,78 @@ pub struct SampleServiceConfig {
5961#[ derive( Clone , Debug ) ]
6062pub struct ArchiveConfig {
6163 pub shred_archive_duration : u64 ,
62-
6364 pub archive_path : String ,
6465}
66+
6567#[ async_trait]
6668impl ClientService < SampleServiceConfig > for SampleService {
6769 type ServiceError = tokio:: task:: JoinError ;
70+
6871 fn new ( config : SampleServiceConfig ) -> Self {
6972 let sampler_handle = tokio:: spawn ( async move {
7073 let rpc_url = endpoint ( config. cluster ) ;
7174 let pub_sub = convert_to_websocket ! ( rpc_url) ;
75+
7276 let mut threads = Vec :: default ( ) ;
7377
7478 let ( slot_update_tx, slot_update_rx) = crossbeam:: channel:: unbounded :: < u64 > ( ) ;
7579 let ( shred_tx, shred_rx) = crossbeam:: channel:: unbounded ( ) ;
7680 let ( verified_shred_tx, verified_shred_rx) = crossbeam:: channel:: unbounded ( ) ;
77- let status_arc = Arc :: clone ( & config. status_sampler ) ;
81+
82+ let status_arc = config. status_sampler . clone ( ) ;
83+
84+ // waits on new slots => triggers shred_update_loop
7885 threads. push ( tokio:: spawn ( slot_update_loop (
7986 slot_update_tx,
8087 pub_sub,
8188 config. status_sampler ,
8289 ) ) ) ;
90+
91+ // sample shreds from new slot
92+ // verify each shred in shred_verify_loop
8393 threads. push ( tokio:: spawn ( shred_update_loop (
8494 slot_update_rx,
8595 rpc_url,
8696 shred_tx,
8797 status_arc,
8898 ) ) ) ;
8999
100+ // verify shreds + store in db in shred_archiver
90101 threads. push ( tokio:: spawn ( shred_verify_loop ( shred_rx, verified_shred_tx) ) ) ;
102+
91103 if let Some ( archive_config) = config. archive_config {
92104 threads. push ( tokio:: spawn ( shred_archiver (
93105 verified_shred_rx,
94106 archive_config,
95107 config. instance ,
96108 ) ) ) ;
97109 }
110+
98111 for thread in threads {
99- thread. await ;
112+ thread. await . unwrap ( ) ;
100113 }
101114 } ) ;
115+
102116 let sample_indices: Vec < u64 > = Vec :: default ( ) ;
103117 Self {
104118 sampler_handle,
105119 sample_indices,
106120 }
107121 }
122+
108123 async fn join ( self ) -> std:: result:: Result < ( ) , Self :: ServiceError > {
109124 self . sampler_handle . await
110125 }
111126}
127+
112128pub fn gen_random_indices ( max_shreds_per_slot : usize , sample_qty : usize ) -> Vec < usize > {
113129 let mut rng = StdRng :: from_entropy ( ) ;
114- let vec = ( 0 ..max_shreds_per_slot )
130+ let vec = ( 0 ..sample_qty )
115131 . map ( |_| rng. gen_range ( 0 ..max_shreds_per_slot) )
116132 . collect :: < Vec < usize > > ( ) ;
117- vec. as_slice ( ) [ 0 ..sample_qty ] . to_vec ( )
133+ vec
118134}
135+
119136pub async fn request_shreds (
120137 slot : usize ,
121138 indices : Vec < usize > ,
@@ -205,6 +222,7 @@ async fn shred_update_loop(
205222 }
206223
207224 if let Ok ( slot) = slot_update_rx. recv ( ) {
225+ // get shred length
208226 let shred_for_one = request_shreds ( slot as usize , vec ! [ 0 ] , endpoint. clone ( ) ) . await ;
209227 // info!("res {:?}", shred_for_one);
210228 let shred_indices_for_slot = match shred_for_one {
@@ -251,6 +269,8 @@ async fn shred_update_loop(
251269 None
252270 }
253271 } ;
272+
273+ // get a random sample of shreds
254274 info ! ( "indices of: {:?} {:?}" , shred_indices_for_slot, slot) ;
255275 if let Some ( shred_indices_for_slot) = shred_indices_for_slot. clone ( ) {
256276 let shreds_for_slot = request_shreds (
@@ -314,12 +334,14 @@ async fn shred_update_loop(
314334 }
315335 }
316336}
337+
317338// use solana_ledger::shred::dispatch;
339+
340+ // verifies the merkle proof of the shread
318341pub fn verify_sample ( shred : & Shred , leader : solana_ledger:: shred:: Pubkey ) -> bool {
319342 // @TODO fix error handling here
320343 let verify_merkle_root = match shred {
321344 Shred :: ShredData ( ShredData :: Merkle ( shred) ) => Some ( shred. verify_merkle_proof ( ) ) ,
322-
323345 Shred :: ShredCode ( ShredCode :: Merkle ( shred) ) => Some ( shred. verify_merkle_proof ( ) ) ,
324346 _ => None ,
325347 } ;
@@ -338,6 +360,7 @@ pub fn verify_sample(shred: &Shred, leader: solana_ledger::shred::Pubkey) -> boo
338360 . all ( |s| * s) ;
339361 verified
340362}
363+
341364pub async fn shred_verify_loop (
342365 shred_rx : Receiver < ( Vec < Option < Shred > > , solana_ledger:: shred:: Pubkey ) > ,
343366 verified_shred_tx : Sender < ( Shred , solana_ledger:: shred:: Pubkey ) > ,
@@ -373,6 +396,9 @@ pub async fn shred_verify_loop(
373396 }
374397 }
375398}
399+
400+
401+ // store verified shreds in db
376402pub async fn shred_archiver (
377403 verified_shred_rx : Receiver < ( Shred , solana_ledger:: shred:: Pubkey ) > ,
378404 _archive_config : ArchiveConfig ,
@@ -413,6 +439,8 @@ pub async fn shred_archiver(
413439 }
414440 }
415441}
442+
443+
416444pub async fn pull_and_verify_shreds ( slot : usize , endpoint : String ) -> bool {
417445 let shred_for_one = request_shreds ( slot, vec ! [ 0 ] , endpoint. clone ( ) ) . await ;
418446 // info!("res {:?}", shred_for_one);
@@ -522,6 +550,7 @@ pub async fn pull_and_verify_shreds(slot: usize, endpoint: String) -> bool {
522550 false
523551 }
524552}
553+
525554pub fn put_serialized < T : serde:: Serialize + std:: fmt:: Debug > (
526555 instance : & rocksdb:: DB ,
527556 cf : & ColumnFamily ,
0 commit comments