Skip to content

Commit

Permalink
Update roles to use new jd protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3 committed Dec 17, 2024
1 parent 79f939f commit 8e4bf35
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 228 deletions.
15 changes: 0 additions & 15 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ mod impls;
#[cfg(feature = "with_buffer_pool")]
use buffer_sv2::Slice;

use alloc::vec::Vec;

/// The `SizeHint` trait provides a mechanism to return the encoded bytes size of a decodable type.
///
/// It defines two methods for retrieving the size of an encoded message:
Expand Down Expand Up @@ -111,19 +109,6 @@ pub trait Fixed {
const SIZE: usize;
}

// Not used and will be removed during refactoring
pub trait Variable {
const HEADER_SIZE: usize;
//const ELEMENT_SIZE: usize;
const MAX_SIZE: usize;

fn inner_size(&self) -> usize;

// Retrieves the header as a byte vector. This header typically contains information
// about the size or type of the data that follows.
fn get_header(&self) -> Vec<u8>;
}

impl<T: Fixed> SizeHint for T {
fn size_hint(_data: &[u8], _offset: usize) -> Result<usize, Error> {
Ok(Self::SIZE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use alloc::vec::Vec;
mod inner;
mod seq_inner;

#[allow(dead_code)]
trait IntoOwned {
fn into_owned(self) -> Self;
}
Expand Down
14 changes: 0 additions & 14 deletions protocols/v2/noise-sv2/src/cipher_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,6 @@ where
res
}

fn into_aesg(mut self) -> Option<Cipher<Aes256Gcm>> {
#[allow(clippy::clone_on_copy)]
let k = self.get_k().clone()?;
let c = Aes256Gcm::from_key(k);
Some(Cipher::from_cipher(c))
}

fn into_chacha(mut self) -> Option<Cipher<ChaCha20Poly1305>> {
#[allow(clippy::clone_on_copy)]
let k = self.get_k().clone()?;
let c = ChaCha20Poly1305::from_key(k);
Some(Cipher::from_cipher(c))
}

// Encrypts the provided `data` in place using the cipher and AAD (`ad`).
//
// Performs authenticated encryption on the provided `data` buffer, modifying it in place to
Expand Down
21 changes: 1 addition & 20 deletions protocols/v2/noise-sv2/src/handshake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@
use crate::{aed_cipher::AeadCipher, cipher_state::CipherState, NOISE_HASHED_PROTOCOL_NAME_CHACHA};
use chacha20poly1305::ChaCha20Poly1305;
use secp256k1::{
ecdh::SharedSecret,
hashes::{sha256::Hash as Sha256Hash, Hash},
rand, Keypair, Secp256k1, SecretKey, XOnlyPublicKey,
rand, Keypair, Secp256k1
};

// Represents the operations needed during a Noise protocol handshake.
Expand Down Expand Up @@ -166,17 +165,6 @@ pub trait HandshakeOp<Cipher: AeadCipher>: CipherState<Cipher> {
(out_1, out_2)
}

fn hkdf_3(
chaining_key: &[u8; 32],
input_key_material: &[u8],
) -> ([u8; 32], [u8; 32], [u8; 32]) {
let temp_key = Self::hmac_hash(chaining_key, input_key_material);
let out_1 = Self::hmac_hash(&temp_key, &[0x1]);
let out_2 = Self::hmac_hash(&temp_key, &[&out_1[..], &[0x2][..]].concat());
let out_3 = Self::hmac_hash(&temp_key, &[&out_2[..], &[0x3][..]].concat());
(out_1, out_2, out_3)
}

// Mixes the input key material into the current chaining key (`ck`) and initializes the
// handshake cipher with an updated encryption key (`k`).
//
Expand Down Expand Up @@ -229,13 +217,6 @@ pub trait HandshakeOp<Cipher: AeadCipher>: CipherState<Cipher> {
Ok(())
}

fn ecdh(private: &[u8], public: &[u8]) -> [u8; 32] {
let private = SecretKey::from_slice(private).expect("Wrong key");
let x_public = XOnlyPublicKey::from_slice(public).expect("Wrong key");
let res = SharedSecret::new(&x_public.public_key(crate::PARITY), &private);
res.secret_bytes()
}

// Initializes the handshake state by setting the initial chaining key (`ck`) and handshake
// hash (`h`).
//
Expand Down
1 change: 1 addition & 0 deletions protocols/v2/roles-logic-sv2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ with_serde = [ "serde",
prop_test = ["template_distribution_sv2/prop_test"]
# Code coverage tools may conflict with the nopanic logic, so we can disable it when needed
disable_nopanic = []
cargo-clippy = []

[package.metadata.docs.rs]
all-features = true
6 changes: 0 additions & 6 deletions protocols/v2/roles-logic-sv2/src/job_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ struct DownstreamJob {
extended_job_id: u32,
}

#[derive(Debug)]
struct ExtendedJobs {
#[allow(dead_code)]
upstream_target: Vec<u8>,
}

/// Used by proxies to keep track of standard jobs in the group channel
/// created with the sv2 server
#[derive(Debug)]
Expand Down
14 changes: 7 additions & 7 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/jd-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ path = "src/lib/mod.rs"
stratum-common = { version = "1.0.0", path = "../../common" }
async-channel = "1.5.1"
async-recursion = "0.3.2"
binary_sv2 = { version = "^1.0.0", path = "../../protocols/v2/binary-sv2/binary-sv2" }
binary_sv2 = { version = "^2.0.0", path = "../../protocols/v2/binary-sv2/binary-sv2" }
buffer_sv2 = { version = "^1.0.0", path = "../../utils/buffer" }
codec_sv2 = { version = "^1.0.1", path = "../../protocols/v2/codec-sv2", features = ["noise_sv2", "with_buffer_pool"] }
framing_sv2 = { version = "^2.0.0", path = "../../protocols/v2/framing-sv2" }
network_helpers_sv2 = { version = "2.0.0", path = "../roles-utils/network-helpers", features=["with_tokio", "with_buffer_pool"] }
roles_logic_sv2 = { version = "^2.0.0", path = "../../protocols/v2/roles-logic-sv2" }
roles_logic_sv2 = { version = "^3.0.0", path = "../../protocols/v2/roles-logic-sv2" }
serde = { version = "1.0.89", default-features = false, features = ["derive", "alloc"] }
futures = "0.3.25"
tokio = { version = "1", features = ["full"] }
Expand Down
16 changes: 1 addition & 15 deletions roles/jd-client/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use roles_logic_sv2::{
handlers::{job_declaration::ParseServerJobDeclarationMessages, SendTo_},
job_declaration_sv2::{
AllocateMiningJobTokenSuccess, DeclareMiningJobError, DeclareMiningJobSuccess,
IdentifyTransactions, IdentifyTransactionsSuccess, ProvideMissingTransactions,
ProvideMissingTransactionsSuccess,
ProvideMissingTransactions, ProvideMissingTransactionsSuccess,
},
parsers::JobDeclaration,
};
Expand Down Expand Up @@ -36,19 +35,6 @@ impl ParseServerJobDeclarationMessages for JobDeclarator {
Ok(SendTo::None(None))
}

fn handle_identify_transactions(
&mut self,
message: IdentifyTransactions,
) -> Result<SendTo, Error> {
let message_identify_transactions = IdentifyTransactionsSuccess {
request_id: message.request_id,
tx_data_hashes: Vec::new().into(),
};
let message_enum =
JobDeclaration::IdentifyTransactionsSuccess(message_identify_transactions);
Ok(SendTo::Respond(message_enum))
}

fn handle_provide_missing_transactions(
&mut self,
message: ProvideMissingTransactions,
Expand Down
21 changes: 9 additions & 12 deletions roles/jd-client/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use roles_logic_sv2::{
mining_sv2::SubmitSharesExtended,
parsers::{JobDeclaration, PoolMessages},
template_distribution_sv2::SetNewPrevHash,
utils::{hash_lists_tuple, Mutex},
utils::Mutex,
};
use std::{collections::HashMap, convert::TryInto, str::FromStr};
use stratum_common::bitcoin::{util::psbt::serialize::Deserialize, Transaction};
Expand Down Expand Up @@ -240,21 +240,20 @@ impl JobDeclarator {
self_mutex: &Arc<Mutex<Self>>,
template: NewTemplate<'static>,
token: Vec<u8>,
tx_list_: Seq064K<'static, B016M<'static>>,
tx_list: Seq064K<'static, B016M<'static>>,
excess_data: B064K<'static>,
coinbase_pool_output: Vec<u8>,
) {
let (id, _, sender) = self_mutex
.safe_lock(|s| (s.req_ids.next(), s.min_extranonce_size, s.sender.clone()))
.unwrap();
// TODO: create right nonce
let tx_short_hash_nonce = 0;
let mut tx_list: Vec<Transaction> = Vec::new();
for tx in tx_list_.to_vec() {
//TODO remove unwrap
let mut tx_ids = vec![];
for tx in tx_list.to_vec() {

Check warning on line 251 in roles/jd-client/src/lib/job_declarator/mod.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-client/src/lib/job_declarator/mod.rs#L250-L251

Added lines #L250 - L251 were not covered by tests
let tx = Transaction::deserialize(&tx).unwrap();
tx_list.push(tx);
let id: U256 = tx.txid().to_vec().try_into().unwrap();
tx_ids.push(id);

Check warning on line 254 in roles/jd-client/src/lib/job_declarator/mod.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-client/src/lib/job_declarator/mod.rs#L253-L254

Added lines #L253 - L254 were not covered by tests
}
let tx_ids: Seq064K<'static, U256> = Seq064K::from(tx_ids);

Check warning on line 256 in roles/jd-client/src/lib/job_declarator/mod.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-client/src/lib/job_declarator/mod.rs#L256

Added line #L256 was not covered by tests
let declare_job = DeclareMiningJob {
request_id: id,
mining_job_token: token.try_into().unwrap(),
Expand All @@ -265,16 +264,14 @@ impl JobDeclarator {
coinbase_suffix: self_mutex
.safe_lock(|s| s.coinbase_tx_suffix.clone())
.unwrap(),
tx_short_hash_nonce,
tx_short_hash_list: hash_lists_tuple(tx_list.clone(), tx_short_hash_nonce).0,
tx_hash_list_hash: hash_lists_tuple(tx_list.clone(), tx_short_hash_nonce).1,
tx_list: tx_ids,
excess_data, // request transaction data
};
let last_declare = LastDeclareJob {
declare_job: declare_job.clone(),
template,
coinbase_pool_output,
tx_list: tx_list_.clone(),
tx_list,
};
Self::update_last_declare_job_sent(self_mutex, id, last_declare);
let frame: StdFrame =
Expand Down
6 changes: 3 additions & 3 deletions roles/jd-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ path = "src/lib/mod.rs"
[dependencies]
stratum-common = { version = "1.0.0", path = "../../common" }
async-channel = "1.5.1"
binary_sv2 = { version = "^1.0.0", path = "../../protocols/v2/binary-sv2/binary-sv2" }
binary_sv2 = { version = "^2.0.0", path = "../../protocols/v2/binary-sv2/binary-sv2" }
buffer_sv2 = { version = "^1.0.0", path = "../../utils/buffer" }
codec_sv2 = { version = "^1.0.1", path = "../../protocols/v2/codec-sv2", features = ["noise_sv2"] }
const_sv2 = { version = "^2.0.0", path = "../../protocols/v2/const-sv2" }
const_sv2 = { version = "^3.0.0", path = "../../protocols/v2/const-sv2" }
network_helpers_sv2 = { version = "2.0.0", path = "../roles-utils/network-helpers", features = ["with_tokio"] }
noise_sv2 = { version = "1.1.0", path = "../../protocols/v2/noise-sv2" }
rand = "0.8.4"
roles_logic_sv2 = { version = "^2.0.0", path = "../../protocols/v2/roles-logic-sv2" }
roles_logic_sv2 = { version = "^3.0.0", path = "../../protocols/v2/roles-logic-sv2" }
tokio = { version = "1", features = ["full"] }
ext-config = { version = "0.14.0", features = ["toml"], package = "config" }
tracing = { version = "0.1" }
Expand Down
Loading

0 comments on commit 8e4bf35

Please sign in to comment.