Skip to content

Commit 8bca829

Browse files
Restore format_merkle_path func to do only one thing
1 parent 13d985e commit 8bca829

File tree

2 files changed

+42
-48
lines changed

2 files changed

+42
-48
lines changed
Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
1-
use crate::{db::Receipt, server::types::GetReceiptsResponse};
1+
pub(super) fn format_merkle_path(bytes: &[u8]) -> Result<Vec<String>, String> {
2+
if bytes.is_empty() {
3+
return Ok(vec![]);
4+
}
25

3-
pub(super) fn format_merkle_paths(paths: Vec<Receipt>) -> Result<Vec<GetReceiptsResponse>, String> {
4-
paths
5-
.into_iter()
6-
.map(|receipt| {
7-
if let Some(merkle_path) = &receipt.merkle_path {
8-
if merkle_path.is_empty() {
9-
return Ok(GetReceiptsResponse {
10-
status: receipt.status,
11-
merkle_path: Vec::new(),
12-
nonce: receipt.nonce,
13-
address: receipt.address,
14-
});
15-
}
16-
if merkle_path.len() % 32 != 0 {
17-
return Err("merkle path length is not a multiple of 32 bytes".into());
18-
}
6+
if bytes.len() % 32 != 0 {
7+
return Err("merkle path length is not a multiple of 32 bytes".into());
8+
}
199

20-
let formatted_merkle_path = merkle_path
21-
.chunks(32)
22-
.map(|chunk| format!("0x{}", hex::encode(chunk)))
23-
.collect();
24-
25-
Ok(GetReceiptsResponse {
26-
status: receipt.status,
27-
merkle_path: formatted_merkle_path,
28-
nonce: receipt.nonce,
29-
address: receipt.address,
30-
})
31-
} else {
32-
Ok(GetReceiptsResponse {
33-
status: receipt.status,
34-
merkle_path: Vec::new(),
35-
nonce: receipt.nonce,
36-
address: receipt.address,
37-
})
38-
}
39-
})
40-
.collect()
10+
Ok(bytes
11+
.chunks(32)
12+
.map(|chunk| format!("0x{}", hex::encode(chunk)))
13+
.collect())
4114
}

aggregation_mode/batcher/src/server/http.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ use aligned_sdk::aggregation_layer::AggregationModeProvingSystem;
1111
use sqlx::types::BigDecimal;
1212

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

1818
use crate::{
1919
config::Config,
2020
db::Db,
2121
server::types::{
22-
SubmitProofRequest, SubmitProofRequestMessageRisc0, SubmitProofRequestMessageSP1,
22+
GetReceiptsResponse, SubmitProofRequest, SubmitProofRequestMessageRisc0,
23+
SubmitProofRequestMessageSP1,
2324
},
2425
};
2526

@@ -232,14 +233,34 @@ impl BatcherServer {
232233
));
233234
};
234235

235-
match format_merkle_paths(receipts) {
236-
Ok(receipts) => {
237-
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
238-
"receipts": receipts
239-
})))
240-
}
241-
Err(_) => HttpResponse::InternalServerError()
242-
.json(AppResponse::new_unsucessfull("Internal server error", 500)),
236+
let mut responses: Vec<GetReceiptsResponse> = Vec::new();
237+
for receipt in receipts {
238+
let Some(merkle_path) = receipt.merkle_path else {
239+
responses.push(GetReceiptsResponse {
240+
status: receipt.status,
241+
merkle_path: Vec::new(),
242+
nonce: receipt.nonce,
243+
address: receipt.address,
244+
});
245+
246+
continue;
247+
};
248+
249+
let Ok(formatted_merkle_path) = format_merkle_path(&merkle_path) else {
250+
return HttpResponse::InternalServerError()
251+
.json(AppResponse::new_unsucessfull("Internal server error", 500));
252+
};
253+
254+
responses.push(GetReceiptsResponse {
255+
status: receipt.status,
256+
merkle_path: formatted_merkle_path,
257+
nonce: receipt.nonce,
258+
address: receipt.address,
259+
});
243260
}
261+
262+
HttpResponse::Ok().json(AppResponse::new_sucessfull(serde_json::json!({
263+
"receipts": responses
264+
})))
244265
}
245266
}

0 commit comments

Comments
 (0)