Skip to content

Commit b9fd335

Browse files
hangleangpovi
authored andcommitted
Add blobs v2 rpc, update blob index and length check
1 parent 40beb6c commit b9fd335

File tree

8 files changed

+78
-26
lines changed

8 files changed

+78
-26
lines changed

fork_choice_store/src/store.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1710,8 +1710,14 @@ impl<P: Preset> Store<P> {
17101710
let block_header = blob_sidecar.signed_block_header.message;
17111711

17121712
// [REJECT] The sidecar's index is consistent with MAX_BLOBS_PER_BLOCK -- i.e. blob_sidecar.index < MAX_BLOBS_PER_BLOCK.
1713+
let max_blobs_per_block =
1714+
if self.chain_config().phase_at_slot::<P>(block_header.slot) == Phase::Electra {
1715+
P::MaxBlobsPerBlockElectra::U64
1716+
} else {
1717+
P::MaxBlobsPerBlock::U64
1718+
};
17131719
ensure!(
1714-
blob_sidecar.index < P::MaxBlobsPerBlock::U64,
1720+
blob_sidecar.index < max_blobs_per_block,
17151721
Error::BlobSidecarInvalidIndex { blob_sidecar },
17161722
);
17171723

p2p/src/network.rs

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,16 @@ impl<P: Preset> Network<P> {
951951
debug!("received BeaconBlocksByRange request (peer_id: {peer_id}, request: {request:?})");
952952

953953
let start_slot = request.start_slot();
954-
let difference = request.count().min(MAX_FOR_DOS_PREVENTION);
954+
let max_request_blocks = match request {
955+
OldBlocksByRangeRequest::V1(_) => self.controller.chain_config().max_request_blocks,
956+
OldBlocksByRangeRequest::V2(_) => {
957+
self.controller.chain_config().max_request_blocks_deneb
958+
}
959+
};
960+
let difference = request
961+
.count()
962+
.min(max_request_blocks)
963+
.min(MAX_FOR_DOS_PREVENTION);
955964

956965
// `end_slot` is exclusive.
957966
let end_slot = start_slot
@@ -1013,17 +1022,28 @@ impl<P: Preset> Network<P> {
10131022
) -> Result<()> {
10141023
debug!("received BlobSidecarsByRange request (peer_id: {peer_id}, request: {request:?})");
10151024

1016-
let BlobsByRangeRequest { start_slot, count } = request;
1017-
let difference = count
1018-
.min(self.controller.chain_config().max_request_blob_sidecars)
1025+
let start_slot = request.start_slot();
1026+
let max_request_blob_sidecars = match request {
1027+
BlobsByRangeRequest::V1(_) => self.controller.chain_config().max_request_blob_sidecars,
1028+
BlobsByRangeRequest::V2(_) => {
1029+
self.controller
1030+
.chain_config()
1031+
.max_request_blob_sidecars_electra
1032+
}
1033+
};
1034+
let difference = request
1035+
.count()
1036+
.min(max_request_blob_sidecars)
10191037
.min(MAX_FOR_DOS_PREVENTION);
10201038

1021-
let end_slot = start_slot
1022-
.checked_add(difference)
1023-
.ok_or(Error::EndSlotOverflow {
1024-
start_slot,
1025-
difference,
1026-
})?;
1039+
let end_slot =
1040+
request
1041+
.start_slot()
1042+
.checked_add(difference)
1043+
.ok_or(Error::EndSlotOverflow {
1044+
start_slot,
1045+
difference,
1046+
})?;
10271047

10281048
let controller = self.controller.clone_arc();
10291049
let network_to_service_tx = self.network_to_service_tx.clone();
@@ -1078,11 +1098,18 @@ impl<P: Preset> Network<P> {
10781098
debug!("received BlobsByRootRequest request (peer_id: {peer_id}, request: {request:?})");
10791099

10801100
// TODO(feature/deneb): MIN_EPOCHS_FOR_BLOB_SIDECARS_REQUESTS
1081-
let BlobsByRootRequest { blob_ids } = request;
1101+
let max_request_blob_sidecars = match request {
1102+
BlobsByRootRequest::V1(_) => self.controller.chain_config().max_request_blob_sidecars,
1103+
BlobsByRootRequest::V2(_) => {
1104+
self.controller
1105+
.chain_config()
1106+
.max_request_blob_sidecars_electra
1107+
}
1108+
};
1109+
let blob_ids = request.blob_ids();
10821110

10831111
let controller = self.controller.clone_arc();
10841112
let network_to_service_tx = self.network_to_service_tx.clone();
1085-
let max_request_blob_sidecars = self.controller.chain_config().max_request_blob_sidecars;
10861113

10871114
self.dedicated_executor
10881115
.spawn(async move {
@@ -1136,6 +1163,10 @@ impl<P: Preset> Network<P> {
11361163
request_id: IncomingRequestId,
11371164
request: BlocksByRootRequest,
11381165
) {
1166+
let max_request_blocks = match request {
1167+
BlocksByRootRequest::V1(_) => self.controller.chain_config().max_request_blocks,
1168+
BlocksByRootRequest::V2(_) => self.controller.chain_config().max_request_blocks_deneb,
1169+
};
11391170
let block_roots = request.block_roots();
11401171

11411172
debug!(
@@ -1149,7 +1180,7 @@ impl<P: Preset> Network<P> {
11491180
.spawn(async move {
11501181
let block_roots = block_roots
11511182
.into_iter()
1152-
.take(MAX_FOR_DOS_PREVENTION.try_into()?);
1183+
.take(MAX_FOR_DOS_PREVENTION.min(max_request_blocks).try_into()?);
11531184

11541185
let blocks = controller.blocks_by_root(block_roots)?;
11551186

@@ -1652,7 +1683,7 @@ impl<P: Preset> Network<P> {
16521683
count: u64,
16531684
) {
16541685
// TODO: is count capped in eth2_libp2p?
1655-
let request = BlobsByRangeRequest { start_slot, count };
1686+
let request = BlobsByRangeRequest::new(start_slot, count);
16561687

16571688
debug!(
16581689
"sending BlobSidecarsByRange request (request_id: {request_id} peer_id: {peer_id}, \

transition_functions/src/deneb/block_processing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn process_execution_payload_for_gossip<P: Preset>(
220220

221221
ensure!(
222222
in_block <= maximum,
223-
Error::<P>::TooManyBlockKzgCommitments { in_block },
223+
Error::<P>::TooManyBlockKzgCommitments { in_block, maximum },
224224
);
225225

226226
Ok(())

transition_functions/src/electra/block_processing.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ fn process_execution_payload_for_gossip<P: Preset>(
216216
Error::<P>::ExecutionPayloadTimestampMismatch { computed, in_block },
217217
);
218218

219-
// > [New in Deneb:EIP4844] Verify commitments are under limit
220-
let maximum = P::MaxBlobsPerBlock::USIZE;
219+
// > [Modified in Electra:EIP7691] Verify commitments are under limit
220+
let maximum = P::MaxBlobsPerBlockElectra::USIZE;
221221
let in_block = body.blob_kzg_commitments.len();
222222

223223
ensure!(
224224
in_block <= maximum,
225-
Error::<P>::TooManyBlockKzgCommitments { in_block },
225+
Error::<P>::TooManyBlockKzgCommitments { in_block, maximum },
226226
);
227227

228228
Ok(())

transition_functions/src/unphased/error.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use thiserror::Error;
2-
use typenum::Unsigned as _;
32
use types::{
43
capella::containers::Withdrawal,
54
combined::Attestation,
@@ -101,11 +100,8 @@ pub enum Error<P: Preset> {
101100
SlotNotLater { current: Slot, target: Slot },
102101
#[error("state root in block ({in_block:?}) does not match state ({computed:?})")]
103102
StateRootMismatch { computed: H256, in_block: H256 },
104-
#[error(
105-
"too many blob KZG commitments (maximum: {}, in_block: {in_block})",
106-
P::MaxBlobsPerBlock::USIZE
107-
)]
108-
TooManyBlockKzgCommitments { in_block: usize },
103+
#[error("too many blob KZG commitments (maximum: {maximum}, in_block: {in_block})")]
104+
TooManyBlockKzgCommitments { maximum: usize, in_block: usize },
109105
#[error("validator {index} exited in epoch {exit_epoch}")]
110106
ValidatorAlreadyExited {
111107
index: ValidatorIndex,

types/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ pub struct Config {
148148
pub blob_sidecar_subnet_count: NonZeroU64,
149149
#[serde(with = "serde_utils::string_or_native")]
150150
pub data_column_sidecar_subnet_count: u64,
151+
#[serde(with = "serde_utils::string_or_native")]
152+
pub max_request_blob_sidecars_electra: u64,
153+
#[serde(with = "serde_utils::string_or_native")]
154+
pub blob_sidecar_subnet_count_electra: NonZeroU64,
151155

152156
// Transition
153157
pub terminal_block_hash: ExecutionBlockHash,
@@ -245,6 +249,8 @@ impl Default for Config {
245249
min_epochs_for_block_requests: 33024,
246250
blob_sidecar_subnet_count: nonzero!(6_u64),
247251
data_column_sidecar_subnet_count: 64,
252+
max_request_blob_sidecars_electra: 1152,
253+
blob_sidecar_subnet_count_electra: nonzero!(9_u64),
248254

249255
// Transition
250256
terminal_block_hash: ExecutionBlockHash::zero(),

types/src/preset.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ pub trait Preset: Copy + Eq + Ord + Hash + Default + Debug + Send + Sync + 'stat
176176
+ Debug
177177
+ Send
178178
+ Sync;
179+
type MaxBlobsPerBlockElectra: MerkleElements<Blob<Self>>
180+
+ MerkleElements<KzgCommitment>
181+
+ MerkleElements<KzgProof>
182+
+ Eq
183+
+ Debug
184+
+ Send
185+
+ Sync;
179186

180187
// Derived type-level variables
181188
type MaxAggregatorsPerSlot: MerkleElements<ValidatorIndex>
@@ -297,6 +304,7 @@ impl Preset for Mainnet {
297304
type PendingDepositsLimit = U134217728;
298305
type PendingConsolidationsLimit = U262144;
299306
type PendingPartialWithdrawalsLimit = U134217728;
307+
type MaxBlobsPerBlockElectra = U9;
300308

301309
// Derived type-level variables
302310
type MaxAggregatorsPerSlot = Prod<Self::MaxValidatorsPerCommittee, Self::MaxCommitteesPerSlot>;
@@ -361,6 +369,7 @@ impl Preset for Minimal {
361369
type MaxAttesterSlashingsElectra;
362370
type MaxConsolidationRequestsPerPayload;
363371
type PendingDepositsLimit;
372+
type MaxBlobsPerBlockElectra;
364373
}
365374

366375
// Phase 0
@@ -463,6 +472,7 @@ impl Preset for Medalla {
463472
type PendingDepositsLimit;
464473
type PendingConsolidationsLimit;
465474
type PendingPartialWithdrawalsLimit;
475+
type MaxBlobsPerBlockElectra;
466476

467477
// Derived type-level variables
468478
type MaxAggregatorsPerSlot;
@@ -885,6 +895,8 @@ pub struct ElectraPreset {
885895
pending_partial_withdrawals_limit: u64,
886896
#[serde(with = "serde_utils::string_or_native")]
887897
whistleblower_reward_quotient_electra: NonZeroU64,
898+
#[serde(with = "serde_utils::string_or_native")]
899+
max_blobs_per_block_electra: u64,
888900
}
889901

890902
impl ElectraPreset {
@@ -905,6 +917,7 @@ impl ElectraPreset {
905917
pending_consolidations_limit: P::PendingConsolidationsLimit::U64,
906918
pending_partial_withdrawals_limit: P::PendingPartialWithdrawalsLimit::U64,
907919
whistleblower_reward_quotient_electra: P::WHISTLEBLOWER_REWARD_QUOTIENT_ELECTRA,
920+
max_blobs_per_block_electra: P::MaxBlobsPerBlockElectra::U64,
908921
}
909922
}
910923
}

0 commit comments

Comments
 (0)