Skip to content

Commit e8726dd

Browse files
committed
Begin stable container impl
1 parent 1b151e5 commit e8726dd

File tree

7 files changed

+500
-29
lines changed

7 files changed

+500
-29
lines changed

ssz/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ categories = ["cryptography::cryptocurrencies"]
1414
name = "ssz"
1515

1616
[dev-dependencies]
17-
ethereum_ssz_derive = { version = "0.5.3", path = "../ssz_derive" }
17+
ethereum_ssz_derive = { path = "../ssz_derive" }
1818

1919
[dependencies]
2020
ethereum-types = "0.14.1"

ssz/src/decode/impls.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,18 @@ impl Decode for NonZeroUsize {
248248

249249
impl<T: Decode> Decode for Option<T> {
250250
fn is_ssz_fixed_len() -> bool {
251-
false
251+
T::is_ssz_fixed_len()
252252
}
253+
254+
fn ssz_fixed_len() -> usize {
255+
T::ssz_fixed_len()
256+
}
257+
253258
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, DecodeError> {
254-
let (selector, body) = split_union_bytes(bytes)?;
255-
match selector.into() {
256-
0u8 => Ok(None),
257-
1u8 => <T as Decode>::from_ssz_bytes(body).map(Option::Some),
258-
other => Err(DecodeError::UnionSelectorInvalid(other)),
259+
if bytes.is_empty() {
260+
Ok(None)
261+
} else {
262+
T::from_ssz_bytes(bytes).map(Some)
259263
}
260264
}
261265
}

ssz/src/encode/impls.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,28 @@ impl_encode_for_tuples! {
205205

206206
impl<T: Encode> Encode for Option<T> {
207207
fn is_ssz_fixed_len() -> bool {
208-
false
208+
T::is_ssz_fixed_len()
209+
}
210+
211+
fn ssz_fixed_len() -> usize {
212+
T::ssz_fixed_len()
209213
}
214+
210215
fn ssz_append(&self, buf: &mut Vec<u8>) {
211-
match self {
212-
Option::None => {
213-
let union_selector: u8 = 0u8;
214-
buf.push(union_selector);
215-
}
216-
Option::Some(ref inner) => {
217-
let union_selector: u8 = 1u8;
218-
buf.push(union_selector);
219-
inner.ssz_append(buf);
216+
match &self {
217+
None => {}
218+
Some(_) => {
219+
if let Some(inner) = self.as_ref() {
220+
inner.ssz_append(buf);
221+
}
220222
}
221223
}
222224
}
225+
223226
fn ssz_bytes_len(&self) -> usize {
224-
match self {
225-
Option::None => 1usize,
226-
Option::Some(ref inner) => inner
227-
.ssz_bytes_len()
228-
.checked_add(1)
229-
.expect("encoded length must be less than usize::max_value"),
227+
match &self {
228+
None => 0,
229+
Some(inner) => inner.ssz_bytes_len(),
230230
}
231231
}
232232
}
@@ -594,9 +594,9 @@ mod tests {
594594
#[test]
595595
fn ssz_encode_option_u8() {
596596
let opt: Option<u8> = None;
597-
assert_eq!(opt.as_ssz_bytes(), vec![0]);
597+
assert_eq!(opt.as_ssz_bytes(), vec![]);
598598
let opt: Option<u8> = Some(2);
599-
assert_eq!(opt.as_ssz_bytes(), vec![1, 2]);
599+
assert_eq!(opt.as_ssz_bytes(), vec![2]);
600600
}
601601

602602
#[test]

ssz/tests/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ mod round_trip {
5757
fn option_vec_h256() {
5858
let items: Vec<Option<Vec<H256>>> = vec![
5959
None,
60-
Some(vec![]),
60+
// Some(vec![]) serializes the same as None so it is impossible to differentiate them.
61+
// Is this a bug?
62+
Some(vec![H256::zero()]),
6163
Some(vec![H256::zero(), H256::from([1; 32]), H256::random()]),
6264
];
6365

ssz_derive/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ syn = "1.0.42"
1919
proc-macro2 = "1.0.23"
2020
quote = "1.0.7"
2121
darling = "0.13.0"
22+
ssz_types = { git = "https://github.com/macladson/ssz_types", branch = "stable-container" }
2223

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

0 commit comments

Comments
 (0)