Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ed05143
Add macros instead of handwritten impls
Shourya742 Jun 19, 2026
143c309
Implement conversion from buffer_pool slice to a decodable field
Shourya742 Jun 19, 2026
9e397d7
Correct the semantics around size_hint for fixed type
Shourya742 Jun 19, 2026
14e0be6
improve the ergonomics around sv2 list primitives and make sure it do…
Shourya742 Jun 19, 2026
058e398
have a helper for binary to hex and remove duplication from display t…
Shourya742 Jun 19, 2026
4484d44
update sv2_to_sv1 to not use list type internal representation
Shourya742 Jun 19, 2026
51904c3
remove from_vec and from_vec_unchecked
Shourya742 Jun 19, 2026
8aa1e51
Correct payload size calculation for fixed types
Shourya742 Jun 19, 2026
20fc62a
Make sure from_reader doesn't read_to_end and follow a conservative p…
Shourya742 Jun 19, 2026
0638148
decode now returns an error instead of using unchecked conversions
Shourya742 Jun 19, 2026
97c3d41
remove unchecked implementation
Shourya742 Jun 19, 2026
5aee7e6
Expose better surface API's and remove old redundant API's from binar…
Shourya742 Jun 20, 2026
5edc690
Remove u32AsRef as its not part of specs and not really helpful
Shourya742 Jun 22, 2026
2bd2c63
remove unused error types
Shourya742 Jun 22, 2026
0f19f08
Add MAC sv2 primitive
Shourya742 Jun 22, 2026
64a1403
Remove duplicate decode_owned
Shourya742 Jun 22, 2026
dc5c0fe
xRemove u8Owned
Shourya742 Jun 22, 2026
88b8d11
Remove u256_from_int and commented import and make sure not to vec it…
Shourya742 Jun 22, 2026
f57772f
Remove into_owned
Shourya742 Jun 22, 2026
6093a7a
Make sure writer doesn't end up in recursion loop
Shourya742 Jun 22, 2026
627aa94
Remove from bytes and copy_from_slice methdos
Shourya742 Jun 22, 2026
07b0f31
Remove redundant imports
Shourya742 Jun 22, 2026
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
4 changes: 1 addition & 3 deletions fuzz/fuzz_targets/end_to_end_serialization_for_datatypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod common;

use binary_sv2::{
Decodable, Encodable, GetSize, PubKey, Seq0255, Seq064K, Signature, Str0255, Sv2Option,
U32AsRef, B016M, B0255, B032, B064K, U24, U256,
B016M, B0255, B032, B064K, U24, U256,
};
use libfuzzer_sys::fuzz_target;

