Skip to content

Feat/clarity cli serialized output #2684

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 6 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/clarity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use rusqlite::{Connection, OpenFlags, NO_PARAMS};
use address::c32::c32_address;
use chainstate::stacks::index::{storage::TrieFileStorage, MarfTrieId};
use util::db::FromColumn;
use util::hash::Sha512Trunc256Sum;
use util::hash::{bytes_to_hex, Sha512Trunc256Sum};

use util::log;
use vm::ContractName;
Expand Down Expand Up @@ -65,6 +65,8 @@ use serde::Serialize;
use serde_json::json;
use util::strings::StacksString;

use codec::StacksMessageCodec;

use std::convert::TryFrom;

use crate::clarity_vm::database::marf::MarfedKV;
Expand Down Expand Up @@ -766,6 +768,14 @@ pub fn add_assets(result: &mut serde_json::Value, assets: bool, asset_map: Asset
}
}

pub fn add_serialized_output(result: &mut serde_json::Value, value: Value) {
let result_raw = {
let bytes = (&value).serialize_to_vec();
bytes_to_hex(&bytes)
};
result["output_serialized"] = serde_json::to_value(result_raw.as_str()).unwrap();
}

/// Returns (process-exit-code, Option<json-output>)
pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_json::Value>) {
if args.len() < 1 {
Expand Down Expand Up @@ -1158,9 +1168,11 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
match result_and_cost {
(Ok(result), cost) => {
let mut result_json = json!({
"output": serde_json::to_value(&result).unwrap()
"output": serde_json::to_value(&result).unwrap(),
"success": true,
});

add_serialized_output(&mut result_json, result);
add_costs(&mut result_json, costs, cost);

(0, Some(result_json))
Expand All @@ -1169,7 +1181,8 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
let mut result_json = json!({
"error": {
"runtime": serde_json::to_value(&format!("{}", error)).unwrap()
}
},
"success": false,
});

add_costs(&mut result_json, costs, cost);
Expand Down Expand Up @@ -1208,9 +1221,11 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
match result_and_cost {
(Ok(result), cost) => {
let mut result_json = json!({
"output": serde_json::to_value(&result).unwrap()
"output": serde_json::to_value(&result).unwrap(),
"success": true,
});

add_serialized_output(&mut result_json, result);
add_costs(&mut result_json, costs, cost);

(0, Some(result_json))
Expand All @@ -1219,7 +1234,8 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
let mut result_json = json!({
"error": {
"runtime": serde_json::to_value(&format!("{}", error)).unwrap()
}
},
"success": false,
});

add_costs(&mut result_json, costs, cost);
Expand Down Expand Up @@ -1278,9 +1294,11 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
match result_and_cost {
(Ok(result), cost) => {
let mut result_json = json!({
"output": serde_json::to_value(&result).unwrap()
"output": serde_json::to_value(&result).unwrap(),
"success": true,
});

add_serialized_output(&mut result_json, result);
add_costs(&mut result_json, costs, cost);

(0, Some(result_json))
Expand All @@ -1289,7 +1307,8 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
let mut result_json = json!({
"error": {
"runtime": serde_json::to_value(&format!("{}", error)).unwrap()
}
},
"success": false,
});

add_costs(&mut result_json, costs, cost);
Expand Down Expand Up @@ -1480,8 +1499,10 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
let mut result = json!({
"message": "Transaction executed and committed.",
"output": serde_json::to_value(&data.data).unwrap(),
"success": true,
});

add_serialized_output(&mut result, *data.data);
add_costs(&mut result, costs, cost);
add_assets(&mut result, assets, asset_map);

Expand All @@ -1496,9 +1517,12 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
let mut result = json!({
"message": "Aborted.",
"output": serde_json::to_value(&data.data).unwrap(),
"success": false,
});

add_costs(&mut result, costs, cost);
add_serialized_output(&mut result, *data.data);
add_assets(&mut result, assets, asset_map);

(0, Some(result))
}
Expand All @@ -1507,7 +1531,8 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
"error": {
"runtime": "Expected a ResponseType result from transaction.",
"output": serde_json::to_value(&x).unwrap()
}
},
"success": false,
});
(1, Some(result))
}
Expand All @@ -1517,7 +1542,8 @@ pub fn invoke_command(invoked_by: &str, args: &[String]) -> (i32, Option<serde_j
"error": {
"runtime": "Transaction execution error.",
"error": serde_json::to_value(&format!("{}", error)).unwrap()
}
},
"success": false,
});
(1, Some(result))
}
Expand Down