Bump ssz_types to v0.12.2 (#8032)

https://github.com/sigp/lighthouse/issues/8012


  Replace all instances of `VariableList::from` and `FixedVector::from` to their `try_from` variants.

While I tried to use proper error handling in most cases, there were certain situations where adding an `expect` for situations where `try_from` can trivially never fail avoided adding a lot of extra complexity.


Co-Authored-By: Mac L <mjladson@pm.me>

Co-Authored-By: Michael Sproul <michaelsproul@users.noreply.github.com>

Co-Authored-By: Michael Sproul <michael@sigmaprime.io>
This commit is contained in:
Mac L
2025-10-28 08:01:09 +04:00
committed by GitHub
parent 5840004c36
commit f5809aff87
39 changed files with 758 additions and 465 deletions

View File

@@ -1002,8 +1002,9 @@ mod tests {
let mut block: BeaconBlockBellatrix<_, FullPayload<Spec>> =
BeaconBlockBellatrix::empty(spec);
let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 5000).collect::<Vec<_>>());
let tx = VariableList::try_from(vec![0; 1024]).unwrap();
let txs =
VariableList::try_from(std::iter::repeat_n(tx, 5000).collect::<Vec<_>>()).unwrap();
block.body.execution_payload.execution_payload.transactions = txs;
@@ -1021,8 +1022,9 @@ mod tests {
let mut block: BeaconBlockBellatrix<_, FullPayload<Spec>> =
BeaconBlockBellatrix::empty(spec);
let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 100000).collect::<Vec<_>>());
let tx = VariableList::try_from(vec![0; 1024]).unwrap();
let txs =
VariableList::try_from(std::iter::repeat_n(tx, 100000).collect::<Vec<_>>()).unwrap();
block.body.execution_payload.execution_payload.transactions = txs;
@@ -1080,7 +1082,7 @@ mod tests {
data_column_ids: RuntimeVariableList::new(
vec![DataColumnsByRootIdentifier {
block_root: Hash256::zero(),
columns: VariableList::from(vec![0, 1, 2]),
columns: VariableList::try_from(vec![0, 1, 2]).unwrap(),
}],
spec.max_request_blocks(fork_name),
)

View File

@@ -29,15 +29,21 @@ pub const MAX_ERROR_LEN: u64 = 256;
#[derive(Debug, Clone)]
pub struct ErrorType(pub VariableList<u8, MaxErrorLen>);
impl From<String> for ErrorType {
fn from(s: String) -> Self {
Self(VariableList::from(s.as_bytes().to_vec()))
impl From<&str> for ErrorType {
// This will truncate the error if `string.as_bytes()` exceeds `MaxErrorLen`.
fn from(s: &str) -> Self {
let mut bytes = s.as_bytes().to_vec();
bytes.truncate(MAX_ERROR_LEN as usize);
Self(
VariableList::try_from(bytes)
.expect("length should not exceed MaxErrorLen after truncation"),
)
}
}
impl From<&str> for ErrorType {
fn from(s: &str) -> Self {
Self(VariableList::from(s.as_bytes().to_vec()))
impl From<String> for ErrorType {
fn from(s: String) -> Self {
Self::from(s.as_str())
}
}

View File

@@ -70,13 +70,15 @@ pub static BLOB_SIDECAR_SIZE_MINIMAL: LazyLock<usize> =
LazyLock::new(BlobSidecar::<MinimalEthSpec>::max_size);
pub static ERROR_TYPE_MIN: LazyLock<usize> = LazyLock::new(|| {
VariableList::<u8, MaxErrorLen>::from(Vec::<u8>::new())
VariableList::<u8, MaxErrorLen>::try_from(Vec::<u8>::new())
.expect("MaxErrorLen should not exceed MAX_ERROR_LEN")
.as_ssz_bytes()
.len()
});
pub static ERROR_TYPE_MAX: LazyLock<usize> = LazyLock::new(|| {
VariableList::<u8, MaxErrorLen>::from(vec![0u8; MAX_ERROR_LEN as usize])
VariableList::<u8, MaxErrorLen>::try_from(vec![0u8; MAX_ERROR_LEN as usize])
.expect("MaxErrorLen should not exceed MAX_ERROR_LEN")
.as_ssz_bytes()
.len()
});

View File

@@ -26,8 +26,8 @@ type E = MinimalEthSpec;
/// Bellatrix block with length < max_rpc_size.
fn bellatrix_block_small(spec: &ChainSpec) -> BeaconBlock<E> {
let mut block = BeaconBlockBellatrix::<E>::empty(spec);
let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 5000).collect::<Vec<_>>());
let tx = VariableList::try_from(vec![0; 1024]).unwrap();
let txs = VariableList::try_from(std::iter::repeat_n(tx, 5000).collect::<Vec<_>>()).unwrap();
block.body.execution_payload.execution_payload.transactions = txs;
@@ -41,8 +41,8 @@ fn bellatrix_block_small(spec: &ChainSpec) -> BeaconBlock<E> {
/// Hence, we generate a bellatrix block just greater than `MAX_RPC_SIZE` to test rejection on the rpc layer.
fn bellatrix_block_large(spec: &ChainSpec) -> BeaconBlock<E> {
let mut block = BeaconBlockBellatrix::<E>::empty(spec);
let tx = VariableList::from(vec![0; 1024]);
let txs = VariableList::from(std::iter::repeat_n(tx, 100000).collect::<Vec<_>>());
let tx = VariableList::try_from(vec![0; 1024]).unwrap();
let txs = VariableList::try_from(std::iter::repeat_n(tx, 100000).collect::<Vec<_>>()).unwrap();
block.body.execution_payload.execution_payload.transactions = txs;
@@ -1018,14 +1018,17 @@ fn test_tcp_columns_by_root_chunked_rpc() {
},
signature: Signature::empty(),
},
column: vec![vec![0; E::bytes_per_blob()].into()].into(),
kzg_commitments: vec![KzgCommitment::empty_for_testing()].into(),
kzg_proofs: vec![KzgProof::empty()].into(),
column: vec![vec![0; E::bytes_per_cell()].try_into().unwrap()]
.try_into()
.unwrap(),
kzg_commitments: vec![KzgCommitment::empty_for_testing()].try_into().unwrap(),
kzg_proofs: vec![KzgProof::empty()].try_into().unwrap(),
kzg_commitments_inclusion_proof: vec![
Hash256::zero();
E::kzg_commitments_inclusion_proof_depth()
]
.into(),
.try_into()
.unwrap(),
});
let rpc_response = Response::DataColumnsByRoot(Some(data_column.clone()));
@@ -1160,14 +1163,17 @@ fn test_tcp_columns_by_range_chunked_rpc() {
},
signature: Signature::empty(),
},
column: vec![vec![0; E::bytes_per_blob()].into()].into(),
kzg_commitments: vec![KzgCommitment::empty_for_testing()].into(),
kzg_proofs: vec![KzgProof::empty()].into(),
column: vec![vec![0; E::bytes_per_cell()].try_into().unwrap()]
.try_into()
.unwrap(),
kzg_commitments: vec![KzgCommitment::empty_for_testing()].try_into().unwrap(),
kzg_proofs: vec![KzgProof::empty()].try_into().unwrap(),
kzg_commitments_inclusion_proof: vec![
Hash256::zero();
E::kzg_commitments_inclusion_proof_depth()
]
.into(),
.try_into()
.unwrap(),
});
let rpc_response = Response::DataColumnsByRange(Some(data_column.clone()));