ralexstokes / ssz-rs

Implementation of ethereum's `ssz`
Apache License 2.0
103 stars 41 forks source link

Remove panic defaults in List and Vector #56

Closed doubledup closed 1 year ago

doubledup commented 1 year ago

These panic cases shouldn't have come up in practice, but removing them from the source ensures they can't.

Also did a pass over the other default implementations, but there aren't any explicit panics and the functions used look safe. An exception is BitVector that has an assert! for N > 0 - I've added the same assert to Vector. An initial attempt to move this to a compile-time check with static_assertions gave some errors I couldn't easily interpret, so left it for now.

doubledup commented 1 year ago

I've gone with try_from, unwrap and a safety comment for now. This way we keep the single entry point and unwrap is addressed in the same way as the other uses.

As an aside, are a List[type, 0] and Bitlist[0] valid? The spec makes this exclusion for the vector types, but not the lists. Seems like a reasonable exclusion because the lists would have to be empty.

ralexstokes commented 1 year ago

As an aside, are a List[type, 0] and Bitlist[0] valid?

I ran into the same question the other day haha

the spec says they are valid via omission and while they are strange types I don't see a problem w/ them

I'd have to go review the spec to say more but the high-level idea is that we don't want "empty" fixed sized types but variably-sized types are fine

codecov[bot] commented 1 year ago

Codecov Report

Patch coverage: 14.77% and project coverage change: -16.12 :warning:

Comparison is base (9cc2fb1) 59.91% compared to head (0135d8e) 43.80%.

:exclamation: Current head 0135d8e differs from pull request most recent head e42bbfc. Consider uploading reports for the commit e42bbfc to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #56 +/- ## =========================================== - Coverage 59.91% 43.80% -16.12% =========================================== Files 20 20 Lines 953 1372 +419 =========================================== + Hits 571 601 +30 - Misses 382 771 +389 ``` | [Impacted Files](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes) | Coverage Δ | | |---|---|---| | [ssz-rs-derive/src/lib.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzLWRlcml2ZS9zcmMvbGliLnJz) | `0.00% <0.00%> (ø)` | | | [ssz-rs-test-gen/src/main.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzLXRlc3QtZ2VuL3NyYy9tYWluLnJz) | `0.00% <0.00%> (ø)` | | | [ssz-rs/src/de.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9kZS5ycw==) | `75.51% <0.00%> (+4.67%)` | :arrow_up: | | [ssz-rs/src/lib.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9saWIucnM=) | `100.00% <ø> (+50.00%)` | :arrow_up: | | [ssz-rs/src/merkleization/mod.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9tZXJrbGVpemF0aW9uL21vZC5ycw==) | `90.90% <ø> (+0.78%)` | :arrow_up: | | [ssz-rs/src/serde.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9zZXJkZS5ycw==) | `100.00% <ø> (+7.14%)` | :arrow_up: | | [ssz-rs/src/bitvector.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9iaXR2ZWN0b3IucnM=) | `82.95% <81.81%> (+7.95%)` | :arrow_up: | | [ssz-rs/src/bitlist.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9iaXRsaXN0LnJz) | `78.49% <89.47%> (+6.90%)` | :arrow_up: | | [ssz-rs/src/list.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9saXN0LnJz) | `75.00% <100.00%> (+16.23%)` | :arrow_up: | | [ssz-rs/src/ser.rs](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes#diff-c3N6LXJzL3NyYy9zZXIucnM=) | `76.59% <100.00%> (-1.41%)` | :arrow_down: | | ... and [2 more](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes) | | ... and [5 files with indirect coverage changes](https://app.codecov.io/gh/ralexstokes/ssz-rs/pull/56/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Alex+Stokes)

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.