Skip to content
Merged
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
5 changes: 3 additions & 2 deletions protocols/stratum-translation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ name = "stratum_translation"
path = "src/lib.rs"

[dependencies]
bitcoin = { version = "0.32.5" }
binary_sv2 = { path = "../v2/binary-sv2", version = "^5.0.0" }
mining_sv2 = { path = "../v2/subprotocols/mining", version = "^5.0.0" }
mining_sv2 = { path = "../v2/subprotocols/mining", version = "^6.0.0" }
channels_sv2 = { path = "../v2/channels-sv2", version = "^2.0.0" }
v1 = { path = "../v1", package = "sv1_api", version = "^2.0.0" }
tracing = "0.1"
tracing = "0.1"
9 changes: 5 additions & 4 deletions protocols/stratum-translation/src/sv1_to_sv2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::error::{Result, StratumTranslationError};
use mining_sv2::{OpenExtendedMiningChannel, SubmitSharesExtended, Target};
use bitcoin::Target;
use mining_sv2::{OpenExtendedMiningChannel, SubmitSharesExtended};
use v1::{client_to_server, utils::HexU32Be};

/// Builds an SV2 `OpenExtendedMiningChannel` message from the provided inputs.
Expand Down Expand Up @@ -28,7 +29,7 @@ pub fn build_sv2_open_extended_mining_channel(
.try_into()
.map_err(|_| StratumTranslationError::InvalidUserIdentity(user_identity))?,
nominal_hash_rate,
max_target: max_target.into(),
max_target: max_target.to_le_bytes().into(),
min_extranonce_size,
})
}
Expand Down Expand Up @@ -163,7 +164,7 @@ mod tests {

#[test]
fn test_build_sv2_open_extended_mining_channel_happy() {
let max_target: Target = [0xffu8; 32].into();
let max_target = Target::from_le_bytes([0xffu8; 32]);
let res = build_sv2_open_extended_mining_channel(
123,
"user.worker1".to_string(),
Expand All @@ -182,7 +183,7 @@ mod tests {

#[test]
fn test_build_sv2_open_extended_mining_channel_invalid_user() {
let max_target: Target = [0xffu8; 32].into();
let max_target = Target::from_le_bytes([0xffu8; 32]);
// Create a user identity that's too long (> 255 chars)
let long_user = "x".repeat(300);
let res = build_sv2_open_extended_mining_channel(1, long_user, 1.0, max_target, 8);
Expand Down
23 changes: 16 additions & 7 deletions protocols/stratum-translation/src/sv2_to_sv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
//! - SV2 difficulty targets to SV1 set_difficulty messages

use crate::error::{Result, StratumTranslationError};
use channels_sv2::{bip141::try_strip_bip141, target::target_to_difficulty};
use mining_sv2::{NewExtendedMiningJob, SetNewPrevHash, SetTarget, Target};
use bitcoin::Target;
use channels_sv2::bip141::try_strip_bip141;
use mining_sv2::{NewExtendedMiningJob, SetNewPrevHash, SetTarget};
use tracing::debug;
use v1::{
json_rpc, server_to_client,
Expand Down Expand Up @@ -100,7 +101,14 @@ pub fn build_sv1_notify_from_sv2(
pub fn build_sv1_set_difficulty_from_sv2_set_target(
set_target: SetTarget<'_>,
) -> Result<json_rpc::Message> {
build_sv1_set_difficulty_from_sv2_target(set_target.maximum_target.into())
build_sv1_set_difficulty_from_sv2_target(Target::from_le_bytes(
set_target
.maximum_target
.clone()
.as_ref()
.try_into()
.unwrap(),
))
}

/// Builds an SV1 `mining.set_difficulty` JSON-RPC message from an SV2 target.
Expand All @@ -111,7 +119,7 @@ pub fn build_sv1_set_difficulty_from_sv2_set_target(
/// # Returns
/// * `Ok(json_rpc::Message)` - The constructed SV1 mining.set_difficulty message.
pub fn build_sv1_set_difficulty_from_sv2_target(target: Target) -> Result<json_rpc::Message> {
let value = target_to_difficulty(target);
let value = target.difficulty_float();
let set_target = v1::methods::server_to_client::SetDifficulty { value };
Ok(set_target.into())
}
Expand All @@ -120,10 +128,11 @@ pub fn build_sv1_set_difficulty_from_sv2_target(target: Target) -> Result<json_r
mod tests {
use super::*;
use binary_sv2::{Seq0255, Sv2Option, U256};
use mining_sv2::{NewExtendedMiningJob, SetNewPrevHash, SetTarget as Sv2SetTarget, Target};
use bitcoin::Target;
use mining_sv2::{NewExtendedMiningJob, SetNewPrevHash, SetTarget as Sv2SetTarget};

fn dummy_target() -> Target {
[0xffu8; 32].into()
Target::from_le_bytes([0xffu8; 32])
}

#[test]
Expand All @@ -145,7 +154,7 @@ mod tests {
fn test_build_sv1_set_difficulty_from_sv2_set_target() {
let set_target = Sv2SetTarget {
channel_id: 1,
maximum_target: dummy_target().into(),
maximum_target: dummy_target().to_le_bytes().into(),
};
let msg = build_sv1_set_difficulty_from_sv2_set_target(set_target)
.expect("Should convert SetTarget to difficulty");
Expand Down
2 changes: 1 addition & 1 deletion protocols/v2/channels-sv2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ keywords = ["stratum", "mining", "bitcoin", "protocol"]
[dependencies]
binary_sv2 = { path = "../binary-sv2", version = "^5.0.0" }
common_messages_sv2 = { path = "../subprotocols/common-messages", version = "^6.0.0" }
mining_sv2 = { path = "../subprotocols/mining", version = "^5.0.0" }
mining_sv2 = { path = "../subprotocols/mining", version = "^6.0.0" }
template_distribution_sv2 = { path = "../subprotocols/template-distribution", version = "^4.0.0" }
job_declaration_sv2 = { path = "../subprotocols/job-declaration", version = "^5.0.0" }
tracing = { version = "0.1"}
Expand Down
47 changes: 21 additions & 26 deletions protocols/v2/channels-sv2/src/client/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
share_accounting::{ShareAccounting, ShareValidationError, ShareValidationResult},
},
merkle_root::merkle_root_from_path,
target::{bytes_to_hex, target_to_difficulty, u256_to_block_hash},
target::{bytes_to_hex, u256_to_block_hash},
MAX_EXTRANONCE_PREFIX_LEN,
};
use alloc::{format, string::String, vec, vec::Vec};
Expand All @@ -24,11 +24,11 @@ use bitcoin::{
consensus::{serialize, Decodable},
hashes::sha256d::Hash,
transaction::Version,
CompactTarget, OutPoint, Sequence, Target as BitcoinTarget, Transaction, TxIn, TxOut, Witness,
CompactTarget, OutPoint, Sequence, Target, Transaction, TxIn, TxOut, Witness,
};
use mining_sv2::{
NewExtendedMiningJob, SetCustomMiningJob, SetCustomMiningJobSuccess,
SetNewPrevHash as SetNewPrevHashMp, SubmitSharesExtended, Target,
SetNewPrevHash as SetNewPrevHashMp, SubmitSharesExtended,
};
use tracing::debug;

Expand Down Expand Up @@ -64,7 +64,7 @@ pub struct ExtendedChannel<'a> {
user_identity: String,
extranonce_prefix: Vec<u8>,
rollable_extranonce_size: u16,
target: Target, // todo: try to use Target from rust-bitcoin
target: Target,
nominal_hashrate: f32,
version_rolling: bool,
// future jobs are indexed with job_id (u32)
Expand Down Expand Up @@ -514,44 +514,40 @@ impl<'a> ExtendedChannel<'a> {
// convert the header hash to a target type for easy comparison
let hash = header.block_hash();
let raw_hash: [u8; 32] = *hash.to_raw_hash().as_ref();
let hash_as_target: Target = raw_hash.into();
let hash_as_diff = target_to_difficulty(hash_as_target.clone());
let block_hash_target = Target::from_le_bytes(raw_hash);
let hash_as_diff = block_hash_target.difficulty_float();

let network_target = BitcoinTarget::from_compact(nbits);
let network_target = Target::from_compact(nbits);

// print hash_as_target and self.target as human readable hex
let hash_as_u256: binary_sv2::U256 = hash_as_target.clone().into();
let mut hash_bytes = hash_as_u256.to_vec();
hash_bytes.reverse(); // Convert to big-endian for display
let target_u256: binary_sv2::U256 = self.target.clone().into();
let mut target_bytes = target_u256.to_vec();
target_bytes.reverse(); // Convert to big-endian for display
let block_hash_target_bytes = block_hash_target.to_be_bytes();
let target_bytes = self.target.to_be_bytes();

debug!(
"share validation \nshare:\t\t{}\nchannel target:\t{}\nnetwork target:\t{}",
bytes_to_hex(&hash_bytes),
bytes_to_hex(&block_hash_target_bytes),
bytes_to_hex(&target_bytes),
format!("{:x}", network_target)
);

// check if a block was found
if network_target.is_met_by(hash) {
self.share_accounting.update_share_accounting(
target_to_difficulty(self.target.clone()) as u64,
self.target.difficulty_float() as u64,
share.sequence_number,
hash.to_raw_hash(),
);
return Ok(ShareValidationResult::BlockFound);
}

// check if the share hash meets the channel target
if hash_as_target < self.target {
if block_hash_target < self.target {
if self.share_accounting.is_share_seen(hash.to_raw_hash()) {
return Err(ShareValidationError::DuplicateShare);
}

self.share_accounting.update_share_accounting(
target_to_difficulty(self.target.clone()) as u64,
self.target.difficulty_float() as u64,
share.sequence_number,
hash.to_raw_hash(),
);
Expand All @@ -573,6 +569,7 @@ mod tests {
share_accounting::{ShareValidationError, ShareValidationResult},
};
use binary_sv2::Sv2Option;
use bitcoin::Target;
use mining_sv2::{
NewExtendedMiningJob, SetNewPrevHash as SetNewPrevHashMp, SubmitSharesExtended,
};
Expand All @@ -587,7 +584,7 @@ mod tests {
0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let target = [0xff; 32].into();
let target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let version_rolling = true;
let rollable_extranonce_size = 4u16;
Expand Down Expand Up @@ -669,7 +666,7 @@ mod tests {
0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let target = [0xff; 32].into();
let target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let version_rolling = true;
let rollable_extranonce_size = 4u16;
Expand Down Expand Up @@ -742,7 +739,7 @@ mod tests {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
]
.to_vec();
let target = [0xff; 32].into();
let target = Target::from_le_bytes([0xff; 32]);
let nominal_hashrate = 1.0;
let version_rolling = true;
let rollable_extranonce_size = 8u16;
Expand Down Expand Up @@ -830,12 +827,11 @@ mod tests {
]
.to_vec();
// channel target: 0000ffff00000000000000000000000000000000000000000000000000000000
let target = [
let target = Target::from_be_bytes([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00,
]
.into();
]);
let nominal_hashrate = 1.0;
let version_rolling = true;
let rollable_extranonce_size = 8u16;
Expand Down Expand Up @@ -926,12 +922,11 @@ mod tests {
]
.to_vec();
// channel target: 0000ffff00000000000000000000000000000000000000000000000000000000
let target = [
let target = Target::from_le_bytes([
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00,
]
.into();
]);
let nominal_hashrate = 1.0;
let version_rolling = true;
let rollable_extranonce_size = 8u16;
Expand Down
Loading
Loading