-
Notifications
You must be signed in to change notification settings - Fork 600
[Feature] Bincode support for alloy-consensus and alloy-primitive types #1349
Description
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
alloy/crates/rpc-types-beacon/src/payload.rs
Lines 275 to 298 in 5c7470c
| /// 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
alloy/crates/consensus/src/block.rs
Lines 15 to 18 in cf9a98f
| #[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:
HeaderTxEip2930TxEip1559TxLegacy
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
Projects
Status