Expand All @@ -14,7 +14,6 @@ enum FuzzInput {
PubKey(Vec<u8>),
Signature(Vec<u8>),
Str0255(Vec<u8>),
U32AsRef(Vec<u8>),
B016M(Vec<u8>),
B0255(Vec<u8>),
B032(Vec<u8>),
Expand Down Expand Up @@ -77,7 +76,6 @@ fuzz_target!(|input: FuzzInput| {
FuzzInput::PubKey(data) => test_datatype_roundtrip!(PubKey, data),
FuzzInput::Signature(data) => test_datatype_roundtrip!(Signature, data),
FuzzInput::Str0255(data) => test_datatype_roundtrip!(Str0255, data),
FuzzInput::U32AsRef(data) => test_datatype_roundtrip!(U32AsRef, data),
FuzzInput::B016M(data) => test_datatype_roundtrip!(B016M, data),
FuzzInput::B0255(data) => test_datatype_roundtrip!(B0255, data),
FuzzInput::B032(data) => test_datatype_roundtrip!(B032, data),
Expand Down
10 changes: 5 additions & 5 deletions stratum-core/stratum-translation/src/sv2_to_sv1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ pub fn build_sv1_notify_from_sv2(
clean_jobs: bool,
) -> Result<server_to_client::Notify<'static>> {
let new_job = match try_strip_bip141(
new_job.coinbase_tx_prefix.inner_as_ref(),
new_job.coinbase_tx_suffix.inner_as_ref(),
new_job.coinbase_tx_prefix.as_bytes(),
new_job.coinbase_tx_suffix.as_bytes(),
)
.map_err(StratumTranslationError::FailedToTryToStripBip141)?
{
Expand All @@ -66,9 +66,9 @@ pub fn build_sv1_notify_from_sv2(

let job_id = new_job.job_id.to_string();
let prev_hash = PrevHash(new_prev_hash.prev_hash.clone());
let coin_base1 = new_job.coinbase_tx_prefix.to_vec().into();
let coin_base2 = new_job.coinbase_tx_suffix.to_vec().into();
let merkle_path = new_job.merkle_path.clone().into_static().0;
let coin_base1 = new_job.coinbase_tx_prefix.to_owned_bytes().into();
let coin_base2 = new_job.coinbase_tx_suffix.to_owned_bytes().into();
let merkle_path = new_job.merkle_path.clone().into_static().into_inner();
let merkle_branch: Vec<MerkleNode> = merkle_path.into_iter().map(MerkleNode).collect();
let version = HexU32Be(new_job.version);
let bits = HexU32Be(new_prev_hash.nbits);
Expand Down
8 changes: 4 additions & 4 deletions sv1/src/methods/client_to_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Submit<'_> {

impl From<Submit<'_>> for Message {
fn from(submit: Submit) -> Self {
let ex: String = submit.extra_nonce2.0.inner_as_ref().to_lower_hex_string();
let ex: String = submit.extra_nonce2.0.as_bytes().to_lower_hex_string();
let mut params: Vec<Value> = vec![
submit.user_name.into(),
submit.job_id.into(),
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<'a> TryFrom<Subscribe<'a>> for Message {

fn try_from(subscribe: Subscribe) -> Result<Self, Error> {
let params = match (subscribe.agent_signature, subscribe.extranonce1) {
(a, Some(b)) => vec![a, b.0.inner_as_ref().to_lower_hex_string()],
(a, Some(b)) => vec![a, b.0.as_bytes().to_lower_hex_string()],
(a, None) => vec![a],
};
Ok(Message::StandardRequest(StandardRequest {
Expand Down Expand Up @@ -800,7 +800,7 @@ fn test_subscribe_with_odd_length_extranonce() {
assert_eq!(subscribe.agent_signature, "test-agent");
assert!(subscribe.extranonce1.is_some());
let extranonce = subscribe.extranonce1.unwrap();
assert_eq!(extranonce.0.inner_as_ref(), &[0x0a, 0xbc]); // "0abc" -> [10, 188]
assert_eq!(extranonce.0.as_bytes(), &[0x0a, 0xbc]); // "0abc" -> [10, 188]
}

#[test]
Expand All @@ -816,5 +816,5 @@ fn test_subscribe_with_even_length_extranonce() {
assert_eq!(subscribe.agent_signature, "test-agent");
assert!(subscribe.extranonce1.is_some());
let extranonce = subscribe.extranonce1.unwrap();
assert_eq!(extranonce.0.inner_as_ref(), &[0xab, 0xcd]); // "abcd" -> [171, 205]
assert_eq!(extranonce.0.as_bytes(), &[0xab, 0xcd]); // "abcd" -> [171, 205]
}
47 changes: 28 additions & 19 deletions sv1/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ pub struct Extranonce<'a>(pub B032<'a>);

impl fmt::Display for Extranonce<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0.inner_as_ref().to_lower_hex_string())
f.write_str(&self.0.as_bytes().to_lower_hex_string())
}
}

impl Extranonce<'_> {
pub fn len(&self) -> usize {
self.0.inner_as_ref().len()
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.inner_as_ref().is_empty()
self.0.is_empty()
}
}

Expand All @@ -37,7 +37,7 @@ impl<'a> TryFrom<Vec<u8>> for Extranonce<'a> {

impl<'a> From<Extranonce<'a>> for Vec<u8> {
fn from(v: Extranonce<'a>) -> Self {
v.0.to_vec()
v.0.into_bytes()
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl<'a> TryFrom<&str> for Extranonce<'a> {

impl<'a> From<Extranonce<'a>> for String {
fn from(bytes: Extranonce<'a>) -> String {
bytes.0.inner_as_ref().to_lower_hex_string()
bytes.0.as_bytes().to_lower_hex_string()
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ impl fmt::Display for PrevHash<'_> {

impl<'a> From<PrevHash<'a>> for Vec<u8> {
fn from(p_hash: PrevHash<'a>) -> Self {
p_hash.0.to_vec()
p_hash.0.into_bytes()
}
}

Expand Down Expand Up @@ -226,9 +226,19 @@ impl<'a> TryFrom<&str> for PrevHash<'a> {
}
Ok(PrevHash(prev_hash_arr.into()))
}
_ => Err(error::Error::BadBytesConvert(
binary_sv2::Error::InvalidU256(prev_hash_stratum_order.len()),
)),
_ => {
let len = prev_hash_stratum_order.len();
Err(error::Error::BadBytesConvert(
binary_sv2::Error::ValueExceedsMaxSize(
true,
32,
0,
0,
prev_hash_stratum_order,
len,
),
))
}
}
}
}
Expand All @@ -245,7 +255,7 @@ impl From<PrevHash<'_>> for String {
fn from(v: PrevHash) -> Self {
let mut prev_hash_stratum_cursor = std::io::Cursor::new(Vec::new());
// swap every u32 from little endian to big endian
for chunk in v.0.inner_as_ref().chunks(size_of::<u32>()) {
for chunk in v.0.as_bytes().chunks(size_of::<u32>()) {
let prev_hash_word = LittleEndian::read_u32(chunk);
prev_hash_stratum_cursor
.write_u32::<BigEndian>(prev_hash_word)
Expand All @@ -258,7 +268,7 @@ impl From<PrevHash<'_>> for String {
// / Referencing the internal part of hex bytes
impl AsRef<[u8]> for PrevHash<'_> {
fn as_ref(&self) -> &[u8] {
self.0.inner_as_ref()
self.0.as_bytes()
}
}

Expand All @@ -272,7 +282,7 @@ impl<'a> AsRef<U256<'a>> for PrevHash<'a> {
/// Referencing the internal part of hex bytes
impl AsRef<[u8]> for Extranonce<'_> {
fn as_ref(&self) -> &[u8] {
self.0.inner_as_ref()
self.0.as_bytes()
}
}

Expand All @@ -281,13 +291,13 @@ pub struct MerkleNode<'a>(pub U256<'a>);

impl fmt::Display for MerkleNode<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.inner_as_ref().to_lower_hex_string())
write!(f, "{}", self.0.as_bytes().to_lower_hex_string())
}
}

impl MerkleNode<'_> {
pub fn is_empty(&self) -> bool {
self.0.inner_as_ref().is_empty()
self.0.is_empty()
}
}

Expand All @@ -302,7 +312,7 @@ impl<'a> TryFrom<Vec<u8>> for MerkleNode<'a> {

impl<'a> From<MerkleNode<'a>> for Vec<u8> {
fn from(v: MerkleNode<'a>) -> Self {
v.0.to_vec()
v.0.into_bytes()
}
}

Expand All @@ -315,7 +325,7 @@ impl<'a> From<MerkleNode<'a>> for Value {
/// Referencing the internal part of hex bytes
impl AsRef<[u8]> for MerkleNode<'_> {
fn as_ref(&self) -> &[u8] {
self.0.inner_as_ref()
self.0.as_bytes()
}
}

Expand All @@ -330,7 +340,7 @@ impl<'a> TryFrom<&str> for MerkleNode<'a> {

impl<'a> From<MerkleNode<'a>> for String {
fn from(bytes: MerkleNode<'a>) -> String {
bytes.0.inner_as_ref().to_lower_hex_string()
bytes.0.as_bytes().to_lower_hex_string()
}
}

Expand Down Expand Up @@ -432,8 +442,7 @@ mod tests {
let value_to_string = back_to_hex_value.as_str().unwrap();

let chunk_size: usize = size_of::<u32>();
let me_chunks = me.clone().0.to_vec();
let me_chunks = me_chunks.chunks(chunk_size);
let me_chunks = me.0.as_bytes().chunks(chunk_size);
for (be_chunk, le_chunk) in bytes.clone().chunks(chunk_size).zip(me_chunks) {
let le_chunk = [le_chunk[0], le_chunk[1], le_chunk[2], le_chunk[3]];
let be_chunk = [be_chunk[0], be_chunk[1], be_chunk[2], be_chunk[3]];
Expand Down
4 changes: 1 addition & 3 deletions sv2/binary-sv2/examples/encode_decode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use binary_sv2::{from_bytes, to_bytes, U24};
pub use binary_sv2::{Decodable as Deserialize, Encodable as Serialize};
use binary_sv2::{from_bytes, to_bytes, Deserialize, Serialize, U24};
use core::convert::TryInto;
pub use derive_codec_sv2::{Decodable as Deserialize, Encodable as Serialize};

// The `Test` struct is expanded using the `Deserialize` and `Serialize` procedural macros.
// These macros provide the necessary methods for serializing and deserializing the struct.
Expand Down
Loading
Loading