Skip to content

Commit 6f6eebf

Browse files
authored
[Altair] Make anonymous var-length SSZ decoding explicit (#2362)
* Add explicit function for anon var length items * Remove unused import * Update comment
1 parent 8377565 commit 6f6eebf

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

beacon_node/store/src/impls/beacon_state.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::*;
22
use ssz::{DecodeError, Encode};
33
use ssz_derive::Encode;
44
use std::convert::TryInto;
5-
use types::beacon_state::{BeaconStateBase, CloneConfig, CommitteeCache, CACHED_EPOCHS};
5+
use types::beacon_state::{CloneConfig, CommitteeCache, CACHED_EPOCHS};
66

77
pub fn store_full_state<E: EthSpec>(
88
state_root: &Hash256,
@@ -65,9 +65,7 @@ impl<T: EthSpec> StorageContainer<T> {
6565
// compose with the other SSZ utils, so we duplicate some parts of `ssz_derive` here.
6666
let mut builder = ssz::SszDecoderBuilder::new(bytes);
6767

68-
// Register the message type as `BeaconStateBase`, even though that isn't accurate.
69-
// Really we just need some variable-length type to provide here.
70-
builder.register_type::<BeaconStateBase<T>>()?;
68+
builder.register_anonymous_variable_length_item()?;
7169
builder.register_type::<Vec<CommitteeCache>>()?;
7270

7371
let mut decoder = builder.build()?;

consensus/ssz/src/decode.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,31 @@ impl<'a> SszDecoderBuilder<'a> {
145145
}
146146
}
147147

148+
/// Registers a variable-length object as the next item in `bytes`, without specifying the
149+
/// actual type.
150+
///
151+
/// ## Notes
152+
///
153+
/// Use of this function is generally discouraged since it cannot detect if some type changes
154+
/// from variable to fixed length.
155+
///
156+
/// Use `Self::register_type` wherever possible.
157+
pub fn register_anonymous_variable_length_item(&mut self) -> Result<(), DecodeError> {
158+
struct Anonymous;
159+
160+
impl Decode for Anonymous {
161+
fn is_ssz_fixed_len() -> bool {
162+
false
163+
}
164+
165+
fn from_ssz_bytes(_bytes: &[u8]) -> Result<Self, DecodeError> {
166+
unreachable!("Anonymous should never be decoded")
167+
}
168+
}
169+
170+
self.register_type::<Anonymous>()
171+
}
172+
148173
/// Declares that some type `T` is the next item in `bytes`.
149174
pub fn register_type<T: Decode>(&mut self) -> Result<(), DecodeError> {
150175
if T::is_ssz_fixed_len() {

consensus/types/src/signed_beacon_block.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ impl<E: EthSpec> SignedBeaconBlock<E> {
7676
// compose with the other SSZ utils, so we duplicate some parts of `ssz_derive` here.
7777
let mut builder = ssz::SszDecoderBuilder::new(bytes);
7878

79-
// Register the message type as `BeaconBlockBase`, even though that isn't accurate.
80-
// Really we just need some variable-length type to provide here.
81-
builder.register_type::<BeaconBlockBase<E>>()?;
79+
builder.register_anonymous_variable_length_item()?;
8280
builder.register_type::<Signature>()?;
8381

8482
let mut decoder = builder.build()?;

0 commit comments

Comments
 (0)