From 79abf2ba0fae5876f5cfdd66fe315975abc1ec27 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 13 Nov 2023 18:37:14 +0100 Subject: [PATCH] chore: remove ckzg dep from rpc types (#5392) --- Cargo.lock | 1 - bin/reth/src/debug_cmd/build_block.rs | 23 +++++--------- crates/payload/builder/src/payload.rs | 8 ++--- crates/primitives/src/transaction/sidecar.rs | 17 ++++++++++ .../rpc-types-compat/src/engine/payload.rs | 15 +-------- crates/rpc/rpc-types/Cargo.toml | 1 - .../rpc/rpc-types/src/eth/engine/payload.rs | 31 +++++++++++++++---- .../rpc/rpc-types/src/eth/transaction/kzg.rs | 12 +++++++ .../rpc/rpc-types/src/eth/transaction/mod.rs | 1 + .../rpc-types/src/eth/transaction/typed.rs | 7 +++-- 10 files changed, 71 insertions(+), 45 deletions(-) create mode 100644 crates/rpc/rpc-types/src/eth/transaction/kzg.rs diff --git a/Cargo.lock b/Cargo.lock index d21536ea91..93a8876613 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6470,7 +6470,6 @@ dependencies = [ "alloy-rlp", "arbitrary", "bytes", - "c-kzg", "itertools 0.11.0", "jsonrpsee-types", "proptest", diff --git a/bin/reth/src/debug_cmd/build_block.rs b/bin/reth/src/debug_cmd/build_block.rs index e544057e75..082c9a7067 100644 --- a/bin/reth/src/debug_cmd/build_block.rs +++ b/bin/reth/src/debug_cmd/build_block.rs @@ -152,7 +152,6 @@ impl Command { Factory::new(self.chain.clone()), Arc::clone(&self.chain), ); - let _tree_config = BlockchainTreeConfig::default(); let tree = BlockchainTree::new(tree_externals, BlockchainTreeConfig::default(), None)?; let blockchain_tree = ShareableBlockchainTree::new(tree); @@ -198,27 +197,21 @@ impl Command { "encountered a blob tx. `--blobs-bundle-path` must be provided" ))?; - let (commitments, proofs, blobs) = - blobs_bundle.take(blob_versioned_hashes.len()); + let sidecar: BlobTransactionSidecar = + blobs_bundle.pop_sidecar(blob_versioned_hashes.len()).into(); // first construct the tx, calculating the length of the tx with sidecar before // insertion - let sidecar = BlobTransactionSidecar::new( - blobs.clone(), - commitments.clone(), - proofs.clone(), - ); - let tx = - BlobTransaction::try_from_signed(transaction.as_ref().clone(), sidecar) - .expect("should not fail to convert blob tx if it is already eip4844"); + let tx = BlobTransaction::try_from_signed( + transaction.as_ref().clone(), + sidecar.clone(), + ) + .expect("should not fail to convert blob tx if it is already eip4844"); let pooled = PooledTransactionsElement::BlobTransaction(tx); let encoded_length = pooled.length_without_header(); // insert the blob into the store - blob_store.insert( - transaction.hash, - BlobTransactionSidecar { blobs, commitments, proofs }, - )?; + blob_store.insert(transaction.hash, sidecar)?; encoded_length } diff --git a/crates/payload/builder/src/payload.rs b/crates/payload/builder/src/payload.rs index acb82c0325..b3e68da2b8 100644 --- a/crates/payload/builder/src/payload.rs +++ b/crates/payload/builder/src/payload.rs @@ -12,7 +12,7 @@ use reth_rpc_types::engine::{ use reth_rpc_types_compat::engine::payload::{ block_to_payload_v3, convert_block_to_payload_field_v2, - convert_standalone_withdraw_to_withdrawal, from_primitive_sidecar, try_block_to_payload_v1, + convert_standalone_withdraw_to_withdrawal, try_block_to_payload_v1, }; use revm_primitives::{BlobExcessGasAndPrice, BlockEnv, CfgEnv, SpecId}; @@ -116,11 +116,7 @@ impl From for ExecutionPayloadEnvelopeV3 { // Spec: // should_override_builder: false, - blobs_bundle: sidecars - .into_iter() - .map(from_primitive_sidecar) - .collect::>() - .into(), + blobs_bundle: sidecars.into_iter().map(Into::into).collect::>().into(), } } } diff --git a/crates/primitives/src/transaction/sidecar.rs b/crates/primitives/src/transaction/sidecar.rs index 0f51c0df73..ac98c0af9a 100644 --- a/crates/primitives/src/transaction/sidecar.rs +++ b/crates/primitives/src/transaction/sidecar.rs @@ -353,6 +353,20 @@ impl BlobTransactionSidecar { } } +impl From for BlobTransactionSidecar { + fn from(value: reth_rpc_types::BlobTransactionSidecar) -> Self { + // SAFETY: Same repr and size + unsafe { std::mem::transmute(value) } + } +} + +impl From for reth_rpc_types::BlobTransactionSidecar { + fn from(value: BlobTransactionSidecar) -> Self { + // SAFETY: Same repr and size + unsafe { std::mem::transmute(value) } + } +} + // Wrapper for c-kzg rlp #[repr(C)] struct BlobTransactionSidecarRlp { @@ -364,6 +378,9 @@ struct BlobTransactionSidecarRlp { const _: [(); std::mem::size_of::()] = [(); std::mem::size_of::()]; +const _: [(); std::mem::size_of::()] = + [(); std::mem::size_of::()]; + impl BlobTransactionSidecarRlp { fn wrap_ref(other: &BlobTransactionSidecar) -> &Self { // SAFETY: Same repr and size diff --git a/crates/rpc/rpc-types-compat/src/engine/payload.rs b/crates/rpc/rpc-types-compat/src/engine/payload.rs index 2d55c5ba42..adb7c6cd03 100644 --- a/crates/rpc/rpc-types-compat/src/engine/payload.rs +++ b/crates/rpc/rpc-types-compat/src/engine/payload.rs @@ -360,18 +360,6 @@ pub fn convert_to_payload_body_v1(value: Block) -> ExecutionPayloadBodyV1 { ExecutionPayloadBodyV1 { transactions: transactions.collect(), withdrawals: withdraw } } -/// Transforms a [reth_primitives::BlobTransactionSidecar] into a -/// [reth_rpc_types::BlobTransactionSidecar] -pub fn from_primitive_sidecar( - sidecar: reth_primitives::BlobTransactionSidecar, -) -> reth_rpc_types::BlobTransactionSidecar { - reth_rpc_types::BlobTransactionSidecar { - blobs: sidecar.blobs, - commitments: sidecar.commitments, - proofs: sidecar.proofs, - } -} - /// Transforms a [SealedBlock] into a [ExecutionPayloadV1] pub fn execution_payload_from_sealed_block(value: SealedBlock) -> ExecutionPayloadV1 { let transactions = value @@ -403,11 +391,10 @@ pub fn execution_payload_from_sealed_block(value: SealedBlock) -> ExecutionPaylo #[cfg(test)] mod tests { + use super::{block_to_payload_v3, try_payload_v3_to_block}; use reth_primitives::{hex, Bytes, U256}; use reth_rpc_types::{engine::ExecutionPayloadV3, ExecutionPayloadV1, ExecutionPayloadV2}; - use super::{block_to_payload_v3, try_payload_v3_to_block}; - #[test] fn roundtrip_payload_to_block() { let first_transaction_raw = Bytes::from_static(&hex!("02f9017a8501a1f0ff438211cc85012a05f2008512a05f2000830249f094d5409474fd5a725eab2ac9a8b26ca6fb51af37ef80b901040cc7326300000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000001bdd2ed4b616c800000000000000000000000000001e9ee781dd4b97bdef92e5d1785f73a1f931daa20000000000000000000000007a40026a3b9a41754a95eec8c92c6b99886f440c000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000009ae80eb647dd09968488fa1d7e412bf8558a0b7a0000000000000000000000000f9815537d361cb02befd9918c95c97d4d8a4a2bc001a0ba8f1928bb0efc3fcd01524a2039a9a2588fa567cd9a7cc18217e05c615e9d69a0544bfd11425ac7748e76b3795b57a5563e2b0eff47b5428744c62ff19ccfc305")[..]); diff --git a/crates/rpc/rpc-types/Cargo.toml b/crates/rpc/rpc-types/Cargo.toml index 0c0d2127ff..e867d13479 100644 --- a/crates/rpc/rpc-types/Cargo.toml +++ b/crates/rpc/rpc-types/Cargo.toml @@ -20,7 +20,6 @@ serde_with = "3.3" serde_json.workspace = true jsonrpsee-types = { workspace = true, optional = true } alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde"] } -c-kzg = { workspace = true, features = ["serde"] } url = "2.3" # necessary so we don't hit a "undeclared 'std'": # https://github.com/paradigmxyz/reth/pull/177#discussion_r1021172198 diff --git a/crates/rpc/rpc-types/src/eth/engine/payload.rs b/crates/rpc/rpc-types/src/eth/engine/payload.rs index a69aeb7a43..739a6a10ad 100644 --- a/crates/rpc/rpc-types/src/eth/engine/payload.rs +++ b/crates/rpc/rpc-types/src/eth/engine/payload.rs @@ -1,7 +1,9 @@ -use crate::eth::transaction::BlobTransactionSidecar; pub use crate::Withdrawal; +use crate::{ + eth::transaction::BlobTransactionSidecar, + kzg::{Blob, Bytes48}, +}; use alloy_primitives::{Address, Bloom, Bytes, B256, B64, U256}; -use c_kzg::{Blob, Bytes48}; use serde::{ser::SerializeMap, Deserialize, Deserializer, Serialize, Serializer}; /// The execution payload body response that allows for `null` values. @@ -202,8 +204,11 @@ pub struct BlobsBundleV1 { pub blobs: Vec, } -impl From> for BlobsBundleV1 { - fn from(sidecars: Vec) -> Self { +impl BlobsBundleV1 { + /// Creates a new blob bundle from the given sidecars. + /// + /// This folds the sidecar fields into single commit, proof, and blob vectors. + pub fn new(sidecars: impl IntoIterator) -> Self { let (commitments, proofs, blobs) = sidecars.into_iter().fold( (Vec::new(), Vec::new(), Vec::new()), |(mut commitments, mut proofs, mut blobs), sidecar| { @@ -215,9 +220,7 @@ impl From> for BlobsBundleV1 { ); Self { commitments, proofs, blobs } } -} -impl BlobsBundleV1 { /// Take `len` blob data from the bundle. /// /// # Panics @@ -230,6 +233,22 @@ impl BlobsBundleV1 { self.blobs.drain(0..len).collect(), ) } + + /// Returns the sidecar from the bundle + /// + /// # Panics + /// + /// If len is more than the blobs bundle len. + pub fn pop_sidecar(&mut self, len: usize) -> BlobTransactionSidecar { + let (commitments, proofs, blobs) = self.take(len); + BlobTransactionSidecar { commitments, proofs, blobs } + } +} + +impl From> for BlobsBundleV1 { + fn from(sidecars: Vec) -> Self { + Self::new(sidecars) + } } /// An execution payload, which can be either [ExecutionPayloadV1], [ExecutionPayloadV2], or diff --git a/crates/rpc/rpc-types/src/eth/transaction/kzg.rs b/crates/rpc/rpc-types/src/eth/transaction/kzg.rs new file mode 100644 index 0000000000..209fcb7a23 --- /dev/null +++ b/crates/rpc/rpc-types/src/eth/transaction/kzg.rs @@ -0,0 +1,12 @@ +//! KZG type bindings + +use alloy_primitives::FixedBytes; + +/// How many bytes are in a blob +pub const BYTES_PER_BLOB: usize = 131072; + +/// A Blob serialized as 0x-prefixed hex string +pub type Blob = FixedBytes; + +/// A commitment/proof serialized as 0x-prefixed hex string +pub type Bytes48 = FixedBytes<48>; diff --git a/crates/rpc/rpc-types/src/eth/transaction/mod.rs b/crates/rpc/rpc-types/src/eth/transaction/mod.rs index fcaccafad6..d3d20a9f65 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/mod.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/mod.rs @@ -11,6 +11,7 @@ pub use typed::*; mod access_list; mod common; +pub mod kzg; mod receipt; mod request; mod signature; diff --git a/crates/rpc/rpc-types/src/eth/transaction/typed.rs b/crates/rpc/rpc-types/src/eth/transaction/typed.rs index aad6da8e33..ccb779e0fb 100644 --- a/crates/rpc/rpc-types/src/eth/transaction/typed.rs +++ b/crates/rpc/rpc-types/src/eth/transaction/typed.rs @@ -3,10 +3,12 @@ //! transaction deserialized from the json input of an RPC call. Depending on what fields are set, //! it can be converted into the container type [`TypedTransactionRequest`]. -use crate::eth::transaction::AccessList; +use crate::{ + eth::transaction::AccessList, + kzg::{Blob, Bytes48}, +}; use alloy_primitives::{Address, Bytes, B256, U128, U256, U64}; use alloy_rlp::{BufMut, Decodable, Encodable, Error as RlpError}; -use c_kzg::{Blob, Bytes48}; use serde::{Deserialize, Serialize}; /// Container type for various Ethereum transaction requests @@ -136,6 +138,7 @@ impl Decodable for TransactionKind { /// This represents a set of blobs, and its corresponding commitments and proofs. #[derive(Clone, Debug, PartialEq, Eq, Default, Serialize, Deserialize)] +#[repr(C)] pub struct BlobTransactionSidecar { /// The blob data. pub blobs: Vec,