|
| 1 | +// Copyright 2025 PRAGMA |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use crate::consensus::errors::ProcessingFailed; |
| 16 | +use crate::consensus::tip::HeaderTip; |
| 17 | +use amaru_kernel::peer::Peer; |
| 18 | +use amaru_kernel::{Header, Point}; |
| 19 | +use amaru_ouroboros_traits::IsHeader; |
| 20 | +use anyhow::anyhow; |
| 21 | +use async_trait::async_trait; |
| 22 | +use pure_stage::{ExternalEffect, ExternalEffectAPI, Resources}; |
| 23 | +use serde::{Deserialize, Serialize}; |
| 24 | +use std::fmt::Display; |
| 25 | +use std::sync::Arc; |
| 26 | + |
| 27 | +pub type ResourceForwardEventListener = Arc<dyn ForwardEventListener + Send + Sync>; |
| 28 | + |
| 29 | +/// A listener interface for forward events (new headers or rollbacks). |
| 30 | +/// These events are either caught for tests or forwarded to downstream peers (see the TcpForwardEventListener implementation). |
| 31 | +#[async_trait] |
| 32 | +pub trait ForwardEventListener { |
| 33 | + async fn send(&self, event: ForwardEvent) -> anyhow::Result<()>; |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 37 | +pub enum ForwardEvent { |
| 38 | + Forward(Header), |
| 39 | + Backward(HeaderTip), |
| 40 | +} |
| 41 | + |
| 42 | +impl ForwardEvent { |
| 43 | + pub fn point(&self) -> Point { |
| 44 | + match self { |
| 45 | + ForwardEvent::Forward(header) => header.point(), |
| 46 | + ForwardEvent::Backward(tip) => tip.point(), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl Display for ForwardEvent { |
| 52 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 53 | + match self { |
| 54 | + ForwardEvent::Forward(header) => write!(f, "Forward({})", header.point()), |
| 55 | + ForwardEvent::Backward(tip) => write!(f, "Backward({})", tip), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] |
| 61 | +pub struct ForwardEventEffect { |
| 62 | + peer: Peer, |
| 63 | + event: ForwardEvent, |
| 64 | +} |
| 65 | + |
| 66 | +impl ForwardEventEffect { |
| 67 | + pub fn new(peer: &Peer, event: ForwardEvent) -> Self { |
| 68 | + Self { |
| 69 | + peer: peer.clone(), |
| 70 | + event, |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl ExternalEffect for ForwardEventEffect { |
| 76 | + #[expect(clippy::expect_used)] |
| 77 | + fn run( |
| 78 | + self: Box<Self>, |
| 79 | + resources: Resources, |
| 80 | + ) -> pure_stage::BoxFuture<'static, Box<dyn pure_stage::SendData>> { |
| 81 | + Box::pin(async move { |
| 82 | + let listener = resources |
| 83 | + .get::<ResourceForwardEventListener>() |
| 84 | + .expect("ForwardEventEffect requires a ForwardEventListener") |
| 85 | + .clone(); |
| 86 | + |
| 87 | + let point = self.event.point(); |
| 88 | + let result: <Self as ExternalEffectAPI>::Response = |
| 89 | + listener.send(self.event).await.map_err(|e| { |
| 90 | + ProcessingFailed::new( |
| 91 | + &self.peer, |
| 92 | + anyhow!("Cannot send the forward event {}: {e}", &point), |
| 93 | + ) |
| 94 | + }); |
| 95 | + Box::new(result) as Box<dyn pure_stage::SendData> |
| 96 | + }) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl ExternalEffectAPI for ForwardEventEffect { |
| 101 | + type Response = Result<(), ProcessingFailed>; |
| 102 | +} |
0 commit comments