miden_node_utils/
limiter.rs

1//! Limit the size of a parameter list for a specific parameter
2//!
3//! Used for:
4//! 1. the external facing RPC
5//! 2. limiting SQL statements not exceeding parameter limits
6//!
7//! The 1st is good to terminate invalid requests as early as possible,
8//! where the second is both a fallback and a safeguard not benching
9//! pointless parameter combinations.
10
11#[allow(missing_docs)]
12#[derive(Debug, thiserror::Error)]
13#[error("parameter {which} exceeded limit {limit}: {size}")]
14pub struct QueryLimitError {
15    which: &'static str,
16    size: usize,
17    limit: usize,
18}
19
20/// Checks limits against the desired query parameters, per query parameter and
21/// bails if they exceed a defined value.
22pub trait QueryParamLimiter {
23    /// Name of the parameter to mention in the error.
24    const PARAM_NAME: &'static str;
25    /// Limit that causes a bail if exceeded.
26    const LIMIT: usize;
27    /// Do the actual check.
28    fn check(size: usize) -> Result<(), QueryLimitError> {
29        if size > Self::LIMIT {
30            Err(QueryLimitError {
31                which: Self::PARAM_NAME,
32                size,
33                limit: Self::LIMIT,
34            })?;
35        }
36        Ok(())
37    }
38}
39
40/// Used for the following RPC endpoints
41/// * `state_sync`
42pub struct QueryParamAccountIdLimit;
43impl QueryParamLimiter for QueryParamAccountIdLimit {
44    const PARAM_NAME: &str = "account_id";
45    const LIMIT: usize = 1000;
46}
47
48/// Used for the following RPC endpoints
49/// * `select_nullifiers_by_prefix`
50pub struct QueryParamNullifierPrefixLimit;
51impl QueryParamLimiter for QueryParamNullifierPrefixLimit {
52    const PARAM_NAME: &str = "nullifier_prefix";
53    const LIMIT: usize = 1000;
54}
55
56/// Used for the following RPC endpoints
57/// * `select_nullifiers_by_prefix`
58/// * `sync_nullifiers`
59/// * `sync_state`
60pub struct QueryParamNullifierLimit;
61impl QueryParamLimiter for QueryParamNullifierLimit {
62    const PARAM_NAME: &str = "nullifier";
63    const LIMIT: usize = 1000;
64}
65
66/// Used for the following RPC endpoints
67/// * `get_note_sync`
68pub struct QueryParamNoteTagLimit;
69impl QueryParamLimiter for QueryParamNoteTagLimit {
70    const PARAM_NAME: &str = "note_tag";
71    const LIMIT: usize = 1000;
72}
73
74/// Used for the following RPC endpoints
75/// `select_notes_by_id`
76pub struct QueryParamNoteIdLimit;
77impl QueryParamLimiter for QueryParamNoteIdLimit {
78    const PARAM_NAME: &str = "note_id";
79    const LIMIT: usize = 1000;
80}
81
82/// Used for internal queries retrieving note inclusion proofs by commitment.
83pub struct QueryParamNoteCommitmentLimit;
84impl QueryParamLimiter for QueryParamNoteCommitmentLimit {
85    const PARAM_NAME: &str = "note_commitment";
86    const LIMIT: usize = 1000;
87}
88
89/// Only used internally, not exposed via public RPC.
90pub struct QueryParamBlockLimit;
91impl QueryParamLimiter for QueryParamBlockLimit {
92    const PARAM_NAME: &str = "block_header";
93    const LIMIT: usize = 1000;
94}