Skip to content

Commit 80d132a

Browse files
committed
Use separate threads for chains
1 parent a358385 commit 80d132a

4 files changed

Lines changed: 52 additions & 29 deletions

File tree

hyperspace/core/src/lib.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
use futures::{future::ready, StreamExt};
1818
use primitives::Chain;
19+
use std::convert::Infallible;
1920

2021
pub mod chain;
2122
pub mod command;
@@ -30,6 +31,7 @@ use events::{has_packet_events, parse_events};
3031
use futures::TryFutureExt;
3132
use ibc::events::IbcEvent;
3233
use metrics::handler::MetricsHandler;
34+
use tokio::task::JoinHandle;
3335

3436
#[derive(Copy, Debug, Clone)]
3537
pub enum Mode {
@@ -53,19 +55,40 @@ where
5355
let (mut chain_a_finality, mut chain_b_finality) =
5456
(chain_a.finality_notifications().await?, chain_b.finality_notifications().await?);
5557

56-
// loop forever
57-
loop {
58-
tokio::select! {
59-
// new finality event from chain A
60-
result = chain_a_finality.next() => {
61-
process_finality_event!(chain_a, chain_b, chain_a_metrics, mode, result, chain_a_finality, chain_b_finality)
62-
}
63-
// new finality event from chain B
64-
result = chain_b_finality.next() => {
65-
process_finality_event!(chain_b, chain_a, chain_b_metrics, mode, result, chain_b_finality, chain_a_finality)
66-
}
58+
let mut chain_a_cl = chain_a.clone();
59+
let mut chain_b_cl = chain_b.clone();
60+
61+
let jh1: JoinHandle<anyhow::Result<Infallible>> = tokio::spawn(async move {
62+
loop {
63+
let result = chain_a_finality.next().await;
64+
process_finality_event!(
65+
chain_a_cl,
66+
chain_b_cl,
67+
chain_a_metrics,
68+
mode,
69+
result,
70+
chain_a_finality
71+
)
6772
}
68-
}
73+
});
74+
75+
let jh2: JoinHandle<anyhow::Result<Infallible>> = tokio::spawn(async move {
76+
loop {
77+
let result = chain_b_finality.next().await;
78+
process_finality_event!(
79+
chain_b,
80+
chain_a,
81+
chain_b_metrics,
82+
mode,
83+
result,
84+
chain_b_finality
85+
)
86+
}
87+
});
88+
89+
jh1.await??;
90+
jh2.await??;
91+
Ok(())
6992
}
7093

7194
pub async fn fish<A, B>(chain_a: A, chain_b: B) -> Result<(), anyhow::Error>

hyperspace/core/src/macros.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414

1515
#[macro_export]
1616
macro_rules! process_finality_event {
17-
($source:ident, $sink:ident, $metrics:expr, $mode:ident, $result:ident, $stream_source:ident, $stream_sink:ident) => {
17+
($source:ident, $sink:ident, $metrics:expr, $mode:ident, $result:ident, $stream:ident) => {
1818
match $result {
1919
// stream closed
2020
None => {
2121
log::warn!("Stream closed for {}", $source.name());
22-
$stream_source = loop {
22+
$stream = loop {
2323
match $source.finality_notifications().await {
2424
Ok(stream) => break stream,
2525
Err(e) => {
@@ -28,15 +28,6 @@ macro_rules! process_finality_event {
2828
},
2929
};
3030
};
31-
$stream_sink = loop {
32-
match $sink.finality_notifications().await {
33-
Ok(stream) => break stream,
34-
Err(e) => {
35-
log::error!("Failed to get finality notifications for {} {:?}. Trying again in 30 seconds...", $sink.name(), e);
36-
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
37-
},
38-
};
39-
};
4031
},
4132
Some(finality_event) => {
4233
log::info!("=======================================================");

hyperspace/core/src/queue.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use ibc_proto::google::protobuf::Any;
1616
use metrics::handler::MetricsHandler;
1717
use primitives::Chain;
18+
use tokio::task::JoinHandle;
1819

1920
/// This sends messages to the sink chain in a gas-aware manner.
2021
pub async fn flush_message_batch(
@@ -32,7 +33,11 @@ pub async fn flush_message_batch(
3233
log::debug!(target: "hyperspace", "Outgoing messages weight: {} block max weight: {}", batch_weight, block_max_weight);
3334
let ratio = (batch_weight / block_max_weight) as usize;
3435
if ratio == 0 {
35-
sink.submit(msgs).await?;
36+
let sink = sink.clone();
37+
let _join_handler: JoinHandle<Result<_, anyhow::Error>> = tokio::spawn(async move {
38+
sink.submit(msgs).await?;
39+
Ok(())
40+
});
3641
return Ok(())
3742
}
3843

@@ -51,10 +56,14 @@ pub async fn flush_message_batch(
5156
);
5257
let chunk_size = (msgs.len() / chunk).max(1);
5358
// TODO: return number of failed messages and record it to metrics
54-
for batch in msgs.chunks(chunk_size) {
55-
// send out batches.
56-
sink.submit(batch.to_vec()).await?;
57-
}
59+
let sink = sink.clone();
60+
let _join_handler: JoinHandle<Result<_, anyhow::Error>> = tokio::spawn(async move {
61+
for batch in msgs.chunks(chunk_size) {
62+
// send out batches.
63+
sink.submit(batch.to_vec()).await?;
64+
}
65+
Ok(())
66+
});
5867

5968
Ok(())
6069
}

hyperspace/primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub fn apply_prefix(mut commitment_prefix: Vec<u8>, path: impl Into<Vec<u8>>) ->
9898
#[async_trait::async_trait]
9999
pub trait IbcProvider {
100100
/// Finality event type, passed on to [`Chain::query_latest_ibc_events`]
101-
type FinalityEvent: Debug;
101+
type FinalityEvent: Debug + Send;
102102
/// A representation of the transaction id for the chain
103103
type TransactionId: Debug;
104104
/// Asset Id

0 commit comments

Comments
 (0)