From c9262c3271290eebf5fd396d9967b8abbec06d9d Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Tue, 4 Apr 2023 19:25:29 +0400 Subject: [PATCH 01/10] environment metadata spec --- crates/env/Cargo.toml | 2 - crates/ink/codegen/src/generator/metadata.rs | 1 + crates/metadata/src/specs.rs | 96 +++++++++++++++----- 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/crates/env/Cargo.toml b/crates/env/Cargo.toml index 504758ee928..a57c0e378c8 100644 --- a/crates/env/Cargo.toml +++ b/crates/env/Cargo.toml @@ -16,7 +16,6 @@ categories = ["no-std", "embedded"] include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"] [dependencies] -ink_metadata = { version = "4.1.0", path = "../metadata", default-features = false, features = ["derive"], optional = true } ink_allocator = { version = "4.1.0", path = "../allocator", default-features = false } ink_storage_traits = { version = "4.1.0", path = "../storage/traits", default-features = false } ink_prelude = { version = "4.1.0", path = "../prelude", default-features = false } @@ -56,7 +55,6 @@ ink = { path = "../ink" } [features] default = ["std"] std = [ - "ink_metadata/std", "ink_allocator/std", "ink_prelude/std", "ink_primitives/std", diff --git a/crates/ink/codegen/src/generator/metadata.rs b/crates/ink/codegen/src/generator/metadata.rs index 803372ca930..3bb12c1629c 100644 --- a/crates/ink/codegen/src/generator/metadata.rs +++ b/crates/ink/codegen/src/generator/metadata.rs @@ -86,6 +86,7 @@ impl Metadata<'_> { let constructors = self.generate_constructors(); let messages = self.generate_messages(); let events = self.generate_events(); + let env = self.contract.config().env(); let docs = self .contract .module() diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index b8b71b4a8bd..4a5a6f58809 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -14,33 +14,15 @@ #![allow(clippy::new_ret_no_self)] -use crate::{ - serde_hex, - utils::trim_extra_whitespace, -}; +use crate::{serde_hex, utils::trim_extra_whitespace}; #[cfg(not(feature = "std"))] -use alloc::{ - format, - vec, - vec::Vec, -}; +use alloc::{format, vec, vec::Vec}; use core::marker::PhantomData; use scale_info::{ - form::{ - Form, - MetaForm, - PortableForm, - }, - meta_type, - IntoPortable, - Registry, - TypeInfo, -}; -use serde::{ - de::DeserializeOwned, - Deserialize, - Serialize, + form::{Form, MetaForm, PortableForm}, + meta_type, IntoPortable, Registry, TypeInfo, }; +use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Describes a contract. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -516,6 +498,18 @@ mod state { pub struct IsPayable; /// Type state for the message return type. pub struct Returns; + /// Type state for the `AccountId` type of the environment. + pub struct AccountId; + /// Type state for the `Balance` type of the environment. + pub struct Balance; + /// Type state for the `Timestampt` type of the environment. + pub struct Timestamp; + /// Type state for the BlockNumber` type of the environment. + pub struct BlockNumber; + /// Type state for the `ChainExtension` type of the environment. + pub struct ChainExtension; + /// Type state for the max number of topics specified in the environment. + pub struct MaxEventTopics; } impl MessageSpec @@ -1298,3 +1292,59 @@ where self.spec } } + +/// Describes a contract message. +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(bound( + serialize = "F::Type: Serialize, F::String: Serialize", + deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned" +))] +#[serde(rename_all = "camelCase")] +pub struct EnvironmentSpec { + account_id: TypeSpec, + balance: TypeSpec, + hash: TypeSpec, + timestamp: TypeSpec, + block_number: TypeSpec, + chain_extension: TypeSpec, + max_event_topics: usize, +} + +impl EnvironmentSpec +where + F: Form, +{ + pub fn account_id(&self) -> &TypeSpec { + &self.account_id + } + + pub fn balance(&self) -> &TypeSpec { + &self.balance + } + pub fn hash(&self) -> &TypeSpec { + &self.hash + } + pub fn timestamp(&self) -> &TypeSpec { + &self.timestamp + } + pub fn block_number(&self) -> &TypeSpec { + &self.block_number + } + pub fn chain_extension(&self) -> &TypeSpec { + &self.chain_extension + } + pub fn max_event_topics(&self) -> usize { + self.max_event_topics + } +} + +/// An environment specification builder. +#[must_use] +pub struct EnvironmentSpecBuilder +where + F: Form, +{ + spec: EnvironmentSpec, +} + + From 2b20d846e29f2f4b1414b70871e2e6e833ace850 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 5 Apr 2023 20:45:51 +0400 Subject: [PATCH 02/10] persisting env in metadata --- crates/env/src/types.rs | 1 + crates/ink/codegen/src/generator/env.rs | 2 + crates/ink/codegen/src/generator/metadata.rs | 39 ++- crates/metadata/src/lib.rs | 2 + crates/metadata/src/specs.rs | 305 ++++++++++++++++++- crates/metadata/src/tests.rs | 91 ++++++ 6 files changed, 423 insertions(+), 17 deletions(-) diff --git a/crates/env/src/types.rs b/crates/env/src/types.rs index 1e861c9daa1..fab2503dc21 100644 --- a/crates/env/src/types.rs +++ b/crates/env/src/types.rs @@ -177,6 +177,7 @@ pub trait Environment { } /// Placeholder for chains that have no defined chain extension. +#[cfg_attr(feature = "std", derive(TypeInfo))] pub enum NoChainExtension {} /// The fundamental types of the default configuration. diff --git a/crates/ink/codegen/src/generator/env.rs b/crates/ink/codegen/src/generator/env.rs index 31361cd9cbd..c72627732ac 100644 --- a/crates/ink/codegen/src/generator/env.rs +++ b/crates/ink/codegen/src/generator/env.rs @@ -39,6 +39,8 @@ impl GenerateCode for Env<'_> { type Hash = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::Hash; type Timestamp = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::Timestamp; type BlockNumber = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::BlockNumber; + type ChainExtension = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::ChainExtension; + const MAX_EVENT_TOPICS: usize = <<#storage_ident as ::ink::env::ContractEnv>::Env as ::ink::env::Environment>::MAX_EVENT_TOPICS; } } } diff --git a/crates/ink/codegen/src/generator/metadata.rs b/crates/ink/codegen/src/generator/metadata.rs index 3bb12c1629c..76fa16c3ce2 100644 --- a/crates/ink/codegen/src/generator/metadata.rs +++ b/crates/ink/codegen/src/generator/metadata.rs @@ -28,7 +28,10 @@ use quote::{ quote, quote_spanned, }; -use syn::spanned::Spanned as _; +use syn::{ + parse_quote, + spanned::Spanned as _, +}; /// Generates code to generate the metadata of the contract. #[derive(From)] @@ -86,7 +89,6 @@ impl Metadata<'_> { let constructors = self.generate_constructors(); let messages = self.generate_messages(); let events = self.generate_events(); - let env = self.contract.config().env(); let docs = self .contract .module() @@ -97,6 +99,7 @@ impl Metadata<'_> { ::ink::LangError }; let error = Self::generate_type_spec(&error_ty); + let environment = self.generate_environment(); quote! { ::ink::metadata::ContractSpec::new() .constructors([ @@ -114,6 +117,9 @@ impl Metadata<'_> { .lang_error( #error ) + .environment( + #environment + ) .done() } } @@ -408,6 +414,35 @@ impl Metadata<'_> { ) }) } + + fn generate_environment(&self) -> TokenStream2 { + let span = self.contract.module().span(); + + let account_id: syn::Type = parse_quote!(AccountId); + let balance: syn::Type = parse_quote!(Balance); + let hash: syn::Type = parse_quote!(Hash); + let timestamp: syn::Type = parse_quote!(Timestamp); + let block_number: syn::Type = parse_quote!(BlockNumber); + let chain_extension: syn::Type = parse_quote!(ChainExtension); + + let account_id = Self::generate_type_spec(&account_id); + let balance = Self::generate_type_spec(&balance); + let hash = Self::generate_type_spec(&hash); + let timestamp = Self::generate_type_spec(×tamp); + let block_number = Self::generate_type_spec(&block_number); + let chain_extension = Self::generate_type_spec(&chain_extension); + quote_spanned!(span=> + ::ink::metadata::EnvironmentSpec::new() + .account_id(#account_id) + .balance(#balance) + .hash(#hash) + .timestamp(#timestamp) + .block_number(#block_number) + .chain_extension(#chain_extension) + .max_event_topics(MAX_EVENT_TOPICS) + .done() + ) + } } #[cfg(test)] diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index 9e19dae96df..cbe4ee3f550 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -36,6 +36,8 @@ pub use self::specs::{ ContractSpec, ContractSpecBuilder, DisplayName, + EnvironmentSpec, + EnvironmentSpecBuilder, EventParamSpec, EventParamSpecBuilder, EventSpec, diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index 4a5a6f58809..04bf4d05284 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -14,15 +14,33 @@ #![allow(clippy::new_ret_no_self)] -use crate::{serde_hex, utils::trim_extra_whitespace}; +use crate::{ + serde_hex, + utils::trim_extra_whitespace, +}; #[cfg(not(feature = "std"))] -use alloc::{format, vec, vec::Vec}; +use alloc::{ + format, + vec, + vec::Vec, +}; use core::marker::PhantomData; use scale_info::{ - form::{Form, MetaForm, PortableForm}, - meta_type, IntoPortable, Registry, TypeInfo, + form::{ + Form, + MetaForm, + PortableForm, + }, + meta_type, + IntoPortable, + Registry, + TypeInfo, +}; +use serde::{ + de::DeserializeOwned, + Deserialize, + Serialize, }; -use serde::{de::DeserializeOwned, Deserialize, Serialize}; /// Describes a contract. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] @@ -30,7 +48,10 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; serialize = "F::Type: Serialize, F::String: Serialize", deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned" ))] -pub struct ContractSpec { +pub struct ContractSpec +where + TypeSpec: Default, +{ /// The set of constructors of the contract. constructors: Vec>, /// The external messages of the contract. @@ -41,6 +62,7 @@ pub struct ContractSpec { docs: Vec, /// The language specific error type. lang_error: TypeSpec, + environment: EnvironmentSpec, } impl IntoPortable for ContractSpec { @@ -65,6 +87,7 @@ impl IntoPortable for ContractSpec { .collect::>(), docs: registry.map_into_portable(self.docs), lang_error: self.lang_error.into_portable(registry), + environment: self.environment.into_portable(registry), } } } @@ -72,6 +95,7 @@ impl IntoPortable for ContractSpec { impl ContractSpec where F: Form, + TypeSpec: Default, { /// Returns the set of constructors of the contract. pub fn constructors(&self) -> &[ConstructorSpec] { @@ -97,6 +121,10 @@ where pub fn lang_error(&self) -> &TypeSpec { &self.lang_error } + + pub fn environment(&self) -> &EnvironmentSpec { + &self.environment + } } /// The message builder is ready to finalize construction. @@ -108,6 +136,7 @@ pub enum Invalid {} pub struct ContractSpecBuilder where F: Form, + TypeSpec: Default, { /// The to-be-constructed contract specification. spec: ContractSpec, @@ -118,6 +147,7 @@ where impl ContractSpecBuilder where F: Form, + TypeSpec: Default, { /// Sets the constructors of the contract specification. pub fn constructors(self, constructors: C) -> ContractSpecBuilder @@ -138,6 +168,7 @@ where impl ContractSpecBuilder where F: Form, + TypeSpec: Default, { /// Sets the messages of the contract specification. pub fn messages(self, messages: M) -> Self @@ -184,7 +215,7 @@ where } } - /// Sets the language error of the contract specification + /// Sets the language error of the contract specification. pub fn lang_error(self, lang_error: TypeSpec) -> Self { Self { spec: ContractSpec { @@ -194,11 +225,23 @@ where ..self } } + + /// sets the environment types of the contract specification. + pub fn environment(self, environment: EnvironmentSpec) -> Self { + Self { + spec: ContractSpec { + environment, + ..self.spec + }, + ..self + } + } } impl ContractSpecBuilder where F: Form, + TypeSpec: Default, { /// Finalizes construction of the contract specification. pub fn done(self) -> ContractSpec { @@ -218,6 +261,7 @@ impl ContractSpec where F: Form, TypeSpec: Default, + EnvironmentSpec: Default, { /// Creates a new contract specification. pub fn new() -> ContractSpecBuilder { @@ -228,6 +272,7 @@ where events: Vec::new(), docs: Vec::new(), lang_error: Default::default(), + environment: Default::default(), }, marker: PhantomData, } @@ -500,15 +545,17 @@ mod state { pub struct Returns; /// Type state for the `AccountId` type of the environment. pub struct AccountId; - /// Type state for the `Balance` type of the environment. + /// Type state for the `Balance` type of the environment. pub struct Balance; - /// Type state for the `Timestampt` type of the environment. + /// Type state for the `Hash` type of the environment. + pub struct Hash; + /// Type state for the `Timestamp` type of the environment. pub struct Timestamp; - /// Type state for the BlockNumber` type of the environment. + /// Type state for the BlockNumber` type of the environment. pub struct BlockNumber; - /// Type state for the `ChainExtension` type of the environment. + /// Type state for the `ChainExtension` type of the environment. pub struct ChainExtension; - /// Type state for the max number of topics specified in the environment. + /// Type state for the max number of topics specified in the environment. pub struct MaxEventTopics; } @@ -1300,7 +1347,10 @@ where deserialize = "F::Type: DeserializeOwned, F::String: DeserializeOwned" ))] #[serde(rename_all = "camelCase")] -pub struct EnvironmentSpec { +pub struct EnvironmentSpec +where + TypeSpec: Default, +{ account_id: TypeSpec, balance: TypeSpec, hash: TypeSpec, @@ -1310,14 +1360,48 @@ pub struct EnvironmentSpec { max_event_topics: usize, } +impl Default for EnvironmentSpec +where + F: Form, + TypeSpec: Default, +{ + fn default() -> Self { + Self { + account_id: Default::default(), + balance: Default::default(), + hash: Default::default(), + timestamp: Default::default(), + block_number: Default::default(), + chain_extension: Default::default(), + max_event_topics: Default::default(), + } + } +} + +impl IntoPortable for EnvironmentSpec { + type Output = EnvironmentSpec; + + fn into_portable(self, registry: &mut Registry) -> Self::Output { + EnvironmentSpec { + account_id: self.account_id.into_portable(registry), + balance: self.balance.into_portable(registry), + hash: self.hash.into_portable(registry), + timestamp: self.timestamp.into_portable(registry), + block_number: self.block_number.into_portable(registry), + chain_extension: self.chain_extension.into_portable(registry), + max_event_topics: self.max_event_topics, + } + } +} + impl EnvironmentSpec where F: Form, + TypeSpec: Default, { pub fn account_id(&self) -> &TypeSpec { &self.account_id } - pub fn balance(&self) -> &TypeSpec { &self.balance } @@ -1338,13 +1422,204 @@ where } } +#[allow(clippy::type_complexity)] +impl EnvironmentSpec +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn new() -> EnvironmentSpecBuilder< + F, + Missing, + Missing, + Missing, + Missing, + Missing, + Missing, + Missing, + > { + EnvironmentSpecBuilder { + spec: Default::default(), + marker: PhantomData, + } + } +} + /// An environment specification builder. +#[allow(clippy::type_complexity)] #[must_use] -pub struct EnvironmentSpecBuilder +pub struct EnvironmentSpecBuilder where F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, { spec: EnvironmentSpec, + marker: PhantomData (A, B, H, T, BN, C, M)>, +} + +impl + EnvironmentSpecBuilder, B, H, T, BN, C, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn account_id( + self, + account_id: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + account_id, + ..self.spec + }, + marker: PhantomData, + } + } +} + +impl + EnvironmentSpecBuilder, H, T, BN, C, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn balance( + self, + balance: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + balance, + ..self.spec + }, + marker: PhantomData, + } + } +} + +impl + EnvironmentSpecBuilder, T, BN, C, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn hash( + self, + hash: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { hash, ..self.spec }, + marker: PhantomData, + } + } +} + +impl + EnvironmentSpecBuilder, BN, C, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn timestamp( + self, + timestamp: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + timestamp, + ..self.spec + }, + marker: PhantomData, + } + } +} + +impl + EnvironmentSpecBuilder, C, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn block_number( + self, + block_number: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + block_number, + ..self.spec + }, + marker: PhantomData, + } + } } +impl + EnvironmentSpecBuilder, M> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn chain_extension( + self, + chain_extension: TypeSpec, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + chain_extension, + ..self.spec + }, + marker: PhantomData, + } + } +} +impl + EnvironmentSpecBuilder> +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn max_event_topics( + self, + max_event_topics: usize, + ) -> EnvironmentSpecBuilder { + EnvironmentSpecBuilder { + spec: EnvironmentSpec { + max_event_topics, + ..self.spec + }, + marker: PhantomData, + } + } +} + +impl + EnvironmentSpecBuilder< + F, + state::AccountId, + state::Balance, + state::Hash, + state::Timestamp, + state::BlockNumber, + state::ChainExtension, + state::MaxEventTopics, + > +where + F: Form, + TypeSpec: Default, + EnvironmentSpec: Default, +{ + pub fn done(self) -> EnvironmentSpec { + self.spec + } +} diff --git a/crates/metadata/src/tests.rs b/crates/metadata/src/tests.rs index c539ffc377b..f463ac75bda 100644 --- a/crates/metadata/src/tests.rs +++ b/crates/metadata/src/tests.rs @@ -55,6 +55,17 @@ fn spec_constructor_selector_must_serialize_to_hex() { #[test] fn spec_contract_json() { + #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] + pub enum NoChainExtension {} + + type AccountId = ink_primitives::AccountId; + type Balance = u64; + type Hash = ink_primitives::Hash; + type Timestamp = u64; + type BlockNumber = u128; + type ChainExtension = NoChainExtension; + const MAX_EVENT_TOPICS: usize = 4; + // given let contract: ContractSpec = ContractSpec::new() .constructors(vec![ @@ -117,6 +128,47 @@ fn spec_contract_json() { ::core::convert::AsRef::as_ref, ), )) + .environment( + EnvironmentSpec::new() + .account_id(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["AccountId"]), + ::core::convert::AsRef::as_ref, + ), + )) + .balance(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Balance"]), + ::core::convert::AsRef::as_ref, + ), + )) + .hash(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Hash"]), + ::core::convert::AsRef::as_ref, + ), + )) + .timestamp(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["Timestamp"]), + ::core::convert::AsRef::as_ref, + ), + )) + .block_number(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["BlockNumber"]), + ::core::convert::AsRef::as_ref, + ), + )) + .chain_extension(TypeSpec::with_name_segs::( + ::core::iter::Iterator::map( + ::core::iter::IntoIterator::into_iter(["ChainExtension"]), + ::core::convert::AsRef::as_ref, + ), + )) + .max_event_topics(MAX_EVENT_TOPICS) + .done(), + ) .done(); let mut registry = Registry::new(); @@ -172,6 +224,45 @@ fn spec_contract_json() { } ], "docs": [], + "environment": { + "accountId": { + "displayName": [ + "AccountId", + ], + "type": 4, + }, + "balance": { + "displayName": [ + "Balance", + ], + "type": 7, + }, + "blockNumber": { + "displayName": [ + "BlockNumber", + ], + "type": 9, + }, + "chainExtension": { + "displayName": [ + "ChainExtension", + ], + "type": 10, + }, + "hash": { + "displayName": [ + "Hash", + ], + "type": 8, + }, + "maxEventTopics": 4, + "timestamp": { + "displayName": [ + "Timestamp", + ], + "type": 7, + }, + }, "events": [], "lang_error": { "displayName": [ From ba5d3e55949283dd2ac5a7bdcb0a1f4c1a5adb17 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 5 Apr 2023 20:48:18 +0400 Subject: [PATCH 03/10] Changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aacbef652ac..d86200ae5b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Persist `Environment` in metadata - [#1741](https://github.com/paritytech/ink/pull/1741) + ### Changed - Upgraded `syn` to version `2` - [#1731](https://github.com/paritytech/ink/pull/1731) From 0e0e1b1246d32b1ddc6cb132271eb512cafdb15d Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 5 Apr 2023 20:53:13 +0400 Subject: [PATCH 04/10] docs --- crates/metadata/src/specs.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index 04bf4d05284..6ceb1b56d15 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -1340,7 +1340,7 @@ where } } -/// Describes a contract message. +/// Describes a contract environment. #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] #[serde(bound( serialize = "F::Type: Serialize, F::String: Serialize", @@ -1399,24 +1399,31 @@ where F: Form, TypeSpec: Default, { + /// Returns the `AccountId` type of the environment. pub fn account_id(&self) -> &TypeSpec { &self.account_id } + /// Returns the `Balance` type of the environment. pub fn balance(&self) -> &TypeSpec { &self.balance } + /// Returns the `Hash` type of the environment. pub fn hash(&self) -> &TypeSpec { &self.hash } + /// Returns the `Timestamp` type of the environment. pub fn timestamp(&self) -> &TypeSpec { &self.timestamp } + /// Returns the `BlockNumber` type of the environment. pub fn block_number(&self) -> &TypeSpec { &self.block_number } + /// Returns the `ChainExtension` type of the environment. pub fn chain_extension(&self) -> &TypeSpec { &self.chain_extension } + /// Returns the `MAX_EVENT_TOPICS` value of the environment. pub fn max_event_topics(&self) -> usize { self.max_event_topics } @@ -1466,6 +1473,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `AccountId` type of the environment. pub fn account_id( self, account_id: TypeSpec, @@ -1487,6 +1495,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `Balance` type of the environment. pub fn balance( self, balance: TypeSpec, @@ -1508,6 +1517,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `Hash` type of the environment. pub fn hash( self, hash: TypeSpec, @@ -1526,6 +1536,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `Timestamp` type of the environment. pub fn timestamp( self, timestamp: TypeSpec, @@ -1547,6 +1558,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `BlockNumber` type of the environment. pub fn block_number( self, block_number: TypeSpec, @@ -1568,6 +1580,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `ChainExtension` type of the environment. pub fn chain_extension( self, chain_extension: TypeSpec, @@ -1589,6 +1602,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Sets the `MAX_EVENT_TOPICS` value of the environment. pub fn max_event_topics( self, max_event_topics: usize, @@ -1619,6 +1633,7 @@ where TypeSpec: Default, EnvironmentSpec: Default, { + /// Finished constructing the `EnvironmentSpec` object. pub fn done(self) -> EnvironmentSpec { self.spec } From 6107e2d35705dee09416661b63dc51828fb7d33c Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 5 Apr 2023 20:55:36 +0400 Subject: [PATCH 05/10] typo --- crates/metadata/src/specs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index 6ceb1b56d15..da84ee77a56 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -551,7 +551,7 @@ mod state { pub struct Hash; /// Type state for the `Timestamp` type of the environment. pub struct Timestamp; - /// Type state for the BlockNumber` type of the environment. + /// Type state for the `BlockNumber` type of the environment. pub struct BlockNumber; /// Type state for the `ChainExtension` type of the environment. pub struct ChainExtension; From 5477fcc76695f1e5797fc9fbd06b7c866da5b015 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 5 Apr 2023 21:14:34 +0400 Subject: [PATCH 06/10] derive TypeInfo for chain extension --- crates/ink/codegen/src/generator/chain_extension.rs | 3 +++ crates/ink/src/lib.rs | 2 ++ crates/metadata/src/lib.rs | 1 + 3 files changed, 6 insertions(+) diff --git a/crates/ink/codegen/src/generator/chain_extension.rs b/crates/ink/codegen/src/generator/chain_extension.rs index 8f36e838eec..d35a2a82e42 100644 --- a/crates/ink/codegen/src/generator/chain_extension.rs +++ b/crates/ink/codegen/src/generator/chain_extension.rs @@ -133,6 +133,9 @@ impl GenerateCode for ChainExtension<'_> { let instance_ident = format_ident!("__ink_{}Instance", ident); quote_spanned!(span => #(#attrs)* + #[cfg_attr(feature = "std", derive( + ::scale_info::TypeInfo, + ))] pub enum #ident {} const _: () = { diff --git a/crates/ink/src/lib.rs b/crates/ink/src/lib.rs index ca0b8d4b695..f599470ef80 100644 --- a/crates/ink/src/lib.rs +++ b/crates/ink/src/lib.rs @@ -36,6 +36,8 @@ pub use ink_env as env; pub use ink_metadata as metadata; pub use ink_prelude as prelude; pub use ink_primitives as primitives; +#[cfg(feature = "std")] +pub use metadata::TypeInfo; pub mod storage { pub mod traits { diff --git a/crates/metadata/src/lib.rs b/crates/metadata/src/lib.rs index cbe4ee3f550..29673caeba3 100644 --- a/crates/metadata/src/lib.rs +++ b/crates/metadata/src/lib.rs @@ -53,6 +53,7 @@ pub use self::specs::{ use impl_serde::serialize as serde_hex; +pub use scale_info::TypeInfo; #[cfg(feature = "derive")] use scale_info::{ form::PortableForm, From 40bd22833117161bb75429cc3f2ef98b662476f0 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Thu, 6 Apr 2023 11:42:43 +0400 Subject: [PATCH 07/10] address clippy complaints --- crates/ink/tests/return_type_metadata.rs | 33 ++++++++++++------------ 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/crates/ink/tests/return_type_metadata.rs b/crates/ink/tests/return_type_metadata.rs index cd956899f72..994e2e03da9 100644 --- a/crates/ink/tests/return_type_metadata.rs +++ b/crates/ink/tests/return_type_metadata.rs @@ -66,21 +66,21 @@ mod tests { ) -> (&'a Type, &'a Type) { assert_eq!( "Result", - format!("{}", ty.path()), + format!("{}", ty.path), "Message return type should be a Result" ); - match ty.type_def() { + match &ty.type_def { TypeDef::Variant(variant) => { - assert_eq!(2, variant.variants().len()); - let ok_variant = &variant.variants()[0]; - let ok_field = &ok_variant.fields()[0]; - let ok_ty = resolve_type(metadata, ok_field.ty().id()); - assert_eq!("Ok", ok_variant.name()); + assert_eq!(2, variant.variants.len()); + let ok_variant = &variant.variants[0]; + let ok_field = &ok_variant.fields[0]; + let ok_ty = resolve_type(metadata, ok_field.ty.id); + assert_eq!("Ok", ok_variant.name); - let err_variant = &variant.variants()[1]; - let err_field = &err_variant.fields()[0]; - let err_ty = resolve_type(metadata, err_field.ty().id()); - assert_eq!("Err", err_variant.name()); + let err_variant = &variant.variants[1]; + let err_field = &err_variant.fields[0]; + let err_ty = resolve_type(metadata, err_field.ty.id); + assert_eq!("Err", err_variant.name); (ok_ty, err_ty) } @@ -107,10 +107,10 @@ mod tests { assert_eq!("TraitDefinition::get_value", message.label()); let type_spec = message.return_type().opt_type().unwrap(); - let ty = resolve_type(&metadata, type_spec.ty().id()); + let ty = resolve_type(&metadata, type_spec.ty().id); let (ok_ty, _) = extract_result(&metadata, ty); - assert_eq!(&TypeDef::Primitive(TypeDefPrimitive::U32), ok_ty.type_def()); + assert_eq!(&TypeDef::Primitive(TypeDefPrimitive::U32), &ok_ty.type_def); } #[test] @@ -125,19 +125,18 @@ mod tests { format!("{}", type_spec.display_name()) ); - let outer_result_ty = resolve_type(&metadata, type_spec.ty().id()); + let outer_result_ty = resolve_type(&metadata, type_spec.ty().id); let (outer_ok_ty, outer_err_ty) = extract_result(&metadata, outer_result_ty); let (inner_ok_ty, _) = extract_result(&metadata, outer_ok_ty); assert_eq!( - format!("{}", outer_err_ty.path()), + format!("{}", outer_err_ty.path), "ink_primitives::LangError" ); let unit_ty = TypeDef::Tuple(TypeDefTuple::new_portable(vec![])); assert_eq!( - &unit_ty, - inner_ok_ty.type_def(), + &unit_ty, &inner_ok_ty.type_def, "Ok variant should be a unit `()` type" ); } From 56064c55b34938fa39afc004e5ab52da2ba219aa Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Thu, 6 Apr 2023 12:14:53 +0400 Subject: [PATCH 08/10] remove unnecessary borrow --- crates/ink/tests/return_type_metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ink/tests/return_type_metadata.rs b/crates/ink/tests/return_type_metadata.rs index 148bbf77324..d7f41a9bb93 100644 --- a/crates/ink/tests/return_type_metadata.rs +++ b/crates/ink/tests/return_type_metadata.rs @@ -110,7 +110,7 @@ mod tests { let ty = resolve_type(&metadata, type_spec.ty().id); let (ok_ty, _) = extract_result(&metadata, ty); - assert_eq!(&TypeDef::Primitive(TypeDefPrimitive::U32), &ok_ty.type_def); + assert_eq!(TypeDef::Primitive(TypeDefPrimitive::U32), ok_ty.type_def); } #[test] From a1e5c8cb87063a5dbccc4a5725f746b67f77b124 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 12 Apr 2023 21:07:26 +0200 Subject: [PATCH 09/10] add comments --- crates/metadata/src/specs.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index da84ee77a56..39656d1c07b 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -62,6 +62,7 @@ where docs: Vec, /// The language specific error type. lang_error: TypeSpec, + /// The environment types of the contract specification. environment: EnvironmentSpec, } @@ -121,7 +122,7 @@ where pub fn lang_error(&self) -> &TypeSpec { &self.lang_error } - + // Returns The environment types of the contract specification. pub fn environment(&self) -> &EnvironmentSpec { &self.environment } @@ -226,7 +227,7 @@ where } } - /// sets the environment types of the contract specification. + /// Sets the environment types of the contract specification. pub fn environment(self, environment: EnvironmentSpec) -> Self { Self { spec: ContractSpec { From 3b7d8ff0a25600d34059da2c3edb6e41886c8318 Mon Sep 17 00:00:00 2001 From: SkymanOne Date: Wed, 12 Apr 2023 21:07:51 +0200 Subject: [PATCH 10/10] typo --- crates/metadata/src/specs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/metadata/src/specs.rs b/crates/metadata/src/specs.rs index 39656d1c07b..c817db7049e 100644 --- a/crates/metadata/src/specs.rs +++ b/crates/metadata/src/specs.rs @@ -122,7 +122,7 @@ where pub fn lang_error(&self) -> &TypeSpec { &self.lang_error } - // Returns The environment types of the contract specification. + // Returns the environment types of the contract specification. pub fn environment(&self) -> &EnvironmentSpec { &self.environment }