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

Persist Environment in metadata #1741

Merged
merged 11 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 0 additions & 2 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this because it seems it is not used anywhere.

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 }
Expand Down Expand Up @@ -56,7 +55,6 @@ ink = { path = "../ink" }
[features]
default = ["std"]
std = [
"ink_metadata/std",
"ink_allocator/std",
"ink_prelude/std",
"ink_primitives/std",
Expand Down
1 change: 1 addition & 0 deletions crates/env/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions crates/ink/codegen/src/generator/chain_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 _: () = {
Expand Down
2 changes: 2 additions & 0 deletions crates/ink/codegen/src/generator/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
38 changes: 37 additions & 1 deletion crates/ink/codegen/src/generator/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -96,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([
Expand All @@ -113,6 +117,9 @@ impl Metadata<'_> {
.lang_error(
#error
)
.environment(
#environment
)
.done()
}
}
Expand Down Expand Up @@ -407,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(&timestamp);
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)]
Expand Down
2 changes: 2 additions & 0 deletions crates/ink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub use self::specs::{
ContractSpec,
ContractSpecBuilder,
DisplayName,
EnvironmentSpec,
EnvironmentSpecBuilder,
EventParamSpec,
EventParamSpecBuilder,
EventSpec,
Expand All @@ -51,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,
Expand Down
Loading