-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Add epoch activity REST endpoints with optional historical storage #89
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
Merged
sandtreader
merged 4 commits into
input-output-hk:main
from
SundaeSwap-finance:whankinsiv/epoch-activity-rest
Jul 21, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f68273b
feat: Add epoch activity REST endpoints with optional historical storage
whankinsiv dce1f50
fix: use structured objects for vrf_key_hashes in epoch responses
whankinsiv 5d8aeae
fix: Use OpenAPI componenets and use get_current_epoch to set EpochAc…
whankinsiv 4c91755
fix: set end epoch test to start at epoch 0
whankinsiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use crate::state::State; | ||
use acropolis_common::messages::RESTResponse; | ||
use anyhow::Result; | ||
use serde::Serialize; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Serialize)] | ||
pub struct EpochActivityRest { | ||
pub epoch: u64, | ||
pub total_blocks: usize, | ||
pub total_fees: u64, | ||
pub vrf_vkey_hashes: Vec<VRFKeyCount>, | ||
} | ||
#[derive(Serialize)] | ||
pub struct VRFKeyCount { | ||
pub vrf_key_hash: String, | ||
pub block_count: usize, | ||
} | ||
|
||
/// Handles /epoch | ||
pub async fn handle_epoch(state: Arc<Mutex<State>>) -> Result<RESTResponse> { | ||
let locked = state.lock().await; | ||
let epoch_data = locked.get_current_epoch(); | ||
|
||
let response = EpochActivityRest { | ||
epoch: epoch_data.epoch, | ||
total_blocks: epoch_data.total_blocks, | ||
total_fees: epoch_data.total_fees, | ||
vrf_vkey_hashes: epoch_data | ||
.vrf_vkey_hashes | ||
.iter() | ||
.map(|(key, count)| VRFKeyCount { | ||
vrf_key_hash: hex::encode(key), | ||
block_count: *count, | ||
}) | ||
.collect(), | ||
}; | ||
|
||
let json = match serde_json::to_string(&response) { | ||
Ok(j) => j, | ||
Err(e) => { | ||
return Ok(RESTResponse::with_text( | ||
500, | ||
&format!("Internal server error while retrieving current epoch: {e}"), | ||
)); | ||
} | ||
}; | ||
Ok(RESTResponse::with_json(200, &json)) | ||
} | ||
|
||
/// Handles /epochs/{epoch} | ||
pub async fn handle_historical_epoch( | ||
state: Arc<Mutex<State>>, | ||
epoch: String, | ||
) -> Result<RESTResponse> { | ||
let parsed_epoch = match epoch.parse::<u64>() { | ||
Ok(v) => v, | ||
Err(_) => { | ||
return Ok(RESTResponse::with_text( | ||
400, | ||
&format!("Invalid epoch number: {epoch}"), | ||
)) | ||
} | ||
}; | ||
|
||
let locked = state.lock().await; | ||
match locked.get_historical_epoch(parsed_epoch) { | ||
Err(_) => Ok(RESTResponse::with_text( | ||
501, | ||
"Historical epoch storage not enabled", | ||
)), | ||
Ok(Some(epoch_data)) => { | ||
let response = EpochActivityRest { | ||
epoch: epoch_data.epoch, | ||
total_blocks: epoch_data.total_blocks, | ||
total_fees: epoch_data.total_fees, | ||
vrf_vkey_hashes: epoch_data | ||
.vrf_vkey_hashes | ||
.iter() | ||
.map(|(key, count)| VRFKeyCount { | ||
vrf_key_hash: hex::encode(key), | ||
block_count: *count, | ||
}) | ||
.collect(), | ||
}; | ||
let json = match serde_json::to_string(&response) { | ||
Ok(j) => j, | ||
Err(e) => { | ||
return Ok(RESTResponse::with_text( | ||
500, | ||
&format!( | ||
"Internal server error while retrieving epoch {parsed_epoch}: {e}" | ||
), | ||
)); | ||
} | ||
}; | ||
Ok(RESTResponse::with_json(200, &json)) | ||
} | ||
Ok(None) => Ok(RESTResponse::with_text( | ||
404, | ||
&format!("Epoch {parsed_epoch} not found"), | ||
)), | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like this could be the more natural thing to store (without the Rest suffix), and derive the EpochActivityMessage from that? Would save (duplicated) conversions below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was dumb - VRF keys are encoded strings in the REST version. Please ignore!