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
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 @@ -343,7 +343,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 @@ -809,7 +809,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 @@ -825,7 +825,7 @@ 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]
}

#[test]
Expand Down
47 changes: 28 additions & 19 deletions sv1/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,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 @@ -36,7 +36,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 @@ -80,7 +80,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 @@ -170,7 +170,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 @@ -199,9 +199,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 @@ -218,7 +228,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 @@ -231,7 +241,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 @@ -245,7 +255,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 @@ -254,13 +264,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 @@ -275,7 +285,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 @@ -288,7 +298,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 @@ -303,7 +313,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 @@ -381,8 +391,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