paritytech / paritytech/parity-scale-codec

Allow somehow validate the decoded output

Open
#280 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
287
Forks
103
Avg merge
6d 13h
Merged PRs (30d)
1

Description

For example, we want a custom integer type can only old number less than 100:

#[derive(Encode, Decode)]
struct UIntLessThan100(u8);

impl UIntLessThan100 {
  pub new(val: u8) -> Option<Self> { if val < 100 { Some(Self(val)) } else { None } 
  pub add(self, other: Self) -> u8 { self.0 + other.0 } // this can never overflow because we know 100 + 100 < 255
}

But this will result an invalid struct and break the invariant and causing panics.

let bad: UIntLessThan100 = Decode::decode(&mut &[255u8]).unwrap();

BoundedVec have similar issue and the solution is write a custom decode implementation. But it is tedious for large struct.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue’s Decode example and compare it with the mentioned BoundedVec behavior and custom Decode implementations. Determine how decoded values could be validated without requiring a tedious custom implementation for large structs. Done should provide a way to preserve invariants such as UIntLessThan100 rejecting invalid encoded values.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.