-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from 0xPolygonZero/zbrown/adjust-deserializatio…
…n-logic Handle bytestrings and LegacyReceiptRlp decoding
- Loading branch information
Showing
7 changed files
with
115 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
//! Custom deserializers for Serde. | ||
use hex::FromHex; | ||
use plonky2_evm::generation::mpt::LegacyReceiptRlp; | ||
use rlp::DecoderError; | ||
use serde::{ | ||
de::{Error, Visitor}, | ||
Deserialize, Deserializer, | ||
}; | ||
|
||
#[derive(Clone, Debug, Default, Deserialize)] | ||
pub(crate) struct ByteString(#[serde(with = "self")] pub(crate) Vec<u8>); | ||
|
||
impl From<ByteString> for Vec<u8> { | ||
fn from(v: ByteString) -> Self { | ||
v.0 | ||
} | ||
} | ||
|
||
impl TryFrom<ByteString> for LegacyReceiptRlp { | ||
type Error = DecoderError; | ||
|
||
fn try_from(value: ByteString) -> Result<Self, Self::Error> { | ||
rlp::decode(&value.0) | ||
} | ||
} | ||
|
||
fn remove_hex_prefix_if_present(data: &str) -> &str { | ||
let prefix = &data[..2]; | ||
|
||
match matches!(prefix, "0x" | "0X") { | ||
false => data, | ||
true => &data[2..], | ||
} | ||
} | ||
|
||
// Gross, but there is no Serde crate that can both parse a hex string with a | ||
// prefix and also deserialize from a `Vec<u8>`. | ||
fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<u8>, D::Error> { | ||
struct PrefixHexStrVisitor(); | ||
|
||
impl<'de> Visitor<'de> for PrefixHexStrVisitor { | ||
type Value = Vec<u8>; | ||
|
||
fn visit_str<E>(self, data: &str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
FromHex::from_hex(remove_hex_prefix_if_present(data)).map_err(Error::custom) | ||
} | ||
|
||
fn visit_borrowed_str<E>(self, data: &'de str) -> Result<Self::Value, E> | ||
where | ||
E: Error, | ||
{ | ||
FromHex::from_hex(remove_hex_prefix_if_present(data)).map_err(Error::custom) | ||
} | ||
|
||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(f, "a hex encoded string with a prefix") | ||
} | ||
} | ||
|
||
deserializer.deserialize_string(PrefixHexStrVisitor()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters