Skip to content

Commit

Permalink
remove bitcoin dep to avoid alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesFoundation committed Aug 19, 2024
1 parent f07d920 commit 60b6549
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
3 changes: 1 addition & 2 deletions stratum-v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
99 changes: 52 additions & 47 deletions stratum-v1/src/client/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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
);
}
Expand Down Expand Up @@ -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
};
Expand All @@ -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,
},
})
Expand Down Expand Up @@ -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,
}
})
Expand All @@ -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,
}
})
Expand All @@ -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,
}
})
Expand All @@ -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,
}
})
Expand All @@ -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,
}
})
Expand Down

0 comments on commit 60b6549

Please sign in to comment.