Skip to content

Commit 7d6425e

Browse files
Change the GetProofMerklePathQuery method to GetReceipts
1 parent da5dce6 commit 7d6425e

3 files changed

Lines changed: 36 additions & 24 deletions

File tree

aggregation_mode/batcher/src/db.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ impl Db {
4545
.map(|res| res.flatten())
4646
}
4747

48+
pub async fn get_tasks_by_address(
49+
&self,
50+
address: &str,
51+
limit: i64,
52+
) -> Result<Vec<Option<Vec<u8>>>, sqlx::Error> {
53+
sqlx::query_scalar::<_, Option<Vec<u8>>>(
54+
"SELECT merkle_path FROM tasks
55+
WHERE address = $1
56+
ORDER BY created_at DESC
57+
LIMIT $2",
58+
)
59+
.bind(address.to_lowercase())
60+
.bind(limit)
61+
.fetch_all(&self.pool)
62+
.await
63+
}
64+
4865
pub async fn insert_task(
4966
&self,
5067
address: &str,

aggregation_mode/batcher/src/server/http.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use sqlx::types::BigDecimal;
1212

1313
use super::{
1414
helpers::format_merkle_path,
15-
types::{AppResponse, GetProofMerklePathQueryParams},
15+
types::{AppResponse, GetReceiptsParams},
1616
};
1717

1818
use crate::{
@@ -44,7 +44,7 @@ impl BatcherServer {
4444
App::new()
4545
.app_data(Data::new(state.clone()))
4646
.route("/nonce/{address}", web::get().to(Self::get_nonce))
47-
.route("/proof/merkle", web::get().to(Self::get_proof_merkle_path))
47+
.route("/receipts", web::get().to(Self::get_receipts))
4848
.route("/proof/sp1", web::post().to(Self::post_proof_sp1))
4949
.route("/proof/risc0", web::post().to(Self::post_proof_risc0))
5050
})
@@ -55,6 +55,7 @@ impl BatcherServer {
5555
.expect("Server to never end");
5656
}
5757

58+
// Returns the nonce (number of submitted tasks) for a given address
5859
async fn get_nonce(req: HttpRequest) -> impl Responder {
5960
let Some(address) = req.match_info().get("address") else {
6061
return HttpResponse::BadRequest()
@@ -80,6 +81,7 @@ impl BatcherServer {
8081
}
8182
}
8283

84+
// Posts an SP1 proof to the batcher, recovering the address from the signature
8385
async fn post_proof_sp1(
8486
req: HttpRequest,
8587
body: web::Json<SubmitProofRequest<SubmitProofRequestMessageSP1>>,
@@ -160,16 +162,19 @@ impl BatcherServer {
160162
}
161163

162164
/// TODO: complete for risc0 (see `post_proof_sp1`)
165+
// Posts a Risc0 proof to the batcher, recovering the address from the signature
163166
async fn post_proof_risc0(
164167
_req: HttpRequest,
165168
_body: web::Json<SubmitProofRequest<SubmitProofRequestMessageRisc0>>,
166169
) -> impl Responder {
167170
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({})))
168171
}
169172

170-
async fn get_proof_merkle_path(
173+
// Returns the last 100 receipt merkle proofs for the address received in the URL.
174+
// In case of also receiving a nonce on the query param, it returns only the merkle proof for that nonce.
175+
async fn get_receipts(
171176
req: HttpRequest,
172-
params: web::Query<GetProofMerklePathQueryParams>,
177+
params: web::Query<GetReceiptsParams>,
173178
) -> impl Responder {
174179
let Some(state) = req.app_data::<Data<BatcherServer>>() else {
175180
return HttpResponse::InternalServerError()
@@ -179,34 +184,23 @@ impl BatcherServer {
179184
let state = state.get_ref();
180185

181186
// TODO: maybe also accept proof commitment in query param
182-
let Some(id) = params.id.clone() else {
187+
let Some(address) = params.address.clone() else {
183188
return HttpResponse::BadRequest().json(AppResponse::new_unsucessfull(
184-
"Provide task `id` query param",
189+
"Provide task `address` query param",
185190
400,
186191
));
187192
};
188193

189-
if id.is_empty() {
194+
if address.is_empty() {
190195
return HttpResponse::BadRequest().json(AppResponse::new_unsucessfull(
191-
"Proof id cannot be empty",
196+
"Address cannot be empty",
192197
400,
193198
));
194199
}
195200

196-
let Ok(proof_id) = sqlx::types::Uuid::parse_str(&id) else {
197-
return HttpResponse::BadRequest()
198-
.json(AppResponse::new_unsucessfull("Proof id invalid uuid", 400));
199-
};
200-
201-
let db_result = state.db.get_merkle_path_by_task_id(proof_id).await;
202-
let merkle_path = match db_result {
203-
Ok(Some(merkle_path)) => merkle_path,
204-
Ok(None) => {
205-
return HttpResponse::NotFound().json(AppResponse::new_unsucessfull(
206-
"Proof merkle path not found",
207-
404,
208-
))
209-
}
201+
let db_result = state.db.get_tasks_by_address(&address, 100).await;
202+
let merkle_paths = match db_result {
203+
Ok(merkle_paths) => merkle_paths,
210204
Err(_) => {
211205
return HttpResponse::InternalServerError()
212206
.json(AppResponse::new_unsucessfull("Internal server error", 500));

aggregation_mode/batcher/src/server/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ impl AppResponse {
2828

2929
/// Query parameters accepted by `GET /proof/merkle`, containing an optional proof id.
3030
#[derive(Deserialize, Clone)]
31-
pub(super) struct GetProofMerklePathQueryParams {
32-
pub id: Option<String>,
31+
pub(super) struct GetReceiptsParams {
32+
pub address: Option<String>,
33+
pub nonce: Option<u64>,
3334
}
3435

3536
#[derive(Serialize, Deserialize, Clone, Debug)]

0 commit comments

Comments
 (0)