Skip to content

Commit df77499

Browse files
gabriele-0201pepyakin
authored andcommitted
move namespace validation logic
move the logic from the pallet into primitives
1 parent e0b73ca commit df77499

File tree

6 files changed

+97
-77
lines changed

6 files changed

+97
-77
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sugondat/chain/pallets/blobs/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ sugondat-nmt = { workspace = true }
3030

3131
[dev-dependencies]
3232
sugondat-nmt = { workspace = true, default-features = true }
33-
quickcheck = { workspace = true }
34-
quickcheck_macros = { workspace = true }
3533

3634
# Substrate
3735
sp-core = { workspace = true }
Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,18 @@
11
//! Namespaces as a parameter.
2-
//!
3-
//! Namespaces are encoded as 16-byte arrays, with the following schema:
4-
//! - The first byte is reserved for a version byte which determines the format
5-
//! of the following 15 bytes. At the moment, the only supported value for this byte
6-
//! is `0x00`, which indicates version 0.
7-
//! - In version 0, bytes 1 through 5 are required to be equal to `0x00` and bytes 6 through
8-
//! 15 are allowed to hold any value.
92
103
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
114
use scale_info::TypeInfo;
125
use sp_runtime::RuntimeDebug;
13-
14-
/// An error in namespace validation.
15-
pub enum NamespaceValidationError {
16-
/// Unrecognized version.
17-
UnrecognizedVersion(u8),
18-
/// V0: reserved bytes are non-zero.
19-
V0NonZeroReserved,
20-
}
21-
22-
/// Validate a namespace against known schemas.
23-
pub fn validate(namespace: &[u8; 16]) -> Result<(), NamespaceValidationError> {
24-
if namespace[0] != 0 {
25-
return Err(NamespaceValidationError::UnrecognizedVersion(namespace[0]));
26-
}
27-
if &namespace[1..6] != &[0, 0, 0, 0, 0] {
28-
return Err(NamespaceValidationError::V0NonZeroReserved);
29-
}
30-
31-
Ok(())
32-
}
6+
use sugondat_primitives::namespace;
337

348
/// Type-safe wrapper around an unvalidated blob namespace.
359
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen, Clone, PartialEq, RuntimeDebug)]
3610
pub struct UnvalidatedNamespace([u8; 16]);
3711

