Skip to content

Commit 4531ba6

Browse files
authored
Serialize storage keys in metadata as SCALE encoded (#2048)
* Use scale encoding in metadata for keys * Fix tests
1 parent db5600e commit 4531ba6

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/metadata/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ serde = { workspace = true, features = ["derive", "alloc"] }
2222
impl-serde = { workspace = true, default-features = true }
2323
derive_more = { workspace = true, features = ["from"] }
2424
linkme = { workspace = true }
25+
scale = { workspace = true }
2526
scale-info = { workspace = true, features = ["derive", "serde", "decode", "schema"] }
2627
schemars = { workspace = true }
2728

crates/metadata/src/layout/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ use crate::{
2929
use derive_more::From;
3030
use ink_prelude::collections::btree_map::BTreeMap;
3131
use ink_primitives::Key;
32+
use scale::{
33+
Decode,
34+
Encode,
35+
};
3236
use scale_info::{
3337
form::{
3438
Form,
@@ -42,7 +46,10 @@ use scale_info::{
4246
};
4347
use schemars::JsonSchema;
4448
use serde::{
45-
de::DeserializeOwned,
49+
de::{
50+
DeserializeOwned,
51+
Error,
52+
},
4653
Deserialize,
4754
Serialize,
4855
};
@@ -87,7 +94,7 @@ impl serde::Serialize for LayoutKey {
8794
where
8895
S: serde::Serializer,
8996
{
90-
serde_hex::serialize(&self.key.to_be_bytes(), serializer)
97+
serde_hex::serialize(&self.key.encode(), serializer)
9198
}
9299
}
93100

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

crates/metadata/src/layout/tests.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use scale_info::Path;
2020
fn layout_key_works() {
2121
let layout_key = LayoutKey::from(&1);
2222
let json = serde_json::to_string(&layout_key).unwrap();
23-
assert_eq!(json, "\"0x00000001\"",);
23+
assert_eq!(json, "\"0x01000000\"",);
2424
}
2525

2626
fn named_fields_struct_layout(key: &Key) -> Layout {
@@ -47,7 +47,7 @@ fn named_fields_work() {
4747
{
4848
"layout": {
4949
"leaf": {
50-
"key": "0x00000159",
50+
"key": "0x59010000",
5151
"ty": 0,
5252
}
5353
},
@@ -56,7 +56,7 @@ fn named_fields_work() {
5656
{
5757
"layout": {
5858
"leaf": {
59-
"key": "0x00000159",
59+
"key": "0x59010000",
6060
"ty": 1,
6161
}
6262
},
@@ -94,7 +94,7 @@ fn tuple_struct_work() {
9494
{
9595
"layout": {
9696
"leaf": {
97-
"key": "0x000000ea",
97+
"key": "0xea000000",
9898
"ty": 0,
9999
}
100100
},
@@ -103,7 +103,7 @@ fn tuple_struct_work() {
103103
{
104104
"layout": {
105105
"leaf": {
106-
"key": "0x000000ea",
106+
"key": "0xea000000",
107107
"ty": 1,
108108
}
109109
},
@@ -139,7 +139,7 @@ fn clike_enum_work() {
139139
let expected = serde_json::json! {
140140
{
141141
"enum": {
142-
"dispatchKey": "0x0000007b",
142+
"dispatchKey": "0x7b000000",
143143
"name": "Enum",
144144
"variants": {
145145
"0": {
@@ -219,7 +219,7 @@ fn mixed_enum_work() {
219219
let expected = serde_json::json! {
220220
{
221221
"enum": {
222-
"dispatchKey": "0x000001c8",
222+
"dispatchKey": "0xc8010000",
223223
"name": "Enum",
224224
"variants": {
225225
"0": {
@@ -231,7 +231,7 @@ fn mixed_enum_work() {
231231
{
232232
"layout": {
233233
"leaf": {
234-
"key": "0x000001c8",
234+
"key": "0xc8010000",
235235
"ty": 0,
236236
}
237237
},
@@ -240,7 +240,7 @@ fn mixed_enum_work() {
240240
{
241241
"layout": {
242242
"leaf": {
243-
"key": "0x000001c8",
243+
"key": "0xc8010000",
244244
"ty": 1,
245245
}
246246
},
@@ -254,7 +254,7 @@ fn mixed_enum_work() {
254254
{
255255
"layout": {
256256
"leaf": {
257-
"key": "0x000001c8",
257+
"key": "0xc8010000",
258258
"ty": 0,
259259
}
260260
},
@@ -263,7 +263,7 @@ fn mixed_enum_work() {
263263
{
264264
"layout": {
265265
"leaf": {
266-
"key": "0x000001c8",
266+
"key": "0xc8010000",
267267
"ty": 1,
268268
}
269269
},
@@ -304,11 +304,11 @@ fn unbounded_layout_works() {
304304
"hash": {
305305
"layout": {
306306
"leaf": {
307-
"key": "0x00000237",
307+
"key": "0x37020000",
308308
"ty": 0
309309
}
310310
},
311-
"offset": "0x00000237",
311+
"offset": "0x37020000",
312312
"strategy": {
313313
"hasher": "Blake2x256",
314314
"prefix": "0x696e6b2073746f7261676520686173686d6170",

0 commit comments

Comments
 (0)