Skip to content

Commit

Permalink
Merge pull request #1226 from jbesraa/2024-10-18-jds/c-test-starters
Browse files Browse the repository at this point in the history
Add JDC/S initializers in integration tests
  • Loading branch information
plebhash authored Nov 19, 2024
2 parents 8c98647 + ee69573 commit 67a5926
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
2 changes: 2 additions & 0 deletions roles/Cargo.lock

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

117 changes: 117 additions & 0 deletions roles/jd-client/src/lib/proxy_config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
use key_utils::{Secp256k1PublicKey, Secp256k1SecretKey};
use roles_logic_sv2::{errors::Error, utils::CoinbaseOutput as CoinbaseOutput_};
use serde::Deserialize;
Expand All @@ -10,6 +11,15 @@ pub struct CoinbaseOutput {
output_script_value: String,
}

impl CoinbaseOutput {
pub fn new(output_script_type: String, output_script_value: String) -> Self {
Self {
output_script_type,
output_script_value,
}
}
}

impl TryFrom<&CoinbaseOutput> for CoinbaseOutput_ {
type Error = Error;

Expand Down Expand Up @@ -46,6 +56,97 @@ pub struct ProxyConfig {
pub test_only_do_not_send_solution_to_tp: Option<bool>,
}

pub struct PoolConfig {
authority_public_key: Secp256k1PublicKey,
authority_secret_key: Secp256k1SecretKey,
}

impl PoolConfig {
pub fn new(
authority_public_key: Secp256k1PublicKey,
authority_secret_key: Secp256k1SecretKey,
) -> Self {
Self {
authority_public_key,
authority_secret_key,
}
}
}

pub struct TPConfig {
cert_validity_sec: u64,
tp_address: String,
tp_authority_public_key: Option<Secp256k1PublicKey>,
}

impl TPConfig {
pub fn new(
cert_validity_sec: u64,
tp_address: String,
tp_authority_public_key: Option<Secp256k1PublicKey>,
) -> Self {
Self {
cert_validity_sec,
tp_address,
tp_authority_public_key,
}
}
}

pub struct ProtocolConfig {
max_supported_version: u16,
min_supported_version: u16,
min_extranonce2_size: u16,
coinbase_outputs: Vec<CoinbaseOutput>,
}

impl ProtocolConfig {
pub fn new(
max_supported_version: u16,
min_supported_version: u16,
min_extranonce2_size: u16,
coinbase_outputs: Vec<CoinbaseOutput>,
) -> Self {
Self {
max_supported_version,
min_supported_version,
min_extranonce2_size,
coinbase_outputs,
}
}
}

impl ProxyConfig {
pub fn new(
listening_address: std::net::SocketAddr,
protocol_config: ProtocolConfig,
withhold: bool,
pool_config: PoolConfig,
tp_config: TPConfig,
upstreams: Vec<Upstream>,
timeout: Duration,
) -> Self {
Self {
downstream_address: listening_address.ip().to_string(),
downstream_port: listening_address.port(),
max_supported_version: protocol_config.max_supported_version,
min_supported_version: protocol_config.min_supported_version,
min_extranonce2_size: protocol_config.min_extranonce2_size,
withhold,
authority_public_key: pool_config.authority_public_key,
authority_secret_key: pool_config.authority_secret_key,
cert_validity_sec: tp_config.cert_validity_sec,
tp_address: tp_config.tp_address,
tp_authority_public_key: tp_config.tp_authority_public_key,
retry: 0,
upstreams,
timeout,
coinbase_outputs: protocol_config.coinbase_outputs,
test_only_do_not_send_solution_to_tp: None,
}
}
}

#[derive(Debug, Deserialize, Clone)]
pub struct Upstream {
pub authority_pubkey: Secp256k1PublicKey,
Expand All @@ -54,6 +155,22 @@ pub struct Upstream {
pub pool_signature: String, // string be included in coinbase tx input scriptsig
}

impl Upstream {
pub fn new(
authority_pubkey: Secp256k1PublicKey,
pool_address: String,
jd_address: String,
pool_signature: String,
) -> Self {
Self {
authority_pubkey,
pool_address,
jd_address,
pool_signature,
}
}
}

fn duration_from_toml<'de, D>(deserializer: D) -> Result<Duration, D::Error>
where
D: serde::Deserializer<'de>,
Expand Down
2 changes: 2 additions & 0 deletions roles/tests-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ bitcoind = "0.36.0"
codec_sv2 = { path = "../../protocols/v2/codec-sv2", features = ["noise_sv2"] }
const_sv2 = { path = "../../protocols/v2/const-sv2" }
flate2 = "1.0.32"
jd_client = { path = "../jd-client" }
jd_server = { path = "../jd-server" }
key-utils = { path = "../../utils/key-utils" }
minreq = { version = "2.12.0", features = ["https"] }
once_cell = "1.19.0"
Expand Down
98 changes: 98 additions & 0 deletions roles/tests-integration/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,101 @@ pub async fn start_template_provider(tp_port: u16) -> TemplateProvider {
template_provider.generate_blocks(16);
template_provider
}

pub async fn start_jdc(
pool_address: SocketAddr,
tp_address: SocketAddr,
jds_address: SocketAddr,
) -> SocketAddr {
use jd_client::proxy_config::{
CoinbaseOutput, PoolConfig, ProtocolConfig, ProxyConfig, TPConfig, Upstream,
};
let jdc_address = get_available_address();
let max_supported_version = 2;
let min_supported_version = 2;
let min_extranonce2_size = 8;
let withhold = false;
let authority_public_key = Secp256k1PublicKey::try_from(
"9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72".to_string(),
)
.unwrap();
let authority_secret_key = Secp256k1SecretKey::try_from(
"mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n".to_string(),
)
.unwrap();
let cert_validity_sec = 3600;
let coinbase_outputs = vec![CoinbaseOutput::new(
"P2WPKH".to_string(),
"036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075".to_string(),
)];
let authority_pubkey = Secp256k1PublicKey::try_from(
"9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72".to_string(),
)
.unwrap();
let pool_signature = "Stratum v2 SRI Pool".to_string();
let upstreams = vec![Upstream::new(
authority_pubkey,
pool_address.to_string(),
jds_address.to_string(),
pool_signature,
)];
let pool_config = PoolConfig::new(authority_public_key, authority_secret_key);
let tp_config = TPConfig::new(1000, tp_address.to_string(), None);
let protocol_config = ProtocolConfig::new(
max_supported_version,
min_supported_version,
min_extranonce2_size,
coinbase_outputs,
);
let jd_client_proxy = ProxyConfig::new(
jdc_address,
protocol_config,
withhold,
pool_config,
tp_config,
upstreams,
std::time::Duration::from_secs(cert_validity_sec),
);
let ret = jd_client::JobDeclaratorClient::new(jd_client_proxy);
tokio::spawn(async move { ret.start().await });
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
jdc_address
}

pub async fn start_jds(tp_address: SocketAddr) -> SocketAddr {
use jd_server::{CoinbaseOutput, Configuration, CoreRpc, JobDeclaratorServer};
let authority_public_key = Secp256k1PublicKey::try_from(
"9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72".to_string(),
)
.unwrap();
let authority_secret_key = Secp256k1SecretKey::try_from(
"mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n".to_string(),
)
.unwrap();
let listen_jd_address = get_available_address();
let cert_validity_sec = 3600;
let coinbase_outputs = vec![CoinbaseOutput::new(
"P2WPKH".to_string(),
"036adc3bdf21e6f9a0f0fb0066bf517e5b7909ed1563d6958a10993849a7554075".to_string(),
)];
let core_rpc = CoreRpc::new(
tp_address.ip().to_string(),
tp_address.port(),
"tp_username".to_string(),
"tp_password".to_string(),
);
let config = Configuration::new(
listen_jd_address.to_string(),
authority_public_key,
authority_secret_key,
cert_validity_sec,
coinbase_outputs,
core_rpc,
std::time::Duration::from_secs(1),
);
tokio::spawn(async move {
JobDeclaratorServer::new(config).start().await;
});
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
listen_jd_address
}

0 comments on commit 67a5926

Please sign in to comment.