Skip to content

Commit

Permalink
tproxy test working with sv1-mining-device
Browse files Browse the repository at this point in the history
- TRASLATION PROXY MG TEST
A new test for the message generator. This test uses the sv1-mining-device in example. It was necessary as it is very ugly to include a cpuminer binary within the sri. This sv1-mining-device now works for the purposes of this test (for example, the miner target is hardcoded and it is below the target sent to the TProxy within the OpenExtendedMiningchannelSuccess message). The test is similar to a pool's mock, that simulates the communication between a TProxy and the upstream.

The miner can be replaced with the preferred sv1 miner. You can do this by launching it as execution command in the test, or running a test without miner and launching separately a miner

 - SV1 MINING DEVICE (credits fi3)
The sv1-mining-device is now working

 -- changes in /examples/sv1-mining-device/src/client.rs
before the client submitted to the upstream, the version bits are changed. Now they are not, for simplicity
before a job was obtained by notify using the implementation of From trait. This has a flaw, as the notify message does not include the extranonce, while the job contains the merkle root, in which is included the coinbase tx and, in turn, the extranonce. So it is impossible to obtain a job from a Notify message. Now the job is contructed properly using also the extranonce using Job::from_notify

 -- changes in /examples/sv1-mining-device/src/mod.rs
The implementation of From trait for turning a Notify into a Job is now a method for Job and the merkle root is no longer hardcoded, but it is calculated using the coinbase_tx suffix, prefix, extranonce and path through merkle_root_from_path function

 -- changes in /examples/sv1-mining-device/src/miner.rs
also the hash of the block is printed

 -- changes in /roles/translator/src/downstream_sv1/downstream.rs
before the version_rolling_mask was obtained from new_version_rolling_mask, that was kind of hardcoded. Now it is set by the Configure message sent by the downstream. The same is for version_rolling_min_bit.

 -- changes in /roles/translator/src/downstream_sv1/mod.rs
the above functions new_version_rolling_mask and new_version_rolling_min_bit are now not used elsewhere and can be removed.

 - OTHER CHANGES

 -- changes to protocols/v2/roles-logic-sv2/src/selectors.rs, to make
clippy happy
  • Loading branch information
lorbax committed Apr 6, 2023
1 parent c3e5d5b commit 2bba6d3
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 206 deletions.
428 changes: 269 additions & 159 deletions Cargo.lock

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions INTEROPERABILITY-TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

How to test Sv2 compliant software against the SRI implementation.

## With Message Generator
## With Message Generator (MG)

First thing you need to write a test that can be executed by the message generator. In order to do
that read the [message generator doc](https://github.com/stratum-mining/stratum/blob/main/utils/message-generator/README.md)
Expand All @@ -18,6 +18,16 @@ and if needed the binaries [here](https://github.com/stratum-mining/stratum/tree
the test with `./message-generator-tests.sh`. If you keep the codebase unchanged you should be able
to easly merge the updates from upstream main so that test are always against the last SRI version.

### TProxy test with MG
As many people may try the SRI with a sv1 mining device (MD), a MG test for the translation proxy has
been added:
`test/message-generator/test/transation-proxy.json`
You can try this test with your desired sv1 mining device in two ways.
1. launching it from the test itself, editing the part of `execution_commands` relative to the
launch of the MD.
2. removing the part of the test that launches the `/examples/sv1-mining-device` within the SRI.
Then launch the test and therefore launch separately your desired sv1 MD.

## Using the SRI role implementations

The easiest way to test various configurations is to use the SRI role implementations. For example,
Expand Down Expand Up @@ -97,4 +107,4 @@ Do this if you'd like to run your own TP instead of connecting to the hosted TP
1. `make`
1. Start and initialize bitcoind
1. `./src/bitcoind -regtest` // this will start bitcoind in regtest mode
1. create at least 16 blocks with `./src/bitcoin-cli -regtest generatetoaddress 16 bcrt1qttuwhmpa7a0ls5kr3ye6pjc24ng685jvdrksxx`
1. create at least 16 blocks with `./src/bitcoin-cli -regtest generatetoaddress 16 bcrt1qttuwhmpa7a0ls5kr3ye6pjc24ng685jvdrksxx`
6 changes: 3 additions & 3 deletions examples/sv1-mining-device/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ impl Client {
// TODO: This is hard coded for the purposes of a demo, should be set by the SV1
// `mining.set_difficulty` message received from the Upstream role
let target_vec: [u8; 32] = [
0, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255, 255, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0,
];
let default_target = Uint256::from_be_bytes(target_vec);
miner.safe_lock(|m| m.new_target(default_target)).unwrap();
Expand Down Expand Up @@ -301,7 +301,7 @@ impl IsClient<'static> for Client {
extranonce.push(0)
}

let new_job = Job::from_notify(notify, extranonce);
let new_job = Job::from_notify(notify, extranonce);
self.miner.safe_lock(|m| m.new_header(new_job)).unwrap();
Ok(())
}
Expand Down
18 changes: 14 additions & 4 deletions examples/sv1-mining-device/src/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) struct Job {
}

