@@ -2,43 +2,81 @@ mod error;
2
2
mod metrics;
3
3
mod middleware;
4
4
mod processor;
5
+ mod types;
5
6
6
- use std:: { net:: SocketAddr , sync:: Arc } ;
7
+ pub use crate :: processor:: Processor ;
8
+
9
+ use std:: net:: SocketAddr ;
7
10
8
11
use anyhow:: Context ;
9
12
use axum:: {
10
- extract:: { Multipart , Path , Request } ,
13
+ extract:: { Path , Request , State } ,
11
14
middleware:: Next ,
12
15
routing:: { get, post} ,
13
16
Router ,
14
17
} ;
18
+ use error:: ProcessorError ;
15
19
use tokio:: sync:: watch;
16
- use zksync_basic_types:: commitment:: L1BatchCommitmentMode ;
17
- use zksync_config:: configs:: external_proof_integration_api:: ExternalProofIntegrationApiConfig ;
18
- use zksync_dal:: { ConnectionPool , Core } ;
19
- use zksync_object_store:: ObjectStore ;
20
+ use types:: { ExternalProof , ProofGenerationDataResponse } ;
21
+ use zksync_basic_types:: L1BatchNumber ;
20
22
21
23
use crate :: {
22
24
metrics:: { CallOutcome , Method } ,
23
25
middleware:: MetricsMiddleware ,
24
- processor:: Processor ,
25
26
} ;
26
27
27
- pub async fn run_server (
28
- config : ExternalProofIntegrationApiConfig ,
29
- blob_store : Arc < dyn ObjectStore > ,
30
- connection_pool : ConnectionPool < Core > ,
31
- commitment_mode : L1BatchCommitmentMode ,
32
- mut stop_receiver : watch:: Receiver < bool > ,
33
- ) -> anyhow:: Result < ( ) > {
34
- let bind_address = SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , config. http_port ) ) ;
35
- tracing:: info!( "Starting external prover API server on {bind_address}" ) ;
36
- let app = create_router ( blob_store, connection_pool, commitment_mode) . await ;
28
+ /// External API implementation.
29
+ #[ derive( Debug ) ]
30
+ pub struct Api {
31
+ router : Router ,
32
+ port : u16 ,
33
+ }
37
34
38
- let listener = tokio:: net:: TcpListener :: bind ( bind_address)
39
- . await
40
- . with_context ( || format ! ( "Failed binding external prover API server to {bind_address}" ) ) ?;
41
- axum:: serve ( listener, app)
35
+ impl Api {
36
+ pub fn new ( processor : Processor , port : u16 ) -> Self {
37
+ let middleware_factory = |method : Method | {
38
+ axum:: middleware:: from_fn ( move |req : Request , next : Next | async move {
39
+ let middleware = MetricsMiddleware :: new ( method) ;
40
+ let response = next. run ( req) . await ;
41
+ let outcome = match response. status ( ) . is_success ( ) {
42
+ true => CallOutcome :: Success ,
43
+ false => CallOutcome :: Failure ,
44
+ } ;
45
+ middleware. observe ( outcome) ;
46
+ response
47
+ } )
48
+ } ;
49
+
50
+ let router = Router :: new ( )
51
+ . route (
52
+ "/proof_generation_data" ,
53
+ get ( Api :: latest_generation_data)
54
+ . layer ( middleware_factory ( Method :: GetLatestProofGenerationData ) ) ,
55
+ )
56
+ . route (
57
+ "/proof_generation_data/:l1_batch_number" ,
58
+ get ( Api :: generation_data_for_existing_batch)
59
+ . layer ( middleware_factory ( Method :: GetSpecificProofGenerationData ) ) ,
60
+ )
61
+ . route (
62
+ "/verify_proof/:l1_batch_number" ,
63
+ post ( Api :: verify_proof) . layer ( middleware_factory ( Method :: VerifyProof ) ) ,
64
+ )
65
+ . with_state ( processor) ;
66
+
67
+ Self { router, port }
68
+ }
69
+
70
+ pub async fn run ( self , mut stop_receiver : watch:: Receiver < bool > ) -> anyhow:: Result < ( ) > {
71
+ let bind_address = SocketAddr :: from ( ( [ 0 , 0 , 0 , 0 ] , self . port ) ) ;
72
+ tracing:: info!( "Starting external prover API server on {bind_address}" ) ;
73
+
74
+ let listener = tokio:: net:: TcpListener :: bind ( bind_address)
75
+ . await
76
+ . with_context ( || {
77
+ format ! ( "Failed binding external prover API server to {bind_address}" )
78
+ } ) ?;
79
+ axum:: serve ( listener, self . router )
42
80
. with_graceful_shutdown ( async move {
43
81
if stop_receiver. changed ( ) . await . is_err ( ) {
44
82
tracing:: warn!( "Stop signal sender for external prover API server was dropped without sending a signal" ) ;
@@ -47,57 +85,32 @@ pub async fn run_server(
47
85
} )
48
86
. await
49
87
. context ( "External prover API server failed" ) ?;
50
- tracing:: info!( "External prover API server shut down" ) ;
51
- Ok ( ( ) )
52
- }
88
+ tracing:: info!( "External prover API server shut down" ) ;
89
+ Ok ( ( ) )
90
+ }
53
91
54
- async fn create_router (
55
- blob_store : Arc < dyn ObjectStore > ,
56
- connection_pool : ConnectionPool < Core > ,
57
- commitment_mode : L1BatchCommitmentMode ,
58
- ) -> Router {
59
- let mut processor =
60
- Processor :: new ( blob_store. clone ( ) , connection_pool. clone ( ) , commitment_mode) ;
61
- let verify_proof_processor = processor. clone ( ) ;
62
- let specific_proof_processor = processor. clone ( ) ;
92
+ async fn latest_generation_data (
93
+ State ( processor) : State < Processor > ,
94
+ ) -> Result < ProofGenerationDataResponse , ProcessorError > {
95
+ processor. get_proof_generation_data ( ) . await
96
+ }
63
97
64
- let middleware_factory = |method : Method | {
65
- axum:: middleware:: from_fn ( move |req : Request , next : Next | async move {
66
- let middleware = MetricsMiddleware :: new ( method) ;
67
- let response = next. run ( req) . await ;
68
- let outcome = match response. status ( ) . is_success ( ) {
69
- true => CallOutcome :: Success ,
70
- false => CallOutcome :: Failure ,
71
- } ;
72
- middleware. observe ( outcome) ;
73
- response
74
- } )
75
- } ;
98
+ async fn generation_data_for_existing_batch (
99
+ State ( processor) : State < Processor > ,
100
+ Path ( l1_batch_number) : Path < u32 > ,
101
+ ) -> Result < ProofGenerationDataResponse , ProcessorError > {
102
+ processor
103
+ . proof_generation_data_for_existing_batch ( L1BatchNumber ( l1_batch_number) )
104
+ . await
105
+ }
76
106
77
- Router :: new ( )
78
- . route (
79
- "/proof_generation_data" ,
80
- get ( move || async move { processor. get_proof_generation_data ( ) . await } )
81
- . layer ( middleware_factory ( Method :: GetLatestProofGenerationData ) ) ,
82
- )
83
- . route (
84
- "/proof_generation_data/:l1_batch_number" ,
85
- get ( move |l1_batch_number : Path < u32 > | async move {
86
- specific_proof_processor
87
- . proof_generation_data_for_existing_batch ( l1_batch_number)
88
- . await
89
- } )
90
- . layer ( middleware_factory ( Method :: GetSpecificProofGenerationData ) ) ,
91
- )
92
- . route (
93
- "/verify_proof/:l1_batch_number" ,
94
- post (
95
- move |l1_batch_number : Path < u32 > , multipart : Multipart | async move {
96
- verify_proof_processor
97
- . verify_proof ( l1_batch_number, multipart)
98
- . await
99
- } ,
100
- )
101
- . layer ( middleware_factory ( Method :: VerifyProof ) ) ,
102
- )
107
+ async fn verify_proof (
108
+ State ( processor) : State < Processor > ,
109
+ Path ( l1_batch_number) : Path < u32 > ,
110
+ proof : ExternalProof ,
111
+ ) -> Result < ( ) , ProcessorError > {
112
+ processor
113
+ . verify_proof ( L1BatchNumber ( l1_batch_number) , proof)
114
+ . await
115
+ }
103
116
}
0 commit comments