Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemplateProvider cleanup #1369

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions roles/tests-integration/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use utils::get_available_address;

pub mod sniffer;
pub mod template_provider;
mod utils;
pub(crate) mod utils;

pub async fn start_sniffer(
identifier: String,
Expand Down Expand Up @@ -86,7 +86,7 @@ pub async fn start_pool(template_provider_address: Option<SocketAddr>) -> (PoolS
(pool, listening_address)
}

pub async fn start_template_provider(sv2_interval: Option<u32>) -> (TemplateProvider, SocketAddr) {
pub fn start_template_provider(sv2_interval: Option<u32>) -> (TemplateProvider, SocketAddr) {
let address = get_available_address();
let sv2_interval = sv2_interval.unwrap_or(20);
let template_provider = TemplateProvider::start(address.port(), sv2_interval);
Expand Down
87 changes: 6 additions & 81 deletions roles/tests-integration/lib/template_provider.rs
Original file line number Diff line number Diff line change
@@ -1,84 +1,9 @@
use corepc_node::{Conf, Node};
use flate2::read::GzDecoder;
use std::{
env,
fs::{create_dir_all, File},
io::{BufReader, Read},
path::{Path, PathBuf},
};
use tar::Archive;
use std::{env, fs::create_dir_all, path::PathBuf};

const VERSION_TP: &str = "0.1.13";

fn download_bitcoind_tarball(download_url: &str, retries: usize) -> Vec<u8> {
for attempt in 1..=retries {
let response = minreq::get(download_url).send();
match response {
Ok(res) if res.status_code == 200 => {
return res.as_bytes().to_vec();
}
Ok(res) if res.status_code == 503 => {
// If the response is 503, log and prepare for retry
eprintln!(
"Attempt {}: URL {} returned status code 503 (Service Unavailable)",
attempt + 1,
download_url
);
}
Ok(res) => {
// For other status codes, log and stop retrying
panic!(
"URL {} returned unexpected status code {}. Aborting.",
download_url, res.status_code
);
}
Err(err) => {
eprintln!(
"Attempt {}: Failed to fetch URL {}: {:?}",
attempt + 1,
download_url,
err
);
}
}

if attempt < retries {
let delay = 1u64 << (attempt - 1);
eprintln!("Retrying in {} seconds (exponential backoff)...", delay);
std::thread::sleep(std::time::Duration::from_secs(delay));
}
}
// If all retries fail, panic with an error message
panic!(
"Cannot reach URL {} after {} attempts",
download_url, retries
);
}
use crate::utils::{http, tarball};

fn read_tarball_from_file(path: &str) -> Vec<u8> {
let file = File::open(path).unwrap_or_else(|_| {
panic!(
"Cannot find {:?} specified with env var BITCOIND_TARBALL_FILE",
path
)
});
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).unwrap();
buffer
}

fn unpack_tarball(tarball_bytes: &[u8], destination: &Path) {
let decoder = GzDecoder::new(tarball_bytes);
let mut archive = Archive::new(decoder);
for mut entry in archive.entries().unwrap().flatten() {
if let Ok(file) = entry.path() {
if file.ends_with("bitcoind") {
entry.unpack_in(destination).unwrap();
}
}
}
}
const VERSION_TP: &str = "0.1.13";

fn get_bitcoind_filename(os: &str, arch: &str) -> String {
match (os, arch) {
Expand Down Expand Up @@ -128,7 +53,7 @@ impl TemplateProvider {

if !bitcoin_exe_home.exists() {
let tarball_bytes = match env::var("BITCOIND_TARBALL_FILE") {
Ok(path) => read_tarball_from_file(&path),
Ok(path) => tarball::read_from_file(&path),
Err(_) => {
let download_endpoint =
env::var("BITCOIND_DOWNLOAD_ENDPOINT").unwrap_or_else(|_| {
Expand All @@ -138,15 +63,15 @@ impl TemplateProvider {
"{}/sv2-tp-{}/{}",
download_endpoint, VERSION_TP, download_filename
);
download_bitcoind_tarball(&url, 5)
http::make_get_request(&url, 5)
}
};

if let Some(parent) = bitcoin_exe_home.parent() {
create_dir_all(parent).unwrap();
}

unpack_tarball(&tarball_bytes, &tp_dir);
tarball::unpack(&tarball_bytes, &tp_dir);

if os == "macos" {
let bitcoind_binary = bitcoin_exe_home.join("bitcoind");
Expand Down
82 changes: 82 additions & 0 deletions roles/tests-integration/lib/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,85 @@ fn get_available_port() -> u16 {
}
}
}

pub mod http {
pub fn make_get_request(download_url: &str, retries: usize) -> Vec<u8> {
for attempt in 1..=retries {
let response = minreq::get(download_url).send();
match response {
Ok(res) => {
let status_code = res.status_code;
if status_code >= 200 && status_code < 300 {
return res.as_bytes().to_vec();
} else if status_code >= 500 && status_code < 600 {
eprintln!(
"Attempt {}: URL {} returned a server error code {}",
attempt + 1,
download_url,
status_code
);
} else {
panic!(
"URL {} returned unexpected status code {}. Aborting.",
download_url, status_code
);
}
}
Err(err) => {
eprintln!(
"Attempt {}: Failed to fetch URL {}: {:?}",
attempt + 1,
download_url,
err
);
}
}

if attempt < retries {
let delay = 1u64 << (attempt - 1);
eprintln!("Retrying in {} seconds (exponential backoff)...", delay);
std::thread::sleep(std::time::Duration::from_secs(delay));
}
}
// If all retries fail, panic with an error message
panic!(
"Cannot reach URL {} after {} attempts",
download_url, retries
);
}
}

pub mod tarball {
use flate2::read::GzDecoder;
use std::{
fs::File,
io::{BufReader, Read},
path::Path,
};
use tar::Archive;

pub fn read_from_file(path: &str) -> Vec<u8> {
let file = File::open(path).unwrap_or_else(|_| {
panic!(
"Cannot find {:?} specified with env var BITCOIND_TARBALL_FILE",
path
)
});
let mut reader = BufReader::new(file);
let mut buffer = Vec::new();
reader.read_to_end(&mut buffer).unwrap();
buffer
}

pub fn unpack(tarball_bytes: &[u8], destination: &Path) {
let decoder = GzDecoder::new(tarball_bytes);
let mut archive = Archive::new(decoder);
for mut entry in archive.entries().unwrap().flatten() {
if let Ok(file) = entry.path() {
if file.ends_with("bitcoind") {
entry.unpack_in(destination).unwrap();
}
}
}
}
}
4 changes: 2 additions & 2 deletions roles/tests-integration/tests/pool_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use roles_logic_sv2::{
// Pool will connect to the Sniffer, and the Sniffer will connect to the Template Provider.
#[tokio::test]
async fn success_pool_template_provider_connection() {
let (_tp, tp_addr) = start_template_provider(None).await;
let (_tp, tp_addr) = start_template_provider(None);
let (sniffer, sniffer_addr) = start_sniffer("".to_string(), tp_addr, true, None).await;
let _ = start_pool(Some(sniffer_addr)).await;
// here we assert that the downstream(pool in this case) have sent `SetupConnection` message
Expand Down Expand Up @@ -67,7 +67,7 @@ async fn success_pool_template_provider_connection() {
#[tokio::test]
async fn header_timestamp_value_assertion_in_new_extended_mining_job() {
let sv2_interval = Some(5);
let (_tp, tp_addr) = start_template_provider(sv2_interval).await;
let (_tp, tp_addr) = start_template_provider(sv2_interval);
let tp_pool_sniffer_identifier =
"header_timestamp_value_assertion_in_new_extended_mining_job tp_pool sniffer".to_string();
let (tp_pool_sniffer, tp_pool_sniffer_addr) =
Expand Down
4 changes: 2 additions & 2 deletions roles/tests-integration/tests/sniffer_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::convert::TryInto;
// TP -> sniffer_a -> sniffer_b -> Pool
#[tokio::test]
async fn test_sniffer_intercept() {
let (_tp, tp_addr) = start_template_provider(None).await;
let (_tp, tp_addr) = start_template_provider(None);
let message_replacement =
PoolMessages::Common(CommonMessages::SetupConnectionError(SetupConnectionError {
flags: 0,
Expand Down Expand Up @@ -49,7 +49,7 @@ async fn test_sniffer_intercept() {

#[tokio::test]
async fn test_sniffer_wait_for_message_type_with_remove() {
let (_tp, tp_addr) = start_template_provider(None).await;
let (_tp, tp_addr) = start_template_provider(None);
let (sniffer, sniffer_addr) = start_sniffer("".to_string(), tp_addr, false, None).await;
let _ = start_pool(Some(sniffer_addr)).await;
assert!(
Expand Down
2 changes: 1 addition & 1 deletion roles/tests-integration/tests/translator_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use roles_logic_sv2::parsers::{CommonMessages, Mining, PoolMessages};
// shares.
#[tokio::test]
async fn translation_proxy() {
let (_tp, tp_addr) = start_template_provider(None).await;
let (_tp, tp_addr) = start_template_provider(None);
let (_pool, pool_addr) = start_pool(Some(tp_addr)).await;
let (pool_translator_sniffer, pool_translator_sniffer_addr) =
start_sniffer("0".to_string(), pool_addr, false, None).await;
Expand Down
Loading