Skip to main content

hotshot_types/traits/
leaf_fetcher_network.rs

1// Copyright (c) 2021-2024 Espresso Systems (espressosys.com)
2// This file is part of the HotShot repository.
3
4// You should have received a copy of the MIT License
5// along with the HotShot repository. If not, see <https://mit-license.org/>.
6
7//! Network surface used by the test `Leaf2Fetcher` to send catchup
8//! request/response messages.  Decoupled from `ConnectedNetwork` so the
9//! new-protocol can route through its `Coordinator` instead of holding a
10//! second handle to the network.
11
12use std::{marker::PhantomData, sync::Arc};
13
14use async_trait::async_trait;
15
16use crate::{
17    data::ViewNumber,
18    traits::{network::ConnectedNetwork, node_implementation::NodeType},
19};
20
21/// Operations the test `Leaf2Fetcher` needs to drive epoch catchup.
22///
23/// Both methods send a serialized payload to a single recipient.  The
24/// request/response split is for routing flexibility — implementations may
25/// dispatch them differently (e.g. through different queues), even though
26/// both legs go over a direct message in the default `ConnectedNetwork`
27/// adapter.
28#[async_trait]
29pub trait LeafFetcherNetwork<TYPES: NodeType>: Send + Sync + 'static {
30    async fn send_leaf_request(
31        &self,
32        view: ViewNumber,
33        payload: Vec<u8>,
34        recipient: TYPES::SignatureKey,
35    ) -> anyhow::Result<()>;
36
37    async fn send_leaf_response(
38        &self,
39        view: ViewNumber,
40        payload: Vec<u8>,
41        recipient: TYPES::SignatureKey,
42    ) -> anyhow::Result<()>;
43}
44
45/// Adapter that satisfies `LeafFetcherNetwork` by sending direct messages
46/// over a `ConnectedNetwork` handle.  Used by the old-protocol test infra
47/// where the membership owns a clone of the network.
48pub struct ConnectedNetworkLeafFetcher<TYPES: NodeType, N> {
49    network: Arc<N>,
50    _marker: PhantomData<fn() -> TYPES>,
51}
52
53impl<TYPES: NodeType, N> ConnectedNetworkLeafFetcher<TYPES, N> {
54    pub fn new(network: Arc<N>) -> Self {
55        Self {
56            network,
57            _marker: PhantomData,
58        }
59    }
60}
61
62#[async_trait]
63impl<TYPES, N> LeafFetcherNetwork<TYPES> for ConnectedNetworkLeafFetcher<TYPES, N>
64where
65    TYPES: NodeType,
66    N: ConnectedNetwork<TYPES::SignatureKey>,
67{
68    async fn send_leaf_request(
69        &self,
70        view: ViewNumber,
71        payload: Vec<u8>,
72        recipient: TYPES::SignatureKey,
73    ) -> anyhow::Result<()> {
74        self.network
75            .direct_message(view, payload, recipient)
76            .await
77            .map_err(Into::into)
78    }
79
80    async fn send_leaf_response(
81        &self,
82        view: ViewNumber,
83        payload: Vec<u8>,
84        recipient: TYPES::SignatureKey,
85    ) -> anyhow::Result<()> {
86        self.network
87            .direct_message(view, payload, recipient)
88            .await
89            .map_err(Into::into)
90    }
91}