1+ use std:: time:: { SystemTime , UNIX_EPOCH } ;
2+
13use actix_web:: {
4+ http:: StatusCode ,
25 web:: { self , Data } ,
36 App , HttpRequest , HttpResponse , HttpServer , Responder ,
47} ;
@@ -9,7 +12,13 @@ use super::{
912 types:: { AppResponse , ProofMerkleQuery } ,
1013} ;
1114
12- use crate :: { config:: Config , db:: Db , server:: types:: SubmitProofRequest } ;
15+ use crate :: {
16+ config:: Config ,
17+ db:: Db ,
18+ server:: types:: {
19+ SubmitProofRequest , SubmitProofRequestMessageRisc0 , SubmitProofRequestMessageSP1 ,
20+ } ,
21+ } ;
1322
1423#[ derive( Clone , Debug ) ]
1524pub struct BatcherServer {
@@ -60,23 +69,20 @@ impl BatcherServer {
6069 "nonce" : count
6170 }
6271 ) ) ) ,
63- Err ( err) => {
64- tracing:: error!( error = ?err, "failed to count proofs" ) ;
65- HttpResponse :: InternalServerError ( )
66- . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) )
67- }
72+ Err ( _) => HttpResponse :: InternalServerError ( )
73+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ,
6874 }
6975 }
7076
7177 // TODO: receive the proof and 1. decode it, 2. verify it, 3. add to the db
7278 async fn post_proof_sp1 (
7379 req : HttpRequest ,
74- body : web:: Json < SubmitProofRequest > ,
80+ body : web:: Json < SubmitProofRequest < SubmitProofRequestMessageSP1 > > ,
7581 ) -> impl Responder {
7682 let data = body. into_inner ( ) ;
7783
7884 // TODO: validate signature
79- let recovered_address = "" ;
85+ let recovered_address = "0x0000000000000000000000000000000000000000 " ;
8086
8187 let Some ( state) = req. app_data :: < Data < BatcherServer > > ( ) else {
8288 return HttpResponse :: InternalServerError ( )
@@ -85,16 +91,68 @@ impl BatcherServer {
8591 let state = state. get_ref ( ) ;
8692
8793 let Ok ( count) = state. db . count_proofs_by_address ( recovered_address) . await else {
88- return HttpResponse :: InternalServerError ( ) . finish ( ) ;
94+ return HttpResponse :: InternalServerError ( )
95+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
8996 } ;
9097
91- HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!( { } ) ) )
98+ if data. nonce != ( count as u64 ) {
99+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
100+ & format ! ( "Invalid nonce, expected nonce = {count}" ) ,
101+ 400 ,
102+ ) ) ;
103+ }
104+
105+ let now_ts = match SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) {
106+ Ok ( duration) => duration. as_secs ( ) as i64 ,
107+ Err ( _) => {
108+ return HttpResponse :: InternalServerError ( )
109+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
110+ }
111+ } ;
112+
113+ let has_payment = match state
114+ . db
115+ . has_active_payment_event ( recovered_address, now_ts)
116+ . await
117+ {
118+ Ok ( result) => result,
119+ Err ( _) => {
120+ return HttpResponse :: InternalServerError ( )
121+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ;
122+ }
123+ } ;
124+
125+ if !has_payment {
126+ return HttpResponse :: BadRequest ( ) . json ( AppResponse :: new_unsucessfull (
127+ "You have to pay before submitting a proof" ,
128+ 400 ,
129+ ) ) ;
130+ }
131+
132+ match state
133+ . db
134+ . insert_proof (
135+ recovered_address,
136+ AggregationModeProvingSystem :: SP1 . as_u16 ( ) as i32 ,
137+ & data. message . proof ,
138+ & data. message . program_vk_commitment ,
139+ None ,
140+ None ,
141+ )
142+ . await
143+ {
144+ Ok ( proof_id) => HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull (
145+ serde_json:: json!( { "proof_id" : proof_id. to_string( ) } ) ,
146+ ) ) ,
147+ Err ( _) => HttpResponse :: InternalServerError ( )
148+ . json ( AppResponse :: new_unsucessfull ( "Internal server error" , 500 ) ) ,
149+ }
92150 }
93151
94152 /// TODO: complete for risc0 (see `post_proof_sp1`)
95153 async fn post_proof_risc0 (
96154 _req : HttpRequest ,
97- _body : web:: Json < SubmitProofRequest > ,
155+ _body : web:: Json < SubmitProofRequest < SubmitProofRequestMessageRisc0 > > ,
98156 ) -> impl Responder {
99157 HttpResponse :: Ok ( ) . json ( AppResponse :: new_sucessfull ( serde_json:: json!( { } ) ) )
100158 }
0 commit comments