Skip to main content

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    Payload,
25    availability::{BlockQueryData, Certificate2, LeafQueryData, VidCommonQueryData},
26    fetching::NonEmptyRange,
27};
28
29/// A request for a resource.
30pub trait Request<Types>: Copy + Debug + Eq + Hash + Send {
31    /// The type of resource that will be returned as a successful response to this request.
32    type Response: Clone + Send;
33}
34
35/// A request for a payload with a given commitment.
36#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
37pub struct PayloadRequest(pub VidCommitment);
38
39impl<Types: NodeType> Request<Types> for PayloadRequest {
40    type Response = Payload<Types>;
41}
42
43/// A request for a consecutive range of blocks.
44#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
45pub struct BlockRangeRequest {
46    pub start: u64,
47    pub end: u64,
48}
49
50impl<Types: NodeType> Request<Types> for BlockRangeRequest {
51    type Response = NonEmptyRange<BlockQueryData<Types>>;
52}
53
54/// A request for VID common data.
55#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
56pub struct VidCommonRequest(pub VidCommitment);
57
58impl<Types: NodeType> Request<Types> for VidCommonRequest {
59    type Response = VidCommon;
60}
61
62/// A request for a consecutive range of VID common.
63#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
64pub struct VidCommonRangeRequest {
65    pub start: u64,
66    pub end: u64,
67}
68
69impl<Types: NodeType> Request<Types> for VidCommonRangeRequest {
70    type Response = NonEmptyRange<VidCommonQueryData<Types>>;
71}
72
73/// A request for a leaf with a given height.
74#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, From, Into)]
75pub struct LeafRequest {
76    pub height: u64,
77}
78
79impl LeafRequest {
80    pub fn new(height: u64) -> Self {
81        Self { height }
82    }
83}
84
85impl<Types: NodeType> Request<Types> for LeafRequest {
86    type Response = LeafQueryData<Types>;
87}
88
89/// A request for a consecutive range of leaves.
90#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
91pub struct LeafRangeRequest {
92    /// The first block in the requested range.
93    pub start: u64,
94
95    /// The first block after the requested range.
96    pub end: u64,
97}
98
99impl<Types: NodeType> Request<Types> for LeafRangeRequest {
100    type Response = NonEmptyRange<LeafQueryData<Types>>;
101}
102
103/// A request for a cert2 at a given height.
104///
105/// The response is `Option<Certificate2>` since not every height has a cert2.
106#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
107pub struct Certificate2Request {
108    pub height: u64,
109}
110
111impl<Types: NodeType> Request<Types> for Certificate2Request {
112    type Response = Option<Certificate2<Types>>;
113}