drive/query/
proposer_block_count_query.rs

1use crate::drive::credit_pools::epochs::paths::EpochProposers;
2use crate::query::Query;
3use dpp::block::epoch::Epoch;
4use grovedb::{PathQuery, SizedQuery};
5
6/// Represents an optional limit for the number of proposers to retrieve in a query.
7///
8/// - `Some(u16)`: Limits the number of proposers returned.
9/// - `None`: No limit on the number of proposers.
10pub type ProposerQueryLimit = Option<u16>;
11/// Indicates whether the query should include the starting proposer in the results.
12///
13/// - `true`: The starting proposer is included in the results.
14/// - `false`: The starting proposer is excluded.
15pub type ProposerQueryStartAtIncluded = bool;
16/// Represents an optional starting point for a proposer query, consisting of:
17///
18/// - A tuple of a 32-byte array representing the start proposer's identifier and
19///   a boolean indicating whether to include the starting proposer.
20///
21/// - `Some(([u8; 32], bool))`: A specific proposer to start from, with an inclusion flag.
22/// - `None`: The query will start from the beginning or a default point.
23pub type ProposerQueryStartAt = Option<([u8; 32], ProposerQueryStartAtIncluded)>;
24
25/// Specifies the type of query to retrieve proposers, with two options:
26///
27/// - `ByRange(ProposerQueryLimit, ProposerQueryStartAt)`: Query proposers within a range,
28///   with an optional limit and an optional starting point.
29///
30/// - `ByIds(Vec<Vec<u8>>)`: Query specific proposers by their identifiers.
31pub enum ProposerQueryType {
32    /// Queries proposers within a specified range.
33    ///
34    /// - `ProposerQueryLimit`: Limits the number of proposers returned. If `None`, there is no limit.
35    /// - `ProposerQueryStartAt`: Specifies the proposer to start from. If `None`, the query starts at the beginning.
36    ByRange(ProposerQueryLimit, ProposerQueryStartAt),
37
38    /// Queries specific proposers by their identifiers.
39    ///
40    /// - `Vec<Vec<u8>>`: A vector of proposer IDs (byte arrays) to retrieve.
41    ByIds(Vec<Vec<u8>>),
42}
43
44impl ProposerQueryType {
45    /// Should we get optional elements?
46    pub fn allows_optional(&self) -> bool {
47        match self {
48            ProposerQueryType::ByRange(_, _) => false,
49            ProposerQueryType::ByIds(_) => true,
50        }
51    }
52
53    /// Gets the path query for the proposer query type
54    pub fn into_path_query(self, epoch: &Epoch) -> PathQuery {
55        let path_as_vec = epoch.get_proposers_path_vec();
56
57        let mut query = Query::new();
58
59        match self {
60            ProposerQueryType::ByRange(limit, start_at) => {
61                match start_at {
62                    None => {
63                        query.insert_all();
64                    }
65                    Some((identity_id, included)) => {
66                        if included {
67                            query.insert_range_from(identity_id.to_vec()..);
68                        } else {
69                            query.insert_range_after(identity_id.to_vec()..);
70                        }
71                    }
72                }
73                PathQuery::new(path_as_vec, SizedQuery::new(query, limit, None))
74            }
75            ProposerQueryType::ByIds(ids) => {
76                let len = ids.len();
77                query.insert_keys(ids);
78                PathQuery::new(path_as_vec, SizedQuery::new(query, Some(len as u16), None))
79            }
80        }
81    }
82}