Skip to content

Commit

Permalink
Communicate with monero node over proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Aug 29, 2024
1 parent a3a3ae0 commit 05560fc
Show file tree
Hide file tree
Showing 9 changed files with 636 additions and 211 deletions.
454 changes: 378 additions & 76 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions applications/randomx_miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ clap = { version = "3.2", features = ["derive", "env"] }
config = "0.14.0"
crossterm = { version = "0.25.0" }
log = { version = "0.4", features = ["std"] }
reqwest = { version = "0.12.7", features = ["json"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
thiserror = "1.0.63"
tokio = { version = "1.36", default-features = false, features = ["rt-multi-thread"] }

[build-dependencies]
Expand Down
147 changes: 20 additions & 127 deletions applications/randomx_miner/log4rs_sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,159 +13,52 @@ refresh_rate: 30 seconds
appenders:
# An appender named "stdout" that writes to file.
stdout:
kind: rolling_file
path: "{{log_dir}}/log/randomx_miner/stdout.log"
append: false
kind: console
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] {h({l}):5} {m}{n}"
pattern: "{d(%H:%M)} {h({l}):5} {m}{n}"
filters:
- kind: threshold
level: info
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: delete

# An appender named "network" that writes to a file with a custom pattern encoder
network:
kind: rolling_file
path: "{{log_dir}}/log/randomx_miner/network.log"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/log/randomx_miner/network.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [Thread:{I}] {l:5} {m}{n}"

# An appender named "base_layer" that writes to a file with a custom pattern encoder
base_layer:
kind: rolling_file
path: "{{log_dir}}/log/randomx_miner/base_layer.log"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/log/randomx_miner/base_layer.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [Thread:{I}] {l:5} {m}{n}"
level: debug

# An appender named "base_layer" that writes to a file with a custom pattern encoder
other:
kind: rolling_file
path: "{{log_dir}}/log/randomx_miner/other.log"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/log/randomx_miner/other.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [Thread:{I}] {l:5} {m}{n}"

# An appender named "contacts" that writes to a file with a custom pattern encoder
contacts:
randomx_miner:
kind: rolling_file
path: "{{log_dir}}/log/randomx_miner/contacts.log"
path: "{{log_dir}}/log/randomx_miner/randomx_miner.log"
policy:
kind: compound
trigger:
kind: size
limit: 10mb
limit: 200mb
roller:
kind: fixed_window
base: 1
count: 5
pattern: "{{log_dir}}/log/randomx_miner/contacts.{}.log"
count: 50
pattern: "{{log_dir}}/log/randomx_miner/randomx_miner.{}.log"
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [Thread:{I}] {l:5} {m}{n}"
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] {l:5} {m}{n}"

# root (to base_layer)
# root
root:
level: debug
appenders:
- other
- stdout
- randomx_miner

loggers:
# base_layer
randomx_miner:
level: debug
appenders:
- base_layer
- stdout
additive: false
# other
h2:
level: info
appenders:
- other
- stdout
- randomx_miner
additive: false
hyper:
level: info
appenders:
- other
additive: false
tokio_util:
level: error
appenders:
- other
additive: false
# network
comms:
level: info
appenders:
- network
additive: false
contacts:
level: info
appenders:
- contacts
additive: false
comms::noise:
level: error
appenders:
- network
additive: false
p2p:
level: info
appenders:
- network
# Route log events sent to the "mio" logger to the "other" appender
mio:
level: error
appenders:
- network
additive: false
yamux:
level: error
appenders:
- network
- stdout
- randomx_miner
additive: false
tracing:
selectors:
level: error
appenders:
- network
additive: false
# Route R2D2 log events
r2d2:
level: warn
appenders:
- other
additive: false
- stdout
- randomx_miner
additive: false
4 changes: 2 additions & 2 deletions applications/randomx_miner/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ use tari_comms::multiaddr::Multiaddr;
#[serde(deny_unknown_fields)]
pub struct RandomXMinerConfig {
/// The address for the monero node, or merge mining proxy
pub monero_base_node_address: Option<Multiaddr>,
pub monero_base_node_address: Option<String>,
/// Monero wallet address
pub monero_wallet_address: Option<String>,
/// An address to post hash results too
pub universe_address: Option<Multiaddr>,
pub universe_address: Option<String>,
/// What mode to run in, eco or max
pub mode: MiningMode,
/// Selected network
Expand Down
53 changes: 53 additions & 0 deletions applications/randomx_miner/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 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.

#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Config error: {0}")]
Config(#[from] ConfigError),
#[error("Common config error: {0}")]
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),
}

#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
#[error("Missing base node or proxy address")]
MissingBaseNode,
#[error("Missing monero wallet address")]
MissingMoneroWalletAddress,
#[error("Common config error: {0}")]
CommonConfig(#[from] tari_common::configuration::error::ConfigError),
}

#[derive(Debug, thiserror::Error)]
pub enum RequestError {
#[error("Failed to process request `get_block_count`: {0}")]
GetBlockCount(String),
#[error("Failed to process request `get_block_template`: {0}")]
GetBlockTemplate(String),
}
73 changes: 73 additions & 0 deletions applications/randomx_miner/src/json_rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 serde::{Deserialize, Serialize};

#[derive(Serialize)]
pub struct Request<'a> {
jsonrpc: &'a str,
id: &'a str,
method: &'a str,
params: serde_json::Value,
}

impl Request<'_> {
pub fn new(method: &str, params: serde_json::Value) -> Request {
Request {
jsonrpc: "2.0",
id: "0",
method,
params,
}
}
}

#[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,
}
10 changes: 8 additions & 2 deletions applications/randomx_miner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
mod cli;
use cli::Cli;
mod config;
mod error;
pub use error::{ConfigError, Error};
mod json_rpc;
pub use json_rpc::Request;
mod run_miner;
use run_miner::start_miner;
use tari_common::exit_codes::ExitError;
use tari_common::exit_codes::{ExitCode, ExitError};

pub const LOG_TARGET: &str = "minotari::randomx_miner::main";
pub const LOG_TARGET_FILE: &str = "minotari::logging::randomx_miner::main";
Expand All @@ -34,5 +38,7 @@ pub const LOG_TARGET_FILE: &str = "minotari::logging::randomx_miner::main";
minotari_app_utilities::deny_non_64_bit_archs!();

pub async fn run_miner(cli: Cli) -> Result<(), ExitError> {
start_miner(cli).await
start_miner(cli)
.await
.map_err(|e| ExitError::new(ExitCode::UnknownError, e.to_string()))
}
8 changes: 7 additions & 1 deletion applications/randomx_miner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ mod cli;
use cli::Cli;
mod run_miner;
use run_miner::start_miner;
mod error;
use tari_common::exit_codes::ExitCode;
mod json_rpc;
use json_rpc::Request;

mod config;

Expand Down Expand Up @@ -62,5 +66,7 @@ async fn main_inner() -> Result<(), ExitError> {
&cli.common.get_base_path(),
include_str!("../log4rs_sample.yml"),
)?;
start_miner(cli).await
start_miner(cli)
.await
.map_err(|e| ExitError::new(ExitCode::UnknownError, e.to_string()))
}
Loading

0 comments on commit 05560fc

Please sign in to comment.