|
| 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::effects::network_effects::{ForwardEvent, ForwardEventEffect}; |
| 16 | +use crate::consensus::errors::{ProcessingFailed, ValidationFailed}; |
| 17 | +use crate::consensus::events::BlockValidationResult; |
| 18 | +use crate::consensus::span::adopt_current_span; |
| 19 | +use crate::consensus::tip::{AsHeaderTip, HeaderTip}; |
| 20 | +use amaru_kernel::Point; |
| 21 | +use amaru_ouroboros_traits::IsHeader; |
| 22 | +use anyhow::anyhow; |
| 23 | +use pure_stage::{Effects, StageRef}; |
| 24 | +use tracing::{Level, error, info, instrument, trace}; |
| 25 | + |
| 26 | +pub const EVENT_TARGET: &str = "amaru::consensus::forward_chain"; |
| 27 | + |
| 28 | +type State = ( |
| 29 | + HeaderTip, |
| 30 | + StageRef<ValidationFailed>, |
| 31 | + StageRef<ProcessingFailed>, |
| 32 | +); |
| 33 | + |
| 34 | +/// The forward chain stage forwards the headers of validated blocks to downstream peers, via the |
| 35 | +/// `ForwardEventEffect`. The current node tip is maintained in order to double check that the header |
| 36 | +/// we sent out is correct |
| 37 | +#[instrument( |
| 38 | + level = Level::TRACE, |
| 39 | + skip_all, |
| 40 | + name = "stage.forward_chain", |
| 41 | +)] |
| 42 | +pub async fn stage( |
| 43 | + state: State, |
| 44 | + msg: BlockValidationResult, |
| 45 | + eff: Effects<BlockValidationResult>, |
| 46 | +) -> State { |
| 47 | + adopt_current_span(&msg); |
| 48 | + let (mut our_tip, validation_errors, processing_errors) = state; |
| 49 | + match msg { |
| 50 | + BlockValidationResult::BlockValidated { peer, header, .. } => { |
| 51 | + // assert that the new tip is a direct successor of the old tip |
| 52 | + assert_eq!(header.block_height(), our_tip.block_height() + 1); |
| 53 | + match header.parent() { |
| 54 | + Some(parent) => assert_eq!(parent, our_tip.hash()), |
| 55 | + None => assert_eq!(our_tip, HeaderTip::new(Point::Origin, 0)), |
| 56 | + } |
| 57 | + our_tip = header.as_header_tip(); |
| 58 | + trace!( |
| 59 | + target: EVENT_TARGET, |
| 60 | + tip = %header.point(), |
| 61 | + "tip_changed" |
| 62 | + ); |
| 63 | + |
| 64 | + if let Err(e) = eff |
| 65 | + .external(ForwardEventEffect::new( |
| 66 | + &peer, |
| 67 | + ForwardEvent::Forward(header.clone()), |
| 68 | + )) |
| 69 | + .await |
| 70 | + { |
| 71 | + error!( |
| 72 | + target: EVENT_TARGET, |
| 73 | + %e, |
| 74 | + "failed to send forward event" |
| 75 | + ); |
| 76 | + eff.send(&processing_errors, ProcessingFailed::new(&peer, anyhow!(e))) |
| 77 | + .await |
| 78 | + } |
| 79 | + } |
| 80 | + BlockValidationResult::RolledBackTo { |
| 81 | + peer, |
| 82 | + rollback_header, |
| 83 | + .. |
| 84 | + } => { |
| 85 | + info!( |
| 86 | + target: EVENT_TARGET, |
| 87 | + point = %rollback_header.point(), |
| 88 | + "rolled_back_to" |
| 89 | + ); |
| 90 | + |
| 91 | + our_tip = rollback_header.as_header_tip(); |
| 92 | + if let Err(e) = eff |
| 93 | + .external(ForwardEventEffect::new( |
| 94 | + &peer, |
| 95 | + ForwardEvent::Backward(rollback_header.as_header_tip()), |
| 96 | + )) |
| 97 | + .await |
| 98 | + { |
| 99 | + error!( |
| 100 | + target: EVENT_TARGET, |
| 101 | + %e, |
| 102 | + "failed to send backward event" |
| 103 | + ); |
| 104 | + eff.send(&processing_errors, ProcessingFailed::new(&peer, anyhow!(e))) |
| 105 | + .await |
| 106 | + } |
| 107 | + } |
| 108 | + BlockValidationResult::BlockValidationFailed { point, .. } => { |
| 109 | + error!( |
| 110 | + target: EVENT_TARGET, |
| 111 | + slot = %point.slot_or_default(), |
| 112 | + hash = %point.hash(), |
| 113 | + "block validation failed" |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + (our_tip, validation_errors, processing_errors) |
| 118 | +} |
0 commit comments