Skip to content

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

@shekhirin

Description

@shekhirin

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Completed

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions