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: |
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 |
5580dc6 to
772a173
Compare
|
Loupe tests are in, and all are passing, I will link the commits to corresponding loupe issue for reference. |
|
@GitGab19 the commits might be a bit different now, I rewrote the history with test and your suggestions. |
3c25807 to
224a7de
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. Another note; We are removing try_from from Encodable to primitive, more info here: stratum-mining#2200 (comment)
224a7de to
16e8677
Compare
| impl<const SIZE: usize> Inner<'_, true, SIZE, 0, 0> { | ||
| /// Returns the payload bytes as an array reference. | ||
| pub fn as_array(&self) -> &[u8; SIZE] { | ||
| self.as_bytes() | ||
| .try_into() | ||
| .expect("fixed-size SV2 byte wrapper must always match SIZE") | ||
| } | ||
|
|
||
| /// Copies the payload bytes into an array. | ||
| pub fn to_array(&self) -> [u8; SIZE] { | ||
| *self.as_array() | ||
| } | ||
|
|
||
| /// Consumes the value and returns the payload bytes as an array. | ||
| pub fn into_array(self) -> [u8; SIZE] { | ||
| match self { | ||
| Inner::Ref(data) => data | ||
| .try_into() | ||
| .expect("fixed-size SV2 byte wrapper must always match SIZE"), | ||
| Inner::Owned(data) => data | ||
| .try_into() | ||
| .expect("fixed-size SV2 byte wrapper must always match SIZE"), | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I asked gpt-5.3-codex whether these .expects were infallible:
Short answer: conditionally, not absolutely.
- They are infallible if the invariant holds: for
Inner<'_, true, SIZE, 0, 0>, payload length is always exactlySIZE.- With current API, that invariant is usually enforced by checked constructors (
TryFrom,from_bytes_,from_reader_), so in normal flow theseexpects should not fire.- But
Inneris apub enum, so callers can still construct invalid fixed values directly (e.g.Inner::Owned(vec![0; SIZE - 1])), and thenas_array()/into_array()will panic.So: infallible by convention, not by type-level encapsulation.
not a blocker for me, but I thought I'd bring it up anyways
There was a problem hiding this comment.
We gonna be removing Inner, thats the end goal.
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. Another note; We are removing try_from from Encodable to primitive, more info here: stratum-mining#2200 (comment)
…rait This is more of a clean up commit, where we remove repetitive hex conv code in primitive's display trait implementation.
This solves this issue: stratum-mining#2171 There are other variants of same issue which 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
…ad of using unchecked conversions * 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. * 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.
Part of removal of U32AsRef: stratum-mining#2215, we don't really need this.
Completes the issue: stratum-mining#2215, removes u8Owned and uses u8 instead
Here, we remove error type variants across the crate which are not really needed.
The implementation was also handling conversion incorrectly
Solves issue: stratum-mining#2175, via this we are fully adhering to specs and supporting all primitives mentioned
…olicy We are not doing unbounded reads with with_reader
This solves: 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.
…esn't leak internal implementation This solves 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
…y_sv2 This solves the issue: stratum-mining#2213, here we introduce new custom API, and deprecate old rigid API's
16e8677 to
7d89f23
Compare
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