/// Proxy a request received by this server to Monerod
Proxy a request received by this server to Monerod
Proxy a request received by this server to Monerod
async fn proxy_request_to_monerod(
        &self,
        mut req: Request<Body>,
    ) -> Result<(Request<Bytes>, Response<json::Value>), MmProxyError>
    {
        let monerod_uri = self.get_fully_qualified_monerod_url(req.uri())?;
        let bytes = read_body_until_end(req.body_mut()).await?;
        let request = req.map(|_| bytes.freeze());
        let mut should_proxy = true;
        let mut submit_block = false;
        let body: Bytes = request.body().clone();
        let json = json::from_slice::<json::Value>(&body[..]).unwrap_or_default();
        if json["method"] == "submitblock" || json["method"] == "submit_block" {
            submit_block = true;
            // TODO: determine if achieved difficulty from solution matches monero's difficulty and submit it
            // if transient.current_difficulty.unwrap_or_default() < transient.monero_difficulty.unwrap_or_default()
            // {
            //      debug!(target: LOG_TARGET, "json: {:#}", json);
            //
            //      let params = &json["params"][0];
            //      let params_string = params.to_string().replace("\"", "");
            //
            //      debug!(target: LOG_TARGET, "param_string: {:#}", params_string);
            //
            //     let monero_block = helpers::deserialize_monero_block_from_hex(params_string)?;
            //     debug!(target: LOG_TARGET, "monero_block: {:?}", monero_block);
            //
            //     let hash = helpers::extract_tari_hash(&monero_block).ok_or_else(||
            // MmProxyError::MissingDataError("Could not find Tari hash in Monero block".to_string()))?;
            //
            should_proxy = false;
            // }
        }
        if should_proxy {
            debug!(
                target: LOG_TARGET,
                "Proxying request: {} {} {}",
                request.method(),
                monerod_uri,
                json["method"]
            );
            let mut builder = self
                .http_client
                .request(request.method().clone(), monerod_uri)
                .headers(request.headers().clone());
            if self.config.monerod_use_auth {
                // Use HTTP basic auth. This is the only reason we are using `reqwest` over the standard hyper client.
                builder = builder.basic_auth(&self.config.monerod_username, Some(&self.config.monerod_password));
            }
            let resp = builder
                // This is a cheap clone of the request body
                .body(request.body().clone())
                .send()
                .await
                .map_err(MmProxyError::MonerodRequestFailed)?;
            let json_response = convert_reqwest_response_to_hyper_json_response(resp).await?;
            if submit_block {
                debug!(target: LOG_TARGET, "Submit response {}", json_response.body());
            } else {
                debug!(target: LOG_TARGET, "Received response {}", json_response.body());
            }
            Ok((request, json_response))
        } else {
            debug!(
                target: LOG_TARGET,
                "Non-proxying request: {} {} {}",
                request.method(),
                monerod_uri,
                json["method"]
            );
            let json_response = convert_json_to_hyper_json_response(default_accept(&json), monerod_uri).await?;
            debug!(target: LOG_TARGET, "Received response: {}", json);
            if submit_block {
                debug!(target: LOG_TARGET, "Submit response {}", json_response.body());
            } else {
                debug!(target: LOG_TARGET, "Received response {}", json_response.body());
            }
            Ok((request, json_response))
        }
    }
async fn proxy_request_to_monerod ( & self , mut req : Request < Body > , ) -> Result < ( Request < Bytes > , Response < json :: Value > ) , MmProxyError > { let monerod_uri = self . get_fully_qualified_monerod_url ( req . uri ( ) ) ? ; let bytes = read_body_until_end ( req . body_mut ( ) ) . await ? ; let request = req . map ( | _ | bytes . freeze ( ) ) ; let mut should_proxy = true ; let mut submit_block = false ; let body : Bytes = request . body ( ) . clone ( ) ; let json = json :: from_slice :: < json :: Value > ( & body [ .. ] ) . unwrap_or_default ( ) ; if json [ " " ] == " " || json [ " " ] == " " { submit_block = true ; // TODO: determine if achieved difficulty from solution matches monero's difficulty and submit it // if transient.current_difficulty.unwrap_or_default() < transient.monero_difficulty.unwrap_or_default() // { //      debug!(target: LOG_TARGET, "json: {:#}", json); // //      let params = &json["params"][0]; //      let params_string = params.to_string().replace("\"", ""); // //      debug!(target: LOG_TARGET, "param_string: {:#}", params_string); // //     let monero_block = helpers::deserialize_monero_block_from_hex(params_string)?; //     debug!(target: LOG_TARGET, "monero_block: {:?}", monero_block); // //     let hash = helpers::extract_tari_hash(&monero_block).ok_or_else(|| // MmProxyError::MissingDataError("Could not find Tari hash in Monero block".to_string()))?; // should_proxy = false ; // } } if should_proxy { debug ! ( target LOG_TARGET " " request method ( ) monerod_uri json [ " " ] ) ; let mut builder = self . http_client . request ( request . method ( ) . clone ( ) , monerod_uri ) . headers ( request . headers ( ) . clone ( ) ) ; if self . config . monerod_use_auth { // Use HTTP basic auth. This is the only reason we are using `reqwest` over the standard hyper client. builder = builder . basic_auth ( & self . config . monerod_username , Some ( & self . config . monerod_password ) ) ; } let resp = builder // This is a cheap clone of the request body . body ( request . body ( ) . clone ( ) ) . send ( ) . await . map_err ( MmProxyError :: MonerodRequestFailed ) ? ; let json_response = convert_reqwest_response_to_hyper_json_response ( resp ) . await ? ; if submit_block { debug ! ( target LOG_TARGET " " json_response body ( ) ) ; } else { debug ! ( target LOG_TARGET " " json_response body ( ) ) ; } Ok ( ( request , json_response ) ) } else { debug ! ( target LOG_TARGET " " request method ( ) monerod_uri json [ " " ] ) ; let json_response = convert_json_to_hyper_json_response ( default_accept ( & json ) , monerod_uri ) . await ? ; debug ! ( target LOG_TARGET " " json ) ; if submit_block { debug ! ( target LOG_TARGET " " json_response body ( ) ) ; } else { debug ! ( target LOG_TARGET " " json_response body ( ) ) ; } Ok ( ( request , json_response ) ) } }
Proxy a request received by this server to Monerod
Proxy a request received by this server to Monerod
&self
req
Request<Body>