Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ssz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["cryptography::cryptocurrencies"]
name = "ssz"

[dev-dependencies]
ethereum_ssz_derive = { version = "0.5.4", path = "../ssz_derive" }
ethereum_ssz_derive = { path = "../ssz_derive" }

[dependencies]
ethereum-types = "0.14.1"
Expand Down
16 changes: 10 additions & 6 deletions ssz/src/decode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,18 @@ impl Decode for NonZeroUsize {

impl<T: Decode> Decode for Option<T> {
fn is_ssz_fixed_len() -> bool {
false
T::is_ssz_fixed_len()
}

fn ssz_fixed_len() -> usize {
T::ssz_fixed_len()
}

fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
let (selector, body) = split_union_bytes(bytes)?;
match selector.into() {
0u8 => Ok(None),
1u8 => <T as Decode>::from_ssz_bytes(body).map(Option::Some),
other => Err(DecodeError::UnionSelectorInvalid(other)),
if bytes.is_empty() {
Ok(None)
} else {
T::from_ssz_bytes(bytes).map(Some)
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions ssz/src/encode/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,28 @@ impl_encode_for_tuples! {

impl<T: Encode> Encode for Option<T> {
fn is_ssz_fixed_len() -> bool {
false
T::is_ssz_fixed_len()
}

fn ssz_fixed_len() -> usize {
T::ssz_fixed_len()
}

fn ssz_append(&self, buf: &mut Vec<u8>) {
match self {
Option::None => {
let union_selector: u8 = 0u8;
buf.push(union_selector);
}
Option::Some(ref inner) => {
let union_selector: u8 = 1u8;
buf.push(union_selector);
inner.ssz_append(buf);
match &self {
None => {}
Some(_) => {
if let Some(inner) = self.as_ref() {
inner.ssz_append(buf);
}
}
}
}

fn ssz_bytes_len(&self) -> usize {
match self {
Option::None => 1usize,
Option::Some(ref inner) => inner
.ssz_bytes_len()
.checked_add(1)
.expect("encoded length must be less than usize::max_value"),
match &self {
None => 0,
Some(inner) => inner.ssz_bytes_len(),
}
}
}
Expand Down Expand Up @@ -607,9 +607,9 @@ mod tests {
#[test]
fn ssz_encode_option_u8() {
let opt: Option<u8> = None;
assert_eq!(opt.as_ssz_bytes(), vec![0]);
assert_eq!(opt.as_ssz_bytes(), vec![]);
let opt: Option<u8> = Some(2);
assert_eq!(opt.as_ssz_bytes(), vec![1, 2]);
assert_eq!(opt.as_ssz_bytes(), vec![2]);
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion ssz/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ mod round_trip {
fn option_vec_h256() {
let items: Vec<Option<Vec<H256>>> = vec![
None,
Some(vec![]),
// Some(vec![]) serializes the same as None so it is impossible to differentiate them.
// Is this a bug?
Some(vec![H256::zero()]),
Some(vec![H256::zero(), H256::from([1; 32]), H256::random()]),
];

Expand Down
3 changes: 2 additions & 1 deletion ssz_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ syn = "1.0.42"
proc-macro2 = "1.0.23"
quote = "1.0.7"
darling = "0.13.0"
ssz_types = { git = "https://github.com/macladson/ssz_types", branch = "stable-container" }

[dev-dependencies]
ethereum_ssz = { path = "../ssz" }
ethereum_ssz = { git = "https://github.com/macladson/ethereum_ssz", branch = "stable-container" }
Loading