Skip to content

Commit

Permalink
Merge df2bb60 into 46b2823
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Mar 31, 2020
2 parents 46b2823 + df2bb60 commit d0b5d45
Show file tree
Hide file tree
Showing 45 changed files with 391 additions and 246 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Expand Up @@ -228,7 +228,7 @@ publish-docs:
- git fetch origin gh-pages
# Generating Docs
- time cargo doc --no-deps --all-features
-p type-metadata -p ink_abi -p ink_abi_derive -p ink_core -p ink_core_derive
-p scale-info -p ink_abi -p ink_abi_derive -p ink_core -p ink_core_derive
-p ink_primitives -p ink_prelude -p ink_lang -p ink_lang_macro
# saving README and docs
- mv target/doc/ /tmp/
Expand Down
9 changes: 5 additions & 4 deletions abi/Cargo.toml
Expand Up @@ -22,14 +22,15 @@ ink_primitives = { version = "2.1.0", path = "../primitives/", default-features
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
derive_more = { version = "0.99", default-features = false, features = ["from"] }

[dependencies.type-metadata]
git = "https://github.com/type-metadata/type-metadata.git"
rev = "02eae9f35c40c943b56af5b60616219f2b72b47d"
[dependencies.scale-info]
git = "https://github.com/paritytech/scale-info.git"
branch = "aj-merge-type"
default-features = false
features = ["derive"]

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

[features]
default = [
Expand All @@ -40,7 +41,7 @@ std = [
"ink_abi_derive/std",
"ink_prelude/std",
"serde/std",
"type-metadata/std",
"scale-info/std",
]
derive = [
"ink_abi_derive"
Expand Down
4 changes: 2 additions & 2 deletions abi/derive/src/has_layout.rs
Expand Up @@ -84,7 +84,7 @@ fn generate_fields_layout<'a>(
fn generate_struct_fields_layout(fields: &Punctuated<Field, Token![,]>) -> TokenStream2 {
let fields_layout = generate_fields_layout(fields);
quote! {
use type_metadata::Metadata as _;
use scale_info::Metadata as _;
_ink_abi::LayoutStruct::new(Self::meta_type(), __core::vec![
#( #fields_layout, )*
])
Expand All @@ -97,7 +97,7 @@ fn generate_struct_layout(data_struct: &DataStruct) -> TokenStream2 {
Fields::Unnamed(ref fs) => generate_struct_fields_layout(&fs.unnamed),
Fields::Unit => {
quote! {
_ink_abi::LayoutStruct::new(<Self as type_metadata::Metadata>::meta_type(), Vec::new())
_ink_abi::LayoutStruct::new(<Self as scale_info::Metadata>::meta_type(), Vec::new())
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion abi/derive/src/impl_wrapper.rs
Expand Up @@ -22,7 +22,7 @@ pub fn wrap(impl_quote: TokenStream2) -> TokenStream2 {
#[allow(unknown_lints)]
#[cfg_attr(feature = "cargo-clippy", allow(useless_attribute))]
#[allow(rust_2018_idioms)]
use type_metadata as _type_metadata;
use scale_info as _scale_info;
use ink_abi as _ink_abi;

#[cfg(not(feature = "std"))]
Expand Down
68 changes: 11 additions & 57 deletions abi/src/layout.rs
Expand Up @@ -17,14 +17,9 @@ use alloc::{
string::String,
vec::Vec,
};
use core::fmt::Write;

use derive_more::From;
use serde::{
Serialize,
Serializer,
};
use type_metadata::{
use scale_info::{
form::{
CompactForm,
Form,
Expand All @@ -33,6 +28,10 @@ use type_metadata::{
IntoCompact,
Registry,
};
use serde::{
Serialize,
Serializer,
};

/// Implemented by types that have a storage layout.
///
Expand All @@ -49,15 +48,14 @@ impl From<ink_primitives::Key> for LayoutKey {

impl HasLayout for ink_primitives::Key {
fn layout(&self) -> StorageLayout {
LayoutRange::cell(*self, <[u8; 32] as type_metadata::Metadata>::meta_type())
.into()
LayoutRange::cell(*self, <[u8; 32] as scale_info::Metadata>::meta_type()).into()
}
}

/// 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 @@ -94,10 +92,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 @@ -176,13 +173,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")]
elem_ty: F::TypeId,
}

Expand Down Expand Up @@ -228,47 +224,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)
}
23 changes: 21 additions & 2 deletions abi/src/lib.rs
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,12 +54,16 @@ pub use self::{
},
};

use serde::Serialize;
use type_metadata::{
use core::fmt::Write as _;
use scale_info::{
form::CompactForm,
IntoCompact as _,
Registry,
};
use serde::{
Serialize,
Serializer,
};

/// An entire ink! project for ABI file generation purposes.
#[derive(Debug, Serialize)]
Expand All @@ -83,3 +90,15 @@ impl InkProject {
}
}
}

fn hex_encode<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
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)
}
55 changes: 13 additions & 42 deletions abi/src/specs.rs
Expand Up @@ -22,11 +22,7 @@ use alloc::{
};
use core::marker::PhantomData;

use serde::{
Serialize,
Serializer,
};
use type_metadata::{
use scale_info::{
form::{
CompactForm,
Form,
Expand All @@ -36,6 +32,10 @@ use type_metadata::{
Metadata,
Registry,
};
use serde::{
Serialize,
Serializer,
};

/// Describes a contract.
#[derive(Debug, PartialEq, Eq, Serialize)]
Expand Down 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 @@ -550,7 +551,7 @@ impl EventSpec {
/// default setup. Even though it would be useful for third party tools
/// such as the Polkadot UI to know that we are handling with `Balance`
/// types, we currently cannot communicate this without display names.
pub type DisplayName<F> = type_metadata::Namespace<F>;
pub type DisplayName<F> = scale_info::Namespace<F>;

/// A type specification.
///
Expand All @@ -571,9 +572,10 @@ 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,
id: F::TypeId,
/// The compile-time known displayed representation of the type.
display_name: DisplayName<F>,
}
Expand All @@ -583,7 +585,7 @@ impl IntoCompact for TypeSpec {

fn into_compact(self, registry: &mut Registry) -> Self::Output {
TypeSpec {
ty: registry.register_type(&self.ty),
id: registry.register_type(&self.id),
display_name: self.display_name.into_compact(registry),
}
}
Expand Down Expand Up @@ -626,7 +628,7 @@ impl TypeSpec {
S: IntoIterator<Item = <MetaForm as Form>::String>,
{
Self {
ty: T::meta_type(),
id: T::meta_type(),
display_name: DisplayName::new(segments).expect("display name is invalid"),
}
}
Expand All @@ -637,7 +639,7 @@ impl TypeSpec {
T: Metadata,
{
Self {
ty: T::meta_type(),
id: T::meta_type(),
display_name: DisplayName::prelude(),
}
}
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":[]}"#
);
}
super::hex_encode(&s[..], serializer)
}

0 comments on commit d0b5d45

Please sign in to comment.