From 3425383b4d3be75b5167a6f816b83060a9dffd78 Mon Sep 17 00:00:00 2001 From: jbesraa Date: Thu, 14 Nov 2024 11:30:46 +0200 Subject: [PATCH] Add `tproxy` initializer --- roles/Cargo.lock | 3 + roles/tests-integration/Cargo.toml | 3 + roles/tests-integration/tests/common/mod.rs | 89 ++++++++++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/roles/Cargo.lock b/roles/Cargo.lock index f7d1d1a8b1..ce824bb103 100644 --- a/roles/Cargo.lock +++ b/roles/Cargo.lock @@ -1308,10 +1308,13 @@ dependencies = [ "network_helpers_sv2", "once_cell", "pool_sv2", + "rand", "roles_logic_sv2", + "sha2 0.10.8", "tar", "tokio", "tracing", + "translator_sv2", ] [[package]] diff --git a/roles/tests-integration/Cargo.toml b/roles/tests-integration/Cargo.toml index 5c077e6370..5743243207 100644 --- a/roles/tests-integration/Cargo.toml +++ b/roles/tests-integration/Cargo.toml @@ -26,6 +26,9 @@ roles_logic_sv2 = { path = "../../protocols/v2/roles-logic-sv2" } tar = "0.4.41" tokio = { version="1.36.0",features = ["full","tracing"] } tracing = "0.1.40" +translator_sv2 = { path = "../translator" } +rand = "0.8.4" +sha2 = "0.10.6" [lib] path = "tests/common/mod.rs" diff --git a/roles/tests-integration/tests/common/mod.rs b/roles/tests-integration/tests/common/mod.rs index ea23151229..945d382919 100644 --- a/roles/tests-integration/tests/common/mod.rs +++ b/roles/tests-integration/tests/common/mod.rs @@ -1,14 +1,18 @@ mod sniffer; +use binary_sv2::U256; use bitcoind::{bitcoincore_rpc::RpcApi, BitcoinD, Conf}; use flate2::read::GzDecoder; use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey}; use once_cell::sync::Lazy; use pool_sv2::PoolSv2; +use rand::{thread_rng, Rng}; +use roles_logic_sv2::mining_sv2::Target; +use sha2::{Digest, Sha256}; use sniffer::Sniffer; use std::{ collections::HashSet, - convert::TryFrom, + convert::{TryFrom, TryInto}, env, fs::{create_dir_all, File}, io::{BufReader, Read}, @@ -282,3 +286,86 @@ pub async fn start_template_provider(tp_port: u16) -> TemplateProvider { template_provider.generate_blocks(16); template_provider } + +pub async fn start_sv2_translator(upstream: SocketAddr) -> SocketAddr { + let upstream_address = upstream.ip().to_string(); + let upstream_port = upstream.port(); + let upstream_authority_pubkey = Secp256k1PublicKey::try_from( + "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72".to_string(), + ) + .expect("failed"); + let listening_address = get_available_address(); + let listening_port = listening_address.port(); + let hashrate = measure_hashrate(3) as f32 / 100.0; + let min_individual_miner_hashrate = hashrate; + let shares_per_minute = 60.0; + let channel_diff_update_interval = 60; + let channel_nominal_hashrate = hashrate; + let downstream_difficulty_config = + translator_sv2::proxy_config::DownstreamDifficultyConfig::new( + min_individual_miner_hashrate, + shares_per_minute, + 0, + 0, + ); + let upstream_difficulty_config = translator_sv2::proxy_config::UpstreamDifficultyConfig::new( + channel_diff_update_interval, + channel_nominal_hashrate, + 0, + false, + ); + let upstream_conf = translator_sv2::proxy_config::UpstreamConfig::new( + upstream_address, + upstream_port, + upstream_authority_pubkey, + upstream_difficulty_config, + ); + let downstream_conf = translator_sv2::proxy_config::DownstreamConfig::new( + listening_address.ip().to_string(), + listening_port, + downstream_difficulty_config, + ); + + let config = + translator_sv2::proxy_config::ProxyConfig::new(upstream_conf, downstream_conf, 2, 2, 8); + let translator_v2 = translator_sv2::TranslatorSv2::new(config); + tokio::spawn(async move { + translator_v2.start().await; + }); + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + listening_address +} + +fn measure_hashrate(duration_secs: u64) -> f64 { + let mut share = { + let mut rng = thread_rng(); + let mut arr = [0u8; 80]; + rng.fill(&mut arr[..]); + arr + }; + let start_time = std::time::Instant::now(); + let mut hashes: u64 = 0; + let duration = std::time::Duration::from_secs(duration_secs); + + let 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() + }; + + loop { + if start_time.elapsed() >= duration { + break; + } + hash(&mut share); + hashes += 1; + } + + let elapsed_secs = start_time.elapsed().as_secs_f64(); + + hashes as f64 / elapsed_secs +}