impl Job {
pub fn from_notify<'a>(notify_msg: server_to_client::Notify<'a>, extranonce: Vec<u8>) -> Self {
pub fn from_notify(notify_msg: server_to_client::Notify<'_>, extranonce: Vec<u8>) -> Self {
// TODO: Hard coded for demo. Should be properly translated from received Notify message
// Right now, Notify.job_id is a string, but the Job.job_id is a u32 here.
let job_id = 1u32;
Expand All @@ -37,10 +37,20 @@ impl Job {

let coinbase_tx_prefix: Vec<u8> = notify_msg.coin_base1.into();
let coinbase_tx_suffix: Vec<u8> = notify_msg.coin_base2.into();
let path: Vec<Vec<u8>> = notify_msg.merkle_branch.into_iter().map(|node| node.into()).collect();
let path: Vec<Vec<u8>> = notify_msg
.merkle_branch
.into_iter()
.map(|node| node.into())
.collect();

let merkle_root = roles_logic_sv2::utils::merkle_root_from_path(&coinbase_tx_prefix, &coinbase_tx_suffix, &extranonce, &path).unwrap();
let merkle_root: [u8;32] = merkle_root.try_into().unwrap();
let merkle_root = roles_logic_sv2::utils::merkle_root_from_path(
&coinbase_tx_prefix,
&coinbase_tx_suffix,
&extranonce,
&path,
)
.unwrap();
let merkle_root: [u8; 32] = merkle_root.try_into().unwrap();

Job {
job_id,
Expand Down
4 changes: 1 addition & 3 deletions protocols/v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ pub trait IsServer<'a> {
let has_valid_version_bits = match &submit.version_bits {
Some(a) => {
if let Some(version_rolling_mask) = self.version_rolling_mask() {
println!("AAAAAAAAAAA");
version_rolling_mask.check_mask(a)
} else {
println!("BBBBBBBBBBB");
false
}
}
None => dbg!(self.version_rolling_mask().is_none()),
None => self.version_rolling_mask().is_none(),
};

let is_valid_submission = self.is_authorized(&submit.user_name)
Expand Down
5 changes: 1 addition & 4 deletions protocols/v2/roles-logic-sv2/src/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ impl<Down: IsMiningDownstream> DownstreamMiningSelector<Down>
self.channel_id_to_downstream.get(&channel_id).cloned()
}
fn get_all_downstreams(&self) -> Vec<Arc<Mutex<Down>>> {
self.channel_id_to_downstream
.iter()
.map(|(_, v)| v.clone())
.collect()
self.channel_id_to_downstream.values().cloned().collect()
}
}

Expand Down
6 changes: 3 additions & 3 deletions roles/translator/proxy-config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ tp_address = "127.0.0.1:8442"
# Difficulty params
[downstream_difficulty_config]
# hashes/s of the weakest miner that will be connecting
min_individual_miner_hashrate=100_000.0
min_individual_miner_hashrate=500_000.0
# minimum number of shares needed before a mining.set_difficulty is sent for updating targets
miner_num_submits_before_update=5
# target number of shares per minute the miner should be sending
shares_per_minute = 10.0
shares_per_minute = 6.0

[upstream_difficulty_config]
# interval in seconds to elapse before updating channel hashrate with the pool
channel_diff_update_interval = 60
# estimated accumulated hashrate of all downstream miners
channel_nominal_hashrate = 500.0
channel_nominal_hashrate = 5_000_000.0
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,26 @@ downstream_port = 34255
max_supported_version = 2
min_supported_version = 2

# Minimum extranonce2 size
# Minimum extranonce2 size for downstream
# Max value: 16 (leaves 0 bytes for search space splitting of downstreams)
# Max value for CGminer: 8
# Min value: 2
min_extranonce2_size = 8
coinbase_reward_sat = 5_000_000_000

# optional jn config, if set the tproxy start on JN mode
#[jn_config]
#jn_address = "127.0.0.1:34264"
#tp_address = "127.0.0.1:8442"

# Difficulty params
[downstream_difficulty_config]
# hashes/s of the weakest miner that will be connecting
min_individual_miner_hashrate=500_000.0
min_individual_miner_hashrate=100_000.0
# minimum number of shares needed before a mining.set_difficulty is sent for updating targets
miner_num_submits_before_update=5
# target number of shares per minute the miner should be sending
shares_per_minute = 6.0
shares_per_minute = 10.0

[upstream_difficulty_config]
# interval in seconds to elapse before updating channel hashrate with the pool
Expand Down
42 changes: 17 additions & 25 deletions test/message-generator/test/translation-proxy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
"doc": [
"This test does",
"Runs a translator proxy",
"Runs /example/sv1-mining-device",
"Waits that receives SetupConnection message",
"Reply with .Success"
"Reply with .Success",
"Waits that the tproxy sends OpenExtendedMiningChannel",
"Responds with OpenExtendedMiningChannelSuccess",
"Sends NewExtendedMiningJob and SetNewPrevHash to the tproxy",
"Waits a SubmitsSolutionExtended",
"You can use any miner you want, by changing the execution command or by launching it separately"
],
"common_messages": [
],
Expand Down Expand Up @@ -104,7 +110,8 @@
{
"message_ids": ["open_extended_mining_channel_success"],
"role": "server",
"results": []
"results": [],
"actiondoc": "This action responds with OpenExtendedMiningChannelSuccess"
},
{
"message_ids": ["new_extended_mining_job", "set_new_prev_hash"],
Expand All @@ -114,7 +121,8 @@
"type": "match_message_type",
"value": "0x1b"
}
]
],
"actiondoc": "Sends NewExtendedMiningJob and SetNewPrevHash and waits that a SubmitsShareExtended is submitted"
}
],
"setup_commands": [
Expand All @@ -128,7 +136,7 @@
"translator_sv2",
"--",
"-c",
"./test/config/tproxy-config.toml"
"./test/config/tproxy-config-no-jn-sv1-cpu-md.toml"

],
"conditions": {
Expand All @@ -146,29 +154,13 @@
}
},
{
"command": "./minerd",
"command": "cargo",
"args": [
"-a",
"sha256d",
"-o",
"stratum+tcp://localhost:34255",
"-q",
"-D",
"-P"
"run",
"-p",
"sv1-mining-device"
],
"conditions": {
"WithConditions": {
"conditions": [
{
"output_string": "Starting Stratum on stratum+tcp",
"output_location": "StdErr",
"condition": true
}
],
"timer_secs": 240,
"warn_no_panic": false
}
}
"conditions": "None"
}
],
"cleanup_commands": [
Expand Down

0 comments on commit 2bba6d3

Please sign in to comment.