Skip to content

Commit e16d14c

Browse files
As a first version, return the merkle paths on the get_receipts query
1 parent 7d6425e commit e16d14c

File tree

4 files changed

+83
-43
lines changed

4 files changed

+83
-43
lines changed

aggregation_mode/batcher/src/db.rs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,64 @@ impl Db {
4545
.map(|res| res.flatten())
4646
}
4747

48-
pub async fn get_tasks_by_address(
48+
pub async fn get_tasks_by_address_and_nonce(
4949
&self,
50-
address: &str,
50+
address: Option<&str>,
51+
nonce: Option<i64>,
5152
limit: i64,
5253
) -> 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
54+
// TODO: Return merkle paths, task status, address and nonce for each task
55+
// TODO: use dynamic query building instead of match
56+
match (address, nonce) {
57+
(Some(addr), Some(n)) => {
58+
sqlx::query_scalar::<_, Option<Vec<u8>>>(
59+
"SELECT merkle_path FROM tasks
60+
WHERE address = $1
61+
AND nonce = $2
62+
ORDER BY created_at DESC
63+
LIMIT $3",
64+
)
65+
.bind(addr.to_lowercase())
66+
.bind(n)
67+
.bind(limit)
68+
.fetch_all(&self.pool)
69+
.await
70+
}
71+
(Some(addr), None) => {
72+
sqlx::query_scalar::<_, Option<Vec<u8>>>(
73+
"SELECT merkle_path FROM tasks
74+
WHERE address = $1
75+
ORDER BY created_at DESC
76+
LIMIT $2",
77+
)
78+
.bind(addr.to_lowercase())
79+
.bind(limit)
80+
.fetch_all(&self.pool)
81+
.await
82+
}
83+
(None, Some(n)) => {
84+
sqlx::query_scalar::<_, Option<Vec<u8>>>(
85+
"SELECT merkle_path FROM tasks
86+
WHERE nonce = $1
87+
ORDER BY created_at DESC
88+
LIMIT $2",
89+
)
90+
.bind(n)
91+
.bind(limit)
92+
.fetch_all(&self.pool)
93+
.await
94+
}
95+
(None, None) => {
96+
sqlx::query_scalar::<_, Option<Vec<u8>>>(
97+
"SELECT merkle_path FROM tasks
98+
ORDER BY created_at DESC
99+
LIMIT $1",
100+
)
101+
.bind(limit)
102+
.fetch_all(&self.pool)
103+
.await
104+
}
105+
}
63106
}
64107

65108
pub async fn insert_task(
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1-
pub(super) fn format_merkle_path(bytes: &[u8]) -> Result<Vec<String>, String> {
2-
if bytes.is_empty() {
3-
return Ok(vec![]);
4-
}
1+
pub(super) fn format_merkle_paths(paths: Vec<Option<Vec<u8>>>) -> Result<Vec<Vec<String>>, String> {
2+
paths
3+
.iter()
4+
.map(|path_opt| match path_opt {
5+
Some(bytes) => {
6+
if bytes.is_empty() {
7+
return Ok(vec![]);
8+
}
59

6-
if bytes.len() % 32 != 0 {
7-
return Err("merkle path length is not a multiple of 32 bytes".into());
8-
}
10+
if bytes.len() % 32 != 0 {
11+
return Err("merkle path length is not a multiple of 32 bytes".into());
12+
}
913

10-
Ok(bytes
11-
.chunks(32)
12-
.map(|chunk| format!("0x{}", hex::encode(chunk)))
13-
.collect())
14+
Ok(bytes
15+
.chunks(32)
16+
.map(|chunk| format!("0x{}", hex::encode(chunk)))
17+
.collect())
18+
}
19+
None => Ok(vec![]),
20+
})
21+
.collect()
1422
}

aggregation_mode/batcher/src/server/http.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use aligned_sdk::aggregation_layer::AggregationModeProvingSystem;
1111
use sqlx::types::BigDecimal;
1212

1313
use super::{
14-
helpers::format_merkle_path,
14+
helpers::format_merkle_paths,
1515
types::{AppResponse, GetReceiptsParams},
1616
};
1717

@@ -184,21 +184,10 @@ impl BatcherServer {
184184
let state = state.get_ref();
185185

186186
// TODO: maybe also accept proof commitment in query param
187-
let Some(address) = params.address.clone() else {
188-
return HttpResponse::BadRequest().json(AppResponse::new_unsucessfull(
189-
"Provide task `address` query param",
190-
400,
191-
));
192-
};
193-
194-
if address.is_empty() {
195-
return HttpResponse::BadRequest().json(AppResponse::new_unsucessfull(
196-
"Address cannot be empty",
197-
400,
198-
));
199-
}
200-
201-
let db_result = state.db.get_tasks_by_address(&address, 100).await;
187+
let db_result = state
188+
.db
189+
.get_tasks_by_address_and_nonce(params.address.as_deref(), params.nonce, 100)
190+
.await;
202191
let merkle_paths = match db_result {
203192
Ok(merkle_paths) => merkle_paths,
204193
Err(_) => {
@@ -207,10 +196,10 @@ impl BatcherServer {
207196
}
208197
};
209198

210-
match format_merkle_path(&merkle_path) {
211-
Ok(merkle_path) => {
199+
match format_merkle_paths(merkle_paths) {
200+
Ok(merkle_paths) => {
212201
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
213-
"merkle_path": merkle_path
202+
"merkle_paths": merkle_paths
214203
})))
215204
}
216205
Err(_) => HttpResponse::InternalServerError()

aggregation_mode/batcher/src/server/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl AppResponse {
3030
#[derive(Deserialize, Clone)]
3131
pub(super) struct GetReceiptsParams {
3232
pub address: Option<String>,
33-
pub nonce: Option<u64>,
33+
pub nonce: Option<i64>,
3434
}
3535

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

0 commit comments

Comments
 (0)