From 8ec3cadd1b27221b0e29607456473d661e3b5ab5 Mon Sep 17 00:00:00 2001 From: Kasper Ziemianek Date: Tue, 20 Aug 2024 15:18:14 +0200 Subject: [PATCH] Wait for block broadcast (#3006) Co-authored-by: Kai <7630809+Kailai-Wang@users.noreply.github.com> --- tee-worker/service/src/worker.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tee-worker/service/src/worker.rs b/tee-worker/service/src/worker.rs index 136a58ccbb..767a24462f 100644 --- a/tee-worker/service/src/worker.rs +++ b/tee-worker/service/src/worker.rs @@ -23,6 +23,7 @@ use crate::{config::Config, error::Error, initialized_service::TrackInitialization}; use async_trait::async_trait; use codec::{Decode, Encode}; +use futures::future::join_all; use itc_rpc_client::direct_client::{DirectApi, DirectClient as DirectWorkerApi}; use itp_enclave_api::enclave_base::EnclaveBase; use itp_node_api::{api_client::PalletTeebagApi, node_api_factory::CreateNodeApi}; @@ -119,19 +120,22 @@ where let nr_peers = peers.len(); - for url in peers { + let futures = peers.iter().map(|url| { let encoded_blocks_cloned = encoded_blocks.clone(); - tokio::spawn(async move { - let client = DirectWorkerApi::new(url.trusted.to_string()); - + let url = url.trusted.clone(); + tokio::task::spawn_blocking(move || { + let client = DirectWorkerApi::new(url.to_string()); if let Err(e) = client.import_sidechain_blocks(encoded_blocks_cloned) { error!( "Broadcast block request ({}) to {} failed: {:?}", - RPC_METHOD_NAME_IMPORT_BLOCKS, url.trusted, e + RPC_METHOD_NAME_IMPORT_BLOCKS, url, e ); } - }); - } + }) + }); + + join_all(futures).await; + info!("broadcast {} block(s) to {} peers", nr_blocks, nr_peers); Ok(()) }