Skip to content

Commit

Permalink
Add translator_sv2 test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Dec 10, 2024
1 parent 04caafb commit 3ef0d87
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 29 deletions.
40 changes: 20 additions & 20 deletions roles/Cargo.lock

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

4 changes: 2 additions & 2 deletions roles/tests-integration/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ pub async fn start_sv2_translator(upstream: SocketAddr) -> SocketAddr {
.expect("failed");
let listening_address = get_available_address();
let listening_port = listening_address.port();
let hashrate = measure_hashrate(3) as f32 / 100.0;
let hashrate = measure_hashrate(3) as f32 / 20.0;
let min_individual_miner_hashrate = hashrate;
let shares_per_minute = 60.0;
let shares_per_minute = 100.0;
let channel_diff_update_interval = 60;
let channel_nominal_hashrate = hashrate;
let downstream_difficulty_config =
Expand Down
17 changes: 12 additions & 5 deletions roles/tests-integration/tests/common/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ impl Sniffer {
sleep(std::time::Duration::from_secs(1)).await;
}

pub fn print_messages(&self) {
println!(
"Messages from downstream: {:?}",
self.messages_from_downstream
);
}

/// Returns the oldest message sent by downstream.
///
/// The queue is FIFO and once a message is returned it is removed from the queue.
Expand Down Expand Up @@ -462,8 +469,8 @@ macro_rules! assert_message {
}
_ => {
panic!(
"Sent wrong message: {:?}",
message
"Sent wrong message: {:?}; {:?}",
message, $msg
);
}
}
Expand All @@ -478,8 +485,8 @@ macro_rules! assert_message {
PoolMessages::$message_group($nested_message_group::$expected_message_variant(_)) => {}
_ => {
panic!(
"Sent wrong message: {:?}",
message
"Sent wrong message: {:?}; {:?}",
message, $msg
);
}
}
Expand Down Expand Up @@ -519,7 +526,7 @@ macro_rules! assert_mining_message {
assert_message!(Mining, Mining, $msg, $expected_message_variant, $($expected_property, $expected_property_value),*);
};
($msg:expr, $expected_message_variant:ident) => {
assert_message!(Mining, Mining, $msg, $expected_message_variant);
assert_message!(Mining, Mining, $msg, $expected_message_variant);
};
}

Expand Down
65 changes: 63 additions & 2 deletions roles/tests-integration/tests/pool_integration.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
mod common;

use std::convert::TryInto;
use std::{convert::TryInto, time::Duration};

use common::{InterceptMessage, MessageDirection};
use const_sv2::MESSAGE_TYPE_SETUP_CONNECTION_ERROR;
use roles_logic_sv2::{
common_messages_sv2::{Protocol, SetupConnection, SetupConnectionError},
parsers::{CommonMessages, PoolMessages, TemplateDistribution},
parsers::{CommonMessages, Mining, PoolMessages, TemplateDistribution},
};
use tokio::time::sleep;

// This test starts a Template Provider and a Pool, and checks if they exchange the correct
// messages upon connection.
// The Sniffer is used as a proxy between the Upstream(Template Provider) and Downstream(Pool). The
// Pool will connect to the Sniffer, and the Sniffer will connect to the Template Provider.
#[ignore]
#[tokio::test]
async fn success_pool_template_provider_connection() {
let sniffer_addr = common::get_available_address();
Expand Down Expand Up @@ -59,6 +61,7 @@ async fn success_pool_template_provider_connection() {
assert_tp_message!(sniffer.next_message_from_upstream(), SetNewPrevHash);
}

#[ignore]
#[tokio::test]
async fn test_sniffer_interrupter() {
let sniffer_addr = common::get_available_address();
Expand Down Expand Up @@ -94,3 +97,61 @@ async fn test_sniffer_interrupter() {
assert_common_message!(&sniffer.next_message_from_downstream(), SetupConnection);
assert_common_message!(&sniffer.next_message_from_upstream(), SetupConnectionError);
}

// covers
// https://github.com/stratum-mining/stratum/blob/main/test/message-generator/test/translation-proxy/translation-proxy.json
#[tokio::test]
async fn translation_proxy() {
let pool_translator_sniffer_addr = common::get_available_address();
let tp_addr = common::get_available_address();
let pool_addr = common::get_available_address();

let pool_translator_sniffer = common::start_sniffer(
"0".to_string(),
pool_translator_sniffer_addr,
pool_addr,
false,
None,
)
.await;
let _tp = common::start_template_provider(tp_addr.port()).await;
let _pool_1 = common::start_pool(Some(pool_addr), Some(tp_addr)).await;
let tproxy_addr = common::start_sv2_translator(pool_translator_sniffer_addr).await;
let _ = common::start_mining_device_sv1(tproxy_addr).await;
sleep(Duration::from_secs(6)).await;

assert_common_message!(
&pool_translator_sniffer.next_message_from_downstream(),
SetupConnection
);
assert_common_message!(
&pool_translator_sniffer.next_message_from_upstream(),
SetupConnectionSuccess
);
assert_mining_message!(
&pool_translator_sniffer.next_message_from_downstream(),
OpenExtendedMiningChannel
);
assert_mining_message!(
&pool_translator_sniffer.next_message_from_upstream(),
OpenExtendedMiningChannelSuccess
);
assert_mining_message!(
&pool_translator_sniffer.next_message_from_upstream(),
NewExtendedMiningJob
);
assert_mining_message!(
&pool_translator_sniffer.next_message_from_downstream(),
SubmitSharesExtended
);
// pool_translator_sniffer.print_messages();
// assert!(false);
// assert_mining_message!(
// &pool_translator_sniffer.next_message_from_downstream(),
// SetCustomMiningJob
// );
// assert_mining_message!(
// &pool_translator_sniffer.next_message_from_upstream(),
// SetNewPrevHash
// );
}

0 comments on commit 3ef0d87

Please sign in to comment.