Skip to content
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

Merged
merged 4 commits into from
Jul 28, 2023

Conversation

aspect
Copy link
Collaborator

@aspect aspect commented Jul 25, 2023

This PR implements ScriptPubKey serialization/deserialization for JSON.

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"))
Copy link
Collaborator

@biryukovmaxim biryukovmaxim Jul 26, 2023

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replaced

Comment on lines 58 to 60
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"))
Copy link
Collaborator

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)

Copy link
Collaborator Author

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)
Copy link
Collaborator

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

Copy link
Collaborator Author

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

version: ScriptPublicKeyVersion,
script: &'a [u8],
if deserializer.is_human_readable() {
let s = <std::string::String as Deserialize>::deserialize(deserializer)?;
Copy link
Collaborator

@biryukovmaxim biryukovmaxim Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 44 to 46
#[derive(Default, Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Hash)]
#[serde(rename_all = "camelCase")]
struct ScriptPublicKeyInternal<'a> {
Copy link
Collaborator

@biryukovmaxim biryukovmaxim Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@michaelsutton michaelsutton merged commit a590095 into kaspanet:master Jul 28, 2023
5 checks passed
smartgoo pushed a commit to smartgoo/rusty-kaspa that referenced this pull request Jun 18, 2024
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants