mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 17:31:05 +00:00
Update to parity-scale-codec (#3232)
* WIP: update codec * WIP * compiling * WIP * rename parity-scale-codec to codec * WIP * fix * remove old comments * use published crates * fix expected error msg * bump version * fmt and fix * remove old comment * fix wrong decoding impl * implement encode like for structures * undo removal of old pending changes * trailingzeroinput * Apply suggestions from code review Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com> * update codec * fmt * version is 1.0.0 * show more error * fmt
This commit is contained in:
committed by
Bastian Köcher
parent
a0d442333f
commit
4ed67e03a4
@@ -12,22 +12,36 @@ use std::fmt;
|
||||
use std::error::Error as StdError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
/// Error concerning the Parity-Codec based decoder.
|
||||
/// Error for trie node decoding.
|
||||
pub enum Error {
|
||||
/// Bad format.
|
||||
BadFormat,
|
||||
/// Decoding error.
|
||||
Decode(codec::Error)
|
||||
}
|
||||
|
||||
impl From<codec::Error> for Error {
|
||||
fn from(x: codec::Error) -> Self {
|
||||
Error::Decode(x)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="std")]
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
"codec error"
|
||||
match self {
|
||||
Error::BadFormat => "Bad format error",
|
||||
Error::Decode(_) => "Decoding error",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="std")]
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt::Debug::fmt(&self, f)
|
||||
match self {
|
||||
Error::Decode(e) => write!(f, "Decode error: {}", e.what()),
|
||||
Error::BadFormat => write!(f, "Bad format"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> {
|
||||
|
||||
fn decode(data: &[u8]) -> rstd::result::Result<Node, Self::Error> {
|
||||
let input = &mut &*data;
|
||||
let head = NodeHeader::decode(input).ok_or(Error::BadFormat)?;
|
||||
let head = NodeHeader::decode(input)?;
|
||||
match head {
|
||||
NodeHeader::Null => Ok(Node::Empty),
|
||||
NodeHeader::Branch(has_value, nibble_count) => {
|
||||
@@ -69,7 +69,7 @@ impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> {
|
||||
let bitmap_slice = take(input, BITMAP_LENGTH).ok_or(Error::BadFormat)?;
|
||||
let bitmap = Bitmap::decode(&bitmap_slice[..])?;
|
||||
let value = if has_value {
|
||||
let count = <Compact<u32>>::decode(input).ok_or(Error::BadFormat)?.0 as usize;
|
||||
let count = <Compact<u32>>::decode(input)?.0 as usize;
|
||||
Some(take(input, count).ok_or(Error::BadFormat)?)
|
||||
} else {
|
||||
None
|
||||
@@ -78,7 +78,7 @@ impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> {
|
||||
|
||||
for i in 0..nibble_ops::NIBBLE_LENGTH {
|
||||
if bitmap.value_at(i) {
|
||||
let count = <Compact<u32>>::decode(input).ok_or(Error::BadFormat)?.0 as usize;
|
||||
let count = <Compact<u32>>::decode(input)?.0 as usize;
|
||||
children[i] = Some(take(input, count).ok_or(Error::BadFormat)?);
|
||||
}
|
||||
}
|
||||
@@ -98,7 +98,7 @@ impl<H: Hasher> NodeCodecT<H> for NodeCodec<H> {
|
||||
nibble_data,
|
||||
nibble_ops::number_padding(nibble_count),
|
||||
);
|
||||
let count = <Compact<u32>>::decode(input).ok_or(Error::BadFormat)?.0 as usize;
|
||||
let count = <Compact<u32>>::decode(input)?.0 as usize;
|
||||
Ok(Node::Leaf(nibble_slice, take(input, count).ok_or(Error::BadFormat)?))
|
||||
}
|
||||
}
|
||||
@@ -229,11 +229,8 @@ const BITMAP_LENGTH: usize = 2;
|
||||
pub(crate) struct Bitmap(u16);
|
||||
|
||||
impl Bitmap {
|
||||
|
||||
pub fn decode(data: &[u8]) -> Result<Self, Error> {
|
||||
u16::decode(&mut &data[..])
|
||||
.ok_or(Error::BadFormat)
|
||||
.map(|v|Bitmap(v))
|
||||
Ok(Bitmap(u16::decode(&mut &data[..])?))
|
||||
}
|
||||
|
||||
pub fn value_at(&self, i: usize) -> bool {
|
||||
|
||||
@@ -50,18 +50,20 @@ impl Encode for NodeHeader {
|
||||
}
|
||||
}
|
||||
|
||||
impl codec::EncodeLike for NodeHeader {}
|
||||
|
||||
impl Decode for NodeHeader {
|
||||
fn decode<I: Input>(input: &mut I) -> Option<Self> {
|
||||
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let i = input.read_byte()?;
|
||||
if i == trie_constants::EMPTY_TRIE {
|
||||
return Some(NodeHeader::Null);
|
||||
return Ok(NodeHeader::Null);
|
||||
}
|
||||
match i & (0b11 << 6) {
|
||||
trie_constants::LEAF_PREFIX_MASK => Some(NodeHeader::Leaf(decode_size(i, input)?)),
|
||||
trie_constants::BRANCH_WITHOUT_MASK => Some(NodeHeader::Branch(false, decode_size(i, input)?)),
|
||||
trie_constants::BRANCH_WITH_MASK => Some(NodeHeader::Branch(true, decode_size(i, input)?)),
|
||||
trie_constants::LEAF_PREFIX_MASK => Ok(NodeHeader::Leaf(decode_size(i, input)?)),
|
||||
trie_constants::BRANCH_WITHOUT_MASK => Ok(NodeHeader::Branch(false, decode_size(i, input)?)),
|
||||
trie_constants::BRANCH_WITH_MASK => Ok(NodeHeader::Branch(true, decode_size(i, input)?)),
|
||||
// do not allow any special encoding
|
||||
_ => None,
|
||||
_ => Err("Unallowed encoding".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,18 +105,18 @@ fn encode_size_and_prefix(size: usize, prefix: u8, out: &mut impl Output) {
|
||||
}
|
||||
|
||||
/// Decode size only from stream input and header byte.
|
||||
fn decode_size(first: u8, input: &mut impl Input) -> Option<usize> {
|
||||
fn decode_size(first: u8, input: &mut impl Input) -> Result<usize, codec::Error> {
|
||||
let mut result = (first & 255u8 >> 2) as usize;
|
||||
if result < 63 {
|
||||
return Some(result);
|
||||
return Ok(result);
|
||||
}
|
||||
result -= 1;
|
||||
while result <= trie_constants::NIBBLE_SIZE_BOUND {
|
||||
let n = input.read_byte()? as usize;
|
||||
if n < 255 {
|
||||
return Some(result + n + 1);
|
||||
return Ok(result + n + 1);
|
||||
}
|
||||
result += 255;
|
||||
}
|
||||
Some(trie_constants::NIBBLE_SIZE_BOUND)
|
||||
Ok(trie_constants::NIBBLE_SIZE_BOUND)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user