Skip to content

Commit

Permalink
Dry-run result output improvements (#1123)
Browse files Browse the repository at this point in the history
* Show instantiation decoded return value

* Merge result and data, reorder fields

* Replace Result Success! with decoded result

* CHANGELOG.md
  • Loading branch information
ascjones authored Jun 13, 2023
1 parent 4f1840a commit 71a8a42
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions crates/cargo-contract/src/cmd/extrinsics/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ 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
))?;
let dry_run_result = CallDryRunResult {
result: String::from("Success!"),
reverted: ret_val.did_revert(),
data: value,
gas_consumed: result.gas_consumed,
Expand Down Expand Up @@ -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,
Expand All @@ -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!("Data", format!("{}", self.data), DEFAULT_KEY_COL_WIDTH);
}
}
25 changes: 17 additions & 8 deletions crates/cargo-contract/src/cmd/extrinsics/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ use crate::{
};
use anyhow::{
anyhow,
Context,
Result,
};
use contract_build::{
name_value_println,
util::decode_hex,
Verbosity,
};
use contract_transcode::Value;

use pallet_contracts_primitives::ContractInstantiateResult;

Expand Down Expand Up @@ -199,11 +201,20 @@ 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!"),
result: value,
contract: ret_val.account_id.to_string(),
reverted: ret_val.result.did_revert(),
data: ret_val.result.data.clone().into(),
gas_consumed: result.gas_consumed,
gas_required: result.gas_required,
storage_deposit: StorageDeposit::from(&result.storage_deposit),
Expand Down Expand Up @@ -439,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: Bytes,
pub gas_consumed: Weight,
pub gas_required: Weight,
/// Storage deposit after the operation
Expand All @@ -459,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!("Data", format!("{:?}", self.data), DEFAULT_KEY_COL_WIDTH);
name_value_println!("Contract", self.contract, DEFAULT_KEY_COL_WIDTH);
name_value_println!(
"Gas consumed",
self.gas_consumed.to_string(),
Expand Down
21 changes: 18 additions & 3 deletions crates/transcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,22 @@ impl ContractMessageTranscoder {
Ok(Value::Map(map))
}

pub fn decode_return(&self, name: &str, data: &mut &[u8]) -> Result<Value> {
pub fn decode_constructor_return(
&self,
name: &str,
data: &mut &[u8],
) -> Result<Value> {
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<Value> {
let msg_spec = self.find_message_spec(name).ok_or_else(|| {
anyhow::anyhow!("Failed to find message spec with name '{}'", name)
})?;
Expand Down Expand Up @@ -674,7 +689,7 @@ mod tests {

let encoded = Result::<bool, ink::primitives::LangError>::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(
Expand All @@ -694,7 +709,7 @@ mod tests {
let encoded =
Result::<bool, LangError>::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(
Expand Down

0 comments on commit 71a8a42

Please sign in to comment.