From a2a464cad94866c0e9cea93dd70a957c4462d96e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 15:58:51 +0100 Subject: [PATCH 1/8] Supply metadata version as a parameter --- Cargo.lock | 2 +- Cargo.toml | 2 +- metadata/Cargo.toml | 2 +- metadata/lib.rs | 9 +++------ 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 31e29b49..a5b3ee6a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -638,7 +638,7 @@ checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" [[package]] name = "contract-metadata" -version = "0.3.0" +version = "0.4.0" dependencies = [ "pretty_assertions", "semver", diff --git a/Cargo.toml b/Cargo.toml index f1f8298b..01eab70f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ colored = "2.0.0" toml = "0.5.8" rustc_version = "0.4.0" blake2 = "0.9.2" -contract-metadata = { version = "0.3.0", path = "./metadata" } +contract-metadata = { version = "0.4.0", path = "./metadata" } semver = { version = "1.0.4", features = ["serde"] } serde = { version = "1.0.130", default-features = false, features = ["derive"] } serde_json = "1.0.68" diff --git a/metadata/Cargo.toml b/metadata/Cargo.toml index 915f6d9a..81b306bf 100644 --- a/metadata/Cargo.toml +++ b/metadata/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "contract-metadata" -version = "0.3.0" +version = "0.4.0" authors = ["Parity Technologies "] edition = "2018" diff --git a/metadata/lib.rs b/metadata/lib.rs index 90eebbf1..47020513 100644 --- a/metadata/lib.rs +++ b/metadata/lib.rs @@ -52,14 +52,13 @@ //! let json = serde_json::to_value(&metadata).unwrap(); //! ``` +pub use semver::Version; + use core::fmt::{Display, Formatter, Result as DisplayResult, Write}; -use semver::Version; use serde::{Serialize, Serializer}; use serde_json::{Map, Value}; use url::Url; -const METADATA_VERSION: &str = "0.1.0"; - /// Smart contract metadata. #[derive(Clone, Debug, Serialize)] pub struct ContractMetadata { @@ -77,14 +76,12 @@ pub struct ContractMetadata { impl ContractMetadata { /// Construct new contract metadata. pub fn new( + metadata_version: semver::Version, source: Source, contract: Contract, user: Option, abi: Map, ) -> Self { - let metadata_version = semver::Version::parse(METADATA_VERSION) - .expect("METADATA_VERSION is a valid semver string"); - Self { metadata_version, source, -- GitLab From 0b11d4d567b91fcdba884506334b8c5591b3cbba Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 16:01:26 +0100 Subject: [PATCH 2/8] Fix build warning --- src/cmd/build.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index eadd5706..eae92054 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -436,7 +436,6 @@ fn do_optimization( We use this tool to optimize the size of your contract's Wasm binary.\n\n\ wasm-opt is part of the binaryen package. You can find detailed\n\ installation instructions on https://github.com/WebAssembly/binaryen#tools.\n\n\ - There are ready-to-install packages for many platforms:\n\ * Debian/Ubuntu: apt-get install binaryen\n\ * Homebrew: brew install binaryen\n\ -- GitLab From 1da48fffa4542b4a43653ac2b7a83aa9e06c32ad Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 16:20:39 +0100 Subject: [PATCH 3/8] Populate metadata version with ink_metadata crate version --- src/cmd/metadata.rs | 6 +++++- src/crate_metadata.rs | 35 +++++++++++++++++++++-------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/cmd/metadata.rs b/src/cmd/metadata.rs index 92bb3c3b..8caa9285 100644 --- a/src/cmd/metadata.rs +++ b/src/cmd/metadata.rs @@ -51,6 +51,7 @@ struct ExtendedMetadataResult { source: Source, contract: Contract, user: Option, + metadata_version: Version, } /// Generates a file with metadata describing the ABI of the smart-contract. @@ -76,6 +77,7 @@ pub(crate) fn execute( source, contract, user, + metadata_version, } = extended_metadata(crate_metadata, final_contract_wasm)?; let generate_metadata = |manifest_path: &ManifestPath| -> Result<()> { @@ -101,7 +103,7 @@ pub(crate) fn execute( )?; let ink_meta: serde_json::Map = serde_json::from_slice(&stdout)?; - let metadata = ContractMetadata::new(source, contract, user, ink_meta); + let metadata = ContractMetadata::new(metadata_version, source, contract, user, ink_meta); { let mut metadata = metadata.clone(); metadata.remove_source_wasm_attribute(); @@ -149,6 +151,7 @@ fn extended_metadata( ) -> Result { let contract_package = &crate_metadata.root_package; let ink_version = &crate_metadata.ink_version; + let metadata_version = crate_metadata.ink_metadata_version.clone(); let rust_version = Version::parse(&rustc_version::version()?.to_string())?; let contract_name = contract_package.name.clone(); let contract_version = Version::parse(&contract_package.version.to_string())?; @@ -209,6 +212,7 @@ fn extended_metadata( source, contract, user, + metadata_version, }) } diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 9b47b7d1..ee560e9a 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -33,6 +33,7 @@ pub struct CrateMetadata { pub original_wasm: PathBuf, pub dest_wasm: PathBuf, pub ink_version: Version, + pub ink_metadata_version: Version, pub documentation: Option, pub homepage: Option, pub user: Option>, @@ -75,20 +76,25 @@ impl CrateMetadata { dest_wasm.push(lib_name.clone()); dest_wasm.set_extension("wasm"); - let ink_version = metadata - .packages - .iter() - .find_map(|package| { - if package.name == "ink_lang" { - Some( - Version::parse(&package.version.to_string()) - .expect("Invalid ink_lang version string"), - ) - } else { - None - } - }) - .ok_or_else(|| anyhow::anyhow!("No 'ink_lang' dependency found"))?; + let crate_version = |crate_name| { + metadata + .packages + .iter() + .find_map(|package| { + if package.name == crate_name { + Some( + Version::parse(&package.version.to_string()) + .expect(&format!("Invalid {} version string", crate_name)), + ) + } else { + None + } + }) + .ok_or_else(|| anyhow::anyhow!("No '{}' dependency found", crate_name)) + }; + + let ink_version = crate_version("ink_lang")?; + let ink_metadata_version = crate_version("ink_metadata")?; let ExtraMetadata { documentation, @@ -104,6 +110,7 @@ impl CrateMetadata { original_wasm: original_wasm.into(), dest_wasm: dest_wasm.into(), ink_version, + ink_metadata_version, documentation, homepage, user, -- GitLab From 8415809fa51ae16eddb82c2652ac2a2c15218e41 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 16:26:06 +0100 Subject: [PATCH 4/8] Clippy --- src/crate_metadata.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index ee560e9a..220a1639 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -84,7 +84,7 @@ impl CrateMetadata { if package.name == crate_name { Some( Version::parse(&package.version.to_string()) - .expect(&format!("Invalid {} version string", crate_name)), + .unwrap_or_else(|| panic!("Invalid {} version string", crate_name)), ) } else { None -- GitLab From c27f31ad48b87b0172a840aef5df7e3eb9c30d09 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 16:30:36 +0100 Subject: [PATCH 5/8] Fix errors --- metadata/lib.rs | 4 ++-- src/crate_metadata.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/metadata/lib.rs b/metadata/lib.rs index 47020513..8a5a1840 100644 --- a/metadata/lib.rs +++ b/metadata/lib.rs @@ -553,7 +553,7 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(source, contract, Some(user), abi_json); + let metadata = ContractMetadata::new(Version::new(0, 1, 0), source, contract, Some(user), abi_json); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { @@ -618,7 +618,7 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(source, contract, None, abi_json); + let metadata = ContractMetadata::new(Version::new(0, 1, 0), source, contract, None, abi_json); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 220a1639..7c3edfc2 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -84,7 +84,7 @@ impl CrateMetadata { if package.name == crate_name { Some( Version::parse(&package.version.to_string()) - .unwrap_or_else(|| panic!("Invalid {} version string", crate_name)), + .unwrap_or_else(|e| panic!("Invalid {} version string: {}", crate_name, e)), ) } else { None -- GitLab From e503e7fdbb6765e51a0e44ecaf9d1fb5db597d39 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 16:31:07 +0100 Subject: [PATCH 6/8] Fmt --- metadata/lib.rs | 11 +++++++++-- src/crate_metadata.rs | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/metadata/lib.rs b/metadata/lib.rs index 8a5a1840..a2a7940f 100644 --- a/metadata/lib.rs +++ b/metadata/lib.rs @@ -553,7 +553,13 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(Version::new(0, 1, 0), source, contract, Some(user), abi_json); + let metadata = ContractMetadata::new( + Version::new(0, 1, 0), + source, + contract, + Some(user), + abi_json, + ); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { @@ -618,7 +624,8 @@ mod tests { .unwrap() .clone(); - let metadata = ContractMetadata::new(Version::new(0, 1, 0), source, contract, None, abi_json); + let metadata = + ContractMetadata::new(Version::new(0, 1, 0), source, contract, None, abi_json); let json = serde_json::to_value(&metadata).unwrap(); let expected = json! { diff --git a/src/crate_metadata.rs b/src/crate_metadata.rs index 7c3edfc2..e6422e54 100644 --- a/src/crate_metadata.rs +++ b/src/crate_metadata.rs @@ -83,8 +83,9 @@ impl CrateMetadata { .find_map(|package| { if package.name == crate_name { Some( - Version::parse(&package.version.to_string()) - .unwrap_or_else(|e| panic!("Invalid {} version string: {}", crate_name, e)), + Version::parse(&package.version.to_string()).unwrap_or_else(|e| { + panic!("Invalid {} version string: {}", crate_name, e) + }), ) } else { None -- GitLab From 75c05cbd4d556ccb40dd6ce3234845f2f8ea59bd Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 17:01:03 +0100 Subject: [PATCH 7/8] Fix doc test --- metadata/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metadata/lib.rs b/metadata/lib.rs index a2a7940f..c5d67eb2 100644 --- a/metadata/lib.rs +++ b/metadata/lib.rs @@ -45,8 +45,10 @@ //! let user = User::new(user_json); //! // contract abi raw json generated by contract compilation //! let abi_json: Map = Map::new(); +//! // the version of the `ink_metadata` crate used by the contract +//! let metadata_version = Version::new(3, 0, 0); //! -//! let metadata = ContractMetadata::new(source, contract, Some(user), abi_json); +//! let metadata = ContractMetadata::new(metadata_version, source, contract, Some(user), abi_json); //! //! // serialize to json //! let json = serde_json::to_value(&metadata).unwrap(); -- GitLab From f16d6cdcc1c4e48e065265defdb8066e5b2295ff Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Wed, 22 Sep 2021 17:03:16 +0100 Subject: [PATCH 8/8] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1861a8..5dc46906 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Update `cargo contract new` template dependencies to ink! `rc4` - [#332](https://github.com/paritytech/cargo-contract/pull/332) - Update `cargo contract new` template dependencies to ink! `rc5` - [#335](https://github.com/paritytech/cargo-contract/pull/335) +- Populate `metadataVersion` field with `ink_metadata` crate version - [#347](https://github.com/paritytech/cargo-contract/pull/347) ## [0.14.0] - 2021-08-12 -- GitLab