casper-network / casper-network/casper-node

Prefer exhaustive pattern matching around ToBytes/FromBytes

Open
#2,329 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
399
Forks
224
Avg merge
15h 44m
Merged PRs (30d)
2

Description

We should prefer exhaustive wildcard pattern matching and avoid wildcards (`_ => { something; }`) around ToBytes/FromBytes methods.

Recently we had to revert https://github.com/CasperLabs/casper-node/pull/1321 back to a state which includes `_ => U8_SERIALIZED_LENGTH`, which is potentially harmful.

Write new `proptests` and arb generators to validate this change.

Ideally, as part of this task, we should consider using a tag enum with #[derive(FromPrimitive)] over constants:

Instead of https://github.com/CasperLabs/casperlabs-node/blob/556a95a322641fbefdea67abf3da59c9d6ffb169/types/src/execution_result.rs#L50-L69

Something like:

```rust
#[derive(FromPrimitive)]
enum TransformTag {
Identity = 0,
CLValue = 1,
// ...
}

/// ...
fn from_bytes(...) {
let (tag_value, remainder) = u8::from_bytes(bytes)?;
let tag = TransformTag::from_primitive(tag_value).ok_or(Error::Formatting)?;
match tag {
// ... avoid _ => and let your IDE fill in missing variants upon extending
}
}
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the ToBytes/FromBytes implementations and the constants in types/src/execution_result.rs, then inspect the related serialization code and existing proptests and arb generators. Review the reverted PR context before changing wildcard matches. Done means exhaustive tag handling is used, the proposed tag-enum approach is considered, and new property tests validate serialization and deserialization.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
blockchain
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.