Skip to content

Commit

Permalink
Add tproxy initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Nov 14, 2024
1 parent b900d0a commit 3425383
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
3 changes: 3 additions & 0 deletions roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions roles/tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
89 changes: 88 additions & 1 deletion roles/tests-integration/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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
}

0 comments on commit 3425383

Please sign in to comment.