Skip to content

Commit

Permalink
test: add support for `/artifact/cardano-stake-distribution/epoch/:ep…
Browse files Browse the repository at this point in the history
…och` in the fake aggregator
  • Loading branch information
dlachaume committed Aug 12, 2024
1 parent 26e8e43 commit e9c84f4
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions mithril-test-lab/mithril-aggregator-fake/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub async fn aggregator_router() -> Router<SharedState> {
.route("/artifact/cardano-transaction/:hash", get(ctx_snapshot))
.route("/artifact/cardano-stake-distributions", get(csds))
.route("/artifact/cardano-stake-distribution/:hash", get(csd))
.route(
"/artifact/cardano-stake-distribution/epoch/:epoch",
get(csd_by_epoch),
)
.route("/proof/cardano-transaction", get(ctx_proof))
.route("/certificates", get(certificates))
.route("/certificate/:hash", get(certificate))
Expand Down Expand Up @@ -188,6 +192,42 @@ pub async fn csd(
})
}

/// HTTP: return a cardano stake distribution identified by its epoch.
pub async fn csd_by_epoch(
Path(epoch): Path<String>,
State(state): State<SharedState>,
) -> Result<Response<Body>, AppError> {
#[derive(Debug, serde::Deserialize)]
struct TmpCardanoStakeDistributionData {
hash: String,
epoch: u64,
}

let app_state = state.read().await;

let csds = app_state.get_csds().await?;
let csds: Vec<TmpCardanoStakeDistributionData> = serde_json::from_str(&csds)?;

// Find the cardano stake distribution hash corresponding to the epoch
let hash = csds
.into_iter()
.find(|csd| csd.epoch.to_string() == epoch)
.map(|csd| csd.hash)
.ok_or_else(|| {
debug!("No cardano stake distribution found for epoch={epoch}.");
AppError::NotFound
})?;

app_state
.get_csd(&hash)
.await?
.map(|s| s.into_response())
.ok_or_else(|| {
debug!("cardano stake distribution hash={hash} NOT FOUND.");
AppError::NotFound
})
}

#[derive(serde::Deserialize, Default)]
pub struct CardanoTransactionProofQueryParams {
transaction_hashes: String,
Expand Down Expand Up @@ -409,4 +449,37 @@ mod tests {

assert!(matches!(error, AppError::NotFound));
}

#[tokio::test]
async fn existing_csd_epoch() {
#[derive(Debug, serde::Deserialize)]
struct TmpCardanoStakeDistributionData {
epoch: u64,
}

let state: State<SharedState> = State(AppState::default().into());
let hash = default_values::csd_hashes()[0].to_string();
let csds = default_values::csds();
let tmp_csd_data: TmpCardanoStakeDistributionData =
serde_json::from_str(csds.get(&hash).unwrap()).unwrap();
let epoch = Path(tmp_csd_data.epoch.to_string());

let response = csd_by_epoch(epoch, state)
.await
.expect("The handler was expected to succeed since the csd's epoch does exist.");

assert_eq!(StatusCode::OK, response.status());
}

#[tokio::test]
async fn invalid_csd_epoch() {
let state: State<SharedState> = State(AppState::default().into());
let epoch = Path(u64::MAX.to_string());

let error = csd_by_epoch(epoch, state)
.await
.expect_err("The handler was expected to fail since the csd's epoch does not exist.");

assert!(matches!(error, AppError::NotFound));
}
}

0 comments on commit e9c84f4

Please sign in to comment.