Skip to content

Commit f4e8143

Browse files
require addresss on receipts query, and simplify the db interface
1 parent d91c748 commit f4e8143

File tree

2 files changed

+61
-54
lines changed

2 files changed

+61
-54
lines changed

aggregation_mode/batcher/src/db.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,46 +64,34 @@ impl Db {
6464

6565
pub async fn get_tasks_by_address_and_nonce(
6666
&self,
67-
address: Option<&str>,
68-
nonce: Option<i64>,
69-
limit: i64,
67+
address: &str,
68+
nonce: i64,
7069
) -> Result<Vec<Receipt>, sqlx::Error> {
71-
// TODO: use dynamic query building instead of match
72-
match (address, nonce) {
73-
(Some(addr), Some(n)) => {
74-
sqlx::query_as::<_, Receipt>(
75-
"SELECT status,merkle_path,nonce,address FROM tasks
70+
sqlx::query_as::<_, Receipt>(
71+
"SELECT status,merkle_path,nonce,address FROM tasks
7672
WHERE address = $1
77-
AND nonce = $2
78-
LIMIT $3",
79-
)
80-
.bind(addr.to_lowercase())
81-
.bind(n)
82-
.bind(limit)
83-
.fetch_all(&self.pool)
84-
.await
85-
}
86-
(Some(addr), None) => {
87-
sqlx::query_as::<_, Receipt>(
88-
"SELECT status,merkle_path,nonce,address FROM tasks
73+
AND nonce = $2",
74+
)
75+
.bind(address.to_lowercase())
76+
.bind(nonce)
77+
.fetch_all(&self.pool)
78+
.await
79+
}
80+
81+
pub async fn get_tasks_by_address(
82+
&self,
83+
address: &str,
84+
limit: i64,
85+
) -> Result<Vec<Receipt>, sqlx::Error> {
86+
sqlx::query_as::<_, Receipt>(
87+
"SELECT status,merkle_path,nonce,address FROM tasks
8988
WHERE address = $1
9089
LIMIT $2",
91-
)
92-
.bind(addr.to_lowercase())
93-
.bind(limit)
94-
.fetch_all(&self.pool)
95-
.await
96-
}
97-
_ => {
98-
sqlx::query_as::<_, Receipt>(
99-
"SELECT status,merkle_path,nonce,address FROM tasks
100-
LIMIT $1",
101-
)
102-
.bind(limit)
103-
.fetch_all(&self.pool)
104-
.await
105-
}
106-
}
90+
)
91+
.bind(address.to_lowercase())
92+
.bind(limit)
93+
.fetch_all(&self.pool)
94+
.await
10795
}
10896

10997
pub async fn insert_task(

aggregation_mode/batcher/src/server/http.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -186,26 +186,45 @@ impl BatcherServer {
186186

187187
let state = state.get_ref();
188188

189-
if params.address.is_none() && params.nonce.is_some() {
190-
return HttpResponse::BadRequest().json(AppResponse::new_unsucessfull(
191-
"Bad request: Cannot specify nonce without address",
192-
400,
193-
));
194-
}
189+
let Some(address) = params.address.clone() else {
190+
return HttpResponse::BadRequest()
191+
.json(AppResponse::new_unsucessfull("Missing address", 400));
192+
};
195193

196194
// TODO: maybe also accept proof commitment in query param
197-
let db_result = state
198-
.db
199-
.get_tasks_by_address_and_nonce(params.address.as_deref(), params.nonce, 100)
200-
.await;
201-
let receipts = match db_result {
202-
Ok(receipts) => receipts,
203-
Err(e) => {
204-
return HttpResponse::InternalServerError().json(AppResponse::new_unsucessfull(
205-
format!("Internal server error: Failed to get tasks by address and nonce: {e}")
206-
.as_str(),
207-
500,
208-
));
195+
let receipts = if let Some(nonce) = params.nonce {
196+
match state
197+
.db
198+
.get_tasks_by_address_and_nonce(&address, nonce)
199+
.await
200+
{
201+
Ok(receipts) => receipts,
202+
Err(e) => {
203+
return HttpResponse::InternalServerError().json(
204+
AppResponse::new_unsucessfull(
205+
format!(
206+
"Internal server error: Failed to get tasks by address and nonce: {e}"
207+
)
208+
.as_str(),
209+
500,
210+
),
211+
);
212+
}
213+
}
214+
} else {
215+
match state.db.get_tasks_by_address(&address, 100).await {
216+
Ok(receipts) => receipts,
217+
Err(e) => {
218+
return HttpResponse::InternalServerError().json(
219+
AppResponse::new_unsucessfull(
220+
format!(
221+
"Internal server error: Failed to get tasks by address and nonce: {e}"
222+
)
223+
.as_str(),
224+
500,
225+
),
226+
);
227+
}
209228
}
210229
};
211230

0 commit comments

Comments
 (0)