Skip to content

Commit

Permalink
Serialize storage keys in metadata as SCALE encoded (#2048)
Browse files Browse the repository at this point in the history
* Use scale encoding in metadata for keys

* Fix tests
  • Loading branch information
ascjones authored Jan 10, 2024
1 parent db5600e commit 4531ba6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { workspace = true, features = ["derive", "alloc"] }
impl-serde = { workspace = true, default-features = true }
derive_more = { workspace = true, features = ["from"] }
linkme = { workspace = true }
scale = { workspace = true }
scale-info = { workspace = true, features = ["derive", "serde", "decode", "schema"] }
schemars = { workspace = true }

Expand Down
16 changes: 13 additions & 3 deletions crates/metadata/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ use crate::{
use derive_more::From;
use ink_prelude::collections::btree_map::BTreeMap;
use ink_primitives::Key;
use scale::{
Decode,
Encode,
};
use scale_info::{
form::{
Form,
Expand All @@ -42,7 +46,10 @@ use scale_info::{
};
use schemars::JsonSchema;
use serde::{
de::DeserializeOwned,
de::{
DeserializeOwned,
Error,
},
Deserialize,
Serialize,
};
Expand Down Expand Up @@ -87,7 +94,7 @@ impl serde::Serialize for LayoutKey {
where
S: serde::Serializer,
{
serde_hex::serialize(&self.key.to_be_bytes(), serializer)
serde_hex::serialize(&self.key.encode(), serializer)
}
}

Expand All @@ -98,7 +105,10 @@ impl<'de> serde::Deserialize<'de> for LayoutKey {
{
let mut arr = [0; 4];
serde_hex::deserialize_check_len(d, serde_hex::ExpectedLen::Exact(&mut arr[..]))?;
Ok(Key::from_be_bytes(arr).into())
let key = Key::decode(&mut &arr[..]).map_err(|err| {
Error::custom(format!("Error decoding layout key: {}", err))
})?;
Ok(key.into())
}
}

Expand Down
26 changes: 13 additions & 13 deletions crates/metadata/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use scale_info::Path;
fn layout_key_works() {
let layout_key = LayoutKey::from(&1);
let json = serde_json::to_string(&layout_key).unwrap();
assert_eq!(json, "\"0x00000001\"",);
assert_eq!(json, "\"0x01000000\"",);
}

fn named_fields_struct_layout(key: &Key) -> Layout {
Expand All @@ -47,7 +47,7 @@ fn named_fields_work() {
{
"layout": {
"leaf": {
"key": "0x00000159",
"key": "0x59010000",
"ty": 0,
}
},
Expand All @@ -56,7 +56,7 @@ fn named_fields_work() {
{
"layout": {
"leaf": {
"key": "0x00000159",
"key": "0x59010000",
"ty": 1,
}
},
Expand Down Expand Up @@ -94,7 +94,7 @@ fn tuple_struct_work() {
{
"layout": {
"leaf": {
"key": "0x000000ea",
"key": "0xea000000",
"ty": 0,
}
},
Expand All @@ -103,7 +103,7 @@ fn tuple_struct_work() {
{
"layout": {
"leaf": {
"key": "0x000000ea",
"key": "0xea000000",
"ty": 1,
}
},
Expand Down Expand Up @@ -139,7 +139,7 @@ fn clike_enum_work() {
let expected = serde_json::json! {
{
"enum": {
"dispatchKey": "0x0000007b",
"dispatchKey": "0x7b000000",
"name": "Enum",
"variants": {
"0": {
Expand Down Expand Up @@ -219,7 +219,7 @@ fn mixed_enum_work() {
let expected = serde_json::json! {
{
"enum": {
"dispatchKey": "0x000001c8",
"dispatchKey": "0xc8010000",
"name": "Enum",
"variants": {
"0": {
Expand All @@ -231,7 +231,7 @@ fn mixed_enum_work() {
{
"layout": {
"leaf": {
"key": "0x000001c8",
"key": "0xc8010000",
"ty": 0,
}
},
Expand All @@ -240,7 +240,7 @@ fn mixed_enum_work() {
{
"layout": {
"leaf": {
"key": "0x000001c8",
"key": "0xc8010000",
"ty": 1,
}
},
Expand All @@ -254,7 +254,7 @@ fn mixed_enum_work() {
{
"layout": {
"leaf": {
"key": "0x000001c8",
"key": "0xc8010000",
"ty": 0,
}
},
Expand All @@ -263,7 +263,7 @@ fn mixed_enum_work() {
{
"layout": {
"leaf": {
"key": "0x000001c8",
"key": "0xc8010000",
"ty": 1,
}
},
Expand Down Expand Up @@ -304,11 +304,11 @@ fn unbounded_layout_works() {
"hash": {
"layout": {
"leaf": {
"key": "0x00000237",
"key": "0x37020000",
"ty": 0
}
},
"offset": "0x00000237",
"offset": "0x37020000",
"strategy": {
"hasher": "Blake2x256",
"prefix": "0x696e6b2073746f7261676520686173686d6170",
Expand Down

0 comments on commit 4531ba6

Please sign in to comment.