Skip to main content

ab_farmer/farmer_piece_getter/
piece_validator.rs

1//! Farmer-specific validator for pieces retrieved from the network
2
3use crate::node_client::NodeClient;
4use ab_core_primitives::pieces::{Piece, PieceIndex};
5use ab_networking::Node;
6use ab_networking::libp2p::PeerId;
7use ab_networking::utils::piece_provider::PieceValidator;
8use async_trait::async_trait;
9use tracing::{error, warn};
10
11/// Farmer-specific validator for pieces retrieved from the network.
12///
13/// Implements [`PieceValidator`].
14#[derive(Debug, Clone)]
15pub struct SegmentRootPieceValidator<NC> {
16    dsn_node: Node,
17    node_client: NC,
18}
19
20impl<NC> SegmentRootPieceValidator<NC> {
21    /// Create a new instance
22    pub fn new(dsn_node: Node, node_client: NC) -> Self {
23        Self {
24            dsn_node,
25            node_client,
26        }
27    }
28}
29
30#[async_trait]
31impl<NC> PieceValidator for SegmentRootPieceValidator<NC>
32where
33    NC: NodeClient,
34{
35    async fn validate_piece(
36        &self,
37        source_peer_id: PeerId,
38        piece_index: PieceIndex,
39        piece: Piece,
40    ) -> Option<Piece> {
41        if source_peer_id == self.dsn_node.id() {
42            return Some(piece);
43        }
44
45        let segment_index = piece_index.segment_index();
46
47        let maybe_super_segment_header = match self
48            .node_client
49            .super_segment_header_for_segment_index(segment_index)
50            .await
51        {
52            Ok(maybe_super_segment_header) => maybe_super_segment_header,
53            Err(error) => {
54                error!(
55                    %piece_index,
56                    %segment_index,
57                    ?error,
58                    "Failed to retrieve super segment header from node"
59                );
60                return None;
61            }
62        };
63
64        let Some(super_segment_header) = maybe_super_segment_header else {
65            error!(
66                %piece_index,
67                %segment_index,
68                "Super segment header for segment index wasn't found on node"
69            );
70            return None;
71        };
72
73        let is_valid_fut = tokio::task::spawn_blocking(move || {
74            piece
75                .is_valid(
76                    &super_segment_header.root,
77                    super_segment_header.num_segments,
78                    super_segment_header.max_segment_index.as_inner(),
79                    piece_index,
80                )
81                .then_some(piece)
82        });
83
84        if let Some(piece) = is_valid_fut.await.unwrap_or_default() {
85            Some(piece)
86        } else {
87            warn!(
88                %piece_index,
89                %source_peer_id,
90                "Received invalid piece from peer"
91            );
92
93            // We don't care about result here
94            let _: Result<(), _> = self.dsn_node.ban_peer(source_peer_id).await;
95            None
96        }
97    }
98}