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::{data::VidCommitment, traits::node_implementation::NodeType};
19
20use crate::{
21    availability::{LeafHash, LeafQueryData, QcHash, StateCertQueryData},
22    Payload, VidCommon,
23};
24
25/// A request for a resource.
26pub trait Request<Types>: Copy + Debug + Eq + Hash + Send {
27    /// The type of resource that will be returned as a successful response to this request.
28    type Response: Clone + Send;
29}
30
31/// A request for a payload with a given commitment.
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
33pub struct PayloadRequest(pub VidCommitment);
34
35impl<Types: NodeType> Request<Types> for PayloadRequest {
36    type Response = Payload<Types>;
37}
38
39/// A request for VID common data.
40#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
41pub struct VidCommonRequest(pub VidCommitment);
42
43impl<Types: NodeType> Request<Types> for VidCommonRequest {
44    type Response = VidCommon;
45}
46
47/// A request for a leaf with a given height.
48///
49/// The expected hash and QC hash are also provided, so that the request can be verified against a
50/// response from an untrusted provider.
51#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
52pub struct LeafRequest<Types: NodeType> {
53    pub height: u64,
54    pub expected_leaf: LeafHash<Types>,
55    pub expected_qc: QcHash<Types>,
56}
57
58impl<Types: NodeType> LeafRequest<Types> {
59    pub fn new(height: u64, expected_leaf: LeafHash<Types>, expected_qc: QcHash<Types>) -> Self {
60        Self {
61            height,
62            expected_leaf,
63            expected_qc,
64        }
65    }
66}
67
68impl<Types: NodeType> Request<Types> for LeafRequest<Types> {
69    type Response = LeafQueryData<Types>;
70}
71
72/// A request for a light client state update certificate with a given epoch.
73#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
74pub struct StateCertRequest(pub u64);
75
76impl<Types: NodeType> Request<Types> for StateCertRequest {
77    type Response = StateCertQueryData<Types>;
78}