chore: remove ckzg dep from rpc types (#5392)

This commit is contained in:
Matthias Seitz
2023-11-13 18:37:14 +01:00
committed by GitHub
parent 5baa158fbc
commit 79abf2ba0f
10 changed files with 71 additions and 45 deletions

1
Cargo.lock generated
View File

@@ -6470,7 +6470,6 @@ dependencies = [
"alloy-rlp",
"arbitrary",
"bytes",
"c-kzg",
"itertools 0.11.0",
"jsonrpsee-types",
"proptest",

View File

@@ -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
}

View File

@@ -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<BuiltPayload> for ExecutionPayloadEnvelopeV3 {
// Spec:
// <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#specification-2>
should_override_builder: false,
blobs_bundle: sidecars
.into_iter()
.map(from_primitive_sidecar)
.collect::<Vec<_>>()
.into(),
blobs_bundle: sidecars.into_iter().map(Into::into).collect::<Vec<_>>().into(),
}
}
}

View File

@@ -353,6 +353,20 @@ impl BlobTransactionSidecar {
}
}
impl From<reth_rpc_types::BlobTransactionSidecar> for BlobTransactionSidecar {
fn from(value: reth_rpc_types::BlobTransactionSidecar) -> Self {
// SAFETY: Same repr and size
unsafe { std::mem::transmute(value) }
}
}
impl From<BlobTransactionSidecar> 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::<BlobTransactionSidecar>()] =
[(); std::mem::size_of::<BlobTransactionSidecarRlp>()];
const _: [(); std::mem::size_of::<BlobTransactionSidecar>()] =
[(); std::mem::size_of::<reth_rpc_types::BlobTransactionSidecar>()];
impl BlobTransactionSidecarRlp {
fn wrap_ref(other: &BlobTransactionSidecar) -> &Self {
// SAFETY: Same repr and size

View File

@@ -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")[..]);

View File

@@ -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

View File

@@ -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<Blob>,
}
impl From<Vec<BlobTransactionSidecar>> for BlobsBundleV1 {
fn from(sidecars: Vec<BlobTransactionSidecar>) -> 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<Item = BlobTransactionSidecar>) -> 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<Vec<BlobTransactionSidecar>> 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<Vec<BlobTransactionSidecar>> for BlobsBundleV1 {
fn from(sidecars: Vec<BlobTransactionSidecar>) -> Self {
Self::new(sidecars)
}
}
/// An execution payload, which can be either [ExecutionPayloadV1], [ExecutionPayloadV2], or

View File

@@ -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<BYTES_PER_BLOB>;
/// A commitment/proof serialized as 0x-prefixed hex string
pub type Bytes48 = FixedBytes<48>;

View File

@@ -11,6 +11,7 @@ pub use typed::*;
mod access_list;
mod common;
pub mod kzg;
mod receipt;
mod request;
mod signature;

View File

@@ -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<Blob>,