@@ -4,7 +4,6 @@ use std::ops::Deref;
4
4
use std:: path:: PathBuf ;
5
5
use std:: sync:: Arc ;
6
6
use std:: thread;
7
- use std:: time:: Duration ;
8
7
use futures:: StreamExt ;
9
8
use itertools:: { ExactlyOneError , Itertools } ;
10
9
@@ -16,7 +15,7 @@ use tokio::select;
16
15
use tokio:: sync:: broadcast:: { Receiver , Sender } ;
17
16
use tokio:: sync:: broadcast:: error:: TryRecvError ;
18
17
use tokio:: sync:: RwLock ;
19
- use tokio:: time:: { sleep, timeout} ;
18
+ use tokio:: time:: { sleep, Duration , timeout} ;
20
19
use yellowstone_grpc_proto:: geyser:: CommitmentLevel ;
21
20
22
21
use solana_lite_rpc_cluster_endpoints:: grpc_subscription:: create_block_processing_task;
@@ -25,7 +24,8 @@ use solana_lite_rpc_core::structures::produced_block::ProducedBlock;
25
24
26
25
pub const GRPC_VERSION : & str = "1.16.1" ;
27
26
28
- #[ tokio:: main( flavor = "multi_thread" , worker_threads = 16 ) ]
27
+ #[ tokio:: main]
28
+ // #[tokio::main(flavor = "multi_thread", worker_threads = 16)]
29
29
pub async fn main ( ) {
30
30
// info,solana_lite_rpc_cluster_endpoints=debug,stream_via_grpc=trace
31
31
tracing_subscriber:: fmt:: init ( ) ;
@@ -40,8 +40,8 @@ pub async fn main() {
40
40
// testnet - NOTE: this connection has terrible lags (almost 5 minutes)
41
41
// let grpc_addr = "http://147.28.169.13:10000".to_string();
42
42
43
- let ( block_sx_green, blocks_notifier_green) = tokio:: sync:: broadcast:: channel ( 1000 ) ;
44
- // let (block_sx_green, blocks_notifier_green) = start_monkey_broadcast::<ProducedBlock>(1000);
43
+ // let (block_sx_green, blocks_notifier_green) = tokio::sync::broadcast::channel(1000);
44
+ let ( block_sx_green, blocks_notifier_green) = start_monkey_broadcast :: < ProducedBlock > ( 1000 ) ;
45
45
let ( block_sx_blue, blocks_notifier_blue) = tokio:: sync:: broadcast:: channel ( 1000 ) ;
46
46
47
47
let grpc_x_token = None ;
@@ -71,8 +71,8 @@ pub async fn main() {
71
71
72
72
let ( offer_block_sender, mut offer_block_notifier) = tokio:: sync:: mpsc:: channel :: < OfferBlockMsg > ( 100 ) ;
73
73
74
- start_progressor ( "green" . to_string ( ) , blocks_notifier_green, rx_tip. clone ( ) , offer_block_sender. clone ( ) ) ;
75
- start_progressor ( "blue" . to_string ( ) , blocks_notifier_blue, rx_tip. clone ( ) , offer_block_sender. clone ( ) ) ;
74
+ start_progressor ( "green" . to_string ( ) , blocks_notifier_green, rx_tip. clone ( ) , offer_block_sender. clone ( ) ) . await ;
75
+ start_progressor ( "blue" . to_string ( ) , blocks_notifier_blue, rx_tip. clone ( ) , offer_block_sender. clone ( ) ) . await ;
76
76
77
77
78
78
// test
@@ -167,7 +167,13 @@ pub async fn main() {
167
167
} ) ;
168
168
169
169
// "infinite" sleep
170
- sleep ( Duration :: from_secs ( 1800 ) ) . await
170
+ sleep ( Duration :: from_secs ( 1800 ) ) . await ;
171
+
172
+ info ! ( "Shutting down..." ) ;
173
+ info ! ( "...tip variable" ) ;
174
+ drop ( tx_tip) ;
175
+ info ! ( "Shutdown completed." ) ;
176
+
171
177
}
172
178
173
179
#[ derive( Clone , Debug ) ]
@@ -190,9 +196,10 @@ enum OfferBlockMsg {
190
196
NextSlot ( String , BlockRef ) ,
191
197
}
192
198
193
- fn start_progressor ( label : String , blocks_notifier : Receiver < ProducedBlock > , mut rx_tip : tokio:: sync:: watch:: Receiver < Slot > ,
199
+ async fn start_progressor ( label : String , blocks_notifier : Receiver < ProducedBlock > , mut rx_tip : tokio:: sync:: watch:: Receiver < Slot > ,
194
200
offer_block_sender : tokio:: sync:: mpsc:: Sender < OfferBlockMsg > ) {
195
201
tokio:: spawn ( async move {
202
+ // TODO is .resubscribe what we want?
196
203
let mut blocks_notifier = blocks_notifier. resubscribe ( ) ;
197
204
// for test only
198
205
// let start_slot = blocks_notifier.recv().await.unwrap().slot;
@@ -201,33 +208,38 @@ fn start_progressor(label: String, blocks_notifier: Receiver<ProducedBlock>, mut
201
208
let mut local_tip = 0 ;
202
209
203
210
// block after tip offered by this stream
204
- let mut block_after_tip: BlockRef = BlockRef {
211
+ // TODO: block_after_tip is only valid/useful if greater than tip
212
+ let mut highest_block: BlockRef = BlockRef {
205
213
slot : 0 ,
206
214
parent_slot : 0 ,
207
215
} ;
208
216
' main_loop: loop {
209
217
select ! {
210
- _ = rx_tip. changed( ) => {
218
+ result = rx_tip. changed( ) => {
219
+ if result. is_err( ) {
220
+ debug!( "Tip variable closed for {}" , label) ;
221
+ break ' main_loop;
222
+ }
211
223
local_tip = rx_tip. borrow_and_update( ) . clone( ) ;
212
224
info!( "++> {} tip changed to {}" , label, local_tip) ;
213
225
// TODO update local tip
214
226
}
215
- recv_result = blocks_notifier. recv( ) , if !( block_after_tip . slot > local_tip) => {
227
+ recv_result = blocks_notifier. recv( ) , if !( highest_block . slot > local_tip) => {
216
228
match recv_result {
217
229
Ok ( block) => {
218
230
info!( "=> recv on {}: {}" , label, format_block( & block) ) ;
219
231
if block. slot > local_tip {
220
232
info!( "==> {}: beyond tip ({} > {})" , label, block. slot, local_tip) ;
221
- block_after_tip = BlockRef :: from( block) ;
222
- offer_block_sender. send( OfferBlockMsg :: NextSlot ( label. clone( ) , block_after_tip . clone( ) ) ) . await . unwrap( ) ;
233
+ highest_block = BlockRef :: from( block) ;
234
+ offer_block_sender. send( OfferBlockMsg :: NextSlot ( label. clone( ) , highest_block . clone( ) ) ) . await . unwrap( ) ;
223
235
// this thread will sleep and not issue any recvs until we get tip.changed signal
224
236
continue ' main_loop;
225
237
}
226
238
}
227
239
Err ( e) => {
228
240
// TODO what to do?
229
241
error!( "Error receiving block: {}" , e) ;
230
- continue ' main_loop;
242
+ break ' main_loop;
231
243
}
232
244
}
233
245
}
0 commit comments