|
| 1 | +use crate::chain_state_pending::{RequestId, Verification}; |
| 2 | +use crate::control_plane::{ControlPlane, ResolvedState, VerificationError}; |
| 3 | +use crate::retry_runtime::RetrySender; |
| 4 | +use crate::signing::SignedVerification; |
| 5 | +use futures::Stream; |
| 6 | +use std::pin::Pin; |
| 7 | +use std::sync::Arc; |
| 8 | +use tokio::sync::mpsc::unbounded_channel; |
| 9 | +use tokio::task::JoinSet; |
| 10 | +use tokio_stream::StreamExt; |
| 11 | + |
| 12 | +pub(crate) struct TaskManager<CP> { |
| 13 | + control_plane: Arc<CP>, |
| 14 | +} |
| 15 | + |
| 16 | +impl<CP: ControlPlane> TaskManager<CP> |
| 17 | +where |
| 18 | + CP: Send + Sync + 'static, |
| 19 | +{ |
| 20 | + pub fn new(control_plane: impl Into<Arc<CP>>) -> Self { |
| 21 | + Self { |
| 22 | + control_plane: control_plane.into(), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + // `run` spawns a coroutine for each of the steps of processing, and each |
| 27 | + // request also gets its own sub(?)-coroutine to ensure that no slow RPC or |
| 28 | + // dodgy request can block the pipeline for other valid ones |
| 29 | + pub async fn run( |
| 30 | + &self, |
| 31 | + retry_tx: RetrySender, |
| 32 | + mut event_stream: Pin<Box<impl Stream<Item = Verification<RequestId>> + Send + 'static>>, |
| 33 | + ) { |
| 34 | + tracing::info!("starting channel manager"); |
| 35 | + let mut tasks = JoinSet::new(); |
| 36 | + let control_plane = self.control_plane.clone(); |
| 37 | + |
| 38 | + let (tx_verifications, mut rx_verifications) = |
| 39 | + unbounded_channel::<Verification<RequestId>>(); |
| 40 | + let (tx_resolve, mut rx_resolve) = unbounded_channel::<ResolvedState>(); |
| 41 | + let (tx_sign, mut rx_sign) = unbounded_channel::<ResolvedState>(); |
| 42 | + let (tx_submit, mut rx_submit) = unbounded_channel::<SignedVerification>(); |
| 43 | + let (tx_done, mut rx_done) = unbounded_channel::<SignedVerification>(); |
| 44 | + let (tx_err, mut rx_err) = unbounded_channel::<VerificationError>(); |
| 45 | + |
| 46 | + // 'receive' step |
| 47 | + { |
| 48 | + let tx_verifications = tx_verifications.clone(); |
| 49 | + tasks.spawn(async move { |
| 50 | + while let Some(verification) = event_stream.next().await { |
| 51 | + tx_verifications |
| 52 | + .send(verification) |
| 53 | + .expect("failed to send verification on channel"); |
| 54 | + } |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + // 'resolve' step |
| 59 | + { |
| 60 | + let control_plane = control_plane.clone(); |
| 61 | + let tx_resolve = tx_resolve.clone(); |
| 62 | + let tx_err = tx_err.clone(); |
| 63 | + |
| 64 | + // resolve |
| 65 | + tasks.spawn(async move { |
| 66 | + while let Some(verification) = rx_verifications.recv().await { |
| 67 | + let control_plane = control_plane.clone(); |
| 68 | + let tx_resolve = tx_resolve.clone(); |
| 69 | + let tx_err = tx_err.clone(); |
| 70 | + |
| 71 | + tokio::spawn(async move { |
| 72 | + match control_plane.resolve_state(verification.clone()).await { |
| 73 | + Ok(v) => tx_resolve.send(v).expect("error writing on channel"), |
| 74 | + Err(_) => tx_err |
| 75 | + .send(VerificationError::Resolve(verification)) |
| 76 | + .expect("error writing on channel"), |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + |
| 83 | + // 'evaluate' step |
| 84 | + { |
| 85 | + let control_plane = control_plane.clone(); |
| 86 | + let tx_err = tx_err.clone(); |
| 87 | + |
| 88 | + tasks.spawn(async move { |
| 89 | + while let Some(valid) = rx_resolve.recv().await { |
| 90 | + let control_plane = control_plane.clone(); |
| 91 | + let tx_sign = tx_sign.clone(); |
| 92 | + let tx_err = tx_err.clone(); |
| 93 | + |
| 94 | + tokio::spawn(async move { |
| 95 | + match control_plane.evaluate_state(valid.clone()).await { |
| 96 | + Ok(v) => tx_sign.send(v).expect("error writing on channel"), |
| 97 | + Err(_) => tx_err |
| 98 | + .send(VerificationError::Evaluate(valid)) |
| 99 | + .expect("error writing on channel"), |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + // 'sign' step |
| 107 | + { |
| 108 | + let control_plane = control_plane.clone(); |
| 109 | + let tx_err = tx_err.clone(); |
| 110 | + |
| 111 | + tasks.spawn(async move { |
| 112 | + while let Some(state) = rx_sign.recv().await { |
| 113 | + let control_plane = control_plane.clone(); |
| 114 | + let tx_submit = tx_submit.clone(); |
| 115 | + let tx_err = tx_err.clone(); |
| 116 | + |
| 117 | + tokio::spawn(async move { |
| 118 | + match control_plane.sign_state(state.clone()).await { |
| 119 | + Ok(v) => tx_submit.send(v).expect("error writing on channel"), |
| 120 | + Err(_) => tx_err |
| 121 | + .send(VerificationError::Sign(state)) |
| 122 | + .expect("error writing on channel"), |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + // 'submit' step |
| 130 | + { |
| 131 | + let control_plane = control_plane.clone(); |
| 132 | + let tx_done = tx_done.clone(); |
| 133 | + let tx_err = tx_err.clone(); |
| 134 | + |
| 135 | + tasks.spawn(async move { |
| 136 | + while let Some(signed_verification) = rx_submit.recv().await { |
| 137 | + let control_plane = control_plane.clone(); |
| 138 | + let tx_done = tx_done.clone(); |
| 139 | + let tx_err = tx_err.clone(); |
| 140 | + |
| 141 | + tokio::spawn(async move { |
| 142 | + match control_plane |
| 143 | + .submit_state(signed_verification.clone()) |
| 144 | + .await |
| 145 | + { |
| 146 | + Ok(v) => tx_done.send(v).expect("error writing on channel"), |
| 147 | + Err(_) => tx_err |
| 148 | + .send(VerificationError::Submit(signed_verification)) |
| 149 | + .expect("error writing on channel"), |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + // 'done' step |
| 157 | + tasks.spawn(async move { |
| 158 | + while let Some(state) = rx_done.recv().await { |
| 159 | + tracing::info!( |
| 160 | + chain_id = state.src_chain_id.to_string(), |
| 161 | + request_id = state.request_id.to_string(), |
| 162 | + "verification completed successfully" |
| 163 | + ); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + // 'error handling' step |
| 168 | + { |
| 169 | + let control_plane = control_plane.clone(); |
| 170 | + let retry_tx = retry_tx.clone(); |
| 171 | + |
| 172 | + tasks.spawn(async move { |
| 173 | + while let Some(err) = rx_err.recv().await { |
| 174 | + control_plane.handle_error(&err, retry_tx.clone()).await; |
| 175 | + } |
| 176 | + }); |
| 177 | + } |
| 178 | + |
| 179 | + // join all the things so we don't cede control back to the caller |
| 180 | + tracing::info!("all tasks started"); |
| 181 | + if tasks.join_next().await.is_some() { |
| 182 | + panic!("a task manager task ended, but they should run forever") |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments