Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Eqs remove unbounded queries (#1191)
Browse files Browse the repository at this point in the history
* Problem: The size of the events query is unbounded

Return at most 100 events.

* Problem: unbounded get_all_nullifiers endpoint

The endpoint is unused, remove it.
  • Loading branch information
sveitser authored Jul 12, 2022
1 parent 9760b45 commit e6b00dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
9 changes: 4 additions & 5 deletions eqs/api/api.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ HEADING_DESCRIPTION = "Description"
PATH = [ "get_cap_state" ]
DOC = "Returns the current CAP blockchain state."

[route.get_all_nullifiers]
PATH = [ "get_all_nullifiers" ]
DOC = "Returns the current set of nullifiers."

[route.check_nullifier]
PATH = [ "check_nullifier/:nullifier" ]
":nullifier" = "TaggedBase64"
Expand All @@ -49,7 +45,10 @@ ERROR_nullifier = "A valid nullifier is required. Nullifiers begin with NUL~."
PATH = [ "get_events_since/:first", "get_events_since/:first/:max_count" ]
":first" = "Integer"
":max_count" = "Integer"
DOC = "Returns the array of [up to max_count] events since the specified index (inclusive)"
DOC = """Returns the array of [up to max_count] events since the specified index (inclusive)
If `:max_count` is larger than 100 or omitted at most the first 100 events are returned.
"""
ERROR_first = "The index must be a non-negative integer."
ERROR_max_count = "The max_count must be a non-negative, non-zero integer."

Expand Down
24 changes: 10 additions & 14 deletions eqs/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@ use jf_cap::structs::{AssetCode, Nullifier};
use net::server::response;
use seahorse::events::LedgerEvent;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::str::FromStr;
use strum::IntoEnumIterator;
use strum_macros::{AsRefStr, EnumIter, EnumString};

/// Maximum number of events to return in a single response.
const EQS_MAX_EVENT_COUNT: usize = 100;

/// Index entries for documentation fragments
#[allow(non_camel_case_types)]
#[derive(AsRefStr, Copy, Clone, Debug, EnumIter, EnumString)]
pub enum ApiRouteKey {
get_cap_state,
get_all_nullifiers,
check_nullifier,
get_events_since,
get_transaction,
Expand Down Expand Up @@ -104,13 +106,6 @@ pub async fn get_cap_state(query_result_state: &QueryResultState) -> Result<CapS
})
}

/// Obtain all the nullifiers that have been published in the CAPE contract.
pub async fn get_all_nullifiers(
query_result_state: &QueryResultState,
) -> Result<HashSet<Nullifier>, tide::Error> {
Ok(query_result_state.nullifiers.clone())
}

/// Check if a nullifier has already been published.
pub async fn check_nullifier(
bindings: &HashMap<String, RouteBinding>,
Expand All @@ -135,11 +130,13 @@ pub async fn get_events_since(
if first >= events_len {
return Ok(Vec::new());
}
let last = if let Some(max_count) = bindings.get(":max_count") {
std::cmp::min(first + max_count.value.as_u64()? as usize, events_len)
} else {
events_len
let max_count = match bindings.get(":max_count") {
Some(user_max_count) => {
std::cmp::min(user_max_count.value.as_u64()? as usize, EQS_MAX_EVENT_COUNT)
}
None => EQS_MAX_EVENT_COUNT,
};
let last = std::cmp::min(first + max_count, events_len);
Ok(query_result_state.events[first..last].to_vec())
}

Expand Down Expand Up @@ -229,7 +226,6 @@ pub async fn dispatch_url(
let query_state = &*query_state_guard;
match key {
ApiRouteKey::get_cap_state => response(&req, get_cap_state(query_state).await?),
ApiRouteKey::get_all_nullifiers => response(&req, get_all_nullifiers(query_state).await?),
ApiRouteKey::check_nullifier => {
response(&req, check_nullifier(bindings, query_state).await?)
}
Expand Down

0 comments on commit e6b00dd

Please sign in to comment.