From 60b65490134966b8c0c3e38a1f4e966c8b1a10da Mon Sep 17 00:00:00 2001 From: Georges Palauqui Date: Mon, 19 Aug 2024 10:05:30 +0200 Subject: [PATCH] remove bitcoin dep to avoid alloc --- stratum-v1/Cargo.toml | 3 +- stratum-v1/src/client/job.rs | 99 +++++++++++++++++++----------------- 2 files changed, 53 insertions(+), 49 deletions(-) diff --git a/stratum-v1/Cargo.toml b/stratum-v1/Cargo.toml index c735406..4b247d0 100644 --- a/stratum-v1/Cargo.toml +++ b/stratum-v1/Cargo.toml @@ -13,8 +13,7 @@ name = "stratum-v1" version = "0.1.0" [dependencies] -bitcoin = { version = "0.32", default-features = false } -bitcoin_hashes = { workspace = true } +bitcoin_hashes = { workspace = true, default-features = false } defmt = { workspace = true, optional = true } derive_more = { workspace = true, features = ["from"] } embedded-io-async = { workspace = true } diff --git a/stratum-v1/src/client/job.rs b/stratum-v1/src/client/job.rs index 804d5e1..85560cb 100644 --- a/stratum-v1/src/client/job.rs +++ b/stratum-v1/src/client/job.rs @@ -4,14 +4,19 @@ use super::notification::Work; use crate::{Error, Result}; -use bitcoin::{ - block::{Header, Version}, - hash_types::{BlockHash, TxMerkleNode}, - hashes::{sha256d::Hash as DHash, Hash}, - CompactTarget, -}; +use bitcoin_hashes::{sha256d::Hash as DHash, Hash}; use heapless::{String, Vec}; +#[derive(Debug, PartialEq)] +pub struct Header { + pub version: i32, + pub prev_blockhash: [u8; 32], + pub merkle_root: [u8; 32], + pub ntime: u32, + pub nbits: u32, + pub nonce: u32, +} + #[derive(Debug, PartialEq)] pub struct Job { pub job_id: String<32>, @@ -27,11 +32,11 @@ impl defmt::Format for Job { "Job {{ job_id: {}, extranonce2: {:?}, header: {{ version: {:x}, prev_block_hash: {:x}, merkle_root: {:x}, ntime: {:x}, nbits: {:x}, nonce: {:x} }} }}", self.job_id, self.extranonce2, - self.header.version.to_consensus(), - self.header.prev_blockhash.to_byte_array(), - self.header.merkle_root.to_byte_array(), - self.header.time, - self.header.bits.to_consensus(), + self.header.version, + self.header.prev_blockhash, + self.header.merkle_root, + self.header.ntime, + self.header.nbits, self.header.nonce ); } @@ -111,7 +116,7 @@ impl JobCreator { self.version_bits = self.version_bits.wrapping_add(1); (work.version & !self.version_mask) | (((self.version_bits as i32) << self.version_mask.trailing_zeros()) - & self.version_mask) // TODO: test this + & self.version_mask) } else { work.version }; @@ -136,11 +141,11 @@ impl JobCreator { job_id: work.job_id.clone(), extranonce2: self.extranonce2.clone(), header: Header { - version: Version::from_consensus(rolled_version), - prev_blockhash: BlockHash::from_byte_array(work.prev_hash), - merkle_root: TxMerkleNode::from_byte_array(self.merkle_root(work)?), - time: rolled_ntime, - bits: CompactTarget::from_consensus(work.nbits), + version: rolled_version, + prev_blockhash: work.prev_hash, + merkle_root: self.merkle_root(work)?, + ntime: rolled_ntime, + nbits: work.nbits, nonce: 0, }, }) @@ -179,15 +184,15 @@ mod tests { job_id: job_id.clone(), extranonce2: hvec!(u8, 8, &[0]), header: Header { - version: Version::from_consensus(0x2000_0000), - prev_blockhash: BlockHash::from_byte_array([0; 32]), - merkle_root: TxMerkleNode::from_byte_array([ + version: 0x2000_0000, + prev_blockhash: [0; 32], + merkle_root: [ 0x14, 0x06, 0xe0, 0x58, 0x81, 0xe2, 0x99, 0x36, 0x77, 0x66, 0xd3, 0x13, 0xe2, 0x6c, 0x05, 0x56, 0x4e, 0xc9, 0x1b, 0xf7, 0x21, 0xd3, 0x17, 0x26, 0xbd, 0x6e, 0x46, 0xe6, 0x06, 0x89, 0x53, 0x9a, - ]), - time: 0, - bits: CompactTarget::from_consensus(0x1234_5678), + ], + ntime: 0, + nbits: 0x1234_5678, nonce: 0, } }) @@ -199,15 +204,15 @@ mod tests { job_id: job_id.clone(), extranonce2: hvec!(u8, 8, &[0]), header: Header { - version: Version::from_consensus(0x2000_2000), - prev_blockhash: BlockHash::from_byte_array([0; 32]), - merkle_root: TxMerkleNode::from_byte_array([ + version: 0x2000_2000, + prev_blockhash: [0; 32], + merkle_root: [ 0x14, 0x06, 0xe0, 0x58, 0x81, 0xe2, 0x99, 0x36, 0x77, 0x66, 0xd3, 0x13, 0xe2, 0x6c, 0x05, 0x56, 0x4e, 0xc9, 0x1b, 0xf7, 0x21, 0xd3, 0x17, 0x26, 0xbd, 0x6e, 0x46, 0xe6, 0x06, 0x89, 0x53, 0x9a, - ]), - time: 0, - bits: CompactTarget::from_consensus(0x1234_5678), + ], + ntime: 0, + nbits: 0x1234_5678, nonce: 0, } }) @@ -219,15 +224,15 @@ mod tests { job_id: job_id.clone(), extranonce2: hvec!(u8, 8, &[0]), header: Header { - version: Version::from_consensus(0x2000_4000), - prev_blockhash: BlockHash::from_byte_array([0; 32]), - merkle_root: TxMerkleNode::from_byte_array([ + version: 0x2000_4000, + prev_blockhash: [0; 32], + merkle_root: [ 0x14, 0x06, 0xe0, 0x58, 0x81, 0xe2, 0x99, 0x36, 0x77, 0x66, 0xd3, 0x13, 0xe2, 0x6c, 0x05, 0x56, 0x4e, 0xc9, 0x1b, 0xf7, 0x21, 0xd3, 0x17, 0x26, 0xbd, 0x6e, 0x46, 0xe6, 0x06, 0x89, 0x53, 0x9a, - ]), - time: 1, - bits: CompactTarget::from_consensus(0x1234_5678), + ], + ntime: 1, + nbits: 0x1234_5678, nonce: 0, } }) @@ -239,15 +244,15 @@ mod tests { job_id: job_id.clone(), extranonce2: hvec!(u8, 8, &[1]), header: Header { - version: Version::from_consensus(0x2000_6000), - prev_blockhash: BlockHash::from_byte_array([0; 32]), - merkle_root: TxMerkleNode::from_byte_array([ + version: 0x2000_6000, + prev_blockhash: [0; 32], + merkle_root: [ 0x9c, 0x12, 0xcf, 0xdc, 0x04, 0xc7, 0x45, 0x84, 0xd7, 0x87, 0xac, 0x3d, 0x23, 0x77, 0x21, 0x32, 0xc1, 0x85, 0x24, 0xbc, 0x7a, 0xb2, 0x8d, 0xec, 0x42, 0x19, 0xb8, 0xfc, 0x5b, 0x42, 0x5f, 0x70, - ]), - time: 2, - bits: CompactTarget::from_consensus(0x1234_5678), + ], + ntime: 2, + nbits: 0x1234_5678, nonce: 0, } }) @@ -271,15 +276,15 @@ mod tests { job_id: job_id.clone(), extranonce2: hvec!(u8, 8, &[1]), header: Header { - version: Version::from_consensus(0x2000_2000), - prev_blockhash: BlockHash::from_byte_array([0; 32]), - merkle_root: TxMerkleNode::from_byte_array([ + version: 0x2000_2000, + prev_blockhash: [0; 32], + merkle_root: [ 0x9c, 0x12, 0xcf, 0xdc, 0x04, 0xc7, 0x45, 0x84, 0xd7, 0x87, 0xac, 0x3d, 0x23, 0x77, 0x21, 0x32, 0xc1, 0x85, 0x24, 0xbc, 0x7a, 0xb2, 0x8d, 0xec, 0x42, 0x19, 0xb8, 0xfc, 0x5b, 0x42, 0x5f, 0x70, - ]), - time: 1, - bits: CompactTarget::from_consensus(0x1234_5678), + ], + ntime: 1, + nbits: 0x1234_5678, nonce: 0, } })