Skip to content

Commit

Permalink
Merge pull request stratum-mining#150 from Fi3/sv2-tp-crates
Browse files Browse the repository at this point in the history
SpecialMerge from main
  • Loading branch information
Fi3 authored May 10, 2022
2 parents b39711c + c41b561 commit 1aaeadf
Show file tree
Hide file tree
Showing 21 changed files with 99 additions and 67 deletions.
4 changes: 2 additions & 2 deletions protocols/v2/binary-sv2/binary-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub fn clone_message<T: Serialize>(_: T) -> T {
pub fn u256_from_int<V: Into<u64>>(value: V) -> U256<'static> {
let mut u256 = vec![0_u8; 24];
let val: u64 = value.into();
for v in val.to_le_bytes() {
u256.push(v)
for v in &(val.to_le_bytes()) {
u256.push(*v)
}
let u256: U256 = u256.try_into().unwrap();
u256
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::codec::{GetSize, SizeHint};
use crate::datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256};
use crate::Error;
use crate::{
codec::{GetSize, SizeHint},
datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256},
Error,
};
use alloc::vec::Vec;
#[cfg(not(feature = "no_std"))]
use std::io::{Cursor, Read};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::codec::GetSize;
use crate::datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256};
use crate::Error;
use crate::{
codec::GetSize,
datatypes::{Bytes, Signature, Sv2DataType, B016M, B0255, B032, B064K, U24, U256},
Error,
};
use alloc::vec::Vec;
#[cfg(not(feature = "no_std"))]
use std::io::{Error as E, Write};
Expand Down
14 changes: 9 additions & 5 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/impls.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::codec::decodable::{
Decodable, DecodableField, DecodablePrimitive, FieldMarker, GetMarker, PrimitiveMarker,
use crate::{
codec::{
decodable::{
Decodable, DecodableField, DecodablePrimitive, FieldMarker, GetMarker, PrimitiveMarker,
},
encodable::{EncodableField, EncodablePrimitive},
},
datatypes::*,
Error,
};
use crate::codec::encodable::{EncodableField, EncodablePrimitive};
use crate::datatypes::*;
use crate::Error;
use alloc::vec::Vec;
use core::convert::{TryFrom, TryInto};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Copy data types
use crate::codec::Fixed;
use crate::datatypes::Sv2DataType;
use crate::Error;
use crate::{codec::Fixed, datatypes::Sv2DataType, Error};
use core::convert::{TryFrom, TryInto};

#[cfg(not(feature = "no_std"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::codec::{GetSize, SizeHint};
use crate::Error;
use crate::{
codec::{GetSize, SizeHint},
Error,
};
mod non_copy_data_types;

mod copy_data_types;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::IntoOwned;
use crate::codec::{GetSize, SizeHint};
use crate::datatypes::Sv2DataType;
use crate::Error;
use crate::{
codec::{GetSize, SizeHint},
datatypes::Sv2DataType,
Error,
};
use core::convert::TryFrom;

#[cfg(not(feature = "no_std"))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::codec::decodable::{Decodable, DecodableField, FieldMarker, GetMarker, PrimitiveMarker};
use crate::codec::encodable::{EncodableField, EncodablePrimitive};
use crate::codec::Fixed;
use crate::codec::GetSize;
use crate::datatypes::Sv2DataType;
use crate::datatypes::*;
use crate::Error;
use crate::{
codec::{
decodable::{Decodable, DecodableField, FieldMarker, GetMarker, PrimitiveMarker},
encodable::{EncodableField, EncodablePrimitive},
Fixed, GetSize,
},
datatypes::{Sv2DataType, *},
Error,
};
use core::marker::PhantomData;

// TODO add test for that implement also with serde!!!!
Expand Down
16 changes: 7 additions & 9 deletions protocols/v2/binary-sv2/no-serde-sv2/codec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ pub use datatypes::{
U256,
};

pub use crate::codec::decodable::Decodable;
pub use crate::codec::encodable::{Encodable, EncodableField};
pub use crate::codec::GetSize;
pub use crate::codec::SizeHint;
pub use crate::codec::{
decodable::Decodable,
encodable::{Encodable, EncodableField},
GetSize, SizeHint,
};

#[allow(clippy::wrong_self_convention)]
pub fn to_bytes<T: Encodable + GetSize>(src: T) -> Result<Vec<u8>, Error> {
Expand All @@ -56,15 +57,12 @@ pub fn from_bytes<'a, T: Decodable<'a>>(data: &'a mut [u8]) -> Result<T, Error>
}

pub mod decodable {
pub use crate::codec::decodable::Decodable;
pub use crate::codec::decodable::DecodableField;
pub use crate::codec::decodable::FieldMarker;
pub use crate::codec::decodable::{Decodable, DecodableField, FieldMarker};
//pub use crate::codec::decodable::PrimitiveMarker;
}

pub mod encodable {
pub use crate::codec::encodable::Encodable;
pub use crate::codec::encodable::EncodableField;
pub use crate::codec::encodable::{Encodable, EncodableField};
}

#[macro_use]
Expand Down
14 changes: 9 additions & 5 deletions protocols/v2/codec-sv2/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ use binary_sv2::Deserialize;
use binary_sv2::GetSize;
use binary_sv2::Serialize;
use core::marker::PhantomData;
use framing_sv2::framing2::{EitherFrame, Frame as F_, Sv2Frame};
#[cfg(feature = "noise_sv2")]
use framing_sv2::framing2::{HandShakeFrame, NoiseFrame};
use framing_sv2::header::Header;
#[cfg(feature = "noise_sv2")]
use framing_sv2::header::NoiseHeader;

use crate::buffer::{Buffer, SlowAndCorrect};
use crate::error::{Error, Result};
use framing_sv2::{
framing2::{EitherFrame, Frame as F_, Sv2Frame},
header::Header,
};

use crate::{
buffer::{Buffer, SlowAndCorrect},
error::{Error, Result},
};
#[cfg(feature = "noise_sv2")]
use crate::{State, TransportMode};

Expand Down
3 changes: 1 addition & 2 deletions protocols/v2/codec-sv2/src/encoder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::vec::Vec;
use binary_sv2::GetSize;
use binary_sv2::Serialize;
use binary_sv2::{GetSize, Serialize};
#[cfg(feature = "noise_sv2")]
use core::cmp::min;
#[cfg(feature = "noise_sv2")]
Expand Down
3 changes: 1 addition & 2 deletions protocols/v2/codec-sv2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ mod error;

pub use error::Error;

pub use decoder::StandardEitherFrame;
pub use decoder::StandardSv2Frame;
pub use decoder::{StandardEitherFrame, StandardSv2Frame};

pub use decoder::StandardDecoder;
#[cfg(feature = "noise_sv2")]
Expand Down
8 changes: 3 additions & 5 deletions protocols/v2/framing-sv2/src/framing2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::header::Header;
use crate::header::NoiseHeader;
use crate::header::{Header, NoiseHeader};
use alloc::vec::Vec;
use binary_sv2::Serialize;
use binary_sv2::{to_writer, GetSize};
use binary_sv2::{to_writer, GetSize, Serialize};
use core::convert::TryFrom;

const NOISE_MAX_LEN: usize = const_sv2::NOISE_FRAME_MAX_SIZE;
Expand Down Expand Up @@ -57,7 +55,7 @@ pub trait Frame<'a, T: Serialize + GetSize>: Sized {
) -> Option<Self>;
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Sv2Frame<T, B> {
header: Header,
payload: Option<T>,
Expand Down
3 changes: 1 addition & 2 deletions protocols/v2/framing-sv2/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use alloc::vec::Vec;
#[cfg(not(feature = "with_serde"))]
use binary_sv2::binary_codec_sv2;
use binary_sv2::U24;
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Deserialize, Serialize, U24};
use core::convert::TryInto;

#[derive(Debug, Serialize, Deserialize, Copy, Clone)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#[cfg(not(feature = "with_serde"))]
use alloc::vec::Vec;
use binary_sv2::Str0255;
#[cfg(not(feature = "with_serde"))]
use binary_sv2::{
binary_codec_sv2, binary_codec_sv2::CVec, decodable::DecodableField, decodable::FieldMarker,
free_vec, Error, GetSize,
};
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Deserialize, Serialize, Str0255};
use const_sv2::{
SV2_JOB_DISTR_PROTOCOL_DISCRIMINANT, SV2_JOB_NEG_PROTOCOL_DISCRIMINANT,
SV2_MINING_PROTOCOL_DISCRIMINANT, SV2_TEMPLATE_DISTR_PROTOCOL_DISCRIMINANT,
Expand Down Expand Up @@ -129,6 +128,7 @@ pub struct CSetupConnection {
#[cfg(not(feature = "with_serde"))]
impl<'a> CSetupConnection {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<SetupConnection<'a>, Error> {
let endpoint_host: Str0255 = self.endpoint_host.as_mut_slice().try_into()?;
let vendor: Str0255 = self.vendor.as_mut_slice().try_into()?;
Expand Down Expand Up @@ -236,6 +236,7 @@ pub struct CSetupConnectionError {
#[cfg(not(feature = "with_serde"))]
impl<'a> CSetupConnectionError {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<SetupConnectionError<'a>, Error> {
let error_code: Str0255 = self.error_code.as_mut_slice().try_into()?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use alloc::vec::Vec;
use binary_sv2::binary_codec_sv2::{self, free_vec, free_vec_2, CVec, CVec2};
#[cfg(not(feature = "with_serde"))]
use binary_sv2::Error;
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Seq0255, B0255, B064K, U256};
use binary_sv2::{Deserialize, Seq0255, Serialize, B0255, B064K, U256};
#[cfg(not(feature = "with_serde"))]
use core::convert::TryInto;

Expand Down Expand Up @@ -54,6 +53,24 @@ pub struct NewTemplate<'decoder> {
pub merkle_path: Seq0255<'decoder, U256<'decoder>>,
}

impl<'a> NewTemplate<'a> {
pub fn as_static(&self) -> NewTemplate<'static> {
NewTemplate {
template_id: self.template_id,
future_template: self.future_template,
version: self.version,
coinbase_tx_version: self.coinbase_tx_version,
coinbase_prefix: self.coinbase_prefix.clone().into_static(),
coinbase_tx_input_sequence: self.coinbase_tx_input_sequence,
coinbase_tx_value_remaining: self.coinbase_tx_value_remaining,
coinbase_tx_outputs_count: self.coinbase_tx_outputs_count,
coinbase_tx_outputs: self.coinbase_tx_outputs.clone().into_static(),
coinbase_tx_locktime: self.coinbase_tx_locktime,
merkle_path: self.merkle_path.clone().into_static(),
}
}
}

#[repr(C)]
#[cfg(not(feature = "with_serde"))]
pub struct CNewTemplate {
Expand Down Expand Up @@ -107,6 +124,7 @@ impl<'a> From<NewTemplate<'a>> for CNewTemplate {
#[cfg(not(feature = "with_serde"))]
impl<'a> CNewTemplate {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<NewTemplate<'a>, Error> {
let coinbase_prefix: B0255 = self.coinbase_prefix.as_mut_slice().try_into()?;
let coinbase_tx_outputs: B064K = self.coinbase_tx_outputs.as_mut_slice().try_into()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use alloc::vec::Vec;
use binary_sv2::binary_codec_sv2::{self, free_vec, free_vec_2, CVec, CVec2};
#[cfg(not(feature = "with_serde"))]
use binary_sv2::Error;
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Seq064K, Str0255, B016M, B064K};
use binary_sv2::{Deserialize, Seq064K, Serialize, Str0255, B016M, B064K};
#[cfg(not(feature = "with_serde"))]
use core::convert::TryInto;

Expand Down Expand Up @@ -70,6 +69,7 @@ pub struct CRequestTransactionDataSuccess {
#[cfg(not(feature = "with_serde"))]
impl<'a> CRequestTransactionDataSuccess {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<RequestTransactionDataSuccess<'a>, Error> {
let excess_data: B064K = self.excess_data.as_mut_slice().try_into()?;
let transaction_list_ = self.transaction_list.as_mut_slice();
Expand Down Expand Up @@ -132,6 +132,7 @@ pub struct CRequestTransactionDataError {
#[cfg(not(feature = "with_serde"))]
impl<'a> CRequestTransactionDataError {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<RequestTransactionDataError<'a>, Error> {
let error_code: Str0255 = self.error_code.as_mut_slice().try_into()?;
Ok(RequestTransactionDataError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use alloc::vec::Vec;
use binary_sv2::binary_codec_sv2::{self, free_vec, CVec};
#[cfg(not(feature = "with_serde"))]
use binary_sv2::Error;
use binary_sv2::U256;
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Deserialize, Serialize, U256};
#[cfg(not(feature = "with_serde"))]
use core::convert::TryInto;

Expand Down Expand Up @@ -49,6 +48,7 @@ pub struct CSetNewPrevHash {
#[cfg(not(feature = "with_serde"))]
impl<'a> CSetNewPrevHash {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<SetNewPrevHash<'a>, Error> {
let prev_hash: U256 = self.prev_hash.as_mut_slice().try_into()?;
let target: U256 = self.target.as_mut_slice().try_into()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use alloc::vec::Vec;
use binary_sv2::binary_codec_sv2::{self, free_vec, CVec};
#[cfg(not(feature = "with_serde"))]
use binary_sv2::Error;
use binary_sv2::B064K;
use binary_sv2::{Deserialize, Serialize};
use binary_sv2::{Deserialize, Serialize, B064K};
#[cfg(not(feature = "with_serde"))]
use core::convert::TryInto;

Expand Down Expand Up @@ -48,6 +47,7 @@ pub struct CSubmitSolution {
#[cfg(not(feature = "with_serde"))]
impl<'a> CSubmitSolution {
#[cfg(not(feature = "with_serde"))]
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<SubmitSolution<'a>, Error> {
let coinbase_tx: B064K = self.coinbase_tx.as_mut_slice().try_into()?;

Expand Down
9 changes: 6 additions & 3 deletions protocols/v2/sv2-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ use template_distribution_sv2::{
};

use binary_sv2::{
binary_codec_sv2::CVec, decodable::DecodableField, decodable::FieldMarker,
encodable::EncodableField, from_bytes, Deserialize, Error,
binary_codec_sv2::CVec,
decodable::{DecodableField, FieldMarker},
encodable::EncodableField,
from_bytes, Deserialize, Error,
};

use const_sv2::{
Expand Down Expand Up @@ -144,6 +146,7 @@ impl<'a> From<Sv2Message<'a>> for CSv2Message {
}

impl<'a> CSv2Message {
#[allow(clippy::wrong_self_convention)]
pub fn to_rust_rep_mut(&'a mut self) -> Result<Sv2Message<'a>, Error> {
match self {
CSv2Message::NewTemplate(v) => Ok(Sv2Message::NewTemplate(v.to_rust_rep_mut()?)),
Expand Down Expand Up @@ -309,7 +312,7 @@ pub extern "C" fn new_encoder() -> *mut EncoderWrapper {

#[no_mangle]
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub extern "C" fn free_encoder(encoder: *mut EncoderWrapper) {
pub extern "C" fn flush_encoder(encoder: *mut EncoderWrapper) {
let mut encoder = unsafe { Box::from_raw(encoder) };
encoder.free = true;
Box::into_raw(encoder);
Expand Down
2 changes: 1 addition & 1 deletion protocols/v2/sv2-ffi/sv2.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ bool is_ok(const CResult<CSv2Message, Sv2Error> *cresult);

EncoderWrapper *new_encoder();

void free_encoder(EncoderWrapper *encoder);
void flush_encoder(EncoderWrapper *encoder);

void free_decoder(DecoderWrapper *decoder);

Expand Down

0 comments on commit 1aaeadf

Please sign in to comment.