Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor framing_sv2 example #1468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

plebhash
Copy link
Collaborator

@plebhash plebhash commented Feb 9, 2025

close #1467


// Example message type (e.g., SetupConnection)
const MSG_TYPE: u8 = 1;
// Example extension type (e.g., a standard Sv2 message)
const EXT_TYPE: u16 = 0x0001;

#[derive(Serialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
Copy link
Collaborator Author

@plebhash plebhash Feb 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is causing the following error:

error[E0308]: mismatched types
  --> v2/framing-sv2/examples/sv2_frame.rs:29:21
   |
29 | #[derive(Serialize, Deserialize, Debug)]
   |                     ^^^^^^^^^^^
   |                     |
   |                     expected `&[u8]`, found `&Vec<FieldMarker>`
   |                     arguments to this method are incorrect
   |
   = note: expected reference `&[u8]`
              found reference `&Vec<FieldMarker>`
note: method defined here
  --> /Users/plebhash/develop/stratum/protocols/v2/binary-sv2/no-serde-sv2/codec/src/codec/mod.rs:85:8
   |
85 |     fn size_hint_(&self, data: &[u8], offset: usize) -> Result<usize, Error>;
   |        ^^^^^^^^^^
   = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0308`.
error: could not compile `framing_sv2` (example "sv2_frame") due to previous error

this error is the main reason why this PR is still draft

I wonder if this is related to #1455, and if it requires anything from #1455 or #1462?

cc @Shourya742

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rebased to bring in the changes from #1455 and the error did not go away.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change variable name from data, to anything else. something like

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CustomMessage {
    pub a: u32,
}

Copy link
Collaborator Author

@plebhash plebhash Feb 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that did the trick!

but now I'm quite puzzled! is data somehow causing some kind of namespace conflict with binary_sv2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You managed to break the binary_sv2 implementation.
The error was caused by the following code:

fn get_structure(data: &[u8]) -> Result<Vec<FieldMarker>, Error> {
    let mut fields = Vec::new();
    let mut offset = 0;
    let data: Vec<FieldMarker> = u32::get_structure(&data[offset..])?;
    offset += a.size_hint_(&data, offset)?;
    let a = a.try_into()?;
    fields.push(a);

    Ok(fields)
}

When the procedural macro deserialization expands, it generates an implementation for our CustomMessage struct, which includes a data field. Because we named the field data, this line:

let data: Vec<FieldMarker> = u32::get_structure(&data[offset..])?;

shadows the original data slice with a Vec<FieldMarker>. This causes an issue because get_structure expects a slice of u8, not a Vec<FieldMarker>. We need to have a unique identifier rather than this data. As I might have mentioned in KT session of our, the proc_macro implementation can be improved, and this was one of the reasons to propose that proposal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that sounds like a bug in the proc_macro of derive_codec_sv2, right?

should we take note of this in #1462 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will add all the observations made so far to the issue and update it.

Copy link

codecov bot commented Feb 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 18.06%. Comparing base (49c78e3) to head (cd6226b).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1468   +/-   ##
=======================================
  Coverage   18.06%   18.06%           
=======================================
  Files         127      127           
  Lines        9555     9555           
=======================================
  Hits         1726     1726           
  Misses       7829     7829           
Flag Coverage Δ
binary_codec_sv2-coverage 0.00% <ø> (ø)
binary_sv2-coverage 6.95% <ø> (ø)
bip32_derivation-coverage 0.00% <ø> (ø)
buffer_sv2-coverage 25.02% <ø> (ø)
codec_sv2-coverage 0.02% <ø> (ø)
common_messages_sv2-coverage 0.17% <ø> (ø)
const_sv2-coverage 0.00% <ø> (ø)
error_handling-coverage 0.00% <ø> (ø)
framing_sv2-coverage 0.37% <ø> (ø)
jd_client-coverage 0.42% <ø> (ø)
jd_server-coverage 9.02% <ø> (ø)
job_declaration_sv2-coverage 0.00% <ø> (ø)
key-utils-coverage 2.39% <ø> (ø)
mining-coverage 3.17% <ø> (ø)
mining_device-coverage 0.00% <ø> (ø)
mining_proxy_sv2-coverage 0.82% <ø> (ø)
noise_sv2-coverage 5.78% <ø> (ø)
protocols 23.92% <ø> (ø)
roles 6.43% <ø> (ø)
roles_logic_sv2-coverage 11.64% <ø> (ø)
sv2_ffi-coverage 0.00% <ø> (ø)
template_distribution_sv2-coverage 0.00% <ø> (ø)
translator_sv2-coverage 9.53% <ø> (ø)
utils 25.13% <ø> (ø)
v1-coverage 3.11% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@plebhash plebhash force-pushed the refactor-framing-sv2-example branch 4 times, most recently from 393cc68 to 7d24f3f Compare February 10, 2025 15:19
we need to assert that the deserialized message has the same content from the original
@plebhash plebhash force-pushed the refactor-framing-sv2-example branch from 7d24f3f to 52e824f Compare February 10, 2025 17:13
@plebhash plebhash marked this pull request as ready for review February 10, 2025 17:13
@plebhash plebhash changed the title [WIP] refactor framing_sv2 example refactor framing_sv2 example Feb 10, 2025
Copy link
Contributor

@jbesraa jbesraa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. do we actually run this in the ci?

@plebhash
Copy link
Collaborator Author

Looks good. do we actually run this in the ci?

nice catch, we didn't but I just added a commit that runs it from test.yml

@Shourya742
Copy link
Contributor

Looks good. do we actually run this in the ci?

nice catch, we didn't but I just added a commit that runs it from test.yml

Should we also add other examples that we recently included during our documentation efforts, since not all of them have been added to CI?


// Example message type (e.g., SetupConnection)
const MSG_TYPE: u8 = 1;
// Example extension type (e.g., a standard Sv2 message)
const EXT_TYPE: u16 = 0x0001;

#[derive(Serialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CustomMessage {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking out loud here, wouldn't it be more beneficial to actually have an SV2 message here? for the MSG_TYPE, SETUP_CONNECTION is used, maybe here as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

framing_sv2 example is incomplete
3 participants