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

New metadata format #296

Merged
merged 31 commits into from
Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6175cda
Move spec tests to separate file
ascjones Dec 10, 2019
8cd1c8b
Add contract spec serialization test
ascjones Dec 10, 2019
89df1a3
Camel case field names
ascjones Dec 10, 2019
f1e75b1
Encode selector as hex string
ascjones Dec 10, 2019
65da756
Move specs module back to top level
ascjones Dec 10, 2019
7aeacca
Move layout test to tests.rs
ascjones Dec 10, 2019
d64facd
Layout externally tagged enums & camelCase
ascjones Dec 10, 2019
73118f0
Format code
ascjones Dec 10, 2019
de1e257
Rename TypeSpec::ty -> TypeSpec::id
ascjones Dec 12, 2019
1d5be04
Merge branch 'master' into aj-metadata
ascjones Dec 17, 2019
603ed1a
Merge branch 'master' into aj-metadata
ascjones Jan 20, 2020
273a679
Merge branch 'master' into aj-metadata
ascjones Feb 4, 2020
534e84b
Merge branch 'master' into aj-metadata
ascjones Mar 16, 2020
dec5a80
TEMPORARY: Use my type-metadata branch
ascjones Mar 17, 2020
e998b15
Derive TypeInfo for Key
ascjones Mar 17, 2020
5d06b5e
Switch from type_metadata to scale_info
ascjones Mar 19, 2020
9e3c8f3
Convert layout to using scale-info
ascjones Mar 20, 2020
b0ec18e
Merge branch 'master' into aj-metadata
ascjones Mar 23, 2020
43a38d2
Fix scale-info optional dependencies
ascjones Mar 24, 2020
49e5d32
Merge branch 'master' into aj-metadata
ascjones Mar 24, 2020
557f45e
Fmt
ascjones Mar 24, 2020
66b0d3b
Merge branch 'master' into aj-metadata
ascjones Mar 26, 2020
df2bb60
Fix up straggling type-metadata dependency
ascjones Mar 31, 2020
3301ce0
Merge branch 'master' into aj-metadata
ascjones Jun 11, 2020
aa27877
scake-info master
ascjones Jun 11, 2020
0dd1e32
Use scale-info from crates.io
ascjones Jun 12, 2020
8e82661
oops
ascjones Jun 12, 2020
9d21bcd
Fix compilation errors
ascjones Jun 12, 2020
367ba7e
Fmt
ascjones Jun 12, 2020
1698489
Fix examples manifests
ascjones Jun 12, 2020
a8f3ad6
Remove redundant into()
ascjones Jun 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type-metadata = { git = "https://github.com/type-metadata/type-metadata.git", de

[dev-dependencies]
serde_json = "1.0"
assert-json-diff = "1.0.1"

[features]
default = [
Expand Down
55 changes: 5 additions & 50 deletions abi/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use alloc::{
string::String,
vec::Vec,
};
use core::fmt::Write;

use derive_more::From;
use serde::{
Expand All @@ -44,7 +43,7 @@ pub trait HasLayout {
/// Either a concrete layout bound or another layout sub-struct.
#[derive(Debug, PartialEq, Eq, Serialize, From)]
#[serde(bound = "F::TypeId: Serialize")]
#[serde(untagged)]
#[serde(rename_all = "lowercase")]
pub enum StorageLayout<F: Form = MetaForm> {
/// A concrete layout bound.
Range(LayoutRange<F>),
Expand Down Expand Up @@ -81,10 +80,9 @@ pub struct LayoutKey(
#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(bound = "F::TypeId: Serialize")]
pub struct LayoutStruct<F: Form = MetaForm> {
#[serde(rename = "struct.type")]
#[serde(rename = "type")]
self_ty: F::TypeId,
/// The sub-fields of the struct.
#[serde(rename = "struct.fields")]
fields: Vec<LayoutField<F>>,
}

Expand Down Expand Up @@ -163,13 +161,12 @@ impl IntoCompact for LayoutField {
#[serde(bound = "F::TypeId: Serialize")]
pub struct LayoutRange<F: Form = MetaForm> {
/// The single key for cells or the starting key address for chunks.
#[serde(rename = "range.offset", serialize_with = "serialize_key")]
#[serde(serialize_with = "serialize_key")]
offset: LayoutKey,
/// The amount of associated key addresses starting from the offset key.
#[serde(rename = "range.len")]
len: u32,
/// The element type stored under the associated keys.
#[serde(rename = "range.elem_type")]
#[serde(rename = "elemType")]
ascjones marked this conversation as resolved.
Show resolved Hide resolved
elem_ty: F::TypeId,
}

Expand Down Expand Up @@ -215,47 +212,5 @@ fn serialize_key<S>(key: &LayoutKey, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let bytes = key.0;
let mut hex = String::with_capacity(bytes.len() * 2 + 2);
write!(hex, "0x").expect("failed writing to string");
for byte in &bytes {
write!(hex, "{:02x}", byte).expect("failed writing to string");
}

serializer.serialize_str(&hex)
}

#[cfg(test)]
mod tests {
use super::*;
use type_metadata::{
form::{
Form,
MetaForm,
},
IntoCompact,
Registry,
};

#[test]
fn key_must_serialize_to_hex() {
// given
let type_id = <MetaForm as Form>::TypeId::new::<u32>();
let offset = LayoutKey([1; 32]);
let cs: LayoutRange<MetaForm> = LayoutRange {
offset,
len: 1337,
elem_ty: type_id,
};
let mut registry = Registry::new();

// when
let json = serde_json::to_string(&cs.into_compact(&mut registry)).unwrap();

// then
assert_eq!(
json,
"{\"range.offset\":\"0x0101010101010101010101010101010101010101010101010101010101010101\",\"range.len\":1337,\"range.elem_type\":1}"
);
}
super::hex_encode(&key.0[..], serializer)
}
21 changes: 20 additions & 1 deletion abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(test)]
mod tests;

mod layout;
mod specs;

Expand Down Expand Up @@ -51,7 +54,11 @@ pub use self::{
},
};

use serde::Serialize;
use core::fmt::Write as _;
use serde::{
Serialize,
Serializer,
};
use type_metadata::{
form::CompactForm,
IntoCompact as _,
Expand Down Expand Up @@ -83,3 +90,15 @@ impl InkProject {
}
}
}

fn hex_encode<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could use some documentation with a simple example.

where
S: Serializer,
{
let mut hex = String::with_capacity(bytes.len() * 2 + 2);
write!(hex, "0x").expect("failed writing to string");
for byte in bytes {
write!(hex, "{:02x}", byte).expect("failed writing to string");
}
serializer.serialize_str(&hex)
}
35 changes: 3 additions & 32 deletions abi/src/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ impl ConstructorSpecBuilder<state::Selector> {
/// Describes a contract message.
#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(bound = "F::TypeId: Serialize")]
#[serde(rename_all = "camelCase")]
pub struct MessageSpec<F: Form = MetaForm> {
/// The name of the message.
name: F::String,
Expand Down Expand Up @@ -571,6 +572,7 @@ pub type DisplayName<F> = type_metadata::Namespace<F>;
/// simply be a type alias to `fn(i32, i32) -> Ordering`.
#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(bound = "F::TypeId: Serialize")]
#[serde(rename_all = "camelCase")]
pub struct TypeSpec<F: Form = MetaForm> {
/// The actual type.
ty: F::TypeId,
Expand Down Expand Up @@ -830,36 +832,5 @@ fn serialize_selector<S>(s: &[u8; 4], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let hex = format!(
r#"["0x{:02X}","0x{:02X}","0x{:02X}","0x{:02X}"]"#,
s[0], s[1], s[2], s[3]
);
serializer.serialize_str(&hex)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn construct_selector_must_serialize_to_hex() {
// given
let name = "foo";
let cs: ConstructorSpec<MetaForm> = ConstructorSpec {
name,
selector: 123_456_789u32.to_be_bytes(),
args: Vec::new(),
docs: Vec::new(),
};
let mut registry = Registry::new();

// when
let json = serde_json::to_string(&cs.into_compact(&mut registry)).unwrap();

// then
assert_eq!(
json,
r#"{"name":1,"selector":"[\"0x07\",\"0x5B\",\"0xCD\",\"0x15\"]","args":[],"docs":[]}"#
);
}
Comment on lines -840 to -864
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was this test moved or removed?

super::hex_encode(&s[..], serializer)
}