Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add REST endpoint to get all items in a mapping #3365

Open
wants to merge 4 commits into
base: mainnet-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node/rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {

// All the endpoints before the call to `route_layer` are protected with JWT auth.
.route(&format!("/{network}/node/address"), get(Self::get_node_address))
.route(&format!("/{network}/program/:id/mapping/:name"), get(Self::get_mapping_values))
.route_layer(middleware::from_fn(auth_middleware))

// ----------------- DEPRECATED ROUTES -----------------
Expand Down
46 changes: 41 additions & 5 deletions node/rest/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ pub(crate) struct BlockRange {
end: u32,
}

/// The `get_mapping_value` query object.
#[derive(Deserialize, Serialize)]
/// The query object for `get_mapping_value` and `get_mapping_values`.
#[derive(Copy, Clone, Deserialize, Serialize)]
pub(crate) struct Metadata {
metadata: bool,
metadata: Option<bool>,
all: Option<bool>,
}

impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
Expand Down Expand Up @@ -230,13 +231,13 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
pub(crate) async fn get_mapping_value(
State(rest): State<Self>,
Path((id, name, key)): Path<(ProgramID<N>, Identifier<N>, Plaintext<N>)>,
metadata: Option<Query<Metadata>>,
metadata: Query<Metadata>,
) -> Result<ErasedJson, RestError> {
// Retrieve the mapping value.
let mapping_value = rest.ledger.vm().finalize_store().get_value_confirmed(id, name, &key)?;

// Check if metadata is requested and return the value with metadata if so.
if metadata.map(|q| q.metadata).unwrap_or(false) {
if metadata.metadata.unwrap_or(false) {
return Ok(ErasedJson::pretty(json!({
"data": mapping_value,
"height": rest.ledger.latest_height(),
Expand All @@ -247,6 +248,41 @@ impl<N: Network, C: ConsensusStorage<N>, R: Routing<N>> Rest<N, C, R> {
Ok(ErasedJson::pretty(mapping_value))
}

// GET /<network>/program/{programID}/mapping/{mappingName}?all={true}&metadata={true}
pub(crate) async fn get_mapping_values(
State(rest): State<Self>,
Path((id, name)): Path<(ProgramID<N>, Identifier<N>)>,
metadata: Query<Metadata>,
) -> Result<ErasedJson, RestError> {
// Return an error if the `all` query parameter is not set to `true`.
if metadata.all != Some(true) {
return Err(RestError("Invalid query parameter. At this time, 'all=true' must be included".to_string()));
}

// Retrieve the latest height.
let height = rest.ledger.latest_height();

// Retrieve all the mapping values from the mapping.
match tokio::task::spawn_blocking(move || rest.ledger.vm().finalize_store().get_mapping_confirmed(id, name))
.await
{
Ok(Ok(mapping_values)) => {
// Check if metadata is requested and return the mapping with metadata if so.
if metadata.metadata.unwrap_or(false) {
return Ok(ErasedJson::pretty(json!({
"data": mapping_values,
"height": height,
})));
}

// Return the full mapping without metadata.
Ok(ErasedJson::pretty(mapping_values))
}
Ok(Err(err)) => Err(RestError(format!("Unable to read mapping - {err}"))),
Err(err) => Err(RestError(format!("Unable to read mapping - {err}"))),
}
}

// GET /<network>/statePath/{commitment}
pub(crate) async fn get_state_path_for_commitment(
State(rest): State<Self>,
Expand Down