-
Notifications
You must be signed in to change notification settings - Fork 126
Add contract's ink! compatibility check #1334
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
95f63cd
Initial ink version compatibility check
smiasojed dd9f131
Fixed issues
smiasojed ea684f4
fmt applied
smiasojed 80afa30
unit test fixed for check ink compatibility
smiasojed b1de62a
Added json file with version requirements
smiasojed 91a10ce
Formatting fixed
smiasojed 085de18
Added empty line to the end of json file
smiasojed 8abf0b0
Replaced unwrap with expect
smiasojed 033f111
Changed ink versionreq to start from stable version
smiasojed a58ae70
Add total contract deposit
smiasojed 448f19e
Revert "Add total contract deposit"
smiasojed 23d42f4
Remove custom matches implementation for VersionReq
smiasojed 2839777
Code refactored
smiasojed 0de7e04
Code fmt
smiasojed 4c3f87e
Renamed structures
smiasojed b1bee81
Code refactored
smiasojed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "cargo-contract": { | ||
| "1.5.0": { | ||
| "ink": [">=3.0.0,<4.0.0", ">=3.0.0-alpha, <4.0.0-alpha.3"] | ||
| }, | ||
| "4.0.0-alpha": { | ||
| "ink": ["4.0.0-alpha.3", "4.0.0", "5.0.0-alpha"] | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // Copyright 2018-2023 Parity Technologies (UK) Ltd. | ||
| // This file is part of cargo-contract. | ||
| // | ||
| // cargo-contract is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // cargo-contract is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with cargo-contract. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use anyhow::{ | ||
| anyhow, | ||
| bail, | ||
| Result, | ||
| }; | ||
| use semver::{ | ||
| Version, | ||
| VersionReq, | ||
| }; | ||
|
|
||
| /// Version of the currently executing `cargo-contract` binary. | ||
| const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
|
||
| #[derive(Debug, serde::Deserialize)] | ||
| struct Compatibility { | ||
| #[serde(rename = "cargo-contract")] | ||
| cargo_contract_compatibility: HashMap<Version, Requirements>, | ||
| } | ||
|
|
||
| #[derive(Debug, serde::Deserialize)] | ||
| struct Requirements { | ||
| #[serde(rename = "ink")] | ||
| ink_requirements: Vec<VersionReq>, | ||
| } | ||
|
|
||
| /// Checks whether the contract's ink! version is compatible with the cargo-contract | ||
| /// binary. | ||
| pub fn check_contract_ink_compatibility(ink_version: &Version) -> Result<()> { | ||
| let compatibility_list = include_str!("../compatibility_list.json"); | ||
| let compatibility: Compatibility = serde_json::from_str(compatibility_list)?; | ||
| let cargo_contract_version = | ||
| semver::Version::parse(VERSION).expect("Parsing version failed"); | ||
| let ink_req = &compatibility | ||
| .cargo_contract_compatibility | ||
| .get(&cargo_contract_version) | ||
| .ok_or(anyhow!( | ||
| "Missing compatibility configuration for cargo-contract: {}", | ||
| cargo_contract_version | ||
| ))? | ||
| .ink_requirements; | ||
|
|
||
| // Check if the ink! version matches any of the requirement | ||
| if !ink_req.iter().any(|req| req.matches(ink_version)) { | ||
| // Get matching ink! versions | ||
| let ink_matches = ink_req | ||
| .iter() | ||
| .map(|req| format!("'{}'", req)) | ||
| .collect::<Vec<_>>() | ||
| .join(", "); | ||
|
|
||
| let ink_matches_message = if !ink_matches.is_empty() { | ||
| format!("update the contract ink! to a version of {}", ink_matches) | ||
| } else { | ||
| String::default() | ||
| }; | ||
|
|
||
| // Find best cargo-contract version | ||
| let cargo_contract_match_message = compatibility | ||
| .cargo_contract_compatibility | ||
| .iter() | ||
| .filter_map(|(ver, reqs)| { | ||
| if reqs | ||
| .ink_requirements | ||
| .iter() | ||
| .any(|req| req.matches(ink_version)) | ||
| { | ||
| return Some(ver) | ||
| } | ||
| None | ||
| }) | ||
| .max_by(|&a, &b| { | ||
| match (!a.pre.is_empty(), !b.pre.is_empty()) { | ||
| (false, true) => std::cmp::Ordering::Greater, | ||
| (true, false) => std::cmp::Ordering::Less, | ||
| (_, _) => a.cmp(b), | ||
| } | ||
| }) | ||
| .map(|ver| format!("update the cargo-contract to version '{}'", ver)) | ||
| .unwrap_or_default(); | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let matches_message = [cargo_contract_match_message, ink_matches_message] | ||
| .into_iter() | ||
| .filter(|m| !m.is_empty()) | ||
| .collect::<Vec<_>>() | ||
| .join(" or "); | ||
| if !matches_message.is_empty() { | ||
| return Err(anyhow!( | ||
| "The cargo-contract is not compatible with the contract's ink! version. Please {}", | ||
| matches_message | ||
| )) | ||
| } else { | ||
| bail!( | ||
| "The cargo-contract is not compatible with the contract's ink! version, \ | ||
| but a matching version could not be found" | ||
| ) | ||
| } | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use pretty_assertions::assert_eq; | ||
|
|
||
| #[test] | ||
| fn ink_check_failes_when_incompatible_version() { | ||
| let ink_version = Version::new(3, 2, 0); | ||
| let res = check_contract_ink_compatibility(&ink_version) | ||
| .expect_err("Ink version check should fail"); | ||
|
|
||
| assert_eq!( | ||
| res.to_string(), | ||
| "The cargo-contract is not compatible with the contract's ink! version. \ | ||
| Please update the cargo-contract to version '1.5.0' or \ | ||
| update the contract ink! to a version of '^4.0.0-alpha.3', '^4.0.0', '^5.0.0-alpha'" | ||
| ); | ||
|
|
||
| let ink_version = | ||
| Version::parse("4.0.0-alpha.1").expect("Parsing version must work"); | ||
| let res = check_contract_ink_compatibility(&ink_version) | ||
| .expect_err("Ink version check should fail"); | ||
|
|
||
| assert_eq!( | ||
| res.to_string(), | ||
| "The cargo-contract is not compatible with the contract's ink! version. \ | ||
| Please update the cargo-contract to version '1.5.0' or \ | ||
| update the contract ink! to a version of '^4.0.0-alpha.3', '^4.0.0', '^5.0.0-alpha'" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ink_check_succeeds_when_compatible_version() { | ||
| let ink_version = Version::new(4, 2, 3); | ||
| let res = check_contract_ink_compatibility(&ink_version); | ||
| assert!(res.is_ok()); | ||
|
|
||
| let ink_version = | ||
| Version::parse("4.0.0-alpha.4").expect("Parsing version must work"); | ||
| let res = check_contract_ink_compatibility(&ink_version); | ||
| assert!(res.is_ok()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.