-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ScriptPubKey human-readable ser/deser (for json) #232
Conversation
consensus/core/src/tx.rs
Outdated
let mut hex = [0u8; SCRIPT_VECTOR_SIZE * 2 + 4]; | ||
faster_hex::hex_encode(&self.version.to_be_bytes(), &mut hex).expect("Unable to serialize ScriptPubKey version"); | ||
faster_hex::hex_encode(&self.script, &mut hex[4..]).expect("Unable to serialize ScriptPubKey script"); | ||
serializer.serialize_str(str::from_utf8(&hex).expect("hex is must be a valid UTF-8")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's safe to use unsafe from_utf8_unchecked, because hex encoding will never produce non-utf8.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replaced
consensus/core/src/tx.rs
Outdated
faster_hex::hex_encode(&self.version.to_be_bytes(), &mut hex).expect("Unable to serialize ScriptPubKey version"); | ||
faster_hex::hex_encode(&self.script, &mut hex[4..]).expect("Unable to serialize ScriptPubKey script"); | ||
serializer.serialize_str(str::from_utf8(&hex).expect("hex is must be a valid UTF-8")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.expect
-> .map_err(serde::de::Error::custom)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
faster_hex::hex_encode(&self.script, &mut hex[4..]).expect("Unable to serialize ScriptPubKey script"); | ||
serializer.serialize_str(str::from_utf8(&hex).expect("hex is must be a valid UTF-8")) | ||
} else { | ||
ScriptPublicKeyInternal { version: self.version, script: &self.script }.serialize(serializer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let mut state = serializer.serialize_struct("ScriptPublicKey", 2)?;
state.serialize_field("version", &self.version)?;
state.serialize_field("script", self.script.as_ref())?;
state.end()
it can be done without ScriptPublicKeyInternal
, but I dont think it's a big deal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's keep it as-is
consensus/core/src/tx.rs
Outdated
version: ScriptPublicKeyVersion, | ||
script: &'a [u8], | ||
if deserializer.is_human_readable() { | ||
let s = <std::string::String as Deserialize>::deserialize(deserializer)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let s = <std::string::String as Deserialize>::deserialize(deserializer)?; | |
let s = <&str as Deserialize>::deserialize(deserializer)?; |
It needs to be tested, I think it can prevent String allocation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
consensus/core/src/tx.rs
Outdated
#[derive(Default, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)] | ||
#[serde(rename_all = "camelCase")] | ||
struct ScriptPublicKeyInternal<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[derive(Default, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)] | |
#[serde(rename_all = "camelCase")] | |
struct ScriptPublicKeyInternal<'a> { | |
#[derive(Default, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)] | |
#[serde(rename_all = "camelCase")] | |
#[serde(rename = "ScriptPublicKey")] | |
struct ScriptPublicKeyInternal<'a> { |
Could you add this renaming? It was my fault. I think without renaming it, xml and other structure-name sensitive formats can work incorrectly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
* ScriptPubKey human-readable ser/deser (for json) * switch to from_utf8_unchecked() + serde rename ScriptPublicKey * replace serializer use of expect() with map_err(Error::custom) * fix incorrect usage of script length and handling of larger scripts
This PR implements ScriptPubKey serialization/deserialization for JSON.