From 63cb7ce413df938214d9e46911962b4b46895936 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 19 May 2023 16:15:44 +0100 Subject: [PATCH 1/4] Show instantiation decoded return value --- .../cargo-contract/src/cmd/extrinsics/call.rs | 4 ++-- .../src/cmd/extrinsics/instantiate.rs | 18 +++++++++++++--- crates/transcode/src/lib.rs | 21 ++++++++++++++++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/crates/cargo-contract/src/cmd/extrinsics/call.rs b/crates/cargo-contract/src/cmd/extrinsics/call.rs index 2e8bb5dc3..2d4df616c 100644 --- a/crates/cargo-contract/src/cmd/extrinsics/call.rs +++ b/crates/cargo-contract/src/cmd/extrinsics/call.rs @@ -119,7 +119,7 @@ impl CallCommand { match result.result { Ok(ref ret_val) => { let value = transcoder - .decode_return(&self.message, &mut &ret_val.data[..]) + .decode_message_return(&self.message, &mut &ret_val.data[..]) .context(format!( "Failed to decode return value {:?}", &ret_val @@ -329,6 +329,6 @@ impl CallDryRunResult { format!("{:?}", self.reverted), DEFAULT_KEY_COL_WIDTH ); - name_value_println!("Data", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); + name_value_println!("Returned", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); } } diff --git a/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs b/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs index 22ebbdfde..d06d7e6ab 100644 --- a/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs +++ b/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs @@ -45,6 +45,7 @@ use crate::{ }; use anyhow::{ anyhow, + Context, Result, }; use contract_build::{ @@ -52,6 +53,7 @@ use contract_build::{ util::decode_hex, Verbosity, }; +use contract_transcode::Value; use pallet_contracts_primitives::ContractInstantiateResult; @@ -199,11 +201,21 @@ impl Exec { let result = self.instantiate_dry_run().await?; match result.result { Ok(ref ret_val) => { + let value = self + .transcoder + .decode_constructor_return( + &self.args.constructor, + &mut &ret_val.result.data[..], + ) + .context(format!( + "Failed to decode return value {:?}", + &ret_val + ))?; let dry_run_result = InstantiateDryRunResult { result: String::from("Success!"), contract: ret_val.account_id.to_string(), reverted: ret_val.result.did_revert(), - data: ret_val.result.data.clone().into(), + data: value, gas_consumed: result.gas_consumed, gas_required: result.gas_required, storage_deposit: StorageDeposit::from(&result.storage_deposit), @@ -445,7 +457,7 @@ pub struct InstantiateDryRunResult { pub contract: String, /// Was the operation reverted pub reverted: bool, - pub data: Bytes, + pub data: Value, pub gas_consumed: Weight, pub gas_required: Weight, /// Storage deposit after the operation @@ -466,7 +478,7 @@ impl InstantiateDryRunResult { format!("{:?}", self.reverted), DEFAULT_KEY_COL_WIDTH ); - name_value_println!("Data", format!("{:?}", self.data), DEFAULT_KEY_COL_WIDTH); + name_value_println!("Returned", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); } } diff --git a/crates/transcode/src/lib.rs b/crates/transcode/src/lib.rs index 357d084b5..a4287031c 100644 --- a/crates/transcode/src/lib.rs +++ b/crates/transcode/src/lib.rs @@ -349,7 +349,22 @@ impl ContractMessageTranscoder { Ok(Value::Map(map)) } - pub fn decode_return(&self, name: &str, data: &mut &[u8]) -> Result { + pub fn decode_constructor_return( + &self, + name: &str, + data: &mut &[u8], + ) -> Result { + let ctor_spec = self.find_constructor_spec(name).ok_or_else(|| { + anyhow::anyhow!("Failed to find constructor spec with name '{}'", name) + })?; + if let Some(return_ty) = ctor_spec.return_type().opt_type() { + self.decode(return_ty.ty().id, data) + } else { + Ok(Value::Unit) + } + } + + pub fn decode_message_return(&self, name: &str, data: &mut &[u8]) -> Result { let msg_spec = self.find_message_spec(name).ok_or_else(|| { anyhow::anyhow!("Failed to find message spec with name '{}'", name) })?; @@ -674,7 +689,7 @@ mod tests { let encoded = Result::::Ok(true).encode(); let decoded = transcoder - .decode_return("get", &mut &encoded[..]) + .decode_message_return("get", &mut &encoded[..]) .unwrap_or_else(|e| panic!("Error decoding return value {e}")); let expected = Value::Tuple(Tuple::new( @@ -694,7 +709,7 @@ mod tests { let encoded = Result::::Err(LangError::CouldNotReadInput).encode(); let decoded = transcoder - .decode_return("get", &mut &encoded[..]) + .decode_message_return("get", &mut &encoded[..]) .unwrap_or_else(|e| panic!("Error decoding return value {e}")); let expected = Value::Tuple(Tuple::new( From 1b05cc2d4954c5c361c0ec15b249c7f78bde1e85 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 19 May 2023 16:20:07 +0100 Subject: [PATCH 2/4] Merge result and data, reorder fields --- .../src/cmd/extrinsics/instantiate.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs b/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs index d06d7e6ab..7afd0d811 100644 --- a/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs +++ b/crates/cargo-contract/src/cmd/extrinsics/instantiate.rs @@ -212,10 +212,9 @@ impl Exec { &ret_val ))?; let dry_run_result = InstantiateDryRunResult { - result: String::from("Success!"), + result: value, contract: ret_val.account_id.to_string(), reverted: ret_val.result.did_revert(), - data: value, gas_consumed: result.gas_consumed, gas_required: result.gas_required, storage_deposit: StorageDeposit::from(&result.storage_deposit), @@ -451,13 +450,12 @@ impl InstantiateResult { /// Result of the contract call #[derive(serde::Serialize)] pub struct InstantiateDryRunResult { - /// Result of a dry run - pub result: String, + /// The decoded result returned from the constructor + pub result: Value, /// contract address pub contract: String, /// Was the operation reverted pub reverted: bool, - pub data: Value, pub gas_consumed: Weight, pub gas_required: Weight, /// Storage deposit after the operation @@ -471,14 +469,13 @@ impl InstantiateDryRunResult { } pub fn print(&self) { - name_value_println!("Result", self.result, DEFAULT_KEY_COL_WIDTH); - name_value_println!("Contract", self.contract, DEFAULT_KEY_COL_WIDTH); + name_value_println!("Result", format!("{}", self.result), DEFAULT_KEY_COL_WIDTH); name_value_println!( "Reverted", format!("{:?}", self.reverted), DEFAULT_KEY_COL_WIDTH ); - name_value_println!("Returned", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); + name_value_println!("Contract", self.contract, DEFAULT_KEY_COL_WIDTH); } } From bbd943cca9b708594336c583d352093d3488ffc7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Fri, 19 May 2023 16:27:49 +0100 Subject: [PATCH 3/4] Replace Result Success! with decoded result --- crates/cargo-contract/src/cmd/extrinsics/call.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/crates/cargo-contract/src/cmd/extrinsics/call.rs b/crates/cargo-contract/src/cmd/extrinsics/call.rs index 2d4df616c..c4de2a244 100644 --- a/crates/cargo-contract/src/cmd/extrinsics/call.rs +++ b/crates/cargo-contract/src/cmd/extrinsics/call.rs @@ -125,7 +125,6 @@ impl CallCommand { &ret_val ))?; let dry_run_result = CallDryRunResult { - result: String::from("Success!"), reverted: ret_val.did_revert(), data: value, gas_consumed: result.gas_consumed, @@ -305,8 +304,6 @@ pub struct CallRequest { /// Result of the contract call #[derive(serde::Serialize)] pub struct CallDryRunResult { - /// Result of a dry run - pub result: String, /// Was the operation reverted pub reverted: bool, pub data: Value, @@ -323,12 +320,11 @@ impl CallDryRunResult { } pub fn print(&self) { - name_value_println!("Result", self.result, DEFAULT_KEY_COL_WIDTH); + name_value_println!("Result", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); name_value_println!( "Reverted", format!("{:?}", self.reverted), DEFAULT_KEY_COL_WIDTH ); - name_value_println!("Returned", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH); } } From 7947ad62341085be7f3f073c6e9d863d5c427aa7 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 13 Jun 2023 16:49:23 +0100 Subject: [PATCH 4/4] CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b3bb46d0..8e9d187ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- Dry-run result output improvements [1123](https://github.com/paritytech/cargo-contract/pull/1123) + ## [3.0.1] ### Fixed