hotshot_query_service/fetching/
request.rs

1// Copyright (c) 2022 Espresso Systems (espressosys.com)
2// This file is part of the HotShot Query Service library.
3//
4// This program is free software: you can redistribute it and/or modify it under the terms of the GNU
5// General Public License as published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8// even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9// General Public License for more details.
10// You should have received a copy of the GNU General Public License along with this program. If not,
11// see <https://www.gnu.org/licenses/>.
12
13//! Requests for fetching resources.
14
15use std::{fmt::Debug, hash::Hash};
16
17use derive_more::{From, Into};
18use hotshot_types::{
19    data::{VidCommitment, VidCommon},
20    traits::node_implementation::NodeType,
21};
22
23use crate::{
24    availability::{LeafHash, LeafQueryData, QcHash},
25    Payload,
26};
27
28/// A request for a resource.
29pub trait Request<Types>: Copy + Debug + Eq + Hash + Send {
30    /// The type of resource that will be returned as a successful response to this request.
31    type Response: Clone + Send;
32}
33
34/// A request for a payload with a given commitment.
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
36pub struct PayloadRequest(pub VidCommitment);
37
38impl<Types: NodeType> Request<Types> for PayloadRequest {
39    type Response = Payload<Types>;
40}
41
42/// A request for VID common data.
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44pub struct VidCommonRequest(pub VidCommitment);
45
46impl<Types: NodeType> Request<Types> for VidCommonRequest {
47    type Response = VidCommon;
48}
49
50/// A request for a leaf with a given height.
51///
52/// The expected hash and QC hash are also provided, so that the request can be verified against a
53/// response from an untrusted provider.
54#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
55pub struct LeafRequest<Types: NodeType> {
56    pub height: u64,
57    pub expected_leaf: LeafHash<Types>,
58    pub expected_qc: QcHash<Types>,
59}
60
61impl<Types: NodeType> LeafRequest<Types> {
62    pub fn new(height: u64, expected_leaf: LeafHash<Types>, expected_qc: QcHash<Types>) -> Self {
63        Self {
64            height,
65            expected_leaf,
66            expected_qc,
67        }
68    }
69}
70
71impl<Types: NodeType> Request<Types> for LeafRequest<Types> {
72    type Response = LeafQueryData<Types>;
73}