@@ -23,8 +23,7 @@ use crate::{
2323 api:: commitments:: {
2424 server:: CommitmentEvent ,
2525 spec:: {
26- CommitmentError , RejectionError , GET_METADATA_METHOD , GET_VERSION_METHOD ,
27- REQUEST_INCLUSION_METHOD ,
26+ CommitmentError , GET_METADATA_METHOD , GET_VERSION_METHOD , REQUEST_INCLUSION_METHOD ,
2827 } ,
2928 } ,
3029 common:: BOLT_SIDECAR_VERSION ,
@@ -279,82 +278,75 @@ impl CommitmentRequestProcessor {
279278 let rpc_url = self . url . clone ( ) ;
280279
281280 trace ! ( ?rpc_url, text, "received text message from websocket connection" ) ;
282- // Create the channel to send and receive the commitment response
283281 let ( tx, rx) = oneshot:: channel ( ) ;
284282
285- let request = serde_json:: from_str :: < JsonRpcRequestUuid > ( & text) . map_err ( |e| e. to_string ( ) ) ;
286-
287- // FIXME: still too nested, needs to be refactored.
288- let response = match request {
289- Err ( e) => Err ( e) ,
290- Ok ( request) => {
291- let id = request. id ;
292-
293- match request. method . as_str ( ) {
294- GET_VERSION_METHOD => Ok ( JsonResponse {
295- id : Some ( Value :: String ( id. to_string ( ) ) ) ,
296- result : Value :: String ( BOLT_SIDECAR_VERSION . clone ( ) ) ,
297- ..Default :: default ( )
298- } ) ,
299- GET_METADATA_METHOD => Ok ( JsonResponse {
300- id : Some ( Value :: String ( id. to_string ( ) ) ) ,
301- result : serde_json:: to_value ( self . state . limits ) . expect ( "infallible" ) ,
302- ..Default :: default ( )
303- } ) ,
304- REQUEST_INCLUSION_METHOD => {
305- // Parse the inclusion request from the parameters
306- let inclusion_request = serde_json:: from_value :: < InclusionRequest > (
307- request. params . first ( ) . cloned ( ) . unwrap_or_default ( ) ,
308- )
309- . map_err ( RejectionError :: Json )
310- . inspect_err ( |err| error ! ( ?err, "Failed to parse inclusion request" ) ) ;
311-
312- match inclusion_request {
313- Err ( e) => Ok ( JsonResponse {
314- id : Some ( Value :: String ( id. to_string ( ) ) ) ,
315- error : Some ( e. into ( ) ) ,
316- ..Default :: default ( )
317- } ) ,
318- Ok ( inclusion_request) => {
319- let commitment_request =
320- CommitmentRequest :: Inclusion ( inclusion_request) ;
321-
322- let commitment_event =
323- CommitmentEvent { request : commitment_request, response : tx } ;
324-
325- if let Err ( e) = self . api_events_tx . try_send ( commitment_event) {
326- error ! ( ?e, "failed to send commitment event through channel" ) ;
327- // NOTE: should we return an internal error to the RPC
328- // here?
329- return ;
330- }
331-
332- // add the pending response to self buffer for later processing
333- self . pending_commitment_responses
334- . push ( PendingCommitmentResponse :: new ( rx, id) ) ;
335-
336- return ;
337- }
338- }
339- }
340- other => Err ( format ! ( "unsupported method: {}" , other) ) ,
341- }
283+ let request = match serde_json:: from_str :: < JsonRpcRequestUuid > ( & text) {
284+ Ok ( req) => req,
285+ Err ( e) => {
286+ warn ! ( ?e, ?rpc_url, "failed to parse JSON-RPC request" ) ;
287+ return ;
342288 }
343289 } ;
344290
345- match response {
346- Ok ( json_response) => {
347- let message = Message :: text (
348- serde_json:: to_string ( & json_response) . expect ( "to stringify version response" ) ,
349- ) ;
291+ let id = request. id ;
292+ let mut response = JsonResponse {
293+ id : Some ( Value :: String ( id. to_string ( ) ) ) ,
294+ jsonrpc : "2.0" . to_string ( ) ,
295+ ..Default :: default ( )
296+ } ;
350297
351- // Push the message to the outgoing messages queue for later
352- // processing
353- self . outgoing_messages . push_back ( message) ;
298+ match request. method . as_str ( ) {
299+ GET_VERSION_METHOD => {
300+ response. result = Value :: String ( BOLT_SIDECAR_VERSION . clone ( ) ) ;
301+ self . send_response ( response) ;
354302 }
355- Err ( err) => {
356- warn ! ( ?err, ?rpc_url, "failed to parse JSON-RPC request" ) ;
303+ GET_METADATA_METHOD => {
304+ response. result = serde_json:: to_value ( self . state . limits ) . expect ( "infallible" ) ;
305+ self . send_response ( response) ;
357306 }
358- }
307+ REQUEST_INCLUSION_METHOD => {
308+ let Some ( param) = request. params . first ( ) . cloned ( ) else {
309+ response. error = Some (
310+ CommitmentError :: InvalidParams ( "missing inclusion request" . into ( ) ) . into ( ) ,
311+ ) ;
312+ self . send_response ( response) ;
313+ return ;
314+ } ;
315+
316+ let inclusion_request = match serde_json:: from_value :: < InclusionRequest > ( param) {
317+ Ok ( req) => req,
318+ Err ( e) => {
319+ let msg = format ! ( "failed to parse inclusion request: {}" , e) ;
320+ error ! ( ?e, "failed to parse inclusion request" ) ;
321+ response. error = Some ( CommitmentError :: InvalidParams ( msg) . into ( ) ) ;
322+ self . send_response ( response) ;
323+ return ;
324+ }
325+ } ;
326+
327+ let commitment_request = CommitmentRequest :: Inclusion ( inclusion_request) ;
328+ let commitment_event =
329+ CommitmentEvent { request : commitment_request, response : tx } ;
330+
331+ if let Err ( e) = self . api_events_tx . try_send ( commitment_event) {
332+ error ! ( ?e, "failed to send commitment event through channel" ) ;
333+ response. error = Some ( CommitmentError :: Internal . into ( ) ) ;
334+ self . send_response ( response) ;
335+ return ;
336+ }
337+
338+ // Push the pending commitment response to the queue
339+ self . pending_commitment_responses . push ( PendingCommitmentResponse :: new ( rx, id) ) ;
340+ }
341+ other => {
342+ warn ! ( ?rpc_url, "unsupported method: {}" , other) ;
343+ }
344+ } ;
345+ }
346+
347+ fn send_response ( & mut self , response : JsonResponse ) {
348+ let message =
349+ Message :: text ( serde_json:: to_string ( & response) . expect ( "to stringify response" ) ) ;
350+ self . outgoing_messages . push_back ( message) ;
359351 }
360352}
0 commit comments