-
Notifications
You must be signed in to change notification settings - Fork 126
Add decode command for event, message and constructor data decoding
#481
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 12 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
31a35a1
add contract call data to debug log
agryaznov 1375fee
[REVERT!] some debug data to dig into an error
agryaznov e7df87f
save
agryaznov 9e5f387
Merge branch 'master' into ag-decode-command
agryaznov 1875e5d
decode cmd works for both event and message
agryaznov bbafb3b
remove some debugging prints
agryaznov 674574e
test fixed
agryaznov 1de752a
use util decode_hex()
agryaznov c101772
moved decode cmd to root (it's not an extrinsic)
agryaznov 85da815
e2e test for decoding message data
agryaznov eb759c8
add decoding event to the same e2e test
agryaznov ac80ff6
Merge branch 'master' into ag-decode-cmd
agryaznov 88ef5c9
Update src/cmd/decode.rs
agryaznov f6bc3c1
added constructor data decoder
agryaznov b3cdeab
Merge branch 'master' into ag-decode-cmd
agryaznov dec5cd4
ci fixes
agryaznov 1f24d5b
lil test update
agryaznov 03e4da9
Merge branch 'master' into ag-decode-cmd
agryaznov 49e04e5
Partially revert "lil test update" (mainfest fixes)
agryaznov 7f6ef5e
Build before test, and test --release
ascjones 068e052
Revert "Build before test, and test --release"
ascjones 6069a70
Move decode test to an integration test
ascjones 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| // Copyright 2018-2020 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 crate::{ | ||
| cmd::extrinsics::{load_metadata, ContractMessageTranscoder}, | ||
| util::decode_hex, | ||
| DEFAULT_KEY_COL_WIDTH, | ||
| }; | ||
| use anyhow::{Context, Result}; | ||
| use colored::Colorize as _; | ||
|
|
||
| #[derive(Debug, Clone, clap::Args)] | ||
| #[clap(name = "decode", about = "Decode input_data for a contract")] | ||
| pub struct DecodeCommand { | ||
| /// Type of data | ||
| #[clap(arg_enum, short, long)] | ||
| r#type: DataType, | ||
| /// The data to decode | ||
| #[clap(long)] | ||
| data: String, | ||
| } | ||
|
|
||
| #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ArgEnum)] | ||
| enum DataType { | ||
| Event, | ||
| Message, | ||
| } | ||
|
|
||
| impl DecodeCommand { | ||
| pub fn run(&self) -> Result<()> { | ||
| let (_, contract_metadata) = load_metadata(None)?; | ||
| let transcoder = ContractMessageTranscoder::new(&contract_metadata); | ||
|
|
||
| const ERR_MSG: &str = "Failed to decode specified data as a hex value"; | ||
| let decoded_data = match self.r#type { | ||
| DataType::Event => { | ||
| transcoder.decode_contract_event(&mut &decode_hex(&self.data).context(ERR_MSG)?[..]) | ||
| } | ||
| DataType::Message => transcoder | ||
| .decode_contract_message(&mut &decode_hex(&self.data).context(ERR_MSG)?[..]), | ||
| }; | ||
|
|
||
| println!( | ||
| "{:>width$} {:?}", | ||
agryaznov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "Decoded data:".bright_green().bold(), | ||
| decoded_data, | ||
| width = DEFAULT_KEY_COL_WIDTH | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::util::tests::with_new_contract_project; | ||
| use std::path::Path; | ||
|
|
||
| /// Create a `cargo contract` command | ||
| fn cargo_contract(path: &Path) -> assert_cmd::Command { | ||
| let mut cmd = assert_cmd::Command::cargo_bin("cargo-contract").unwrap(); | ||
| cmd.current_dir(path).arg("contract"); | ||
| cmd | ||
| } | ||
|
|
||
| #[test] | ||
| fn decode_works() { | ||
| // given | ||
| let contract = r#" | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| use ink_lang as ink; | ||
|
|
||
| #[ink::contract] | ||
| pub mod switcher { | ||
| #[ink(event)] | ||
| pub struct Switched { | ||
| new_value: bool, | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[ink(storage)] | ||
| pub struct Switcher { | ||
| value: bool, | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl Switcher { | ||
| #[ink(constructor)] | ||
| pub fn new(init_value: bool) -> Self { | ||
| Self { value: init_value } | ||
| } | ||
|
|
||
| #[ink(message, selector = 0xBABEBABE)] | ||
| pub fn switch(&mut self, value: bool) { | ||
| self.value = value; | ||
| } | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }"#; | ||
|
|
||
| // when | ||
| // contract is built | ||
| with_new_contract_project(|manifest_path| { | ||
| let project_dir = manifest_path.directory().expect("directory must exist"); | ||
| let lib = project_dir.join("lib.rs"); | ||
| std::fs::write(&lib, contract)?; | ||
|
|
||
| assert_cmd::Command::new("rustup") | ||
| .arg("override") | ||
| .arg("set") | ||
| .arg("nightly") | ||
| .assert() | ||
| .success(); | ||
|
|
||
| log::info!("Building contract in {}", project_dir.to_string_lossy()); | ||
| cargo_contract(project_dir).arg("build").assert().success(); | ||
|
|
||
| let msg_data: &str = "babebabe01"; | ||
| let msg_decoded: &str = | ||
| r#"Ok(Map(Map { ident: Some("switch"), map: {String("value"): Bool(true)} }))"#; | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // then | ||
| // message data is being decoded properly | ||
ascjones marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| cargo_contract(project_dir) | ||
| .arg("decode") | ||
| .arg("--data") | ||
| .arg(msg_data) | ||
| .arg("-t") | ||
| .arg("message") | ||
| .assert() | ||
| .success() | ||
| .stdout(predicates::str::contains(msg_decoded)); | ||
|
|
||
| let event_data: &str = "080001"; | ||
| let event_decoded: &str = r#"Ok(Map(Map { ident: Some("Switched"), map: {String("new_value"): Bool(true)} }))"#; | ||
|
|
||
| // and | ||
| // event data is being decoded properly | ||
| cargo_contract(project_dir) | ||
| .arg("decode") | ||
| .arg("--data") | ||
| .arg(event_data) | ||
| .arg("-t") | ||
| .arg("event") | ||
| .assert() | ||
| .success() | ||
| .stdout(predicates::str::contains(event_decoded)); | ||
|
|
||
| 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
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
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.