|
| 1 | +use crate::interfaces::HostResult; |
| 2 | +use axum::{extract::State, routing::get, Json, Router}; |
| 3 | +use raiko_reqactor::Actor; |
| 4 | +use raiko_reqpool::{RequestKey, Status, StatusWithContext}; |
| 5 | +use raiko_tasks::{ |
| 6 | + AggregationTaskDescriptor, BatchGuestInputTaskDescriptor, BatchProofTaskDescriptor, |
| 7 | + GuestInputTaskDescriptor, ProofTaskDescriptor, ShastaGuestInputTaskDescriptor, |
| 8 | + ShastaProofTaskDescriptor, TaskDescriptor, TaskReport, TaskStatus, |
| 9 | +}; |
| 10 | +use serde_json::Value; |
| 11 | +use utoipa::OpenApi; |
| 12 | + |
| 13 | +#[utoipa::path(post, path = "/proof/report", |
| 14 | + tag = "Proving", |
| 15 | + responses ( |
| 16 | + (status = 200, description = "Successfully listed all current tasks") |
| 17 | + ) |
| 18 | +)] |
| 19 | +/// List all tasks. |
| 20 | +/// |
| 21 | +/// Retrieve a list of `{ chain_id, blockhash, prover_type, prover, status }` items. |
| 22 | +async fn report_handler(State(actor): State<Actor>) -> HostResult<Json<Value>> { |
| 23 | + let statuses = actor |
| 24 | + .pool_list_status() |
| 25 | + .await |
| 26 | + .map_err(|e| anyhow::anyhow!(e))?; |
| 27 | + |
| 28 | + // For compatibility with the old API, we need to convert the statuses to the old format. |
| 29 | + let to_task_status = |status: StatusWithContext| match status.into_status() { |
| 30 | + Status::Registered => TaskStatus::Registered, |
| 31 | + Status::WorkInProgress => TaskStatus::WorkInProgress, |
| 32 | + Status::Cancelled => TaskStatus::Cancelled, |
| 33 | + Status::Success { .. } => TaskStatus::Success, |
| 34 | + Status::Failed { error } => TaskStatus::AnyhowError(error), |
| 35 | + }; |
| 36 | + let to_task_descriptor = |request_key: RequestKey| match request_key { |
| 37 | + RequestKey::GuestInput(key) => TaskDescriptor::GuestInput(GuestInputTaskDescriptor { |
| 38 | + chain_id: *key.chain_id(), |
| 39 | + block_id: *key.block_number(), |
| 40 | + blockhash: *key.block_hash(), |
| 41 | + }), |
| 42 | + RequestKey::SingleProof(key) => TaskDescriptor::SingleProof(ProofTaskDescriptor { |
| 43 | + chain_id: *key.chain_id(), |
| 44 | + block_id: *key.block_number(), |
| 45 | + blockhash: *key.block_hash(), |
| 46 | + proof_system: *key.proof_type(), |
| 47 | + prover: key.prover_address().clone(), |
| 48 | + }), |
| 49 | + RequestKey::Aggregation(key) => TaskDescriptor::Aggregation(AggregationTaskDescriptor { |
| 50 | + aggregation_ids: key.block_numbers().clone(), |
| 51 | + proof_type: Some(key.proof_type().to_string()), |
| 52 | + }), |
| 53 | + RequestKey::BatchProof(key) => TaskDescriptor::BatchProof(BatchProofTaskDescriptor { |
| 54 | + chain_id: *key.guest_input_key().chain_id(), |
| 55 | + batch_id: *key.guest_input_key().batch_id(), |
| 56 | + l1_height: *key.guest_input_key().l1_inclusion_height(), |
| 57 | + proof_system: *key.proof_type(), |
| 58 | + prover: key.prover_address().clone(), |
| 59 | + }), |
| 60 | + RequestKey::BatchGuestInput(key) => { |
| 61 | + TaskDescriptor::BatchGuestInput(BatchGuestInputTaskDescriptor { |
| 62 | + chain_id: *key.chain_id(), |
| 63 | + batch_id: *key.batch_id(), |
| 64 | + l1_height: *key.l1_inclusion_height(), |
| 65 | + }) |
| 66 | + } |
| 67 | + RequestKey::ShastaGuestInput(key) => { |
| 68 | + TaskDescriptor::ShastaGuestInput(ShastaGuestInputTaskDescriptor { |
| 69 | + proposal_id: *key.proposal_id(), |
| 70 | + l1_network: key.l1_network().clone(), |
| 71 | + l2_network: key.l2_network().clone(), |
| 72 | + }) |
| 73 | + } |
| 74 | + RequestKey::ShastaProof(key) => TaskDescriptor::ShastaProof(ShastaProofTaskDescriptor { |
| 75 | + proposal_id: *key.guest_input_key().proposal_id(), |
| 76 | + l1_network: key.guest_input_key().l1_network().clone(), |
| 77 | + l2_network: key.guest_input_key().l2_network().clone(), |
| 78 | + proof_system: *key.proof_type(), |
| 79 | + prover: key.actual_prover_address().clone(), |
| 80 | + }), |
| 81 | + RequestKey::ShastaAggregation(key) => { |
| 82 | + TaskDescriptor::Aggregation(AggregationTaskDescriptor { |
| 83 | + aggregation_ids: key.block_numbers().clone(), |
| 84 | + proof_type: Some(key.proof_type().to_string()), |
| 85 | + }) |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + let task_report: Vec<TaskReport> = statuses |
| 90 | + .into_iter() |
| 91 | + .map(|(request_key, status)| (to_task_descriptor(request_key), to_task_status(status))) |
| 92 | + .collect(); |
| 93 | + Ok(Json(serde_json::to_value(task_report)?)) |
| 94 | +} |
| 95 | + |
| 96 | +#[derive(OpenApi)] |
| 97 | +#[openapi(paths(report_handler))] |
| 98 | +struct Docs; |
| 99 | + |
| 100 | +pub fn create_docs() -> utoipa::openapi::OpenApi { |
| 101 | + Docs::openapi() |
| 102 | +} |
| 103 | + |
| 104 | +pub fn create_router() -> Router<Actor> { |
| 105 | + Router::new().route("/", get(report_handler)) |
| 106 | +} |
0 commit comments