From 31a35a19d57819972666a93affb551fe22598b9d Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 28 Mar 2022 17:05:54 +0300 Subject: [PATCH 01/18] add contract call data to debug log --- src/cmd/extrinsics/call.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cmd/extrinsics/call.rs b/src/cmd/extrinsics/call.rs index ff95e4ad..03a74be8 100644 --- a/src/cmd/extrinsics/call.rs +++ b/src/cmd/extrinsics/call.rs @@ -132,6 +132,7 @@ impl CallCommand { .to_runtime_api::(); log::debug!("calling contract {:?}", self.contract); + log::debug!("with data: {:?}", hex::encode(&data)); let tx_progress = api .tx() .contracts() -- GitLab From 1375fee80539f2997c6a648b0ebfe84907662147 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 28 Mar 2022 17:06:20 +0300 Subject: [PATCH 02/18] [REVERT!] some debug data to dig into an error --- src/cmd/extrinsics/transcode/decode.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/cmd/extrinsics/transcode/decode.rs b/src/cmd/extrinsics/transcode/decode.rs index 06c34a30..25a8b75e 100644 --- a/src/cmd/extrinsics/transcode/decode.rs +++ b/src/cmd/extrinsics/transcode/decode.rs @@ -151,7 +151,11 @@ impl<'a> Decoder<'a> { variant_type: &TypeDefVariant, input: &mut &[u8], ) -> Result { + let i2 = input.clone(); let discriminant = input.read_byte()?; + println!("type is {:?}", &variant_type.clone()); + println!("input is {:?}", &i2); + println!("discriminant is {:?}", &discriminant); let variant = variant_type .variants() .get(discriminant as usize) -- GitLab From e7df87f2b326d751c62d2e2f5095d8c85f098c23 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 28 Mar 2022 21:14:04 +0300 Subject: [PATCH 03/18] save --- src/cmd/extrinsics/decode.rs | 41 ++++++++++++++++++++++++++++++++++++ src/cmd/extrinsics/events.rs | 1 + src/cmd/extrinsics/mod.rs | 2 ++ src/cmd/mod.rs | 2 +- src/main.rs | 6 +++++- 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/cmd/extrinsics/decode.rs diff --git a/src/cmd/extrinsics/decode.rs b/src/cmd/extrinsics/decode.rs new file mode 100644 index 00000000..434360fc --- /dev/null +++ b/src/cmd/extrinsics/decode.rs @@ -0,0 +1,41 @@ +// 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 . + +use super::load_metadata; +use crate::cmd::extrinsics::{ExtrinsicOpts, ContractMessageTranscoder}; +use anyhow::Result; + + +#[derive(Debug, clap::Args)] +#[clap(name = "decode", about = "Decode input_data for a contract")] +pub struct DecodeCommand { + #[clap(flatten)] + extrinsic_opts: ExtrinsicOpts, + /// The data to decode + #[clap(long)] + data: String, +} + +impl DecodeCommand { + pub fn run(&self) -> Result<()> { + let (_, contract_metadata) = load_metadata(self.extrinsic_opts.manifest_path.as_ref())?; + let transcoder = ContractMessageTranscoder::new(&contract_metadata); + let decoded_data = transcoder.decode_contract_event(&mut &self.data.as_bytes()[..]); + + log::debug!("DECODED DATA: {:?}", decoded_data); + Ok(()) + } +} diff --git a/src/cmd/extrinsics/events.rs b/src/cmd/extrinsics/events.rs index 4adcaddd..e0b09953 100644 --- a/src/cmd/extrinsics/events.rs +++ b/src/cmd/extrinsics/events.rs @@ -68,6 +68,7 @@ pub fn display_events( && field.name() == Some(&"data".to_string()) { // data is a byte vec so the first byte is the length. + log::debug!("EVENT_DATA: {:?}", hex::encode(&event_data)); let _data_len = event_data.read_byte()?; let contract_event = transcoder.decode_contract_event(event_data)?; maybe_println!( diff --git a/src/cmd/extrinsics/mod.rs b/src/cmd/extrinsics/mod.rs index 4a5185a9..70786e0e 100644 --- a/src/cmd/extrinsics/mod.rs +++ b/src/cmd/extrinsics/mod.rs @@ -20,6 +20,7 @@ mod instantiate; mod runtime_api; mod transcode; mod upload; +mod decode; #[cfg(test)] #[cfg(feature = "integration-tests")] @@ -41,6 +42,7 @@ pub use call::CallCommand; pub use instantiate::InstantiateCommand; pub use runtime_api::api::{DispatchError as RuntimeDispatchError, Event as RuntimeEvent}; pub use upload::UploadCommand; +pub use decode::DecodeCommand; type Balance = u128; type CodeHash = ::Hash; diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index aadc8619..56b48a18 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -25,4 +25,4 @@ pub(crate) use self::{ }; mod extrinsics; -pub(crate) use self::extrinsics::{CallCommand, InstantiateCommand, UploadCommand}; +pub(crate) use self::extrinsics::{CallCommand, InstantiateCommand, UploadCommand, DecodeCommand}; diff --git a/src/main.rs b/src/main.rs index 0bc94203..1e2bab8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,7 +23,7 @@ mod workspace; use self::{ cmd::{ metadata::MetadataResult, BuildCommand, CallCommand, CheckCommand, InstantiateCommand, - TestCommand, UploadCommand, + TestCommand, UploadCommand, DecodeCommand }, util::DEFAULT_KEY_COL_WIDTH, workspace::ManifestPath, @@ -454,6 +454,9 @@ enum Command { /// Call a contract #[clap(name = "call")] Call(CallCommand), + /// Decode a contract input data + #[clap(name = "decode")] + Decode(DecodeCommand), } fn main() { @@ -511,6 +514,7 @@ fn exec(cmd: Command) -> Result<()> { Command::Upload(upload) => upload.run(), Command::Instantiate(instantiate) => instantiate.run(), Command::Call(call) => call.run(), + Command::Decode(decode) => decode.run(), } } -- GitLab From 1875e5d1632d6309059b088dcb958e255e57a16e Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 28 Mar 2022 22:23:20 +0300 Subject: [PATCH 04/18] decode cmd works for both event and message save: +output tuned +decoder for message data and specifier of type to DecodeCommand removed redundant args to decode cmd --- src/cmd/extrinsics/call.rs | 3 +- src/cmd/extrinsics/decode.rs | 40 +++++++++++++++++------ src/cmd/extrinsics/events.rs | 4 +-- src/cmd/extrinsics/mod.rs | 4 +-- src/cmd/extrinsics/transcode/decode.rs | 6 ++-- src/cmd/extrinsics/transcode/mod.rs | 45 +++++++++++++++++++++++++- src/cmd/mod.rs | 2 +- src/main.rs | 4 +-- 8 files changed, 86 insertions(+), 22 deletions(-) diff --git a/src/cmd/extrinsics/call.rs b/src/cmd/extrinsics/call.rs index 03a74be8..dbb7f00f 100644 --- a/src/cmd/extrinsics/call.rs +++ b/src/cmd/extrinsics/call.rs @@ -56,6 +56,8 @@ impl CallCommand { let (_, contract_metadata) = load_metadata(self.extrinsic_opts.manifest_path.as_ref())?; let transcoder = ContractMessageTranscoder::new(&contract_metadata); let call_data = transcoder.encode(&self.message, &self.args)?; + log::debug!("message data: {:?}", hex::encode(&call_data)); + let signer = super::pair_signer(self.extrinsic_opts.signer()?); async_std::task::block_on(async { @@ -132,7 +134,6 @@ impl CallCommand { .to_runtime_api::(); log::debug!("calling contract {:?}", self.contract); - log::debug!("with data: {:?}", hex::encode(&data)); let tx_progress = api .tx() .contracts() diff --git a/src/cmd/extrinsics/decode.rs b/src/cmd/extrinsics/decode.rs index 434360fc..8ccfcb76 100644 --- a/src/cmd/extrinsics/decode.rs +++ b/src/cmd/extrinsics/decode.rs @@ -15,27 +15,49 @@ // along with cargo-contract. If not, see . use super::load_metadata; -use crate::cmd::extrinsics::{ExtrinsicOpts, ContractMessageTranscoder}; +use crate::cmd::extrinsics::ContractMessageTranscoder; +use crate::DEFAULT_KEY_COL_WIDTH; use anyhow::Result; +use colored::Colorize as _; - -#[derive(Debug, clap::Args)] +#[derive(Debug, Clone, clap::Args)] #[clap(name = "decode", about = "Decode input_data for a contract")] pub struct DecodeCommand { - #[clap(flatten)] - extrinsic_opts: ExtrinsicOpts, + /// 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(self.extrinsic_opts.manifest_path.as_ref())?; + let (_, contract_metadata) = load_metadata(None)?; let transcoder = ContractMessageTranscoder::new(&contract_metadata); - let decoded_data = transcoder.decode_contract_event(&mut &self.data.as_bytes()[..]); - log::debug!("DECODED DATA: {:?}", decoded_data); - Ok(()) + let decoded_data = match self.r#type { + DataType::Event => { + transcoder.decode_contract_event(&mut &hex::decode(&self.data).unwrap()[..]) + } + DataType::Message => { + transcoder.decode_contract_message(&mut &hex::decode(&self.data).unwrap()[..]) + } + }; + + println!( + "{:>width$} {:?}", + "Decoded data:".bright_green().bold(), + decoded_data, + width = DEFAULT_KEY_COL_WIDTH + ); + + Ok(()) } } diff --git a/src/cmd/extrinsics/events.rs b/src/cmd/extrinsics/events.rs index 29d100aa..98031c9b 100644 --- a/src/cmd/extrinsics/events.rs +++ b/src/cmd/extrinsics/events.rs @@ -66,9 +66,7 @@ pub fn display_events( if ::is_event(&event.pallet, &event.variant) && field.name() == Some(&"data".to_string()) { - // data is a byte vec so the first byte is the length. - log::debug!("EVENT_DATA: {:?}", hex::encode(&event_data)); - + log::debug!("event data: {:?}", hex::encode(&event_data)); let contract_event = transcoder.decode_contract_event(event_data)?; maybe_println!( verbosity, diff --git a/src/cmd/extrinsics/mod.rs b/src/cmd/extrinsics/mod.rs index 70786e0e..ca9f2a59 100644 --- a/src/cmd/extrinsics/mod.rs +++ b/src/cmd/extrinsics/mod.rs @@ -15,12 +15,12 @@ // along with cargo-contract. If not, see . mod call; +mod decode; mod events; mod instantiate; mod runtime_api; mod transcode; mod upload; -mod decode; #[cfg(test)] #[cfg(feature = "integration-tests")] @@ -39,10 +39,10 @@ use sp_core::{crypto::Pair, sr25519}; use subxt::{Config, DefaultConfig}; pub use call::CallCommand; +pub use decode::DecodeCommand; pub use instantiate::InstantiateCommand; pub use runtime_api::api::{DispatchError as RuntimeDispatchError, Event as RuntimeEvent}; pub use upload::UploadCommand; -pub use decode::DecodeCommand; type Balance = u128; type CodeHash = ::Hash; diff --git a/src/cmd/extrinsics/transcode/decode.rs b/src/cmd/extrinsics/transcode/decode.rs index d84196d2..3386f47e 100644 --- a/src/cmd/extrinsics/transcode/decode.rs +++ b/src/cmd/extrinsics/transcode/decode.rs @@ -155,9 +155,9 @@ impl<'a> Decoder<'a> { ) -> Result { let i2 = input.clone(); let discriminant = input.read_byte()?; - println!("type is {:?}", &variant_type.clone()); - println!("input is {:?}", &i2); - println!("discriminant is {:?}", &discriminant); + println!("type is {:?}", &variant_type.clone()); + println!("input is {:?}", &i2); + println!("discriminant is {:?}", &discriminant); let variant = variant_type .variants() .get(discriminant as usize) diff --git a/src/cmd/extrinsics/transcode/mod.rs b/src/cmd/extrinsics/transcode/mod.rs index 4c12c2f4..6ab3410b 100644 --- a/src/cmd/extrinsics/transcode/mod.rs +++ b/src/cmd/extrinsics/transcode/mod.rs @@ -175,7 +175,6 @@ impl<'a> ContractMessageTranscoder<'a> { // data is an encoded `Vec` so is prepended with its length `Compact`, which we // ignore because the structure of the event data is known for decoding. let _len = >::decode(data)?; - let variant_index = data.read_byte()?; let event_spec = self .metadata @@ -203,6 +202,37 @@ impl<'a> ContractMessageTranscoder<'a> { Ok(Value::Map(map)) } + pub fn decode_contract_message(&self, data: &mut &[u8]) -> Result { + let mut msg_selector = [0u8; 4]; + data.read(&mut msg_selector)?; + let msg_spec = self + .metadata + .spec() + .messages() + .iter() + .filter(|x| msg_selector == x.selector().to_bytes()) + .next() + .ok_or_else(|| { + anyhow::anyhow!( + "Message with selector {} not found in contract metadata", + hex::encode(&msg_selector) + ) + })?; + log::debug!("decoding contract message '{}'", msg_spec.label()); + + let mut args = Vec::new(); + for arg in msg_spec.args() { + let name = arg.label().to_string(); + let value = self.transcoder.decode(arg.ty().ty().id(), data)?; + args.push((Value::String(name), value)); + } + + let name = msg_spec.label().to_string(); + let map = Map::new(Some(&name), args.into_iter().collect()); + + Ok(Value::Map(map)) + } + pub fn decode_return(&self, name: &str, data: &mut &[u8]) -> Result { let msg_spec = self .find_message_spec(name) @@ -391,4 +421,17 @@ mod tests { Ok(()) } + + #[test] + fn decode_contract_message() -> Result<()> { + let metadata = generate_metadata(); + let transcoder = ContractMessageTranscoder::new(&metadata); + + let encoded = ([0u32; 32], [1u32; 32]).encode(); + // encode again as a Vec which has a len prefix. + let encoded_bytes = encoded.encode(); + let _ = transcoder.decode_contract_message(&mut &encoded_bytes[..])?; + + Ok(()) + } } diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 56b48a18..4a3553d2 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -25,4 +25,4 @@ pub(crate) use self::{ }; mod extrinsics; -pub(crate) use self::extrinsics::{CallCommand, InstantiateCommand, UploadCommand, DecodeCommand}; +pub(crate) use self::extrinsics::{CallCommand, DecodeCommand, InstantiateCommand, UploadCommand}; diff --git a/src/main.rs b/src/main.rs index 1e2bab8e..4a85c3bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,8 +22,8 @@ mod workspace; use self::{ cmd::{ - metadata::MetadataResult, BuildCommand, CallCommand, CheckCommand, InstantiateCommand, - TestCommand, UploadCommand, DecodeCommand + metadata::MetadataResult, BuildCommand, CallCommand, CheckCommand, DecodeCommand, + InstantiateCommand, TestCommand, UploadCommand, }, util::DEFAULT_KEY_COL_WIDTH, workspace::ManifestPath, -- GitLab From bbafb3bf0dc9d910c82ebf3df6df1541218ce985 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 29 Mar 2022 19:01:13 +0300 Subject: [PATCH 05/18] remove some debugging prints This reverts commit 1375fee80539f2997c6a648b0ebfe84907662147. --- src/cmd/extrinsics/transcode/decode.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/cmd/extrinsics/transcode/decode.rs b/src/cmd/extrinsics/transcode/decode.rs index 3386f47e..2c1291c6 100644 --- a/src/cmd/extrinsics/transcode/decode.rs +++ b/src/cmd/extrinsics/transcode/decode.rs @@ -153,11 +153,7 @@ impl<'a> Decoder<'a> { variant_type: &TypeDefVariant, input: &mut &[u8], ) -> Result { - let i2 = input.clone(); let discriminant = input.read_byte()?; - println!("type is {:?}", &variant_type.clone()); - println!("input is {:?}", &i2); - println!("discriminant is {:?}", &discriminant); let variant = variant_type .variants() .get(discriminant as usize) -- GitLab From 674574ec37e451156c6973dab29789164b75e8cf Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Tue, 29 Mar 2022 21:14:34 +0300 Subject: [PATCH 06/18] test fixed --- src/cmd/extrinsics/transcode/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/cmd/extrinsics/transcode/mod.rs b/src/cmd/extrinsics/transcode/mod.rs index 6ab3410b..7483359f 100644 --- a/src/cmd/extrinsics/transcode/mod.rs +++ b/src/cmd/extrinsics/transcode/mod.rs @@ -427,9 +427,7 @@ mod tests { let metadata = generate_metadata(); let transcoder = ContractMessageTranscoder::new(&metadata); - let encoded = ([0u32; 32], [1u32; 32]).encode(); - // encode again as a Vec which has a len prefix. - let encoded_bytes = encoded.encode(); + let encoded_bytes = hex::decode("633aa551").unwrap(); let _ = transcoder.decode_contract_message(&mut &encoded_bytes[..])?; Ok(()) -- GitLab From 1de752aacc3551e60a0f69362816df9ebfc07455 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 15:22:04 +0300 Subject: [PATCH 07/18] use util decode_hex() --- src/cmd/extrinsics/decode.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cmd/extrinsics/decode.rs b/src/cmd/extrinsics/decode.rs index 8ccfcb76..56a99607 100644 --- a/src/cmd/extrinsics/decode.rs +++ b/src/cmd/extrinsics/decode.rs @@ -15,9 +15,8 @@ // along with cargo-contract. If not, see . use super::load_metadata; -use crate::cmd::extrinsics::ContractMessageTranscoder; -use crate::DEFAULT_KEY_COL_WIDTH; -use anyhow::Result; +use crate::{cmd::extrinsics::ContractMessageTranscoder, util::decode_hex, DEFAULT_KEY_COL_WIDTH}; +use anyhow::{Context, Result}; use colored::Colorize as _; #[derive(Debug, Clone, clap::Args)] @@ -42,13 +41,13 @@ impl DecodeCommand { 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 &hex::decode(&self.data).unwrap()[..]) - } - DataType::Message => { - transcoder.decode_contract_message(&mut &hex::decode(&self.data).unwrap()[..]) + 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!( -- GitLab From c101772bab2d7ec26fcd5069dfcfedc4327c1957 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 31 Mar 2022 15:37:15 +0300 Subject: [PATCH 08/18] moved decode cmd to root (it's not an extrinsic) --- src/cmd/{extrinsics => }/decode.rs | 7 +++++-- src/cmd/extrinsics/mod.rs | 5 ++--- src/cmd/mod.rs | 4 +++- 3 files changed, 10 insertions(+), 6 deletions(-) rename src/cmd/{extrinsics => }/decode.rs (93%) diff --git a/src/cmd/extrinsics/decode.rs b/src/cmd/decode.rs similarity index 93% rename from src/cmd/extrinsics/decode.rs rename to src/cmd/decode.rs index 56a99607..f0211750 100644 --- a/src/cmd/extrinsics/decode.rs +++ b/src/cmd/decode.rs @@ -14,8 +14,11 @@ // You should have received a copy of the GNU General Public License // along with cargo-contract. If not, see . -use super::load_metadata; -use crate::{cmd::extrinsics::ContractMessageTranscoder, util::decode_hex, DEFAULT_KEY_COL_WIDTH}; +use crate::{ + cmd::extrinsics::{load_metadata, ContractMessageTranscoder}, + util::decode_hex, + DEFAULT_KEY_COL_WIDTH, +}; use anyhow::{Context, Result}; use colored::Colorize as _; diff --git a/src/cmd/extrinsics/mod.rs b/src/cmd/extrinsics/mod.rs index ca9f2a59..16b91645 100644 --- a/src/cmd/extrinsics/mod.rs +++ b/src/cmd/extrinsics/mod.rs @@ -15,7 +15,6 @@ // along with cargo-contract. If not, see . mod call; -mod decode; mod events; mod instantiate; mod runtime_api; @@ -29,7 +28,7 @@ mod integration_tests; use anyhow::{anyhow, Context, Result}; use std::{fs::File, path::PathBuf}; -use self::{events::display_events, transcode::ContractMessageTranscoder}; +use self::events::display_events; use crate::{ crate_metadata::CrateMetadata, name_value_println, workspace::ManifestPath, Verbosity, VerbosityFlags, @@ -38,8 +37,8 @@ use pallet_contracts_primitives::ContractResult; use sp_core::{crypto::Pair, sr25519}; use subxt::{Config, DefaultConfig}; +pub use self::transcode::ContractMessageTranscoder; pub use call::CallCommand; -pub use decode::DecodeCommand; pub use instantiate::InstantiateCommand; pub use runtime_api::api::{DispatchError as RuntimeDispatchError, Event as RuntimeEvent}; pub use upload::UploadCommand; diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 4a3553d2..afee32e8 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -15,14 +15,16 @@ // along with cargo-contract. If not, see . pub mod build; +pub mod decode; pub mod metadata; pub mod new; pub mod test; pub(crate) use self::{ build::{BuildCommand, CheckCommand}, + decode::DecodeCommand, test::TestCommand, }; mod extrinsics; -pub(crate) use self::extrinsics::{CallCommand, DecodeCommand, InstantiateCommand, UploadCommand}; +pub(crate) use self::extrinsics::{CallCommand, InstantiateCommand, UploadCommand}; -- GitLab From 85da8150ae96562ac58c949f002342c077ed1f77 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 7 Apr 2022 15:25:18 +0300 Subject: [PATCH 09/18] e2e test for decoding message data --- src/cmd/decode.rs | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index f0211750..c609ec52 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -63,3 +63,86 @@ impl DecodeCommand { 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, + } + + #[ink(storage)] + pub struct Switcher { + value: bool, + } + + 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; + } + } + }"#; + + // 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)} }))"#; + + // then + // message data is being decoded properly + cargo_contract(project_dir) + .arg("decode") + .arg("--data") + .arg(msg_data) + .arg("-t") + .arg("message") + .assert() + .success() + .stdout(predicates::str::contains(msg_decoded)); + + Ok(()) + }) + } +} -- GitLab From eb759c824e9a446d4ec2c8d6400f130ea6244cdd Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 7 Apr 2022 15:37:38 +0300 Subject: [PATCH 10/18] add decoding event to the same e2e test --- src/cmd/decode.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index c609ec52..6586ce54 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -142,6 +142,21 @@ mod tests { .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(()) }) } -- GitLab From 88ef5c940be41ac6a8b4dfdd75779a76b3f497f8 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Thu, 7 Apr 2022 17:12:27 +0300 Subject: [PATCH 11/18] Update src/cmd/decode.rs Co-authored-by: Andrew Jones --- src/cmd/decode.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index 6586ce54..cda4fe95 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -54,7 +54,7 @@ impl DecodeCommand { }; println!( - "{:>width$} {:?}", + "{:>width$} {}", "Decoded data:".bright_green().bold(), decoded_data, width = DEFAULT_KEY_COL_WIDTH -- GitLab From f6bc3c1109f8c6c736332f9125f2d35d2603b9be Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 13:44:04 +0300 Subject: [PATCH 12/18] added constructor data decoder --- src/cmd/decode.rs | 87 +++++++++++++++++------------ src/cmd/extrinsics/transcode/mod.rs | 31 +++++++++- 2 files changed, 80 insertions(+), 38 deletions(-) diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index cda4fe95..faf33474 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -29,7 +29,7 @@ pub struct DecodeCommand { #[clap(arg_enum, short, long)] r#type: DataType, /// The data to decode - #[clap(long)] + #[clap(short, long)] data: String, } @@ -37,6 +37,7 @@ pub struct DecodeCommand { enum DataType { Event, Message, + Constructor, } impl DecodeCommand { @@ -46,11 +47,12 @@ impl DecodeCommand { 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::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)?[..]), + .decode_contract_message(&mut &decode_hex(&self.data).context(ERR_MSG)?[..])?, + DataType::Constructor => transcoder + .decode_contract_constructor(&mut &decode_hex(&self.data).context(ERR_MSG)?[..])?, }; println!( @@ -80,34 +82,34 @@ mod tests { 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, - } - - #[ink(storage)] - pub struct Switcher { - value: bool, - } - - 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; - } - } - }"#; + #![cfg_attr(not(feature = "std"), no_std)] + + use ink_lang as ink; + + #[ink::contract] + mod switcher { + #[ink(event)] + pub struct Switched { + new_value: bool, + } + + #[ink(storage)] + pub struct Switcher { + value: bool, + } + + impl Switcher { + #[ink(constructor, selector = 0xBABEBABE)] + 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; + } + } + }"#; // when // contract is built @@ -128,7 +130,7 @@ mod tests { let msg_data: &str = "babebabe01"; let msg_decoded: &str = - r#"Ok(Map(Map { ident: Some("switch"), map: {String("value"): Bool(true)} }))"#; + r#"switch { value: true }"#; // then // message data is being decoded properly @@ -143,7 +145,7 @@ mod tests { .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)} }))"#; + let event_decoded: &str = r#"Switched { new_value: true }"#; // and // event data is being decoded properly @@ -157,6 +159,21 @@ mod tests { .success() .stdout(predicates::str::contains(event_decoded)); + let constructor_data: &str = "babebabe00"; + let constructor_decoded: &str = r#"new { init_value: false }"#; + + // and + // event data is being decoded properly + cargo_contract(project_dir) + .arg("decode") + .arg("-d") + .arg(constructor_data) + .arg("-t") + .arg("constructor") + .assert() + .success() + .stdout(predicates::str::contains(constructor_decoded)); + Ok(()) }) } diff --git a/src/cmd/extrinsics/transcode/mod.rs b/src/cmd/extrinsics/transcode/mod.rs index 7483359f..53356b2a 100644 --- a/src/cmd/extrinsics/transcode/mod.rs +++ b/src/cmd/extrinsics/transcode/mod.rs @@ -206,10 +206,7 @@ impl<'a> ContractMessageTranscoder<'a> { let mut msg_selector = [0u8; 4]; data.read(&mut msg_selector)?; let msg_spec = self - .metadata - .spec() .messages() - .iter() .filter(|x| msg_selector == x.selector().to_bytes()) .next() .ok_or_else(|| { @@ -233,6 +230,34 @@ impl<'a> ContractMessageTranscoder<'a> { Ok(Value::Map(map)) } + pub fn decode_contract_constructor(&self, data: &mut &[u8]) -> Result { + let mut msg_selector = [0u8; 4]; + data.read(&mut msg_selector)?; + let msg_spec = self + .constructors() + .filter(|x| msg_selector == x.selector().to_bytes()) + .next() + .ok_or_else(|| { + anyhow::anyhow!( + "Constructor with selector {} not found in contract metadata", + hex::encode(&msg_selector) + ) + })?; + log::debug!("decoding contract constructor '{}'", msg_spec.label()); + + let mut args = Vec::new(); + for arg in msg_spec.args() { + let name = arg.label().to_string(); + let value = self.transcoder.decode(arg.ty().ty().id(), data)?; + args.push((Value::String(name), value)); + } + + let name = msg_spec.label().to_string(); + let map = Map::new(Some(&name), args.into_iter().collect()); + + Ok(Value::Map(map)) + } + pub fn decode_return(&self, name: &str, data: &mut &[u8]) -> Result { let msg_spec = self .find_message_spec(name) -- GitLab From dec5cd48d2f2d7e6b143b1ab462ab78672c14ed8 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Fri, 8 Apr 2022 14:00:59 +0300 Subject: [PATCH 13/18] ci fixes --- src/cmd/decode.rs | 3 +-- src/cmd/extrinsics/transcode/mod.rs | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index faf33474..5f798c55 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -129,8 +129,7 @@ mod tests { cargo_contract(project_dir).arg("build").assert().success(); let msg_data: &str = "babebabe01"; - let msg_decoded: &str = - r#"switch { value: true }"#; + let msg_decoded: &str = r#"switch { value: true }"#; // then // message data is being decoded properly diff --git a/src/cmd/extrinsics/transcode/mod.rs b/src/cmd/extrinsics/transcode/mod.rs index 53356b2a..421b2e58 100644 --- a/src/cmd/extrinsics/transcode/mod.rs +++ b/src/cmd/extrinsics/transcode/mod.rs @@ -207,8 +207,7 @@ impl<'a> ContractMessageTranscoder<'a> { data.read(&mut msg_selector)?; let msg_spec = self .messages() - .filter(|x| msg_selector == x.selector().to_bytes()) - .next() + .find(|x| msg_selector == x.selector().to_bytes()) .ok_or_else(|| { anyhow::anyhow!( "Message with selector {} not found in contract metadata", @@ -235,8 +234,7 @@ impl<'a> ContractMessageTranscoder<'a> { data.read(&mut msg_selector)?; let msg_spec = self .constructors() - .filter(|x| msg_selector == x.selector().to_bytes()) - .next() + .find(|x| msg_selector == x.selector().to_bytes()) .ok_or_else(|| { anyhow::anyhow!( "Constructor with selector {} not found in contract metadata", -- GitLab From 1f24d5be6f31a42da70fce89661e7d4ad3d5feaf Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 11 Apr 2022 22:56:12 +0300 Subject: [PATCH 14/18] lil test update --- Cargo.lock | 28 +--------------------------- Cargo.toml | 14 +++++++------- src/cmd/decode.rs | 2 +- 3 files changed, 9 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26951f16..4962eda9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,7 +465,7 @@ dependencies = [ [[package]] name = "cargo-contract" -version = "1.1.1" +version = "1.1.2" dependencies = [ "anyhow", "assert_cmd", @@ -1416,8 +1416,6 @@ dependencies = [ [[package]] name = "ink_allocator" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "156d1399accf10640ba7e712a4c5b68b938b57aae14f4698ed9a43b113bdde11" dependencies = [ "cfg-if", ] @@ -1425,8 +1423,6 @@ dependencies = [ [[package]] name = "ink_engine" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b3d2da83b1a6a121fc6a1d7d9b49a3273a3b9663e0ed3d486c6940890f6f718" dependencies = [ "blake2", "derive_more", @@ -1440,8 +1436,6 @@ dependencies = [ [[package]] name = "ink_env" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5d465fe08377a1d92063ff6b3fa222ab337001bc70de1941884dbc8d7a7e599" dependencies = [ "arrayref", "blake2", @@ -1467,8 +1461,6 @@ dependencies = [ [[package]] name = "ink_eth_compatibility" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "34ab970c1d9d27b4c9d3609dd048a9e49151691955806224a3107b1d31d50010" dependencies = [ "ink_env", "libsecp256k1", @@ -1477,8 +1469,6 @@ dependencies = [ [[package]] name = "ink_lang" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d4466ef3833f113d4b8b685550b16eece4d4fa812d6de3e7ce5725ae5e1dc44f" dependencies = [ "derive_more", "ink_env", @@ -1494,8 +1484,6 @@ dependencies = [ [[package]] name = "ink_lang_codegen" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3060b19aec639eea7811d36d9143c849f97a4f742bf5e43f892ad6ce8c43050a" dependencies = [ "blake2", "derive_more", @@ -1513,8 +1501,6 @@ dependencies = [ [[package]] name = "ink_lang_ir" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9e32f34c687530cca8498e1c10fa1272f374981f865158f51474b19ce2dbfce" dependencies = [ "blake2", "either", @@ -1527,8 +1513,6 @@ dependencies = [ [[package]] name = "ink_lang_macro" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ffcfe6b64bc9fc9ee5ac92c5655c9d4dc27ae6062addc0cba2045446dfea105" dependencies = [ "ink_lang_codegen", "ink_lang_ir", @@ -1541,8 +1525,6 @@ dependencies = [ [[package]] name = "ink_metadata" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fbe60facb61034f78367fc30dc56bd5e385487ce60aefd0b1727e6b680446dc" dependencies = [ "derive_more", "impl-serde", @@ -1555,8 +1537,6 @@ dependencies = [ [[package]] name = "ink_prelude" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4917e51ffd6374c7be1cb030e89607a7b9384f331d47bfa7589750ff8f27fd7f" dependencies = [ "cfg-if", ] @@ -1564,8 +1544,6 @@ dependencies = [ [[package]] name = "ink_primitives" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1793c77ab7354147a4a0de3e0a77a31a29bf6fbd5f97192b8202c3ce2587699" dependencies = [ "cfg-if", "ink_prelude", @@ -1576,8 +1554,6 @@ dependencies = [ [[package]] name = "ink_storage" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c0aead5b91524156263fa11d203f3304f55e5c4d2050949939d1f47ad82ee8f" dependencies = [ "array-init", "cfg-if", @@ -1594,8 +1570,6 @@ dependencies = [ [[package]] name = "ink_storage_derive" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46f9914f3149ccb6333e7d8f1fa013a4e81961cdd6e50c5ed1bbe041b870c516" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index d232bdf2..dec78cd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [".", "metadata"] [package] name = "cargo-contract" -version = "1.1.1" +version = "1.1.2" authors = ["Parity Technologies "] build = "build.rs" edition = "2021" @@ -49,8 +49,8 @@ regex = "1.5.5" # dependencies for extrinsics (deploying and calling a contract) async-std = { version = "1.10.0", features = ["attributes", "tokio1"] } -ink_metadata = { version = "3.0", features = ["derive"] } -ink_env = "3.0" +ink_metadata = { path="../ink/crates/metadata/", features = ["derive"] } +ink_env = {path="../ink/crates/env/"} scale-info = { version = "2.0.1", default-features = false, features = ["derive"] } sp-core = "6.0.0" sp-runtime = "6.0.0" @@ -83,10 +83,10 @@ wabt = "0.10.0" regex = "1.5.5" predicates = "2.1.1" -ink_primitives = "3.0" -ink_storage = "3.0" -ink_lang = "3.0" -ink_lang_codegen = "3.0" +ink_primitives = { path="../ink/crates/primitives/" } +ink_storage = { path="../ink/crates/storage/" } +ink_lang = { path="../ink/crates/lang/" } +ink_lang_codegen = { path="../ink/crates/lang/codegen" } [features] # This `std` feature is required for testing using an inline contract's metadata, because `ink!` annotates the metadata diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index 5f798c55..b969f480 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -73,7 +73,7 @@ mod tests { /// Create a `cargo contract` command fn cargo_contract(path: &Path) -> assert_cmd::Command { - let mut cmd = assert_cmd::Command::cargo_bin("cargo-contract").unwrap(); + let mut cmd = assert_cmd::Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap(); cmd.current_dir(path).arg("contract"); cmd } -- GitLab From 49e04e5a26226326e4acbabc6816aa555a443729 Mon Sep 17 00:00:00 2001 From: Alexander Gryaznov Date: Mon, 11 Apr 2022 23:14:29 +0300 Subject: [PATCH 15/18] Partially revert "lil test update" (mainfest fixes) This reverts commit 1f24d5be6f31a42da70fce89661e7d4ad3d5feaf. --- Cargo.lock | 132 +++++++++++++++-------------------------------------- Cargo.toml | 10 ++-- 2 files changed, 41 insertions(+), 101 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a59b3c8..52c6b229 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -465,7 +465,7 @@ dependencies = [ [[package]] name = "cargo-contract" -version = "1.1.2" +version = "1.1.1" dependencies = [ "anyhow", "assert_cmd", @@ -483,11 +483,11 @@ dependencies = [ "hex", "impl-serde", "indexmap", - "ink_env 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ink_env", "ink_lang", "ink_lang_codegen", - "ink_metadata 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_primitives 3.0.1", + "ink_metadata", + "ink_primitives", "ink_storage", "itertools", "jsonrpsee 0.10.1", @@ -1413,13 +1413,6 @@ dependencies = [ "hashbrown 0.11.2", ] -[[package]] -name = "ink_allocator" -version = "3.0.1" -dependencies = [ - "cfg-if", -] - [[package]] name = "ink_allocator" version = "3.0.1" @@ -1429,19 +1422,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "ink_engine" -version = "3.0.1" -dependencies = [ - "blake2", - "derive_more", - "parity-scale-codec", - "rand 0.8.5", - "secp256k1 0.22.1", - "sha2 0.10.2", - "sha3", -] - [[package]] name = "ink_engine" version = "3.0.1" @@ -1457,31 +1437,6 @@ dependencies = [ "sha3", ] -[[package]] -name = "ink_env" -version = "3.0.1" -dependencies = [ - "arrayref", - "blake2", - "cfg-if", - "derive_more", - "ink_allocator 3.0.1", - "ink_engine 3.0.1", - "ink_metadata 3.0.1", - "ink_prelude 3.0.1", - "ink_primitives 3.0.1", - "num-traits", - "parity-scale-codec", - "paste", - "rand 0.8.5", - "rlibc", - "scale-info", - "secp256k1 0.22.1", - "sha2 0.10.2", - "sha3", - "static_assertions", -] - [[package]] name = "ink_env" version = "3.0.1" @@ -1492,11 +1447,11 @@ dependencies = [ "blake2", "cfg-if", "derive_more", - "ink_allocator 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_engine 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_metadata 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_prelude 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_primitives 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ink_allocator", + "ink_engine", + "ink_metadata", + "ink_prelude", + "ink_primitives", "num-traits", "parity-scale-codec", "paste", @@ -1512,22 +1467,26 @@ dependencies = [ [[package]] name = "ink_eth_compatibility" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ab970c1d9d27b4c9d3609dd048a9e49151691955806224a3107b1d31d50010" dependencies = [ - "ink_env 3.0.1", + "ink_env", "libsecp256k1", ] [[package]] name = "ink_lang" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4466ef3833f113d4b8b685550b16eece4d4fa812d6de3e7ce5725ae5e1dc44f" dependencies = [ "derive_more", - "ink_env 3.0.1", + "ink_env", "ink_eth_compatibility", "ink_lang_macro", - "ink_metadata 3.0.1", - "ink_prelude 3.0.1", - "ink_primitives 3.0.1", + "ink_metadata", + "ink_prelude", + "ink_primitives", "ink_storage", "parity-scale-codec", ] @@ -1535,6 +1494,8 @@ dependencies = [ [[package]] name = "ink_lang_codegen" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3060b19aec639eea7811d36d9143c849f97a4f742bf5e43f892ad6ce8c43050a" dependencies = [ "blake2", "derive_more", @@ -1552,6 +1513,8 @@ dependencies = [ [[package]] name = "ink_lang_ir" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9e32f34c687530cca8498e1c10fa1272f374981f865158f51474b19ce2dbfce" dependencies = [ "blake2", "either", @@ -1564,27 +1527,17 @@ dependencies = [ [[package]] name = "ink_lang_macro" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ffcfe6b64bc9fc9ee5ac92c5655c9d4dc27ae6062addc0cba2045446dfea105" dependencies = [ "ink_lang_codegen", "ink_lang_ir", - "ink_primitives 3.0.1", + "ink_primitives", "parity-scale-codec", "proc-macro2", "syn", ] -[[package]] -name = "ink_metadata" -version = "3.0.1" -dependencies = [ - "derive_more", - "impl-serde", - "ink_prelude 3.0.1", - "ink_primitives 3.0.1", - "scale-info", - "serde", -] - [[package]] name = "ink_metadata" version = "3.0.1" @@ -1593,19 +1546,12 @@ checksum = "7fbe60facb61034f78367fc30dc56bd5e385487ce60aefd0b1727e6b680446dc" dependencies = [ "derive_more", "impl-serde", - "ink_prelude 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "ink_primitives 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ink_prelude", + "ink_primitives", "scale-info", "serde", ] -[[package]] -name = "ink_prelude" -version = "3.0.1" -dependencies = [ - "cfg-if", -] - [[package]] name = "ink_prelude" version = "3.0.1" @@ -1615,16 +1561,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "ink_primitives" -version = "3.0.1" -dependencies = [ - "cfg-if", - "ink_prelude 3.0.1", - "parity-scale-codec", - "scale-info", -] - [[package]] name = "ink_primitives" version = "3.0.1" @@ -1632,7 +1568,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1793c77ab7354147a4a0de3e0a77a31a29bf6fbd5f97192b8202c3ce2587699" dependencies = [ "cfg-if", - "ink_prelude 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ink_prelude", "parity-scale-codec", "scale-info", ] @@ -1640,14 +1576,16 @@ dependencies = [ [[package]] name = "ink_storage" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c0aead5b91524156263fa11d203f3304f55e5c4d2050949939d1f47ad82ee8f" dependencies = [ "array-init", "cfg-if", "derive_more", - "ink_env 3.0.1", - "ink_metadata 3.0.1", - "ink_prelude 3.0.1", - "ink_primitives 3.0.1", + "ink_env", + "ink_metadata", + "ink_prelude", + "ink_primitives", "ink_storage_derive", "parity-scale-codec", "scale-info", @@ -1656,6 +1594,8 @@ dependencies = [ [[package]] name = "ink_storage_derive" version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "46f9914f3149ccb6333e7d8f1fa013a4e81961cdd6e50c5ed1bbe041b870c516" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 4521875c..4bf2e679 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = [".", "metadata"] [package] name = "cargo-contract" -version = "1.1.2" +version = "1.1.1" authors = ["Parity Technologies "] build = "build.rs" edition = "2021" @@ -83,10 +83,10 @@ wabt = "0.10.0" regex = "1.5.5" predicates = "2.1.1" -ink_primitives = { path="../ink/crates/primitives/" } -ink_storage = { path="../ink/crates/storage/" } -ink_lang = { path="../ink/crates/lang/" } -ink_lang_codegen = { path="../ink/crates/lang/codegen" } +ink_primitives = "3.0" +ink_storage = "3.0" +ink_lang = "3.0" +ink_lang_codegen = "3.0" [features] # This `std` feature is required for testing using an inline contract's metadata, because `ink!` annotates the metadata -- GitLab From 7f6ef5eed1ec822b698234fca9b6fb1a5e49f239 Mon Sep 17 00:00:00 2001 From: ascjones Date: Tue, 12 Apr 2022 09:34:06 +0100 Subject: [PATCH 16/18] Build before test, and test --release --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e04655be..1d504fbf 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,8 @@ stages: - check - - test - build + - test variables: GIT_STRATEGY: fetch @@ -110,7 +110,7 @@ test: stage: test <<: *docker-env script: - - cargo test --verbose --workspace --all-features + - cargo test --release --verbose --workspace --all-features test-new-project-template: stage: test -- GitLab From 068e0527f581a17fb298c7ea510880b4bf854a90 Mon Sep 17 00:00:00 2001 From: ascjones Date: Tue, 12 Apr 2022 10:56:03 +0100 Subject: [PATCH 17/18] Revert "Build before test, and test --release" This reverts commit 7f6ef5eed1ec822b698234fca9b6fb1a5e49f239. --- .gitlab-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1d504fbf..e04655be 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,8 +7,8 @@ stages: - check - - build - test + - build variables: GIT_STRATEGY: fetch @@ -110,7 +110,7 @@ test: stage: test <<: *docker-env script: - - cargo test --release --verbose --workspace --all-features + - cargo test --verbose --workspace --all-features test-new-project-template: stage: test -- GitLab From 6069a70e4139b5133c664b6be96c54ab2ff8af45 Mon Sep 17 00:00:00 2001 From: ascjones Date: Tue, 12 Apr 2022 11:25:42 +0100 Subject: [PATCH 18/18] Move decode test to an integration test --- src/cmd/decode.rs | 112 -------------------------------------- tests/decode.rs | 134 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 112 deletions(-) create mode 100644 tests/decode.rs diff --git a/src/cmd/decode.rs b/src/cmd/decode.rs index b969f480..b0e94f7e 100644 --- a/src/cmd/decode.rs +++ b/src/cmd/decode.rs @@ -65,115 +65,3 @@ impl DecodeCommand { 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(env!("CARGO_PKG_NAME")).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] - mod switcher { - #[ink(event)] - pub struct Switched { - new_value: bool, - } - - #[ink(storage)] - pub struct Switcher { - value: bool, - } - - impl Switcher { - #[ink(constructor, selector = 0xBABEBABE)] - 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; - } - } - }"#; - - // 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#"switch { value: true }"#; - - // then - // message data is being decoded properly - 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#"Switched { new_value: 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)); - - let constructor_data: &str = "babebabe00"; - let constructor_decoded: &str = r#"new { init_value: false }"#; - - // and - // event data is being decoded properly - cargo_contract(project_dir) - .arg("decode") - .arg("-d") - .arg(constructor_data) - .arg("-t") - .arg("constructor") - .assert() - .success() - .stdout(predicates::str::contains(constructor_decoded)); - - Ok(()) - }) - } -} diff --git a/tests/decode.rs b/tests/decode.rs new file mode 100644 index 00000000..970458d6 --- /dev/null +++ b/tests/decode.rs @@ -0,0 +1,134 @@ +// 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 . + +use std::path::Path; + +/// Create a `cargo contract` command +fn cargo_contract>(path: P) -> assert_cmd::Command { + let mut cmd = assert_cmd::Command::cargo_bin(env!("CARGO_PKG_NAME")).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] + mod switcher { + #[ink(event)] + pub struct Switched { + new_value: bool, + } + + #[ink(storage)] + pub struct Switcher { + value: bool, + } + + impl Switcher { + #[ink(constructor, selector = 0xBABEBABE)] + 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; + } + } + }"#; + + let tmp_dir = tempfile::Builder::new() + .prefix("cargo-contract.cli.test.") + .tempdir() + .expect("temporary directory creation failed"); + + // cargo contract new decode_test + cargo_contract(tmp_dir.path()) + .arg("new") + .arg("switcher") + .assert() + .success(); + + let project_dir = tmp_dir.path().to_path_buf().join("switcher"); + + let lib = project_dir.join("lib.rs"); + std::fs::write(&lib, contract).expect("Failed to write contract lib.rs"); + + 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") + .arg("--skip-linting") + .assert() + .success(); + + let msg_data: &str = "babebabe01"; + let msg_decoded: &str = r#"switch { value: true }"#; + + // then + // message data is being decoded properly + 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#"Switched { new_value: 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)); + + let constructor_data: &str = "babebabe00"; + let constructor_decoded: &str = r#"new { init_value: false }"#; + + // and + // event data is being decoded properly + cargo_contract(&project_dir) + .arg("decode") + .arg("-d") + .arg(constructor_data) + .arg("-t") + .arg("constructor") + .assert() + .success() + .stdout(predicates::str::contains(constructor_decoded)); +} -- GitLab