diff --git a/roles/Cargo.lock b/roles/Cargo.lock index 26a464a72..ddd871240 100644 --- a/roles/Cargo.lock +++ b/roles/Cargo.lock @@ -78,6 +78,54 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96bd03f33fe50a863e394ee9718a706f988b9079b20c3784fb726e7678b62fb" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" + +[[package]] +name = "anstyle-parse" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + [[package]] name = "async-attributes" version = "1.1.2" @@ -318,43 +366,21 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" -[[package]] -name = "binary_codec_sv2" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d2635651b7acd536bdf016919e5ce3d973cd53d7ac81c6f1d2523faf688a78" -dependencies = [ - "buffer_sv2 0.1.3", -] - [[package]] name = "binary_codec_sv2" version = "1.0.0" dependencies = [ - "buffer_sv2 1.0.0", -] - -[[package]] -name = "binary_sv2" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d073857c17f94784a1bf3477b7d000b4242766788bb67d83081828d966027cb" -dependencies = [ - "binary_codec_sv2 0.1.5", - "derive_codec_sv2 0.1.5", - "serde", - "serde_sv2 0.1.3", - "tracing", + "buffer_sv2", ] [[package]] name = "binary_sv2" version = "1.0.0" dependencies = [ - "binary_codec_sv2 1.0.0", - "derive_codec_sv2 1.0.0", + "binary_codec_sv2", + "derive_codec_sv2", "serde", - "serde_sv2 1.0.0", + "serde_sv2", "tracing", ] @@ -455,21 +481,12 @@ dependencies = [ "sha2 0.9.9", ] -[[package]] -name = "buffer_sv2" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14ad62c33affff6687aa53d2e8124ebf96fa8b92a38fdd0c46fa93f0175a5e94" -dependencies = [ - "aes-gcm", - "serde", -] - [[package]] name = "buffer_sv2" version = "1.0.0" dependencies = [ "aes-gcm", + "serde", ] [[package]] @@ -538,38 +555,70 @@ dependencies = [ ] [[package]] -name = "codec_sv2" -version = "1.0.0" +name = "clap" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bc066a67923782aa8515dbaea16946c5bcc5addbd668bb80af688e53e548a0" dependencies = [ - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "const_sv2 1.0.0", - "framing_sv2 1.0.0", - "noise_sv2 1.1.0", - "tracing", + "clap_builder", + "clap_derive", ] [[package]] -name = "codec_sv2" -version = "1.0.0" +name = "clap_builder" +version = "4.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae129e2e766ae0ec03484e609954119f123cc1fe650337e155d03b022f24f7b4" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528131438037fd55894f62d6e9f068b8f45ac57ffa77517819645d10aed04f64" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.55", +] + +[[package]] +name = "clap_lex" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c306ce9b6e1a23cc2a8decadc6547b5b5240200a285ed826b15f1129c0bb9b5c" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" + +[[package]] +name = "codec_sv2" +version = "1.0.1" dependencies = [ - "binary_sv2 0.1.7", - "buffer_sv2 0.1.3", - "const_sv2 0.1.3", - "framing_sv2 0.1.6", - "noise_sv2 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "binary_sv2", + "buffer_sv2", + "const_sv2", + "framing_sv2", + "noise_sv2", "serde", "tracing", ] +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "common_messages_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "const_sv2", ] [[package]] @@ -581,15 +630,6 @@ dependencies = [ "crossbeam-utils", ] -[[package]] -name = "const_sv2" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52e86c77da33604c86f89f4ce7c7ae585f7cea0ee6e11388439ced40cd599c32" -dependencies = [ - "secp256k1 0.28.2", -] - [[package]] name = "const_sv2" version = "1.0.0" @@ -632,20 +672,11 @@ dependencies = [ "cipher", ] -[[package]] -name = "derive_codec_sv2" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ac6d35ced783f37b45a6315379aa81e01b444f64d90d49c7bb7f43f06dff356" -dependencies = [ - "binary_codec_sv2 0.1.5", -] - [[package]] name = "derive_codec_sv2" version = "1.0.0" dependencies = [ - "binary_codec_sv2 1.0.0", + "binary_codec_sv2", ] [[package]] @@ -769,25 +800,14 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "framing_sv2" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a545e927300897731132ade9109a4150f1b30680ed7b942bb22c36894103334" -dependencies = [ - "binary_sv2 0.1.7", - "buffer_sv2 0.1.3", - "const_sv2 0.1.3", - "serde", -] - [[package]] name = "framing_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "const_sv2", + "serde", ] [[package]] @@ -1001,6 +1021,12 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.1.19" @@ -1175,11 +1201,11 @@ version = "0.1.0" dependencies = [ "async-channel 1.9.0", "async-recursion 0.3.2", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "codec_sv2", "error_handling", - "framing_sv2 1.0.0", + "framing_sv2", "futures", "key-utils", "network_helpers_sv2", @@ -1198,17 +1224,17 @@ name = "jd_server" version = "0.1.0" dependencies = [ "async-channel 1.9.0", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "codec_sv2", + "const_sv2", "error_handling", "hashbrown 0.11.2", "hex", "key-utils", "network_helpers_sv2", "nohash-hasher", - "noise_sv2 1.1.0", + "noise_sv2", "rand", "roles_logic_sv2", "rpc_sv2", @@ -1226,8 +1252,8 @@ dependencies = [ name = "job_declaration_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "const_sv2", ] [[package]] @@ -1313,15 +1339,20 @@ dependencies = [ "async-channel 1.9.0", "async-recursion 0.3.2", "async-std", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "clap", + "codec_sv2", + "const_sv2", "futures", + "key-utils", "network_helpers_sv2", "rand", "roles_logic_sv2", + "sha2 0.10.8", "stratum-common", + "tracing", + "tracing-subscriber", ] [[package]] @@ -1330,10 +1361,10 @@ version = "0.1.0" dependencies = [ "async-channel 1.9.0", "async-recursion 0.3.2", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "codec_sv2", + "const_sv2", "futures", "key-utils", "network_helpers_sv2", @@ -1352,8 +1383,8 @@ dependencies = [ name = "mining_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "const_sv2", ] [[package]] @@ -1378,13 +1409,13 @@ dependencies = [ [[package]] name = "network_helpers_sv2" -version = "1.0.0" +version = "1.0.1" dependencies = [ "async-channel 1.9.0", "async-std", - "binary_sv2 1.0.0", - "codec_sv2 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "const_sv2 1.0.0", + "binary_sv2", + "codec_sv2", + "const_sv2", "futures", "serde", "tokio", @@ -1403,21 +1434,7 @@ version = "1.1.0" dependencies = [ "aes-gcm", "chacha20poly1305", - "const_sv2 1.0.0", - "rand", - "rand_chacha", - "secp256k1 0.28.2", -] - -[[package]] -name = "noise_sv2" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9be5decbcc8c7487c91277675df156c472a025c1a5d83d9a6a3da5ff458c4d3f" -dependencies = [ - "aes-gcm", - "chacha20poly1305", - "const_sv2 0.1.3", + "const_sv2", "rand", "rand_chacha", "secp256k1 0.28.2", @@ -1631,16 +1648,16 @@ version = "0.1.0" dependencies = [ "async-channel 1.9.0", "async-recursion 1.1.0", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "codec_sv2", + "const_sv2", "error_handling", "hex", "key-utils", "network_helpers_sv2", "nohash-hasher", - "noise_sv2 1.1.0", + "noise_sv2", "rand", "roles_logic_sv2", "secp256k1 0.28.2", @@ -1764,11 +1781,11 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" name = "roles_logic_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", + "binary_sv2", "chacha20poly1305", "common_messages_sv2", - "const_sv2 1.0.0", - "framing_sv2 1.0.0", + "const_sv2", + "framing_sv2", "job_declaration_sv2", "mining_sv2", "nohash-hasher", @@ -1907,21 +1924,11 @@ dependencies = [ "serde", ] -[[package]] -name = "serde_sv2" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "149f67cef8328b644f1e48d3d420f07dc57505424f6c399d66b3b10a658cc0d4" -dependencies = [ - "buffer_sv2 0.1.3", - "serde", -] - [[package]] name = "serde_sv2" version = "1.0.0" dependencies = [ - "buffer_sv2 1.0.0", + "buffer_sv2", "serde", ] @@ -2015,6 +2022,12 @@ dependencies = [ "bitcoin", ] +[[package]] +name = "strsim" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" + [[package]] name = "subtle" version = "2.5.0" @@ -2040,7 +2053,7 @@ dependencies = [ name = "sv1_api" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", + "binary_sv2", "bitcoin_hashes 0.3.2", "byteorder", "hex", @@ -2077,8 +2090,8 @@ dependencies = [ name = "template_distribution_sv2" version = "1.0.0" dependencies = [ - "binary_sv2 1.0.0", - "const_sv2 1.0.0", + "binary_sv2", + "const_sv2", ] [[package]] @@ -2247,11 +2260,11 @@ dependencies = [ "async-compat", "async-recursion 0.3.2", "async-std", - "binary_sv2 1.0.0", - "buffer_sv2 1.0.0", - "codec_sv2 1.0.0", + "binary_sv2", + "buffer_sv2", + "codec_sv2", "error_handling", - "framing_sv2 1.0.0", + "framing_sv2", "futures", "key-utils", "network_helpers_sv2", @@ -2298,6 +2311,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + [[package]] name = "valuable" version = "0.1.0" diff --git a/roles/test-utils/mining-device/Cargo.toml b/roles/test-utils/mining-device/Cargo.toml index f308cb146..41429d6a1 100644 --- a/roles/test-utils/mining-device/Cargo.toml +++ b/roles/test-utils/mining-device/Cargo.toml @@ -19,3 +19,8 @@ buffer_sv2 = { version = "1.0.0", path = "../../../utils/buffer"} async-recursion = "0.3.2" rand = "0.8.4" futures = "0.3.5" +key-utils = { version = "^1.0.0", path = "../../../utils/key-utils" } +clap = { version = "^4.5.4", features = ["derive"] } +tracing = { version = "0.1" } +tracing-subscriber = "0.3" +sha2 = "0.10.6" diff --git a/roles/test-utils/mining-device/src/main.rs b/roles/test-utils/mining-device/src/main.rs index a0cc047a2..befd7edff 100644 --- a/roles/test-utils/mining-device/src/main.rs +++ b/roles/test-utils/mining-device/src/main.rs @@ -1,35 +1,117 @@ use async_std::net::TcpStream; -use network_helpers_sv2::PlainConnection; +use key_utils::Secp256k1PublicKey; +use network_helpers_sv2::Connection; use roles_logic_sv2::utils::Id; -use std::{ - net::{IpAddr, Ipv4Addr, SocketAddr}, - sync::Arc, -}; +use std::{net::SocketAddr, sync::Arc, thread::sleep, time::Duration}; +use async_std::net::ToSocketAddrs; +use clap::Parser; +use rand::{thread_rng, Rng}; +use sha2::{Digest, Sha256}; +use std::time::Instant; use stratum_common::bitcoin::{ blockdata::block::BlockHeader, hash_types::BlockHash, hashes::Hash, util::uint::Uint256, }; +use tracing::{error, info}; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + #[arg( + short, + long, + help = "Pool pub key, when left empty the pool certificate is not checked" + )] + pubkey_pool: Option, + #[arg( + short, + long, + help = "Sometimes used by the pool to identify the device" + )] + id_device: Option, + #[arg( + short, + long, + help = "Address of the pool in this format ip:port or domain:port" + )] + address_pool: String, + #[arg( + long, + help = "This value is used to slow down the cpu miner, it rapresents the number of micro-seconds that are awaited between hashes", + default_value = "0" + )] + handicap: u32, + #[arg( + long, + help = "User id, used when a new channel is opened, it can be used by the pool to identify the miner" + )] + id_user: Option, +} -async fn connect(address: SocketAddr, handicap: u32) { - let stream = TcpStream::connect(address).await.unwrap(); +async fn connect( + address: String, + pub_key: Option, + device_id: Option, + user_id: Option, + handicap: u32, +) { + let address = address + .clone() + .to_socket_addrs() + .await + .expect("Invalid pool address, use one of this formats: ip:port, domain:port") + .next() + .expect("Invalid pool address, use one of this formats: ip:port, domain:port"); + info!("Connecting to pool at {}", address); + let socket = loop { + let pool = + async_std::future::timeout(Duration::from_secs(5), TcpStream::connect(address)).await; + match pool { + Ok(result) => match result { + Ok(socket) => break socket, + Err(e) => { + error!( + "Failed to connect to Upstream role at {}, retrying in 5s: {}", + address, e + ); + sleep(Duration::from_secs(5)); + } + }, + Err(_) => { + error!("Pool is unresponsive, terminating"); + std::process::exit(1); + } + } + }; + info!("Pool tcp connection established at {}", address); + let address = socket.peer_addr().unwrap(); + let initiator = Initiator::new(pub_key.map(|e| e.0)); let (receiver, sender): (Receiver, Sender) = - PlainConnection::new(stream, 10).await; - Device::start(receiver, sender, address, handicap).await + Connection::new(socket, codec_sv2::HandshakeRole::Initiator(initiator), 10) + .await + .unwrap(); + info!("Pool noise connection established at {}", address); + Device::start(receiver, sender, address, device_id, user_id, handicap).await } #[async_std::main] async fn main() { - let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 34255); - //task::spawn(async move { connect(socket, 10000).await }); - //task::spawn(async move { connect(socket, 11070).await }); - //task::spawn(async move { connect(socket, 7040).await }); - println!("start"); - connect(socket, 0).await + let args = Args::parse(); + tracing_subscriber::fmt::init(); + info!("start"); + connect( + args.address_pool, + args.pubkey_pool, + args.id_device, + args.id_user, + args.handicap, + ) + .await } use async_channel::{Receiver, Sender}; -use binary_sv2::u256_from_int; -use codec_sv2::{Frame, StandardEitherFrame, StandardSv2Frame}; +use binary_sv2::{u256_from_int, U256}; +use codec_sv2::{Frame, Initiator, StandardEitherFrame, StandardSv2Frame}; use roles_logic_sv2::{ common_messages_sv2::{Protocol, SetupConnection, SetupConnectionSuccess}, common_properties::{IsMiningUpstream, IsUpstream}, @@ -56,12 +138,19 @@ impl SetupConnectionHandler { pub fn new() -> Self { SetupConnectionHandler {} } - fn get_setup_connection_message(address: SocketAddr) -> SetupConnection<'static> { + fn get_setup_connection_message( + address: SocketAddr, + device_id: Option, + ) -> SetupConnection<'static> { let endpoint_host = address.ip().to_string().into_bytes().try_into().unwrap(); let vendor = String::new().try_into().unwrap(); let hardware_version = String::new().try_into().unwrap(); let firmware = String::new().try_into().unwrap(); - let device_id = String::new().try_into().unwrap(); + let device_id = device_id.unwrap_or_default(); + info!( + "Creating SetupConnection message with device id: {:?}", + device_id + ); SetupConnection { protocol: Protocol::MiningProtocol, min_version: 2, @@ -72,22 +161,24 @@ impl SetupConnectionHandler { vendor, hardware_version, firmware, - device_id, + device_id: device_id.try_into().unwrap(), } } pub async fn setup( self_: Arc>, receiver: &mut Receiver, sender: &mut Sender, + device_id: Option, address: SocketAddr, ) { - let setup_connection = Self::get_setup_connection_message(address); + let setup_connection = Self::get_setup_connection_message(address, device_id); let sv2_frame: StdFrame = MiningDeviceMessages::Common(setup_connection.into()) .try_into() .unwrap(); let sv2_frame = sv2_frame.into(); sender.send(sv2_frame).await.unwrap(); + info!("Setup connection sent to {}", address); let mut incoming: StdFrame = receiver.recv().await.unwrap().try_into().unwrap(); let message_type = incoming.get_header().unwrap().msg_type(); @@ -108,6 +199,7 @@ impl ParseUpstreamCommonMessages for SetupConnectionHandler { _: SetupConnectionSuccess, ) -> Result { use roles_logic_sv2::handlers::common::SendTo; + info!("Setup connection success"); Ok(SendTo::None(None)) } @@ -115,6 +207,7 @@ impl ParseUpstreamCommonMessages for SetupConnectionHandler { &mut self, _: roles_logic_sv2::common_messages_sv2::SetupConnectionError, ) -> Result { + error!("Setup connection error"); todo!() } @@ -140,14 +233,17 @@ pub struct Device { sequence_numbers: Id, } -fn open_channel() -> OpenStandardMiningChannel<'static> { - let user_identity = "ABC".to_string().try_into().unwrap(); +fn open_channel(device_id: Option) -> OpenStandardMiningChannel<'static> { + let user_identity = device_id.unwrap_or_default().try_into().unwrap(); let id: u32 = 10; - println!("MINING DEVICE: send open channel with request id {}", id); + info!("Measuring pc hashrate"); + let nominal_hash_rate = measure_hashrate(5) as f32; + info!("Pc hashrate is {}", nominal_hash_rate); + info!("MINING DEVICE: send open channel with request id {}", id); OpenStandardMiningChannel { request_id: id.into(), user_identity, - nominal_hash_rate: 1000.0, // use 1000 or 10000 to test group channels + nominal_hash_rate, max_target: u256_from_int(567_u64), } } @@ -157,11 +253,20 @@ impl Device { mut receiver: Receiver, mut sender: Sender, addr: SocketAddr, + device_id: Option, + user_id: Option, handicap: u32, ) { let setup_connection_handler = Arc::new(Mutex::new(SetupConnectionHandler::new())); - SetupConnectionHandler::setup(setup_connection_handler, &mut receiver, &mut sender, addr) - .await; + SetupConnectionHandler::setup( + setup_connection_handler, + &mut receiver, + &mut sender, + device_id, + addr, + ) + .await; + info!("Pool sv2 connection established at {}", addr); let miner = Arc::new(Mutex::new(Miner::new(handicap))); let self_ = Self { channel_opened: false, @@ -174,7 +279,7 @@ impl Device { sequence_numbers: Id::new(), }; let open_channel = - MiningDeviceMessages::Mining(Mining::OpenStandardMiningChannel(open_channel())); + MiningDeviceMessages::Mining(Mining::OpenStandardMiningChannel(open_channel(user_id))); let frame: StdFrame = open_channel.try_into().unwrap(); self_.sender.send(frame.into()).await.unwrap(); let self_mutex = std::sync::Arc::new(Mutex::new(self_)); @@ -314,7 +419,7 @@ impl ParseUpstreamMiningMessages<(), NullDownstreamMiningSelector, NoRouting> fo self.channel_opened = true; self.channel_id = Some(m.channel_id); let req_id = m.get_request_id_as_u32(); - println!( + info!( "MINING DEVICE: channel opened with: group id {}, channel id {}, request id {}", m.group_channel_id, m.channel_id, req_id ); @@ -357,12 +462,12 @@ impl ParseUpstreamMiningMessages<(), NullDownstreamMiningSelector, NoRouting> fo &mut self, m: SubmitSharesSuccess, ) -> Result, Error> { - println!("SUCCESS {:?}", m); + info!("SUCCESS {:?}", m); Ok(SendTo::None(None)) } fn handle_submit_shares_error(&mut self, _: SubmitSharesError) -> Result, Error> { - println!("Submit shares error"); + info!("Submit shares error"); Ok(SendTo::None(None)) } @@ -425,8 +530,11 @@ impl ParseUpstreamMiningMessages<(), NullDownstreamMiningSelector, NoRouting> fo todo!() } - fn handle_set_target(&mut self, _: SetTarget) -> Result, Error> { - todo!() + fn handle_set_target(&mut self, m: SetTarget) -> Result, Error> { + self.miner + .safe_lock(|miner| miner.new_target(m.maximum_target.to_vec())) + .unwrap(); + Ok(SendTo::None(None)) } fn handle_reconnect(&mut self, _: Reconnect) -> Result, Error> { @@ -457,6 +565,10 @@ impl Miner { fn new_target(&mut self, mut target: Vec) { // target is sent in LE and comparisons in this file are done in BE target.reverse(); + let hex_string = target + .iter() + .fold("".to_string(), |acc, b| acc + format!("{:02x}", b).as_str()); + info!("Set target to {}", hex_string); self.target = Some(Uint256::from_be_bytes(target.try_into().unwrap())); } @@ -489,7 +601,7 @@ impl Miner { hash.reverse(); let hash = Uint256::from_be_bytes(hash); if hash < *self.target.as_ref().ok_or(())? { - println!( + info!( "Found share with nonce: {}, for target: {:?}, with hash: {:?}", header.nonce, self.target, hash, ); @@ -499,3 +611,36 @@ impl Miner { } } } + +// returns hashrate based on how fast the device hashes over the given duration +fn measure_hashrate(duration_secs: u64) -> f64 { + let mut share = generate_random_80_byte_array(); + let start_time = Instant::now(); + let mut hashes: u64 = 0; + let duration = Duration::from_secs(duration_secs); + + while start_time.elapsed() < duration { + for _ in 0..10000 { + hash(&mut share); + hashes += 1; + } + } + + let elapsed_secs = start_time.elapsed().as_secs_f64(); + hashes as f64 / elapsed_secs +} +fn generate_random_80_byte_array() -> [u8; 80] { + let mut rng = thread_rng(); + let mut arr = [0u8; 80]; + rng.fill(&mut arr[..]); + arr +} +fn hash(share: &mut [u8; 80]) -> Target { + let nonce: [u8; 8] = share[0..8].try_into().unwrap(); + let mut nonce = u64::from_le_bytes(nonce); + nonce += 1; + share[0..8].copy_from_slice(&nonce.to_le_bytes()); + let hash = Sha256::digest(&share).to_vec(); + let hash: U256<'static> = hash.try_into().unwrap(); + hash.into() +} diff --git a/utils/key-utils/src/lib.rs b/utils/key-utils/src/lib.rs index fdfc0c7de..4cde25083 100644 --- a/utils/key-utils/src/lib.rs +++ b/utils/key-utils/src/lib.rs @@ -27,6 +27,8 @@ impl Display for Error { } } +impl std::error::Error for Error {} + impl From for Error { fn from(e: Bs58DecodeError) -> Self { Error::Bs58Decode(e)