11use actix_web:: {
2- web:: { self , Data } ,
2+ web:: { self , Data , Query } ,
33 App , HttpRequest , HttpResponse , HttpServer , Responder ,
44} ;
5+ use serde:: Deserialize ;
56
6- use super :: types:: AppResponse ;
7+ use super :: {
8+ helpers:: format_merkle_path,
9+ types:: { AppResponse , ProofMerkleQuery } ,
10+ } ;
711
812use crate :: { config:: Config , db:: Db } ;
913
@@ -27,10 +31,7 @@ impl BatcherServer {
2731 App :: new ( )
2832 . app_data ( Data :: new ( state. clone ( ) ) )
2933 . route ( "/nonce/{address}" , web:: get ( ) . to ( Self :: get_nonce) )
30- . route (
31- "/proof/merkle/:receipt" ,
32- web:: get ( ) . to ( Self :: get_proof_merkle_path) ,
33- )
34+ . route ( "/proof/merkle" , web:: get ( ) . to ( Self :: get_proof_merkle_path) )
3435 . route ( "/proof" , web:: post ( ) . to ( Self :: post_proof) )
3536 } )
3637 . bind ( ( "127.0.0.1" , port) ) ?
@@ -70,8 +71,57 @@ impl BatcherServer {
7071 HttpResponse :: Ok ( )
7172 }
7273
73- // TODO: get the proof merkle path for the receipt id (proof commitment)
74- async fn get_proof_merkle_path ( req : HttpRequest ) -> impl Responder {
75- HttpResponse :: Ok ( )
74+ async fn get_proof_merkle_path (
75+ req : HttpRequest ,
76+ params : web:: Query < ProofMerkleQuery > ,
77+ ) -> impl Responder {
78+ let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
79+ return HttpResponse :: InternalServerError ( )
80+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
81+ } ;
82+
83+ let state = state. get_ref ( ) ;
84+
85+ // TODO: maybe also accept proof commitment in query param
86+ let Some ( id) = params. id . clone ( ) else {
87+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
88+ "Provide proof `id` query param" ,
89+ 400 ,
90+ ) ) ;
91+ } ;
92+
93+ if id. is_empty ( ) {
94+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
95+ "Proof id cannot be empty" ,
96+ 400 ,
97+ ) ) ;
98+ }
99+
100+ let db_result = state. db . get_merkle_path_by_proof_id ( & id) . await ;
101+ let merkle_path = match db_result {
102+ Ok ( Some ( merkle_path) ) => merkle_path,
103+ Ok ( None ) => {
104+ return HttpResponse :: NotFound ( ) . json ( AppResponse :: new_unsucessfull (
105+ "Proof merkle path not found" ,
106+ 404 ,
107+ ) )
108+ }
109+ Err ( _) => {
110+ return HttpResponse :: InternalServerError ( )
111+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) )
112+ }
113+ } ;
114+
115+ match format_merkle_path ( & merkle_path) {
116+ Ok ( merkle_path) => {
117+ HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!( {
118+ "merkle_path" : merkle_path
119+ } ) ) )
120+ }
121+ Err ( _) => {
122+ return HttpResponse :: InternalServerError ( )
123+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) )
124+ }
125+ }
76126 }
77127}
0 commit comments