Unverified Commit d99ea915 authored by Andrew Jones's avatar Andrew Jones Committed by GitHub
Browse files

[metadata] update to scale-info 0.3, removes string interning (#467)

* WIP

* WIP

* Iterating on InkProject types

* Extract InkProject to its own file

* More iteration on contract metadata

* Compiles...ship it

* Fmt

* Add optional fields and doc test

* Add custom serializer impls for compiler/lang

* Fmt

* Clippy

* Update to scale-info 0.3

* Use &'static str instead of compact string (scale-info 0.3)

* Reexport Url and Version

* Fix missing semicolon

* Fix up metadata exports and codegen

* Serialize hash as byte string

* Remove spec nesting

* Remove name from contract spec

* Add name to storage struct

* Flatten SourceCompiler

* Implement ToTokens for Extension metadata

Allows converting an instance into code that can construct that instance. For use from `cargo-contract`.

* User and Version

* Fix up builder ToTokens and other errs/warns

* Fmt

* Url wrapper

* Comment and Version/Url intos

* Fmt

* Fix U

* Promote extension to module dir

* Moved InkProjectExtension to cargo-contract

* Fix doc gen

* Fix json tests

* Use pretty assertions and fix layout json tests

* Remove storage name from layout (ink specific)

* StorageLayout -> Layout

* Remove redundant contract_ident_lit

* Remove redundant dependencies

* Use Self instead of InkProject
parent 5a944ca2
Pipeline #100563 passed with stages
in 10 minutes and 47 seconds
......@@ -255,7 +255,7 @@ publish-docs:
- git fetch origin gh-pages
# Generating Docs
- time cargo doc --no-deps --all-features
-p scale-info -p ink_metadata -p ink_metadata_derive -p ink_core -p ink_core_derive
-p scale-info -p ink_metadata -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/
......
......@@ -39,7 +39,7 @@ blake2 = { version = "0.9", optional = true }
# Sadly couldn't be marked as dev-dependency.
# Never use this crate outside of the off-chain environment!
rand = { version = "0.7", default-features = false, features = ["alloc"], optional = true }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
# Workaround: we actually just need criterion as a dev-dependency, but
# there is an issue with a doubly included std lib when executing
......
......@@ -15,7 +15,7 @@ scale = { package = "parity-scale-codec", version = "1.3", default-features = fa
adder = { version = "2.1.0", path = "adder", default-features = false, features = ["ink-as-dependency"] }
subber = { version = "2.1.0", path = "subber", default-features = false, features = ["ink-as-dependency"] }
accumulator = { version = "2.1.0", path = "accumulator", default-features = false, features = ["ink-as-dependency"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "delegator"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../../core", default-features = false
ink_lang = { version = "2.1.0", path = "../../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "accumulator"
......
......@@ -13,7 +13,7 @@ ink_lang = { version = "2.1.0", path = "../../../lang", default-features = false
accumulator = { version = "2.1.0", path = "../accumulator", default-features = false, features = ["ink-as-dependency"] }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "adder"
......
......@@ -13,7 +13,7 @@ ink_lang = { version = "2.1.0", path = "../../../lang", default-features = false
accumulator = { version = "2.1.0", path = "../accumulator", default-features = false, features = ["ink-as-dependency"] }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "subber"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "dns"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "erc20"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "erc721"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "flipper"
......
......@@ -11,7 +11,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "incrementer"
......
......@@ -12,7 +12,7 @@ ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
ink_prelude = { version = "2.1.0", path = "../../prelude", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "multisig_plain"
......
......@@ -12,7 +12,7 @@ ink_core = { version = "2.1.0", path = "../../core", default-features = false }
ink_lang = { version = "2.1.0", path = "../../lang", default-features = false }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.3", default-features = false, features = ["derive"], optional = true }
[lib]
name = "runtime_storage"
......
......@@ -36,7 +36,7 @@ ink_lang = { version = "2.1.0", path = ".." }
trybuild = "1.0.24"
pretty_assertions = "0.6.1"
scale-info = { version = "0.2", default-features = false, features = ["derive"] }
scale-info = { version = "0.3", default-features = false, features = ["derive"] }
[lib]
name = "ink_lang_macro"
......
......@@ -242,15 +242,13 @@ impl GenerateMetadata<'_> {
}
fn generate_contract(&self) -> TokenStream2 {
let contract_ident_lit = self.contract.ident.to_string();
let constructors = self.generate_constructors();
let messages = self.generate_messages();
let events = self.generate_events();
let docs = self.generate_docs();
quote! {
::ink_metadata::ContractSpec::new(#contract_ident_lit)
::ink_metadata::ContractSpec::new()
.constructors(vec![
#(#constructors ,)*
])
......
......@@ -20,11 +20,11 @@ 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"] }
scale-info = { version = "0.2", default-features = false, features = ["derive"] }
scale-info = { version = "0.3", default-features = false, features = ["derive"] }
[dev-dependencies]
pretty_assertions = "0.6.1"
serde_json = "1.0"
assert-json-diff = "1.0.1"
[features]
default = [
......
......@@ -300,7 +300,7 @@ pub struct FieldLayout<F: Form = MetaForm> {
/// The name of the field.
///
/// Can be missing, e.g. in case of an enum tuple struct variant.
name: Option<F::String>,
name: Option<&'static str>,
/// The kind of the field.
///
/// This is either a direct layout bound
......@@ -312,7 +312,7 @@ impl FieldLayout {
/// Creates a new field layout.
pub fn new<N, L>(name: N, layout: L) -> Self
where
N: Into<Option<<MetaForm as Form>::String>>,
N: Into<Option<&'static str>>,
L: Into<Layout>,
{
Self {
......@@ -327,7 +327,7 @@ impl IntoCompact for FieldLayout {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
FieldLayout {
name: self.name.map(|name| registry.register_string(name)),
name: self.name,
layout: self.layout.into_compact(registry),
}
}
......
......@@ -14,6 +14,7 @@
use super::*;
use ink_primitives::KeyPtr;
use pretty_assertions::assert_eq;
#[test]
fn layout_key_works() {
......@@ -60,7 +61,7 @@ fn named_fields_work() {
"ty": 1,
}
},
"name": 1,
"name": "a",
},
{
"layout": {
......@@ -73,7 +74,7 @@ fn named_fields_work() {
"ty": 2,
}
},
"name": 2,
"name": "b",
}
]
}
......@@ -293,7 +294,7 @@ fn mixed_enum_work() {
"ty": 1,
}
},
"name": 1,
"name": "a",
},
{
"layout": {
......@@ -306,7 +307,7 @@ fn mixed_enum_work() {
"ty": 2,
}
},
"name": 2,
"name": "b",
}
],
},
......
......@@ -41,6 +41,7 @@ pub use self::specs::{
ReturnTypeSpec,
TypeSpec,
};
#[cfg(feature = "derive")]
use scale_info::{
form::CompactForm,
......@@ -49,24 +50,25 @@ use scale_info::{
};
use serde::Serialize;
/// An entire ink! project for ABI file generation purposes.
/// An entire ink! project for metadata file generation purposes.
#[derive(Debug, Serialize)]
pub struct InkProject {
#[serde(flatten)]
registry: Registry,
#[serde(rename = "storage")]
/// The layout of the storage data structure
layout: layout2::Layout<CompactForm>,
#[serde(rename = "contract")]
spec: ContractSpec<CompactForm>,
}
impl InkProject {
/// Creates a new ink! project.
pub fn new<L, S>(layout: L, spec: S) -> Self
where
L: Into<layout2::Layout>,
S: Into<ContractSpec>,
{
let mut registry = Registry::new();
Self {
layout: layout.into().into_compact(&mut registry),
spec: spec.into().into_compact(&mut registry),
......
......@@ -39,8 +39,6 @@ use serde::Serialize;
#[derive(Debug, PartialEq, Eq, Serialize)]
#[serde(bound = "F::TypeId: Serialize")]
pub struct ContractSpec<F: Form = MetaForm> {
/// The name of the contract.
name: F::String,
/// The set of constructors of the contract.
constructors: Vec<ConstructorSpec<F>>,
/// The external messages of the contract.
......@@ -56,7 +54,6 @@ impl IntoCompact for ContractSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
ContractSpec {
name: registry.register_string(&self.name),
constructors: self
.constructors
.into_iter()
......@@ -171,10 +168,9 @@ impl ContractSpecBuilder<Valid> {
impl ContractSpec {
/// Creates a new contract specification.
pub fn new(name: <MetaForm as Form>::String) -> ContractSpecBuilder {
pub fn new() -> ContractSpecBuilder {
ContractSpecBuilder {
spec: Self {
name,
constructors: Vec::new(),
messages: Vec::new(),
events: Vec::new(),
......@@ -190,7 +186,7 @@ impl ContractSpec {
#[serde(bound = "F::TypeId: Serialize")]
pub struct ConstructorSpec<F: Form = MetaForm> {
/// The name of the message.
name: F::String,
name: &'static str,
/// The selector hash of the message.
#[serde(serialize_with = "serialize_as_byte_str")]
selector: [u8; 4],
......@@ -205,7 +201,7 @@ impl IntoCompact for ConstructorSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
ConstructorSpec {
name: registry.register_string(&self.name),
name: self.name,
selector: self.selector,
args: self
.args
......@@ -231,9 +227,7 @@ pub struct ConstructorSpecBuilder<Selector> {
impl ConstructorSpec {
/// Creates a new constructor spec builder.
pub fn new(
name: <MetaForm as Form>::String,
) -> ConstructorSpecBuilder<Missing<state::Selector>> {
pub fn new(name: &'static str) -> ConstructorSpecBuilder<Missing<state::Selector>> {
ConstructorSpecBuilder {
spec: Self {
name,
......@@ -296,7 +290,7 @@ impl ConstructorSpecBuilder<state::Selector> {
#[serde(rename_all = "camelCase")]
pub struct MessageSpec<F: Form = MetaForm> {
/// The name of the message.
name: F::String,
name: &'static str,
/// The selector hash of the message.
#[serde(serialize_with = "serialize_as_byte_str")]
selector: [u8; 4],
......@@ -329,7 +323,7 @@ mod state {
impl MessageSpec {
/// Creates a new message spec builder.
pub fn new(
name: <MetaForm as Form>::String,
name: &'static str,
) -> MessageSpecBuilder<
Missing<state::Selector>,
Missing<state::Mutates>,
......@@ -443,7 +437,7 @@ impl IntoCompact for MessageSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
MessageSpec {
name: registry.register_string(&self.name),
name: self.name,
selector: self.selector,
mutates: self.mutates,
args: self
......@@ -462,7 +456,7 @@ impl IntoCompact for MessageSpec {
#[serde(bound = "F::TypeId: Serialize")]
pub struct EventSpec<F: Form = MetaForm> {
/// The name of the event.
name: F::String,
name: &'static str,
/// The event arguments.
args: Vec<EventParamSpec<F>>,
/// The event documentation.
......@@ -508,7 +502,7 @@ impl IntoCompact for EventSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
EventSpec {
name: registry.register_string(&self.name),
name: self.name,
args: self
.args
.into_iter()
......@@ -549,7 +543,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> = scale_info::Path<F>;
pub type DisplayName = scale_info::Path;
/// A type specification.
///
......@@ -575,7 +569,7 @@ pub struct TypeSpec<F: Form = MetaForm> {
/// The actual type.
id: F::TypeId,
/// The compile-time known displayed representation of the type.
display_name: DisplayName<F>,
display_name: DisplayName,
}
impl IntoCompact for TypeSpec {
......@@ -584,7 +578,7 @@ impl IntoCompact for TypeSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
TypeSpec {
id: registry.register_type(&self.id),
display_name: self.display_name.into_compact(registry),
display_name: self.display_name,
}
}
}
......@@ -623,7 +617,7 @@ impl TypeSpec {
pub fn with_name_segs<T, S>(segments: S) -> Self
where
T: TypeInfo + 'static,
S: IntoIterator<Item = <MetaForm as Form>::String>,
S: IntoIterator<Item = &'static str>,
{
Self {
id: meta_type::<T>(),
......@@ -649,7 +643,7 @@ impl TypeSpec {
#[serde(bound = "F::TypeId: Serialize")]
pub struct EventParamSpec<F: Form = MetaForm> {
/// The name of the parameter.
name: F::String,
name: &'static str,
/// If the event parameter is indexed.
indexed: bool,
/// The type of the parameter.
......@@ -664,7 +658,7 @@ impl IntoCompact for EventParamSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
EventParamSpec {
name: registry.register_string(self.name),
name: self.name,
indexed: self.indexed,
ty: self.ty.into_compact(registry),
docs: self.docs,
......@@ -776,7 +770,7 @@ impl ReturnTypeSpec {
#[serde(bound = "F::TypeId: Serialize")]
pub struct MessageParamSpec<F: Form = MetaForm> {
/// The name of the parameter.
name: F::String,
name: &'static str,
/// The type of the parameter.
#[serde(rename = "type")]
ty: TypeSpec<F>,
......@@ -787,7 +781,7 @@ impl IntoCompact for MessageParamSpec {
fn into_compact(self, registry: &mut Registry) -> Self::Output {
MessageParamSpec {
name: registry.register_string(self.name),
name: self.name,
ty: self.ty.into_compact(registry),
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment