Skip to content

Commit

Permalink
refactor requests to the proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Aug 30, 2024
1 parent 05560fc commit fe6778c
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 102 deletions.
1 change: 0 additions & 1 deletion applications/randomx_miner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use tari_common::{configuration::Network, SubConfigPath};
use tari_comms::multiaddr::Multiaddr;

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
Expand Down
2 changes: 0 additions & 2 deletions applications/randomx_miner/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub enum Error {
CommonConfig(#[from] tari_common::configuration::error::ConfigError),
#[error("Reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("General error: {0}")]
General(String),
#[error("Request error: {0}")]
Request(#[from] RequestError),
}
Expand Down
78 changes: 78 additions & 0 deletions applications/randomx_miner/src/json_rpc/get_block_count.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2024. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::sync::Arc;

use log::{debug, error};
use reqwest::Client;
use serde::Deserialize;
use tokio::sync::Mutex;

use crate::{
error::{Error, RequestError},
Request,
};

pub const LOG_TARGET: &str = "minotari::randomx_miner::json_rpc::get_block_count";

#[allow(dead_code)] // jsonrpc and id fields
#[derive(Deserialize, Debug)]
pub struct GetBlockCountResponse {
jsonrpc: String,
id: String,
pub result: BlockCount,
}

#[derive(Deserialize, Debug)]
pub struct BlockCount {
pub count: u64,
pub status: String,
}

pub async fn get_block_count(client: &Client, node_address: &String, tip: Arc<Mutex<u64>>) -> Result<(), Error> {
let response = client
.post(format!("{}/json_rpc", &node_address.to_string()))
.json(&Request::new("get_block_count", serde_json::Value::Null))
.send()
.await
.map_err(|e| {
error!(target: LOG_TARGET, "Reqwest error: {:?}", e);
Error::from(RequestError::GetBlockCount(e.to_string()))
})?
.json::<GetBlockCountResponse>()
.await?;
debug!(target: LOG_TARGET, "`get_block_count` Response: {:?}", response);

if response.result.status == "OK" {
debug!(target: LOG_TARGET, "`get_block_count` Blockchain tip (block height): {}", response.result.count);
*tip.lock().await = response.result.count;
} else {
debug!(target: LOG_TARGET, "Failed to get the block count. Status: {}", response.result.status);
return Err(RequestError::GetBlockCount(format!(
"Failed to get the block count. Status: {}",
response.result.status
))
.into());
}

Ok(())
}
89 changes: 89 additions & 0 deletions applications/randomx_miner/src/json_rpc/get_block_template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2024. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use log::{debug, error};
use reqwest::Client;
use serde::Deserialize;
use serde_json::json;

use crate::{
error::{Error, RequestError},
Request,
};

pub const LOG_TARGET: &str = "minotari::randomx_miner::json_rpc::get_block_template";

#[allow(dead_code)] // jsonrpc and id fields
#[derive(Deserialize, Debug)]
pub struct GetBlockTemplateResponse {
jsonrpc: String,
id: String,
pub result: BlockTemplate,
}

#[allow(dead_code)] // not all fields are used currently
#[derive(Deserialize, Debug)]
pub struct BlockTemplate {
pub blocktemplate_blob: String,
pub blockhashing_blob: String,
pub difficulty: u64,
pub height: u64,
pub prev_hash: String,
pub reserved_offset: u64,
pub status: String,
}

pub async fn get_block_template(
client: &Client,
node_address: &String,
monero_wallet_address: &String,
) -> Result<BlockTemplate, Error> {
let response = client
.post(format!("{}/json_rpc", &node_address.to_string()))
.json(&Request::new(
"get_block_template",
json!({
"wallet_address": monero_wallet_address,
"reserve_size": 60,
}),
))
.send()
.await
.map_err(|e| {
error!(target: LOG_TARGET, "Reqwest error: {:?}", e);
Error::from(RequestError::GetBlockTemplate(e.to_string()))
})?
.json::<GetBlockTemplateResponse>()
.await?;
debug!(target: LOG_TARGET, "`get_block_template` Response: {:?}", response);

if response.result.status == "OK" {
Ok(response.result)
} else {
debug!(target: LOG_TARGET, "Failed to get the block template. Status: {}", response.result.status);
Err(RequestError::GetBlockCount(format!(
"Failed to get the block template. Status: {}",
response.result.status
))
.into())
}
}
38 changes: 6 additions & 32 deletions applications/randomx_miner/src/json_rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use serde::{Deserialize, Serialize};
use serde::Serialize;

mod get_block_count;
mod get_block_template;
pub use get_block_count::get_block_count;
pub use get_block_template::get_block_template;

#[derive(Serialize)]
pub struct Request<'a> {
Expand All @@ -40,34 +45,3 @@ impl Request<'_> {
}
}
}

#[derive(Deserialize, Debug)]
pub struct GetBlockCountResponse {
jsonrpc: String,
id: String,
pub result: GetBlockCountResult,
}

#[derive(Deserialize, Debug)]
pub struct GetBlockCountResult {
pub count: u64,
pub status: String,
}

#[derive(Deserialize, Debug)]
pub struct GetBlockTemplateResponse {
jsonrpc: String,
id: String,
pub result: GetBlockTemplateResult,
}

#[derive(Deserialize, Debug)]
pub struct GetBlockTemplateResult {
pub blocktemplate_blob: String,
pub blockhashing_blob: String,
pub difficulty: u64,
pub height: u64,
pub prev_hash: String,
pub reserved_offset: u64,
pub status: String,
}
73 changes: 6 additions & 67 deletions applications/randomx_miner/src/run_miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,19 @@

use std::{sync::Arc, time::Duration};

use log::{debug, error, info};
use minotari_app_utilities::parse_miner_input::wallet_payment_address;
use log::info;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tari_common::{load_configuration, DefaultConfigLoader};
use tokio::{sync::Mutex, time::sleep};

use crate::{
cli::Cli,
config::RandomXMinerConfig,
error::{ConfigError, Error, RequestError},
json_rpc::{GetBlockCountResponse, GetBlockTemplateResponse, Request},
error::{ConfigError, Error},
json_rpc::{get_block_count, get_block_template},
};

pub const LOG_TARGET: &str = "minotari::randomx_miner::main";
pub const LOG_TARGET_FILE: &str = "minotari::logging::randomx_miner::main";

pub async fn start_miner(cli: Cli) -> Result<(), Error> {
let config_path = cli.common.config_path();
Expand All @@ -61,66 +57,9 @@ pub async fn start_miner(cli: Cli) -> Result<(), Error> {
loop {
info!(target: LOG_TARGET, "Starting new mining cycle");

get_tip_info(&client, &node_address, tip.clone()).await?;
get_block_template(&client, &node_address, &monero_wallet_address).await?;
sleep(Duration::from_secs(15)).await
}
}

async fn get_tip_info(client: &Client, node_address: &String, tip: Arc<Mutex<u64>>) -> Result<(), Error> {
let response = client
.post(format!("{}/json_rpc", &node_address.to_string())) // Replace with your node's address
.json(&Request::new("get_block_count", serde_json::Value::Null))
.send().await.map_err(|e| {
error!(target: LOG_TARGET, "Reqwest error: {:?}", e);
Error::from(RequestError::GetBlockCount(e.to_string()))
})?
.json::<GetBlockCountResponse>().await?;
debug!(target: LOG_TARGET, "`get_block_count` Response: {:?}", response);

if response.result.status == "OK" {
debug!(target: LOG_TARGET, "`get_block_count` Blockchain tip (block height): {}", response.result.count);
*tip.lock().await = response.result.count;
} else {
debug!(target: LOG_TARGET, "Failed to get the block count. Status: {}", response.result.status);
return Err(RequestError::GetBlockCount(format!(
"Failed to get the block count. Status: {}",
response.result.status
))
.into());
}
get_block_count(&client, &node_address, tip.clone()).await?;
let block_template = get_block_template(&client, &node_address, &monero_wallet_address).await?;

Ok(())
}

async fn get_block_template(
client: &Client,
node_address: &String,
monero_wallet_address: &String,
) -> Result<(), Error> {
let response = client
.post(format!("{}/json_rpc", &node_address.to_string())) // Replace with your node's address
.json(&Request::new("get_block_template", json!({
"wallet_address": monero_wallet_address,
"reserve_size": 60,
})))
.send().await.map_err(|e| {
error!(target: LOG_TARGET, "Reqwest error: {:?}", e);
Error::from(RequestError::GetBlockTemplate(e.to_string()))
})?
.json::<GetBlockTemplateResponse>().await?;
debug!(target: LOG_TARGET, "`get_block_template` Response: {:?}", response);

if response.result.status == "OK" {
debug!(target: LOG_TARGET, "`get_block_template` Block template: {:?}", response.result);
} else {
debug!(target: LOG_TARGET, "Failed to get the block template. Status: {}", response.result.status);
return Err(RequestError::GetBlockCount(format!(
"Failed to get the block template. Status: {}",
response.result.status
))
.into());
sleep(Duration::from_secs(15)).await
}

Ok(())
}

0 comments on commit fe6778c

Please sign in to comment.