Skip to content

Commit

Permalink
Group channels to struct to reduce args number.
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Jul 24, 2023
1 parent c563645 commit 36e48de
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/light_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ pub async fn process_block(
Ok(())
}

pub struct Channels {
pub block_sender: Option<Sender<BlockVerified>>,
pub header_receiver: Receiver<(Header, Instant)>,
pub error_sender: Sender<anyhow::Error>,
}

/// Runs light client.
///
/// # Arguments
Expand All @@ -389,16 +395,14 @@ pub async fn process_block(
pub async fn run(
light_client: impl LightClient,
cfg: LightClientConfig,
block_tx: Option<Sender<BlockVerified>>,
pp: PublicParameters,
metrics: Metrics,
counter: Arc<Mutex<u32>>,
mut message_rx: Receiver<(Header, Instant)>,
error_sender: Sender<anyhow::Error>,
mut channels: Channels,
) {
info!("Starting light client...");

while let Some((header, received_at)) = message_rx.recv().await {
while let Some((header, received_at)) = channels.header_receiver.recv().await {
if let Some(seconds) = cfg.block_processing_delay.sleep_duration(received_at) {
info!("Sleeping for {seconds:?} seconds");
tokio::time::sleep(seconds).await;
Expand All @@ -416,7 +420,7 @@ pub async fn run(
.await
{
error!("Cannot process block: {error}");
if let Err(error) = error_sender.send(error).await {
if let Err(error) = channels.error_sender.send(error).await {
error!("Cannot send error message: {error}");
}
return;
Expand All @@ -429,7 +433,7 @@ pub async fn run(

// notify dht-based application client
// that newly mined block has been received
if let Some(ref channel) = block_tx {
if let Some(ref channel) = channels.block_sender {
if let Err(error) = channel.send(client_msg).await {
error!("Cannot send block verified message: {error}");
continue;
Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,15 +316,19 @@ async fn run(error_sender: Sender<anyhow::Error>) -> Result<()> {

let light_client = light_client::new(db, network_client, rpc_client);

let lc_channels = light_client::Channels {
block_sender: block_tx,
header_receiver: message_rx,
error_sender,
};

tokio::task::spawn(light_client::run(
light_client,
(&cfg).into(),
block_tx,
pp,
lc_metrics,
counter,
message_rx,
error_sender,
lc_channels,
));
Ok(())
}
Expand Down

0 comments on commit 36e48de

Please sign in to comment.