-
Notifications
You must be signed in to change notification settings - Fork 126
Add verify command
#1306
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
Add verify command
#1306
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
51117cc
Add skeleton of `Verify` command
HCastano 13d29dc
Read `BuildInfo` from contract metadata
HCastano 85ccd7d
Manually build contract using `BuildInfo`
HCastano 7de595c
Check built Wasm file against that in the reference metadata
HCastano e67d108
Comopare `SourceWasm`'s instead of byte strings
HCastano b90f126
Check that current `nightly` matches that from contract
HCastano a350f4c
Switch from `env_logger` to `tracing`
HCastano e87c51a
Use helper function to get current Rust toolchain
HCastano 4317f55
Clean up docs a little
HCastano fed9c3f
Bail out if `wasm-opt` versions don't match
HCastano b743987
Use stable `rustc` toolchain
HCastano f6770de
Use `keep_debug_symbols` from metadata
HCastano 2f3ca03
Make output more colourful
HCastano e2c5675
Add missing doc comment
HCastano 963387c
Do a better job of error handling
HCastano e7b9577
Add more error handling improvements
HCastano a64b631
Add `CHANGELOG` entry
HCastano d6db73f
Appease Clippy
HCastano 80233c4
Use updated Rust toolchain build info
HCastano 98cc7fb
Apply fixes for `clap` `v4.0`
HCastano b2d10c2
Compare `cargo-contract` versions instead of `wasm-opt` versions
HCastano 1a71959
Merge branch 'master' into gn/verify-command
SkymanOne 2fd80f1
update code
SkymanOne 02a9e65
add docker support
SkymanOne 5759843
remove docker flag
SkymanOne 3889a62
fix error message
SkymanOne 83373d8
display paths of the contracts
SkymanOne 2154bf3
fix changelog
SkymanOne c81857f
fix file header
SkymanOne 33dd9eb
Merge branch 'master' into gn/verify-command
SkymanOne 56b27de
apply review suggestions
SkymanOne 6e4fee5
prettify code hash output
SkymanOne dee765c
add integration tests
SkymanOne 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,236 @@ | ||
| // Copyright (C) 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 anyhow::{ | ||
| Context, | ||
| Result, | ||
| }; | ||
| use colored::Colorize; | ||
| use contract_build::{ | ||
| execute, | ||
| BuildArtifacts, | ||
| BuildInfo, | ||
| BuildMode, | ||
| ExecuteArgs, | ||
| ImageVariant, | ||
| ManifestPath, | ||
| Verbosity, | ||
| VerbosityFlags, | ||
| }; | ||
| use contract_metadata::ContractMetadata; | ||
|
|
||
| use std::{ | ||
| fs::File, | ||
| path::PathBuf, | ||
| }; | ||
|
|
||
| const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
|
||
| /// Checks if a contract in the given workspace matches that of a reference contract. | ||
| #[derive(Debug, clap::Args)] | ||
| #[clap(name = "verify")] | ||
| pub struct VerifyCommand { | ||
| /// Path to the `Cargo.toml` of the contract to verify. | ||
| #[clap(long, value_parser)] | ||
| manifest_path: Option<PathBuf>, | ||
| /// The reference Wasm contract (`*.contract`) that the workspace will be checked | ||
| /// against. | ||
| contract: PathBuf, | ||
| /// Denotes if output should be printed to stdout. | ||
| #[clap(flatten)] | ||
| verbosity: VerbosityFlags, | ||
| /// Output the result in JSON format | ||
| #[clap(long, conflicts_with = "verbose")] | ||
| output_json: bool, | ||
| } | ||
|
|
||
| impl VerifyCommand { | ||
| pub fn run(&self) -> Result<VerificationResult> { | ||
| let manifest_path = ManifestPath::try_from(self.manifest_path.as_ref())?; | ||
| let verbosity: Verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; | ||
|
|
||
| // 1. Read the given metadata, and pull out the `BuildInfo` | ||
| let path = &self.contract; | ||
| let file = File::open(path) | ||
| .context(format!("Failed to open contract bundle {}", path.display()))?; | ||
|
|
||
| let metadata: ContractMetadata = serde_json::from_reader(&file).context( | ||
| format!("Failed to deserialize contract bundle {}", path.display()), | ||
| )?; | ||
| let build_info = if let Some(info) = metadata.source.build_info { | ||
| info | ||
| } else { | ||
| anyhow::bail!( | ||
| "\nThe metadata does not contain any build information which can be used to \ | ||
| verify a contract." | ||
| .to_string() | ||
| .bright_yellow() | ||
| ) | ||
| }; | ||
|
|
||
| let build_info: BuildInfo = | ||
| serde_json::from_value(build_info.into()).context(format!( | ||
| "Failed to deserialize the build info from {}", | ||
| path.display() | ||
| ))?; | ||
|
|
||
| tracing::debug!( | ||
| "Parsed the following build info from the metadata: {:?}", | ||
| &build_info, | ||
| ); | ||
|
|
||
| let build_mode = if metadata.image.is_some() { | ||
| BuildMode::Verifiable | ||
| } else { | ||
| build_info.build_mode | ||
| }; | ||
|
|
||
| // 2. Check that the build info from the metadata matches our current setup. | ||
| // if the build mode is `Verifiable` we skip | ||
| if build_mode != BuildMode::Verifiable { | ||
| let expected_rust_toolchain = build_info.rust_toolchain; | ||
| let rust_toolchain = contract_build::util::rust_toolchain() | ||
| .expect("`rustc` always has a version associated with it."); | ||
|
|
||
| let rustc_matches = rust_toolchain == expected_rust_toolchain; | ||
| let mismatched_rustc = format!( | ||
| "\nYou are trying to `verify` a contract using the `{rust_toolchain}` toolchain.\n\ | ||
| However, the original contract was built using `{expected_rust_toolchain}`. Please\n\ | ||
| install the correct toolchain (`rustup install {expected_rust_toolchain}`) and\n\ | ||
| re-run the `verify` command.",); | ||
| anyhow::ensure!(rustc_matches, mismatched_rustc.bright_yellow()); | ||
|
|
||
| let expected_cargo_contract_version = build_info.cargo_contract_version; | ||
| let cargo_contract_version = semver::Version::parse(VERSION)?; | ||
|
|
||
| // Note, assuming both versions of `cargo-contract` were installed with the | ||
| // same lockfile (e.g `--locked`) then the versions of `wasm-opt` | ||
| // should also match. | ||
| let cargo_contract_matches = | ||
| cargo_contract_version == expected_cargo_contract_version; | ||
| let mismatched_cargo_contract = format!( | ||
| "\nYou are trying to `verify` a contract using `cargo-contract` version \ | ||
| `{cargo_contract_version}`.\n\ | ||
| However, the original contract was built using `cargo-contract` version \ | ||
| `{expected_cargo_contract_version}`.\n\ | ||
| Please install the matching version and re-run the `verify` command.", | ||
| ); | ||
| anyhow::ensure!( | ||
| cargo_contract_matches, | ||
| mismatched_cargo_contract.bright_yellow() | ||
| ); | ||
| } | ||
|
|
||
| // 3a. Call `cargo contract build` with the `BuildInfo` from the metadata. | ||
| let args = ExecuteArgs { | ||
| manifest_path: manifest_path.clone(), | ||
| verbosity, | ||
| build_mode, | ||
| build_artifact: BuildArtifacts::All, | ||
| optimization_passes: Some(build_info.wasm_opt_settings.optimization_passes), | ||
| keep_debug_symbols: build_info.wasm_opt_settings.keep_debug_symbols, | ||
| image: ImageVariant::from(metadata.image.clone()), | ||
| dylint: false, | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let build_result = execute(args)?; | ||
|
|
||
| // 4. Grab the code hash from the built contract and compare it with the reference | ||
| // one. | ||
| let reference_code_hash = metadata.source.hash; | ||
| let built_contract_path = if let Some(m) = build_result.metadata_result { | ||
| m | ||
| } else { | ||
| // Since we're building the contract ourselves this should always be | ||
| // populated, but we'll bail out here just in case. | ||
| anyhow::bail!( | ||
| "\nThe metadata for the workspace contract does not contain a Wasm binary,\n\ | ||
| therefore we are unable to verify the contract." | ||
| .to_string() | ||
| .bright_yellow() | ||
| ) | ||
| }; | ||
|
|
||
| let target_bundle = built_contract_path.dest_bundle; | ||
|
|
||
| let file = File::open(target_bundle.clone()).context(format!( | ||
| "Failed to open contract bundle {}", | ||
| target_bundle.display() | ||
| ))?; | ||
| let built_contract: ContractMetadata = | ||
| serde_json::from_reader(file).context(format!( | ||
| "Failed to deserialize contract bundle {}", | ||
| target_bundle.display() | ||
| ))?; | ||
|
|
||
| let target_code_hash = built_contract.source.hash; | ||
|
|
||
| if reference_code_hash != target_code_hash { | ||
| tracing::debug!( | ||
| "Expected Code Hash: '{:?}'\n\nGot Code Hash: `{:?}`", | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| &reference_code_hash, | ||
| &target_code_hash | ||
| ); | ||
|
|
||
| anyhow::bail!(format!( | ||
| "\nFailed to verify the authenticity of {} contract against the workspace \n\ | ||
| found at {}.", | ||
| format!("`{}`", metadata.contract.name).bright_white(), | ||
| format!("{:?}", manifest_path.as_ref()).bright_white()).bright_red() | ||
| ); | ||
| } | ||
|
|
||
| Ok(VerificationResult { | ||
| is_verified: true, | ||
| image: metadata.image, | ||
| contract: target_bundle.display().to_string(), | ||
| reference_contract: path.display().to_string(), | ||
| output_json: self.output_json, | ||
| verbosity, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// The result of verification process | ||
| #[derive(serde::Serialize, serde::Deserialize)] | ||
| pub struct VerificationResult { | ||
| pub is_verified: bool, | ||
| pub image: Option<String>, | ||
| pub contract: String, | ||
| pub reference_contract: String, | ||
| #[serde(skip_serializing, skip_deserializing)] | ||
| pub output_json: bool, | ||
| #[serde(skip_serializing, skip_deserializing)] | ||
| pub verbosity: Verbosity, | ||
| } | ||
|
|
||
| impl VerificationResult { | ||
| /// Display the result in a fancy format | ||
| pub fn display(&self) -> String { | ||
| format!( | ||
| "\n{} {} against reference contract {}", | ||
| "Successfully verified contract".bright_green().bold(), | ||
| format!("`{}`", &self.contract).bold(), | ||
| format!("`{}`!", &self.reference_contract).bold() | ||
| ) | ||
| } | ||
|
|
||
| /// Display the build results in a pretty formatted JSON string. | ||
| pub fn serialize_json(&self) -> Result<String> { | ||
| Ok(serde_json::to_string_pretty(self)?) | ||
| } | ||
| } | ||
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
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.