Skip to content

Commit 808b88d

Browse files
mablrmattsse
andauthored
feat(consensus,eips,genesis): add Borsh support (#2946)
* feat(consensus,eips,genesis): add Borsh support * fix: proper features propagation * chore: bump alloy-primitives to v1.41 - unskip borsh on unblocked fields * chore: bump core deps * chore: bump eip crates * chore: bump deps --------- Co-authored-by: Matthias Seitz <[email protected]>
1 parent 3b8e173 commit 808b88d

File tree

19 files changed

+85
-18
lines changed

19 files changed

+85
-18
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ alloy-chains = { version = "0.2", default-features = false }
8787

8888
# eips
8989
alloy-eip2124 = { version = "0.2.0", default-features = false }
90-
alloy-eip2930 = { version = "0.2.0", default-features = false }
91-
alloy-eip7702 = { version = "0.6.1", default-features = false }
90+
alloy-eip2930 = { version = "0.2.3", default-features = false }
91+
alloy-eip7702 = { version = "0.6.3", default-features = false }
9292

9393
# hardforks
9494
alloy-hardforks = "0.2.0"
@@ -153,6 +153,7 @@ bincode = "2.0"
153153
auto_impl = "1.2"
154154
base64 = "0.22"
155155
bimap = "0.6"
156+
borsh = { version = "1.5", default-features = false }
156157
cfg-if = "1"
157158
derive_more = { version = "2", default-features = false }
158159
either = { version = "1.15", default-features = false }

crates/consensus/Cargo.toml

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ c-kzg = { workspace = true, features = ["serde"], optional = true }
4040
arbitrary = { workspace = true, features = ["derive"], optional = true }
4141
rand = { workspace = true, optional = true }
4242

43+
# borsh
44+
borsh = { workspace = true, optional = true, features = ["derive"] }
45+
4346
# serde
4447
serde = { workspace = true, features = ["derive"], optional = true }
4548
serde_with = { workspace = true, optional = true }
@@ -74,21 +77,22 @@ assert_matches.workspace = true
7477
[features]
7578
default = ["std"]
7679
std = [
77-
"alloy-eips/std",
78-
"c-kzg?/std",
79-
"alloy-serde?/std",
80-
"alloy-primitives/std",
81-
"alloy-rlp/std",
82-
"alloy-trie/std",
83-
"derive_more/std",
84-
"k256?/std",
85-
"serde?/std",
86-
"serde_json?/std",
87-
"serde_with?/std",
88-
"thiserror/std",
89-
"either/std",
90-
"once_cell/std",
91-
"secp256k1?/std"
80+
"alloy-eips/std",
81+
"c-kzg?/std",
82+
"alloy-serde?/std",
83+
"alloy-primitives/std",
84+
"alloy-rlp/std",
85+
"alloy-trie/std",
86+
"derive_more/std",
87+
"k256?/std",
88+
"serde?/std",
89+
"serde_json?/std",
90+
"serde_with?/std",
91+
"thiserror/std",
92+
"either/std",
93+
"once_cell/std",
94+
"secp256k1?/std",
95+
"borsh?/std"
9296
]
9397
k256 = ["dep:k256", "alloy-primitives/k256", "alloy-eips/k256"]
9498
secp256k1 = ["dep:secp256k1"]
@@ -120,3 +124,8 @@ serde = [
120124
"alloy-tx-macros/serde"
121125
]
122126
serde-bincode-compat = ["alloy-eips/serde-bincode-compat", "serde_with"]
127+
borsh = [
128+
"dep:borsh",
129+
"alloy-primitives/borsh",
130+
"alloy-eips/borsh",
131+
]

crates/consensus/src/block/header.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::mem;
2121
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
2222
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2323
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
24+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
2425
pub struct Header {
2526
/// The Keccak 256-bit hash of the parent
2627
/// block’s header, in its entirety; formally Hp.

crates/consensus/src/block/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
2727
/// See p2p block encoding reference: <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#block-encoding-and-validity>
2828
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Deref)]
2929
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
30+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
3031
pub struct Block<T, H = Header> {
3132
/// Block header.
3233
#[deref]
@@ -212,6 +213,7 @@ where
212213
/// Withdrawals can be optionally included at the end of the RLP encoded message.
213214
#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
214215
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
216+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
215217
#[rlp(trailing)]
216218
pub struct BlockBody<T, H = Header> {
217219
/// Transactions in this block.

crates/consensus/src/receipt/envelope.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use core::fmt;
2424
#[derive(Clone, Debug, PartialEq, Eq)]
2525
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2626
#[cfg_attr(feature = "serde", serde(tag = "type"))]
27+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
2728
#[doc(alias = "TransactionReceiptEnvelope", alias = "TxReceiptEnvelope")]
2829
pub enum ReceiptEnvelope<T = Log> {
2930
/// Receipt envelope with no type flag.

crates/consensus/src/receipt/receipts.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::fmt;
1212
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
1313
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
1414
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
15+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
1516
#[doc(alias = "TransactionReceipt", alias = "TxReceipt")]
1617
pub struct Receipt<T = Log> {
1718
/// If transaction is executed successfully.
@@ -254,6 +255,7 @@ impl<T> Default for Receipts<T> {
254255
#[derive(Clone, Debug, Default, PartialEq, Eq)]
255256
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
256257
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
258+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
257259
#[doc(alias = "TransactionReceiptWithBloom", alias = "TxReceiptWithBloom")]
258260
pub struct ReceiptWithBloom<T = Receipt<Log>> {
259261
#[cfg_attr(feature = "serde", serde(flatten))]

crates/consensus/src/receipt/status.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use alloy_rlp::{Buf, BufMut, Decodable, Encodable, Error, Header};
44
/// Captures the result of a transaction execution.
55
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
66
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
7+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
78
pub enum Eip658Value {
89
/// A boolean `statusCode` introduced by [EIP-658].
910
///

crates/consensus/src/transaction/eip1559.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use super::{RlpEcdsaDecodableTx, RlpEcdsaEncodableTx};
1313
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
1414
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1515
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
16+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
1617
#[doc(alias = "Eip1559Transaction", alias = "TransactionEip1559", alias = "Eip1559Tx")]
1718
pub struct TxEip1559 {
1819
/// EIP-155: Simple replay attack protection
@@ -56,6 +57,8 @@ pub struct TxEip1559 {
5657
/// The 160-bit address of the message call’s recipient or, for a contract creation
5758
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
5859
#[cfg_attr(feature = "serde", serde(default))]
60+
#[cfg_attr(feature = "borsh", borsh(skip))]
61+
// TODO: Implement Borsh for TxKind in alloy_primitives
5962
pub to: TxKind,
6063
/// A scalar value equal to the number of Wei to
6164
/// be transferred to the message call’s recipient or,
@@ -72,6 +75,8 @@ pub struct TxEip1559 {
7275
// sometimes returning `null` instead of an empty array `[]`.
7376
// More details in <https://github.com/alloy-rs/alloy/pull/2450>.
7477
#[cfg_attr(feature = "serde", serde(deserialize_with = "alloy_serde::null_as_default"))]
78+
#[cfg_attr(feature = "borsh", borsh(skip))]
79+
// TODO: Implement Borsh for AccessList in alloy_eip2930
7580
pub access_list: AccessList,
7681
/// Input has two uses depending if `to` field is Create or Call.
7782
/// pub init: An unlimited size byte array specifying the

crates/consensus/src/transaction/eip2930.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use super::{RlpEcdsaDecodableTx, RlpEcdsaEncodableTx};
1313
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
1414
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1515
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
16+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
1617
#[doc(alias = "Eip2930Transaction", alias = "TransactionEip2930", alias = "Eip2930Tx")]
1718
pub struct TxEip2930 {
1819
/// Added as EIP-pub 155: Simple replay attack protection
@@ -43,6 +44,8 @@ pub struct TxEip2930 {
4344
/// The 160-bit address of the message call’s recipient or, for a contract creation
4445
/// transaction, ∅, used here to denote the only member of B0 ; formally Tt.
4546
#[cfg_attr(feature = "serde", serde(default))]
47+
#[cfg_attr(feature = "borsh", borsh(skip))]
48+
// TODO: Implement Borsh for TxKind in alloy_primitives
4649
pub to: TxKind,
4750
/// A scalar value equal to the number of Wei to
4851
/// be transferred to the message call’s recipient or,
@@ -56,6 +59,8 @@ pub struct TxEip2930 {
5659
/// and `accessed_storage_keys` global sets (introduced in EIP-2929).
5760
/// A gas cost is charged, though at a discount relative to the cost of
5861
/// accessing outside the list.
62+
#[cfg_attr(feature = "borsh", borsh(skip))]
63+
// TODO: Implement Borsh for AccessList in alloy_eip2930
5964
pub access_list: AccessList,
6065
/// Input has two uses depending if `to` field is Create or Call.
6166
/// pub init: An unlimited size byte array specifying the

crates/consensus/src/transaction/eip4844.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use alloy_eips::eip7594::{BlobTransactionSidecarEip7594, BlobTransactionSidecarV
2727
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
2828
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
2929
#[cfg_attr(feature = "serde", serde(untagged))]
30+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
3031
#[doc(alias = "Eip4844TransactionVariant")]
3132
pub enum TxEip4844Variant<T = BlobTransactionSidecar> {
3233
/// A standalone transaction with blob hashes and max blob fee.
@@ -514,6 +515,7 @@ where
514515
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
515516
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
516517
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
518+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
517519
#[doc(alias = "Eip4844Transaction", alias = "TransactionEip4844", alias = "Eip4844Tx")]
518520
pub struct TxEip4844 {
519521
/// Added as EIP-pub 155: Simple replay attack protection
@@ -566,6 +568,8 @@ pub struct TxEip4844 {
566568
/// and `accessed_storage_keys` global sets (introduced in EIP-2929).
567569
/// A gas cost is charged, though at a discount relative to the cost of
568570
/// accessing outside the list.
571+
#[cfg_attr(feature = "borsh", borsh(skip))]
572+
// TODO: Implement Borsh for AccessList in alloy_eip2930
569573
pub access_list: AccessList,
570574

571575
/// It contains a vector of fixed size hash(32 bytes)
@@ -834,6 +838,7 @@ impl<T> From<TxEip4844WithSidecar<T>> for TxEip4844 {
834838
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
835839
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
836840
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
841+
#[cfg_attr(feature = "borsh", derive(borsh::BorshSerialize, borsh::BorshDeserialize))]
837842
#[doc(alias = "Eip4844TransactionWithSidecar", alias = "Eip4844TxWithSidecar")]
838843
pub struct TxEip4844WithSidecar<T = BlobTransactionSidecar> {
839844
/// The actual transaction.

0 commit comments

Comments
 (0)