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

[Feature] Bincode support for alloy-consensus and alloy-primitive types #1349

Closed
shekhirin opened this issue Sep 24, 2024 · 0 comments · Fixed by #1397
Closed

[Feature] Bincode support for alloy-consensus and alloy-primitive types #1349

shekhirin opened this issue Sep 24, 2024 · 0 comments · Fixed by #1397
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@shekhirin
Copy link
Contributor

shekhirin commented Sep 24, 2024

Component

consensus, eips, genesis, serde

Describe the feature you would like

Problem

Bincode does not support #[serde(skip*)] attributes. Additionally, there's a similar issue with rmp-serde.

It means that we cannot use a lot of Alloy types for serialization/deserialization using these formats.

Solution

Introduce helper serde modules for those structs in alloy-consensus that have #[serde(skip*)] attributes on fields and remove those attributes. Modules should be similar to

/// A helper serde module to convert from/to the Beacon API which uses quoted decimals rather than
/// big-endian hex.
pub mod beacon_payload_v1 {
use super::*;
/// Serialize the payload attributes for the beacon API.
pub fn serialize<S>(
payload_attributes: &ExecutionPayloadV1,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
BeaconExecutionPayloadV1::from(payload_attributes).serialize(serializer)
}
/// Deserialize the payload attributes for the beacon API.
pub fn deserialize<'de, D>(deserializer: D) -> Result<ExecutionPayloadV1, D::Error>
where
D: Deserializer<'de>,
{
BeaconExecutionPayloadV1::deserialize(deserializer).map(Into::into)
}
}

Then, these modules should be used in structs that have problematic types as fields. For example, for the Block type we will need to use a #[serde_as] on the Block struct and #[serde_as(as = "HeaderBincode")] on the header field

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Block<T> {
/// Block header.
pub header: Header,

Both of the attributes should be gated by the serde-bincode feature.

The types that need special handling are:

  • Header
  • TxEip2930
  • TxEip1559
  • TxLegacy

As a test, we should be able to serialize and deserialize the Block struct using bincode.

Additional context

No response

@shekhirin shekhirin added enhancement New feature or request good first issue Good for newcomers labels Sep 24, 2024
@yash-atreya yash-atreya added this to the 1.0 milestone Sep 26, 2024
@shekhirin shekhirin self-assigned this Sep 28, 2024
github-merge-queue bot pushed a commit to alloy-rs/op-alloy that referenced this issue Sep 30, 2024
## Motivation

alloy-rs/alloy#1349

## Solution

Same as alloy-rs/alloy#1397

## PR Checklist

- [x] Added Tests
- [x] Added Documentation
- [ ] Breaking changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants