Skip to content

Commit

Permalink
chore: dedupe blob in consensus and rpc (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich authored Mar 26, 2024
1 parent 7e39c85 commit 27a1e36
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 61 deletions.
6 changes: 3 additions & 3 deletions crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub use receipt::{Receipt, ReceiptEnvelope, ReceiptWithBloom, TxReceipt};

mod transaction;
pub use transaction::{
eip4844_utils, BlobTransactionSidecar, SidecarBuilder, SidecarCoder, SignableTransaction,
SimpleCoder, Transaction, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant,
TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction,
eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder,
SignableTransaction, SimpleCoder, Transaction, TxEip1559, TxEip2930, TxEip4844,
TxEip4844Variant, TxEip4844WithSidecar, TxEnvelope, TxLegacy, TxType, TypedTransaction,
};

#[cfg(feature = "kzg")]
Expand Down
17 changes: 11 additions & 6 deletions crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ use alloy_eips::{
eip2930::AccessList,
eip4844::{BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_PROOF, DATA_GAS_PER_BLOB},
};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{
keccak256, Address, Bytes, ChainId, FixedBytes, Signature, TxKind, B256, U256,
};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use sha2::{Digest, Sha256};
use std::mem;

#[cfg(not(feature = "kzg"))]
use alloy_eips::eip4844::{Blob, Bytes48};
pub use alloy_eips::eip4844::{Blob, Bytes48};

#[cfg(feature = "kzg")]
use c_kzg::{Blob, Bytes48, KzgCommitment, KzgProof, KzgSettings};
use c_kzg::{KzgCommitment, KzgProof, KzgSettings};
#[cfg(feature = "kzg")]
use std::ops::Deref;

#[cfg(feature = "kzg")]
pub use c_kzg::{Blob, Bytes48};

/// An error that can occur when validating a [TxEip4844Variant].
#[derive(Debug, thiserror::Error)]
#[cfg(feature = "kzg")]
Expand Down Expand Up @@ -1037,9 +1042,9 @@ impl Decodable for BlobTransactionSidecar {
// Wrapper for c-kzg rlp
#[repr(C)]
struct BlobTransactionSidecarRlp {
blobs: Vec<[u8; BYTES_PER_BLOB]>,
commitments: Vec<[u8; BYTES_PER_COMMITMENT]>,
proofs: Vec<[u8; BYTES_PER_PROOF]>,
blobs: Vec<FixedBytes<BYTES_PER_BLOB>>,
commitments: Vec<FixedBytes<BYTES_PER_COMMITMENT>>,
proofs: Vec<FixedBytes<BYTES_PER_PROOF>>,
}

const _: [(); std::mem::size_of::<BlobTransactionSidecar>()] =
Expand Down
4 changes: 2 additions & 2 deletions crates/consensus/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod eip4844;
#[cfg(feature = "kzg")]
pub use eip4844::BlobTransactionValidationError;
pub use eip4844::{
utils as eip4844_utils, BlobTransactionSidecar, SidecarBuilder, SidecarCoder, SimpleCoder,
TxEip4844, TxEip4844Variant, TxEip4844WithSidecar,
utils as eip4844_utils, Blob, BlobTransactionSidecar, Bytes48, SidecarBuilder, SidecarCoder,
SimpleCoder, TxEip4844, TxEip4844Variant, TxEip4844WithSidecar,
};

mod envelope;
Expand Down
6 changes: 5 additions & 1 deletion crates/rpc-engine-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-rpc-types.workspace = true
alloy-serde.workspace = true
alloy-consensus.workspace = true

# ssz
ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }

Expand All @@ -26,10 +28,12 @@ thiserror.workspace = true

# jsonrpsee
jsonrpsee-types = { version = "0.20", optional = true }
alloy-eips = { workspace= true, optional = true }

[features]
jsonrpsee-types = ["dep:jsonrpsee-types"]
ssz = ["dep:ethereum_ssz", "dep:ethereum_ssz_derive", "alloy-primitives/ssz", "alloy-rpc-types/ssz"]
ssz = ["dep:ethereum_ssz", "dep:ethereum_ssz_derive", "alloy-primitives/ssz", "alloy-rpc-types/ssz", "dep:alloy-eips", "alloy-eips/ssz"]
kzg = ["alloy-consensus/kzg"]

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde", "arbitrary"] }
Expand Down
1 change: 1 addition & 0 deletions crates/rpc-engine-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod forkchoice;
mod optimism;
pub mod payload;
mod transition;

pub use self::{cancun::*, forkchoice::*, optimism::*, payload::*, transition::*};

/// The list of all supported Engine capabilities available over the engine endpoint.
Expand Down
83 changes: 73 additions & 10 deletions crates/rpc-engine-types/src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//! Payload types.
use alloy_consensus::{Blob, Bytes48};
use alloy_primitives::{Address, Bloom, Bytes, B256, B64, U256};
use alloy_rpc_types::{
kzg::{Blob, Bytes48},
transaction::BlobTransactionSidecar,
Withdrawal,
};
use alloy_rpc_types::{transaction::BlobTransactionSidecar, Withdrawal};
use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;

Expand Down Expand Up @@ -405,14 +401,75 @@ impl ssz::Encode for ExecutionPayloadV3 {

/// This includes all bundled blob related data of an executed payload.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "ssz", derive(ssz_derive::Encode, ssz_derive::Decode))]
pub struct BlobsBundleV1 {
/// All commitments in the bundle.
pub commitments: Vec<Bytes48>,
pub commitments: Vec<alloy_consensus::Bytes48>,
/// All proofs in the bundle.
pub proofs: Vec<Bytes48>,
pub proofs: Vec<alloy_consensus::Bytes48>,
/// All blobs in the bundle.
pub blobs: Vec<Blob>,
pub blobs: Vec<alloy_consensus::Blob>,
}

#[cfg(feature = "ssz")]
#[derive(ssz_derive::Encode, ssz_derive::Decode)]
struct BlobsBundleV1Ssz {
commitments: Vec<alloy_primitives::FixedBytes<48>>,
proofs: Vec<alloy_primitives::FixedBytes<48>>,
blobs: Vec<alloy_primitives::FixedBytes<{ alloy_eips::eip4844::BYTES_PER_BLOB }>>,
}

#[cfg(feature = "ssz")]
impl BlobsBundleV1Ssz {
const _ASSERT: [(); std::mem::size_of::<BlobsBundleV1>()] =
[(); std::mem::size_of::<BlobsBundleV1Ssz>()];

const fn wrap_ref(other: &BlobsBundleV1) -> &Self {
// SAFETY: Same repr and size
unsafe { &*(other as *const BlobsBundleV1 as *const Self) }
}

fn unwrap(self) -> BlobsBundleV1 {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(self) }
}
}

#[cfg(feature = "ssz")]
impl ssz::Encode for BlobsBundleV1 {
fn is_ssz_fixed_len() -> bool {
<BlobsBundleV1Ssz as ssz::Encode>::is_ssz_fixed_len()
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
BlobsBundleV1Ssz::wrap_ref(self).ssz_append(buf)
}

fn ssz_bytes_len(&self) -> usize {
BlobsBundleV1Ssz::wrap_ref(self).ssz_bytes_len()
}

fn ssz_fixed_len() -> usize {
<BlobsBundleV1Ssz as ssz::Encode>::ssz_fixed_len()
}

fn as_ssz_bytes(&self) -> Vec<u8> {
BlobsBundleV1Ssz::wrap_ref(self).as_ssz_bytes()
}
}

#[cfg(feature = "ssz")]
impl ssz::Decode for BlobsBundleV1 {
fn is_ssz_fixed_len() -> bool {
<BlobsBundleV1Ssz as ssz::Decode>::is_ssz_fixed_len()
}

fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> {
BlobsBundleV1Ssz::from_ssz_bytes(bytes).map(BlobsBundleV1Ssz::unwrap)
}

fn ssz_fixed_len() -> usize {
<BlobsBundleV1Ssz as ssz::Decode>::ssz_fixed_len()
}
}

impl BlobsBundleV1 {
Expand Down Expand Up @@ -462,6 +519,12 @@ impl From<Vec<BlobTransactionSidecar>> for BlobsBundleV1 {
}
}

impl FromIterator<BlobTransactionSidecar> for BlobsBundleV1 {
fn from_iter<T: IntoIterator<Item = BlobTransactionSidecar>>(iter: T) -> Self {
Self::new(iter)
}
}

/// An execution payload, which can be either [ExecutionPayloadV1], [ExecutionPayloadV2], or
/// [ExecutionPayloadV3].
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exclude.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde", "std"] }
alloy-serde.workspace = true
alloy-consensus.workspace = true
alloy-consensus = { workspace = true, features = ["serde"] }
alloy-eips = {workspace = true, features = ["std", "serde"]}

itertools.workspace = true
Expand Down
16 changes: 0 additions & 16 deletions crates/rpc-types/src/eth/transaction/blob.rs

This file was deleted.

12 changes: 0 additions & 12 deletions crates/rpc-types/src/eth/transaction/kzg.rs

This file was deleted.

24 changes: 14 additions & 10 deletions crates/rpc-types/src/eth/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ use alloy_consensus::{
SignableTransaction, Signed, TxEip1559, TxEip2930, TxEip4844, TxEip4844Variant, TxEnvelope,
TxLegacy, TxType,
};
pub use alloy_eips::eip2930::{AccessList, AccessListItem, AccessListWithGasUsed};
use alloy_primitives::{Address, Bytes, B256, U256, U8};
pub use blob::BlobTransactionSidecar;
pub use common::TransactionInfo;
pub use error::ConversionError;
pub use optimism::OptimismTransactionReceiptFields;
pub use receipt::TransactionReceipt;
pub use request::{TransactionInput, TransactionRequest};
use serde::{Deserialize, Serialize};
pub use signature::{Parity, Signature};

mod blob;
pub use alloy_consensus::BlobTransactionSidecar;
pub use alloy_eips::eip2930::{AccessList, AccessListItem, AccessListWithGasUsed};

mod common;
pub use common::TransactionInfo;

mod error;
pub mod kzg;
pub use error::ConversionError;

pub mod optimism;
pub use optimism::OptimismTransactionReceiptFields;

mod receipt;
pub use receipt::TransactionReceipt;

pub mod request;
pub use request::{TransactionInput, TransactionRequest};

mod signature;
pub use signature::{Parity, Signature};

/// Transaction object used in RPC
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down

0 comments on commit 27a1e36

Please sign in to comment.