Delete RuntimeVariableList::from_vec (#7930)

This method is a footgun because it truncates the list. It is the source of a recent bug:

- https://github.com/sigp/lighthouse/pull/7927


  - Delete uses of `RuntimeVariableList::from_vec` and replace them with `::new` which does validation and can fail.
- Propagate errors where possible, unwrap in tests and use `expect` for obviously-safe uses (in `chain_spec.rs`).
This commit is contained in:
Michael Sproul
2025-08-27 16:52:14 +10:00
committed by GitHub
parent ccf03e1c88
commit d235f2c697
15 changed files with 89 additions and 60 deletions

View File

@@ -481,20 +481,22 @@ pub struct BlocksByRootRequest {
}
impl BlocksByRootRequest {
pub fn new(block_roots: Vec<Hash256>, fork_context: &ForkContext) -> Self {
pub fn new(block_roots: Vec<Hash256>, fork_context: &ForkContext) -> Result<Self, String> {
let max_request_blocks = fork_context
.spec
.max_request_blocks(fork_context.current_fork_name());
let block_roots = RuntimeVariableList::from_vec(block_roots, max_request_blocks);
Self::V2(BlocksByRootRequestV2 { block_roots })
let block_roots = RuntimeVariableList::new(block_roots, max_request_blocks)
.map_err(|e| format!("BlocksByRootRequestV2 too many roots: {e:?}"))?;
Ok(Self::V2(BlocksByRootRequestV2 { block_roots }))
}
pub fn new_v1(block_roots: Vec<Hash256>, fork_context: &ForkContext) -> Self {
pub fn new_v1(block_roots: Vec<Hash256>, fork_context: &ForkContext) -> Result<Self, String> {
let max_request_blocks = fork_context
.spec
.max_request_blocks(fork_context.current_fork_name());
let block_roots = RuntimeVariableList::from_vec(block_roots, max_request_blocks);
Self::V1(BlocksByRootRequestV1 { block_roots })
let block_roots = RuntimeVariableList::new(block_roots, max_request_blocks)
.map_err(|e| format!("BlocksByRootRequestV1 too many roots: {e:?}"))?;
Ok(Self::V1(BlocksByRootRequestV1 { block_roots }))
}
}
@@ -506,12 +508,13 @@ pub struct BlobsByRootRequest {
}
impl BlobsByRootRequest {
pub fn new(blob_ids: Vec<BlobIdentifier>, fork_context: &ForkContext) -> Self {
pub fn new(blob_ids: Vec<BlobIdentifier>, fork_context: &ForkContext) -> Result<Self, String> {
let max_request_blob_sidecars = fork_context
.spec
.max_request_blob_sidecars(fork_context.current_fork_name());
let blob_ids = RuntimeVariableList::from_vec(blob_ids, max_request_blob_sidecars);
Self { blob_ids }
let blob_ids = RuntimeVariableList::new(blob_ids, max_request_blob_sidecars)
.map_err(|e| format!("BlobsByRootRequestV1 too many blob IDs: {e:?}"))?;
Ok(Self { blob_ids })
}
}
@@ -526,9 +529,10 @@ impl<E: EthSpec> DataColumnsByRootRequest<E> {
pub fn new(
data_column_ids: Vec<DataColumnsByRootIdentifier<E>>,
max_request_blocks: usize,
) -> Self {
let data_column_ids = RuntimeVariableList::from_vec(data_column_ids, max_request_blocks);
Self { data_column_ids }
) -> Result<Self, &'static str> {
let data_column_ids = RuntimeVariableList::new(data_column_ids, max_request_blocks)
.map_err(|_| "DataColumnsByRootRequest too many column IDs")?;
Ok(Self { data_column_ids })
}
pub fn max_requested(&self) -> usize {