Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change structure of Transactions for faster block decoding #6609

Draft
wants to merge 14 commits into
base: unstable
Choose a base branch
from
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ slog-term = "2"
sloggers = { version = "2", features = ["json"] }
smallvec = { version = "1.11.2", features = ["arbitrary"] }
snap = "1"
ssz_types = "0.8"
ssz_types = { rev = "26716095d94e02a2cbd72c94a8b3b8549ecf14d1", git = "https://github.com/paulhauner/ssz_types.git" }
strum = { version = "0.24", features = ["derive"] }
superstruct = "0.8"
syn = "1"
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
debug!(
self.log,
"Reconstructed txn";
"bytes" => format!("0x{}", hex::encode(&**txn)),
"bytes" => format!("0x{}", hex::encode(txn)),
);
}

Expand Down
4 changes: 2 additions & 2 deletions beacon_node/beacon_chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,7 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
execution_layer::test_utils::generate_blobs::<E>(num_blobs).unwrap();

payload.execution_payload.transactions = <_>::default();
for tx in Vec::from(transactions) {
for tx in &transactions {
payload.execution_payload.transactions.push(tx).unwrap();
}
message.body.blob_kzg_commitments = bundle.commitments.clone();
Expand All @@ -2857,7 +2857,7 @@ pub fn generate_rand_block_and_blobs<E: EthSpec>(
let (bundle, transactions) =
execution_layer::test_utils::generate_blobs::<E>(num_blobs).unwrap();
payload.execution_payload.transactions = <_>::default();
for tx in Vec::from(transactions) {
for tx in &transactions {
payload.execution_payload.transactions.push(tx).unwrap();
}
message.body.blob_kzg_commitments = bundle.commitments.clone();
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/execution_layer/src/block_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn calculate_execution_block_hash<E: EthSpec>(
// We're currently using a deprecated Parity library for this. We should move to a
// better alternative when one appears, possibly following Reth.
let rlp_transactions_root = ordered_trie_root::<KeccakHasher, _>(
payload.transactions().iter().map(|txn_bytes| &**txn_bytes),
payload.transactions().iter().map(|txn_bytes| txn_bytes),
);

// Calculate withdrawals root (post-Capella).
Expand Down
2 changes: 0 additions & 2 deletions beacon_node/execution_layer/src/engine_api/json_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub struct JsonExecutionPayload<E: EthSpec> {
pub base_fee_per_gas: Uint256,

pub block_hash: ExecutionBlockHash,
#[serde(with = "ssz_types::serde_utils::list_of_hex_var_list")]
pub transactions: Transactions<E>,
#[superstruct(only(V2, V3, V4))]
pub withdrawals: VariableList<JsonWithdrawal, E::MaxWithdrawalsPerPayload>,
Expand Down Expand Up @@ -792,7 +791,6 @@ impl From<ForkchoiceUpdatedResponse> for JsonForkchoiceUpdatedV1Response {
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound = "E: EthSpec")]
pub struct JsonExecutionPayloadBodyV1<E: EthSpec> {
#[serde(with = "ssz_types::serde_utils::list_of_hex_var_list")]
pub transactions: Transactions<E>,
pub withdrawals: Option<VariableList<JsonWithdrawal, E::MaxWithdrawalsPerPayload>>,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ impl<E: EthSpec> ExecutionBlockGenerator<E> {
let mut rng = self.rng.lock();
let num_blobs = rng.gen::<usize>() % (E::max_blobs_per_block() + 1);
let (bundle, transactions) = generate_blobs(num_blobs)?;
for tx in Vec::from(transactions) {
for tx in &transactions {
execution_payload
.transactions_mut()
.push(tx)
Expand Down Expand Up @@ -707,13 +707,15 @@ pub fn generate_blobs<E: EthSpec>(
let (kzg_commitment, kzg_proof, blob) = load_test_blobs_bundle::<E>()?;

let mut bundle = BlobsBundle::<E>::default();
let mut transactions = vec![];
let mut transactions = Transactions::default();

for blob_index in 0..n_blobs {
let tx = static_valid_tx::<E>()
.map_err(|e| format!("error creating valid tx SSZ bytes: {:?}", e))?;

transactions.push(tx);
transactions
.push(&tx)
.map_err(|e| format!("invalid tx: {e:?}"))?;
bundle
.blobs
.push(blob.clone())
Expand All @@ -728,7 +730,7 @@ pub fn generate_blobs<E: EthSpec>(
.map_err(|_| format!("blobs are full, blob index: {:?}", blob_index))?;
}

Ok((bundle, transactions.into()))
Ok((bundle, transactions))
}

pub fn static_valid_tx<E: EthSpec>() -> Result<Transaction<E::MaxBytesPerTransaction>, String> {
Expand Down
8 changes: 3 additions & 5 deletions beacon_node/execution_layer/src/versioned_hashes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloy_consensus::TxEnvelope;
use alloy_rlp::Decodable;
use types::{EthSpec, ExecutionPayloadRef, Hash256, Unsigned, VersionedHash};
use types::{EthSpec, ExecutionPayloadRef, Hash256, VersionedHash};

#[derive(Debug)]
pub enum Error {
Expand Down Expand Up @@ -59,10 +59,8 @@ pub fn extract_versioned_hashes_from_transactions<E: EthSpec>(
Ok(versioned_hashes)
}

pub fn beacon_tx_to_tx_envelope<N: Unsigned>(
tx: &types::Transaction<N>,
) -> Result<TxEnvelope, Error> {
let tx_bytes = Vec::from(tx.clone());
pub fn beacon_tx_to_tx_envelope(tx: &[u8]) -> Result<TxEnvelope, Error> {
let tx_bytes = Vec::from(tx);
TxEnvelope::decode(&mut tx_bytes.as_slice())
.map_err(|e| Error::DecodingTransaction(e.to_string()))
}
Expand Down
6 changes: 1 addition & 5 deletions consensus/types/src/execution_payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash;

pub type Transaction<N> = VariableList<u8, N>;
pub type Transactions<E> = VariableList<
Transaction<<E as EthSpec>::MaxBytesPerTransaction>,
<E as EthSpec>::MaxTransactionsPerPayload,
>;
pub type Transactions<E> = TransactionsOpaque<E>;

pub type Withdrawals<E> = VariableList<Withdrawal, <E as EthSpec>::MaxWithdrawalsPerPayload>;

Expand Down Expand Up @@ -80,7 +77,6 @@ pub struct ExecutionPayload<E: EthSpec> {
pub base_fee_per_gas: Uint256,
#[superstruct(getter(copy))]
pub block_hash: ExecutionBlockHash,
#[serde(with = "ssz_types::serde_utils::list_of_hex_var_list")]
pub transactions: Transactions<E>,
#[superstruct(only(Capella, Deneb, Electra))]
pub withdrawals: Withdrawals<E>,
Expand Down
2 changes: 2 additions & 0 deletions consensus/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub mod signed_voluntary_exit;
pub mod signing_data;
pub mod sync_committee_subscription;
pub mod sync_duty;
pub mod transactions_opaque;
pub mod validator;
pub mod validator_subscription;
pub mod voluntary_exit;
Expand Down Expand Up @@ -247,6 +248,7 @@ pub use crate::sync_committee_subscription::SyncCommitteeSubscription;
pub use crate::sync_duty::SyncDuty;
pub use crate::sync_selection_proof::SyncSelectionProof;
pub use crate::sync_subnet_id::SyncSubnetId;
pub use crate::transactions_opaque::TransactionsOpaque;
pub use crate::validator::Validator;
pub use crate::validator_registration_data::*;
pub use crate::validator_subscription::ValidatorSubscription;
Expand Down
Loading
Loading