Skip to content

Commit

Permalink
[bug fix] - struct tag serialization should not trim leading zeros (M…
Browse files Browse the repository at this point in the history
…ystenLabs#11732)

## Description 

struct tag serialization should not trim leading zeros

## Test Plan 

added unit test
  • Loading branch information
patrickkuo authored May 4, 2023
1 parent 891b17a commit 458a16c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 17 deletions.
26 changes: 13 additions & 13 deletions crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
"recipient": {
"AddressOwner": "0x6a101e9a3af5c8070112f808648b36efbf8dee8a1a82de46d9504e96a1108a17"
},
"objectType": "0x2::example::Object",
"objectType": "0x0000000000000000000000000000000000000000000000000000000000000002::example::Object",
"objectId": "0x8ef76f56c399633a2eb310bca9124e5f2f38ce739eaacbb6600688804e078448",
"version": "2",
"digest": "7PsBHpUW6yfGNov2WrbVafLjgT9nYziQ3gVDbRq6zTbF"
Expand Down Expand Up @@ -543,7 +543,7 @@
"packageId": "0xc54ab30a3d9adc07c1429c4d6bbecaf9457c9af77a91f631760853934d383634",
"transactionModule": "test_module",
"sender": "0xbcf7c32655009a61f1de0eae420a2e4ae1bb772ab2dd5d5a7dfa949c0ef06908",
"type": "0x9::test::TestEvent",
"type": "0x0000000000000000000000000000000000000000000000000000000000000009::test::TestEvent",
"parsedJson": {
"test": "example value"
},
Expand Down Expand Up @@ -859,7 +859,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand Down Expand Up @@ -1064,7 +1064,7 @@
"recipient": {
"AddressOwner": "0x8196d048b7a6d04c8edc89579d86fd3fc90c52f9a14c6b812b94fe613c5bcebb"
},
"objectType": "0x2::example::Object",
"objectType": "0x0000000000000000000000000000000000000000000000000000000000000002::example::Object",
"objectId": "0x5eeb1d449e2516166d57d71fdeb154d0dc9ecdb7b30057d0a932684cac352cdc",
"version": "2",
"digest": "64UQ3a7m1mjWuzgyGoH8RnMyPGDN4XYTC9dS4qiSfdK4"
Expand Down Expand Up @@ -1156,7 +1156,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand All @@ -1180,7 +1180,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand All @@ -1204,7 +1204,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand All @@ -1228,7 +1228,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand All @@ -1252,7 +1252,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "100000000",
Expand Down Expand Up @@ -1442,7 +1442,7 @@
"recipient": {
"AddressOwner": "0xd6e4c0ad5ec4de2449c41ac819f9c162d4af78db04f5789cd4fca497d9d14703"
},
"objectType": "0x2::example::Object",
"objectType": "0x0000000000000000000000000000000000000000000000000000000000000002::example::Object",
"objectId": "0x6b5ad50597c5faf4842f454b9b09c7524afb8e30e88e2f84077773b9572d766c",
"version": "2",
"digest": "66Bhgq1W6nBRNNjS3DuWXuZyNJHBrUcwxQz4zUc9tkxZ"
Expand Down Expand Up @@ -1558,7 +1558,7 @@
"recipient": {
"AddressOwner": "0x92e30e8e1dc5061bbf38c199f22627e75ff9bf8c469ff03d000b917ca77792c4"
},
"objectType": "0x2::example::Object",
"objectType": "0x0000000000000000000000000000000000000000000000000000000000000002::example::Object",
"objectId": "0x2209ab55b0202608150d2aaba7d0b2cc03df659954b577bd64996511a63b3292",
"version": "2",
"digest": "6kwvRMKGTDfX4DH4FFGCiQBh1acaiEpWqNYVFu8Fo9bD"
Expand Down Expand Up @@ -1674,7 +1674,7 @@
"recipient": {
"AddressOwner": "0xc1c1d32a27103fcd457cd60a94838b84d41b7344f39832dd4f5fb5bdc06fc4e2"
},
"objectType": "0x2::example::Object",
"objectType": "0x0000000000000000000000000000000000000000000000000000000000000002::example::Object",
"objectId": "0xa7b675e9b2f5749415c55996bb9d64a48cf227cd51ebf714d6a33167f44e88d3",
"version": "2",
"digest": "5EuYqa54WcsyEaPLGqsw5cPtx5H2PibfLCdzPzHiSokr"
Expand Down Expand Up @@ -1767,7 +1767,7 @@
"storageRebate": "100",
"content": {
"dataType": "moveObject",
"type": "0x2::coin::Coin<0x2::sui::SUI>",
"type": "0x0000000000000000000000000000000000000000000000000000000000000002::coin::Coin<0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI>",
"hasPublicTransfer": true,
"fields": {
"balance": "10000",
Expand Down
36 changes: 32 additions & 4 deletions crates/sui-types/src/sui_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use serde_with::DisplayFromStr;
use serde_with::{Bytes, DeserializeAs, SerializeAs};

use std::fmt::Write;
use sui_protocol_config::ProtocolVersion;

use crate::{parse_sui_struct_tag, parse_sui_type_tag};
Expand Down Expand Up @@ -156,8 +156,36 @@ impl SerializeAs<StructTag> for SuiStructTag {
where
S: Serializer,
{
let s = value.to_string();
s.serialize(serializer)
let f = to_sui_struct_tag_string(value).map_err(S::Error::custom)?;
f.serialize(serializer)
}
}

fn to_sui_struct_tag_string(value: &StructTag) -> Result<String, fmt::Error> {
let mut f = String::new();
write!(
f,
"0x{}::{}::{}",
value.address.to_canonical_string(),
value.module,
value.name
)?;
if let Some(first_ty) = value.type_params.first() {
write!(f, "<")?;
write!(f, "{}", to_sui_type_tag_string(first_ty)?)?;
for ty in value.type_params.iter().skip(1) {
write!(f, ", {}", to_sui_type_tag_string(ty)?)?;
}
write!(f, ">")?;
}
Ok(f)
}

fn to_sui_type_tag_string(value: &TypeTag) -> Result<String, fmt::Error> {
match value {
TypeTag::Vector(t) => Ok(format!("vector<{}>", to_sui_type_tag_string(t)?)),
TypeTag::Struct(s) => to_sui_struct_tag_string(s),
_ => Ok(value.to_string()),
}
}

Expand All @@ -178,7 +206,7 @@ impl SerializeAs<TypeTag> for SuiTypeTag {
where
S: Serializer,
{
let s = value.to_string();
let s = to_sui_type_tag_string(value).map_err(S::Error::custom)?;
s.serialize(serializer)
}
}
Expand Down
25 changes: 25 additions & 0 deletions crates/sui-types/tests/serde_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use move_core_types::language_storage::StructTag;
use serde::Serialize;
use serde_json::Value;
use serde_with::serde_as;
use sui_types::parse_sui_struct_tag;
use sui_types::sui_serde::SuiStructTag;

#[test]
fn test_struct_tag_serde() {
let tag = parse_sui_struct_tag("0x7f89cdffd8968affa0b47bef91adc5314e19509080470c45bfd434cd83a766b::suifrens::SuiFren<0x7f89cdffd8968affa0b47bef91adc5314e19509080470c45bfd434cd83a766b::capy::Capy>").unwrap();
#[serde_as]
#[derive(Serialize)]
struct TestStructTag(#[serde_as(as = "SuiStructTag")] StructTag);

// serialize to json should not trim the leading 0
let Value::String(json) = serde_json::to_value(&TestStructTag(tag.clone()))
.unwrap() else { panic!() };
assert_eq!(json, "0x07f89cdffd8968affa0b47bef91adc5314e19509080470c45bfd434cd83a766b::suifrens::SuiFren<0x07f89cdffd8968affa0b47bef91adc5314e19509080470c45bfd434cd83a766b::capy::Capy>");

let tag2 = parse_sui_struct_tag(&json).unwrap();
assert_eq!(tag, tag2);
}

0 comments on commit 458a16c

Please sign in to comment.