Skip to content

Commit dc4c9a0

Browse files
Return Receipt type on receipt endpoint query
1 parent 15f1bc8 commit dc4c9a0

File tree

3 files changed

+62
-33
lines changed

3 files changed

+62
-33
lines changed

aggregation_mode/batcher/src/db.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ pub enum DbError {
1414
ConnectError(String),
1515
}
1616

17+
#[derive(Debug, Clone, sqlx::Type, serde::Serialize)]
18+
#[sqlx(type_name = "task_status")]
19+
#[sqlx(rename_all = "lowercase")]
20+
enum TaskStatus {
21+
Pending,
22+
Processing,
23+
Verified,
24+
}
25+
26+
#[derive(Debug, Clone, sqlx::FromRow, sqlx::Type, serde::Serialize)]
27+
pub(crate) struct Receipt {
28+
status: TaskStatus,
29+
pub(crate) merkle_path: Option<Vec<u8>>,
30+
nonce: i64,
31+
address: String,
32+
}
33+
1734
impl Db {
1835
pub async fn try_new(connection_url: &str) -> Result<Self, DbError> {
1936
let pool = PgPoolOptions::new()
@@ -50,13 +67,12 @@ impl Db {
5067
address: Option<&str>,
5168
nonce: Option<i64>,
5269
limit: i64,
53-
) -> Result<Vec<Option<Vec<u8>>>, sqlx::Error> {
54-
// TODO: Return merkle paths, task status, address and nonce for each task
70+
) -> Result<Vec<Receipt>, sqlx::Error> {
5571
// TODO: use dynamic query building instead of match
5672
match (address, nonce) {
5773
(Some(addr), Some(n)) => {
58-
sqlx::query_scalar::<_, Option<Vec<u8>>>(
59-
"SELECT merkle_path FROM tasks
74+
sqlx::query_as::<_, Receipt>(
75+
"SELECT status,merkle_path,nonce,address FROM tasks
6076
WHERE address = $1
6177
AND nonce = $2
6278
LIMIT $3",
@@ -68,8 +84,8 @@ impl Db {
6884
.await
6985
}
7086
(Some(addr), None) => {
71-
sqlx::query_scalar::<_, Option<Vec<u8>>>(
72-
"SELECT merkle_path FROM tasks
87+
sqlx::query_as::<_, Receipt>(
88+
"SELECT status,merkle_path,nonce,address FROM tasks
7389
WHERE address = $1
7490
LIMIT $2",
7591
)
@@ -79,8 +95,8 @@ impl Db {
7995
.await
8096
}
8197
(None, Some(n)) => {
82-
sqlx::query_scalar::<_, Option<Vec<u8>>>(
83-
"SELECT merkle_path FROM tasks
98+
sqlx::query_as::<_, Receipt>(
99+
"SELECT status,merkle_path,nonce,address FROM tasks
84100
WHERE nonce = $1
85101
LIMIT $2",
86102
)
@@ -90,8 +106,8 @@ impl Db {
90106
.await
91107
}
92108
(None, None) => {
93-
sqlx::query_scalar::<_, Option<Vec<u8>>>(
94-
"SELECT merkle_path FROM tasks
109+
sqlx::query_as::<_, Receipt>(
110+
"SELECT status,merkle_path,nonce,address FROM tasks
95111
LIMIT $1",
96112
)
97113
.bind(limit)
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
1-
pub(super) fn format_merkle_paths(paths: Vec<Option<Vec<u8>>>) -> Result<Vec<Vec<String>>, String> {
1+
use crate::db::Receipt;
2+
3+
pub(super) fn format_merkle_paths(
4+
paths: Vec<Receipt>,
5+
) -> Result<Vec<(Receipt, Vec<String>)>, String> {
26
paths
3-
.iter()
4-
.map(|path_opt| match path_opt {
5-
Some(bytes) => {
6-
if bytes.is_empty() {
7-
return Ok(vec![]);
7+
.into_iter()
8+
.map(|receipt| {
9+
if let Some(merkle_path) = &receipt.merkle_path {
10+
if merkle_path.is_empty() {
11+
return Ok((receipt, vec![]));
812
}
9-
10-
if bytes.len() % 32 != 0 {
13+
if merkle_path.len() % 32 != 0 {
1114
return Err("merkle path length is not a multiple of 32 bytes".into());
1215
}
1316

14-
Ok(bytes
17+
let formatted_merkle_path = merkle_path
1518
.chunks(32)
1619
.map(|chunk| format!("0x{}", hex::encode(chunk)))
17-
.collect())
20+
.collect();
21+
22+
Ok((receipt, formatted_merkle_path))
23+
} else {
24+
Ok((receipt, vec![]))
1825
}
19-
None => Ok(vec![]),
2026
})
2127
.collect()
2228
}

aggregation_mode/batcher/src/server/http.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ impl BatcherServer {
178178
params: web::Query<GetReceiptsParams>,
179179
) -> impl Responder {
180180
let Some(state) = req.app_data::<Data<BatcherServer>>() else {
181-
return HttpResponse::InternalServerError()
182-
.json(AppResponse::new_unsucessfull("Internal server error", 500));
181+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
182+
"Internal server error: Failed to get app data",
183+
500,
184+
));
183185
};
184186

185187
let state = state.get_ref();
@@ -189,22 +191,27 @@ impl BatcherServer {
189191
.db
190192
.get_tasks_by_address_and_nonce(params.address.as_deref(), params.nonce, 100)
191193
.await;
192-
let merkle_paths = match db_result {
193-
Ok(merkle_paths) => merkle_paths,
194-
Err(_) => {
195-
return HttpResponse::InternalServerError()
196-
.json(AppResponse::new_unsucessfull("Internal server error", 500));
194+
let receipts = match db_result {
195+
Ok(receipts) => receipts,
196+
Err(e) => {
197+
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
198+
format!("Internal server error: Failed to get tasks by address and nonce: {e}")
199+
.as_str(),
200+
500,
201+
));
197202
}
198203
};
199204

200-
match format_merkle_paths(merkle_paths) {
201-
Ok(merkle_paths) => {
205+
match format_merkle_paths(receipts) {
206+
Ok(receipts) => {
202207
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
203-
"merkle_paths": merkle_paths
208+
"receipts": receipts
204209
})))
205210
}
206-
Err(_) => HttpResponse::InternalServerError()
207-
.json(AppResponse::new_unsucessfull("Internal server error", 500)),
211+
Err(e) => HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
212+
format!("Internal server error: {e}").as_str(),
213+
500,
214+
)),
208215
}
209216
}
210217
}

0 commit comments

Comments
 (0)