Skip to content

Commit 0c62d24

Browse files
committed
consensus logic fixes and cleanup
1 parent 474f1a0 commit 0c62d24

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

consensus/state_processing/src/per_block_processing/eip4844/eip4844.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use crate::BlockProcessingError;
2-
use eth2_hashing::{hash, hash_fixed};
2+
use eth2_hashing::hash_fixed;
33
use itertools::{EitherOrBoth, Itertools};
44
use ssz::Decode;
55
use ssz_types::VariableList;
6-
use std::slice::Iter;
7-
use std::vec::IntoIter;
86
use types::consts::eip4844::{BLOB_TX_TYPE, VERSIONED_HASH_VERSION_KZG};
97
use types::{
108
AbstractExecPayload, BeaconBlockBodyRef, EthSpec, ExecPayload, FullPayload, FullPayloadRef,
@@ -47,9 +45,21 @@ pub fn verify_kzg_commitments_against_transactions<T: EthSpec>(
4745
// Need to use `itertools::zip_longest` here because just zipping hides if one iter is shorter
4846
// and `itertools::zip_eq` panics.
4947
.zip_longest(kzg_commitments.into_iter())
50-
.map(|next| match next {
51-
EitherOrBoth::Both(hash, commitmnet) => Ok((hash?, commitmnet)),
52-
_ => Err(BlockProcessingError::BlobVersionHashMismatch),
48+
.enumerate()
49+
.map(|(index, next)| match next {
50+
EitherOrBoth::Both(hash, commitment) => Ok((hash?, commitment)),
51+
// The number of versioned hashes from the blob transactions exceeds the number of
52+
// commitments in the block.
53+
EitherOrBoth::Left(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
54+
commitments_processed_in_block: index,
55+
commitments_processed_in_transactions: index + 1,
56+
}),
57+
// The number of commitments in the block exceeds the number of versioned hashes
58+
// in the blob transactions.
59+
EitherOrBoth::Right(_) => Err(BlockProcessingError::BlobNumCommitmentsMismatch {
60+
commitments_processed_in_block: index + 1,
61+
commitments_processed_in_transactions: index,
62+
}),
5363
});
5464

5565
itertools::process_results(zipped_iter, |mut iter| {
@@ -60,6 +70,7 @@ pub fn verify_kzg_commitments_against_transactions<T: EthSpec>(
6070
})?
6171
}
6272

73+
/// Only transactions of type `BLOB_TX_TYPE` should be passed into this function.
6374
fn tx_peek_blob_versioned_hashes<T: EthSpec>(
6475
opaque_tx: &Transaction<T::MaxBytesPerTransaction>,
6576
) -> Result<
@@ -90,12 +101,13 @@ fn tx_peek_blob_versioned_hashes<T: EthSpec>(
90101
let num_hashes = (tx_len - blob_versioned_hashes_offset as usize) / 32;
91102

92103
Ok((0..num_hashes).into_iter().map(move |i| {
93-
let bytes = opaque_tx.get(i..i + 32).ok_or(
94-
BlockProcessingError::BlobVersionHashIndexOutOfBounds {
104+
let next_version_hash_index = blob_versioned_hashes_offset as usize + (i * 32);
105+
let bytes = opaque_tx
106+
.get(next_version_hash_index..next_version_hash_index + 32)
107+
.ok_or(BlockProcessingError::BlobVersionHashIndexOutOfBounds {
95108
length: tx_len,
96-
index: i + 32,
97-
},
98-
)?;
109+
index: next_version_hash_index as usize + 32,
110+
})?;
99111
Ok(VersionedHash::from_slice(bytes))
100112
}))
101113
}

consensus/state_processing/src/per_block_processing/errors.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ pub enum BlockProcessingError {
7373
},
7474
ExecutionInvalid,
7575
BlobVersionHashMismatch,
76+
/// The number of commitments in blob transactions in the payload does not match the number
77+
/// of commitments in the block.
78+
BlobNumCommitmentsMismatch {
79+
commitments_processed_in_block: usize,
80+
/// This number depic
81+
commitments_processed_in_transactions: usize,
82+
},
7683
BlobVersionHashIndexOutOfBounds {
7784
index: usize,
7885
length: usize,

consensus/types/src/payload.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub trait ExecPayload<T: EthSpec>: Debug + Clone + PartialEq + Hash + TreeHash +
3434
fn block_hash(&self) -> ExecutionBlockHash;
3535
fn fee_recipient(&self) -> Address;
3636
fn gas_limit(&self) -> u64;
37+
38+
/// This will return `None` on blinded blocks or pre-merge blocks.
3739
fn transactions(&self) -> Option<&Transactions<T>>;
3840

3941
// Is this a default payload? (pre-merge)

0 commit comments

Comments
 (0)