3812
impl UnvalidatedNamespace {
3913
/// Validate the namespace, extracting the full data.
40-
pub fn validate(&self) -> Result<u128, NamespaceValidationError> {
41-
validate(&self.0).map(|()| u128::from_be_bytes(self.0))
14+
pub fn validate(&self) -> Result<u128, namespace::NamespaceValidationError> {
15+
namespace::validate(&self.0).map(|()| u128::from_be_bytes(self.0))
4216
}
4317
}
4418

@@ -53,47 +27,3 @@ impl From<u128> for UnvalidatedNamespace {
5327
UnvalidatedNamespace(x.to_be_bytes())
5428
}
5529
}
56-
57-
#[cfg(test)]
58-
mod tests {
59-
use super::*;
60-
use quickcheck::TestResult;
61-
use quickcheck_macros::quickcheck;
62-
use std::matches;
63-
64-
#[quickcheck]
65-
fn namespace_validation_not_v0_fails(version_byte: u8) -> TestResult {
66-
if version_byte == 0x00 {
67-
return TestResult::discard();
68-
}
69-
TestResult::from_bool(matches!(
70-
validate(&[version_byte, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
71-
Err(NamespaceValidationError::UnrecognizedVersion(v)) if v == version_byte,
72-
))
73-
}
74-
75-
#[quickcheck]
76-
fn namespace_validation_v0_reserved_occupied_fails(
77-
reserved: (u8, u8, u8, u8, u8),
78-
) -> TestResult {
79-
if reserved == (0, 0, 0, 0, 0) {
80-
return TestResult::discard();
81-
}
82-
let (a, b, c, d, e) = reserved;
83-
TestResult::from_bool(matches!(
84-
validate(&[0u8, a, b, c, d, e, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
85-
Err(NamespaceValidationError::V0NonZeroReserved),
86-
))
87-
}
88-
89-
#[quickcheck]
90-
fn namespace_validation_v0_works(namespace: Vec<u8>) -> TestResult {
91-
if namespace.len() < 10 {
92-
return TestResult::discard();
93-
}
94-
95-
let mut n = [0u8; 16];
96-
n[6..].copy_from_slice(&namespace[..10]);
97-
TestResult::from_bool(matches!(validate(&n), Ok(())))
98-
}
99-
}

sugondat/chain/primitives/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ sp-runtime = { workspace = true }
1616
sp-core = { workspace = true }
1717
sp-consensus-aura = { workspace = true }
1818

19+
[dev-dependencies]
20+
quickcheck = { workspace = true }
21+
quickcheck_macros = { workspace = true }
22+
1923
[features]
2024
default = ["std"]
2125
std = ["parity-scale-codec/std", "sp-runtime/std", "sp-core/std", "sp-consensus-aura/std"]

sugondat/chain/primitives/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![cfg_attr(not(feature = "std"), no_std)]
22

3+
pub mod namespace;
4+
35
use sp_runtime::{
46
traits::{IdentifyAccount, Verify},
57
MultiSignature,
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//! Namespaces are encoded as 16-byte arrays, with the following schema:
2+
//! - The first byte is reserved for a version byte which determines the format
3+
//! of the following 15 bytes. At the moment, the only supported value for this byte
4+
//! is `0x00`, which indicates version 0.
5+
//! - In version 0, bytes 1 through 5 are required to be equal to `0x00` and bytes 6 through
6+
//! 15 are allowed to hold any value.
7+
8+
use core::fmt;
9+
10+
/// An error in namespace validation.
11+
#[derive(Debug)]
12+
pub enum NamespaceValidationError {
13+
/// Unrecognized version.
14+
UnrecognizedVersion(u8),
15+
/// V0: reserved bytes are non-zero.
16+
V0NonZeroReserved,
17+
}
18+
19+
impl fmt::Display for NamespaceValidationError {
20+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21+
match self {
22+
NamespaceValidationError::UnrecognizedVersion(version) => {
23+
core::write!(f, "Unrecognized version: V{}", version)
24+
}
25+
NamespaceValidationError::V0NonZeroReserved => {
26+
core::write!(f, "V0: reserved bytes are non-zero")
27+
}
28+
}
29+
}
30+
}
31+
32+
/// Validate a namespace against known schemas.
33+
pub fn validate(namespace: &[u8; 16]) -> Result<(), NamespaceValidationError> {
34+
if namespace[0] != 0 {
35+
return Err(NamespaceValidationError::UnrecognizedVersion(namespace[0]));
36+
}
37+
if &namespace[1..6] != &[0, 0, 0, 0, 0] {
38+
return Err(NamespaceValidationError::V0NonZeroReserved);
39+
}
40+
41+
Ok(())
42+
}
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
use quickcheck::TestResult;
48+
use quickcheck_macros::quickcheck;
49+
use std::matches;
50+
51+
#[quickcheck]
52+
fn namespace_validation_not_v0_fails(version_byte: u8) -> TestResult {
53+
if version_byte == 0x00 {
54+
return TestResult::discard();
55+
}
56+
TestResult::from_bool(matches!(
57+
validate(&[version_byte, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
58+
Err(NamespaceValidationError::UnrecognizedVersion(v)) if v == version_byte,
59+
))
60+
}
61+
62+
#[quickcheck]
63+
fn namespace_validation_v0_reserved_occupied_fails(
64+
reserved: (u8, u8, u8, u8, u8),
65+
) -> TestResult {
66+
if reserved == (0, 0, 0, 0, 0) {
67+
return TestResult::discard();
68+
}
69+
let (a, b, c, d, e) = reserved;
70+
TestResult::from_bool(matches!(
71+
validate(&[0u8, a, b, c, d, e, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
72+
Err(NamespaceValidationError::V0NonZeroReserved),
73+
))
74+
}
75+
76+
#[quickcheck]
77+
fn namespace_validation_v0_works(namespace: Vec<u8>) -> TestResult {
78+
if namespace.len() < 10 {
79+
return TestResult::discard();
80+
}
81+
82+
let mut n = [0u8; 16];
83+
n[6..].copy_from_slice(&namespace[..10]);
84+
TestResult::from_bool(matches!(validate(&n), Ok(())))
85+
}
86+
}

0 commit comments

Comments
 (0)