Binary sv2 cleanup#2200
Conversation
04c7653 to
46da424
Compare
|
Awesome, the test are passing, atleast I have not broken any invariants. Need to improve API ergonomics and should be good to go. |
4a79675 to
07b0f31
Compare
|
I will open issues for all the extra points mentioned in the description. |
|
My clanker found this: |
07b0f31 to
a1f2b6a
Compare
issue: stratum-mining#2214, in binary_sv2 we had try_from, from impls from all primitive types to Encodable and decodable variants which were very repetitive. This commit adds macros to remove repetitions and make the file more sane to look at.
Here we add slices as an encodable field in case buffer_sv2 is enabled, not tracked by an issue.
Size_hint basically provides an idea around how much is expected, initially would just provide the size without checking whether buffer would even satisfy requirements. Now, we make sure buffer len should be adequate otherwise we return read error
…esn't leak internal implementation This one fixes two of the loupe issues and one of the ergonomics issue: 1. stratum-mining#2176 -> Making sure the vector conversion doesn't break length invariant. 2. stratum-mining#2179 -> Making sure sv2 primitives doesn't leak internal representation 3. stratum-mining#2216 -> Adding iterators to seq primitives. This commit also updates sv2_to_sv1 to not use list type internal representation
…rait This is more of a clean up commit, where we remove repetitive hex conv code in primitive's display trait implementation.
This is also a loupe issue: stratum-mining#2171 The loupe variant says that we were using unchecked variant which would bypass the length invariant, and at the same time, this is not something we would require, so we are removing them
This solves loupe issue: 1. stratum-mining#2173 -> Here we fix the size of FIXED primitive to be equal to SIZE and not one. 2. stratum-mining#2177 -> make sure header is appended to the writer.
…olicy This solves a loupe issue: stratum-mining#2178, We are not doing unbounded reads with with_reader
Decode now returns an error, previous this was heavly utilizing the unchecked meaning without any constraints to the addition. This now forces us to return error in case if some variants are not met. This also adds owned variant decode where the decode consume the buffer, instead of just referencing the buffer.
In this commit we remove the unchecked variant from_bytes_ and to_slice_ across the crate, as we no longer use them and no point in using unsafe methods.
…y_sv2 This solves the issue: stratum-mining#2213, here we introduce new custom API, and deprecate old rigid API's
Part of removal of U32AsRef: stratum-mining#2215, we don't really need this.
Here, we remove error type variants across the crate which are not really needed.
Solves issue: stratum-mining#2175, via this we are fully adhering to specs and supporting all primitives mentioned
Completes the issue: stratum-mining#2215, removes u8Owned and uses u8 instead
…er instead use copied for from impl
Removes into_owned as internally it calls into_static and no point having this if it just calls into_static
Previously, the writer method would lead to unbounded recursion, this commit solves via directly calling encodable field writer method
a1f2b6a to
fe3c973
Compare
|
@Shourya742 just a suggestion about the point raised by the clanker: Yes. The core fix is to stop using
Right now both flow through The best fix is in // pseudo-shape
if payload_len <= MAXSIZE {
Ok(payload_len + HEADERSIZE)
} else {
Err(Error::ValueExceedsMaxSize(
ISFIXED,
SIZE,
HEADERSIZE,
MAXSIZE,
data.to_vec(),
payload_len,
))
}For the reader path: if expected_length <= MAXSIZE {
Ok(expected_length)
} else {
Err(Error::ValueExceedsMaxSize(
ISFIXED,
SIZE,
HEADERSIZE,
MAXSIZE,
header.to_vec(),
expected_length,
))
}Then Err(Error::OutOfBound | Error::ReadError(_, _)) => read_one_more_byte()
Err(error) => return Err(error)With that change, A slightly cleaner design would add a dedicated error like |
| } | ||
| } | ||
|
|
||
| // IMPL TRY_FROM DECODEC FIELD FOR PRIMITIVES |
There was a problem hiding this comment.
Why did you remove the TryFrom implementations for EncodableField, without replacing them with a macro?
| #[cfg(feature = "with_buffer_pool")] | ||
| impl From<buffer_sv2::Slice> for EncodableField<'_> { | ||
| fn from(_v: buffer_sv2::Slice) -> Self { | ||
| unreachable!() |
There was a problem hiding this comment.
Do you know why this was labeled as unreachable earlier?
|
Given that on 012536a you're fixing two issues found by Loupe, can't you add tests reported by it as well? |
| // | ||
| // ### `Sv2DataType` | ||
| // The `Sv2DataType` trait is implemented for these data types, providing methods for encoding and | ||
| // decoding operations such as `from_bytes_unchecked`, `from_vec_`, `from_reader_` (if `std` is |
There was a problem hiding this comment.
Remove the mention to from_vec_.
|
I would add tests mentioned by Loupe on commit 173e162 as well. |
|
Add Loupe's tests on 5a0d4bb as well. |
| fn decode_owned( | ||
| &self, | ||
| data: &[u8], | ||
| offset: usize, | ||
| ) -> Result<DecodablePrimitive<'static>, Error> { | ||
| macro_rules! decode_owned_copy { | ||
| ($ty:ty, $variant:ident) => {{ | ||
| let mut owned = data[offset..].to_vec(); | ||
| Ok(DecodablePrimitive::$variant(<$ty>::from_bytes_( | ||
| &mut owned, | ||
| )?)) | ||
| }}; | ||
| } | ||
|
|
||
| macro_rules! decode_owned_fixed_inner { | ||
| ($ty:ty, $variant:ident) => {{ | ||
| let data = &data[offset..]; | ||
| let size = <$ty>::size_hint(data, 0)?; | ||
| Ok(DecodablePrimitive::$variant(<$ty>::try_from( | ||
| data[..size].to_vec(), | ||
| )?)) | ||
| }}; | ||
| } |
There was a problem hiding this comment.
Why are you still keeping this one?
closes: #2171 #2173 #2175 #2176 #2177 #2178 #2179 #2213 #2214 #2215 #2216
Apart from the issues mentioned above, this PR introduces several additional improvements:
TryFrom. (More details can be found in the companionsv2-appsPR. I'm open to suggestions for alternative APIs these additions are primarily based on my experience working with thesv2-appscodebase.)MACtype, bringing us into full compliance with the specification.Fromimplementations with MBE macros, significantly reducing duplication.U32RefandU8Owned, as they do not provide any meaningful structural advantages.Seqtypes, allowing users to iterate over elements and perform in-place modifications more ergonomically.companion stratum-mining/sv2-apps#576