Skip to content
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

pretty move run and move publish output #2355

Merged
merged 2 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/event_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use crate::jsonrpc_types::{
AnnotatedMoveStructView, H256View, RoochAddressView, StrView, StructTagView, UnitedAddressView,
AnnotatedMoveStructView, H256View, HumanReadableDisplay, RoochAddressView, StrView,
StructTagView, UnitedAddressView,
};
use moveos_types::moveos_std::{
event::{AnnotatedEvent, Event, EventID, TransactionEvent},
Expand Down Expand Up @@ -103,6 +104,18 @@ impl From<AnnotatedEvent> for EventView {
}
}

impl HumanReadableDisplay for EventView {
fn to_human_readable_string(&self, _verbose: bool, indent: usize) -> String {
format!(
"{indent}event handle id: {}\n{indent}event seq : {}\n{indent}event type : {}",
self.event_id.event_handle_id,
self.event_id.event_seq.0,
self.event_type,
indent = " ".repeat(indent),
)
}
}

#[derive(
Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize, JsonSchema,
)]
Expand Down
21 changes: 20 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/execute_tx_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use super::BytesView;
use super::{ModuleIdView, StateChangeSetView, StrView};
use super::{HumanReadableDisplay, ModuleIdView, StateChangeSetView, StrView};
use crate::jsonrpc_types::event_view::EventView;
use crate::jsonrpc_types::H256View;
use move_core_types::vm_status::{AbortLocation, KeptVMStatus};
Expand Down Expand Up @@ -135,6 +135,25 @@ impl From<TransactionExecutionInfo> for TransactionExecutionInfoView {
}
}

impl HumanReadableDisplay for TransactionExecutionInfoView {
fn to_human_readable_string(&self, _verbose: bool, indent: usize) -> String {
format!(
r#"{indent}Execution info:
{indent} status: {:?}
{indent} gas used: {}
{indent} tx hash: {}
{indent} state root: {}
{indent} event root: {}"#,
self.status,
self.gas_used,
self.tx_hash,
self.state_root,
self.event_root,
indent = " ".repeat(indent)
)
}
}

#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct TransactionOutputView {
pub status: KeptVMStatusView,
Expand Down
17 changes: 9 additions & 8 deletions crates/rooch-rpc-api/src/jsonrpc_types/rooch_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ where
T: HumanReadableDisplay,
C: std::fmt::Display,
{
fn to_human_readable_string(&self, verbose: bool) -> String {
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
let _ = verbose;
format!(
r#"Data:
{}
r#"{indent}Data:
{indent}{}

Next cursor:
{}
{indent}Next cursor:
{indent} {}

Has next page: {:?}"#,
self.data.to_human_readable_string(verbose),
{indent}Has next page: {:?}"#,
self.data.to_human_readable_string(verbose, 4),
self.next_cursor
.as_ref()
.map_or("None".to_string(), |c| c.to_string()),
self.has_next_page
self.has_next_page,
indent = " ".repeat(indent)
)
}
}
Expand Down
153 changes: 121 additions & 32 deletions crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,90 @@ impl From<ObjectMetaView> for ObjectMeta {
}
}

impl HumanReadableDisplay for ObjectMetaView {
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
if verbose {
format!(
r#"{indent}{}
{indent} objectId | {}
{indent} type | {}
{indent} owner | {}
{indent} owner(bitcoin) | {:?}
{indent} status | {}
{indent}{}"#,
"-".repeat(100),
self.id,
self.object_type,
self.owner,
self.owner_bitcoin_address,
human_readable_flag(self.flag),
"-".repeat(100),
indent = " ".repeat(indent),
)
} else {
format!(
r#"{indent}objectId: {}
{indent}type : {}"#,
self.id,
self.object_type,
indent = " ".repeat(indent),
)
}
}
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct StateChangeSetView {
pub state_root: H256View,
pub global_size: StrView<u64>,
pub changes: Vec<ObjectChangeView>,
}

impl StateChangeSetView {
// Parse the new/modified/deleted objects from the state change set.
pub fn parse_changed_objects(
&self,
) -> (
Vec<ObjectMetaView>,
Vec<ObjectMetaView>,
Vec<ObjectMetaView>,
) {
parse_changed_objects(&self.changes)
}
}
impl HumanReadableDisplay for StateChangeSetView {
fn to_human_readable_string(&self, _verbose: bool, indent: usize) -> String {
let (new_objs, modified_objs, deleted_objs) = parse_changed_objects(&self.changes);

format!(
r#"{indent}New objects:
{indent}{}

{indent}Modified objects:
{indent}{}

{indent}Removed objects:
{indent}{}"#,
if new_objs.is_empty() {
" None".to_owned()
} else {
new_objs.to_human_readable_string(false, 4)
},
if modified_objs.is_empty() {
" None".to_owned()
} else {
modified_objs.to_human_readable_string(false, 4)
},
if deleted_objs.is_empty() {
" None".to_owned()
} else {
deleted_objs.to_human_readable_string(false, 4)
},
indent = " ".repeat(indent),
)
}
}

impl From<StateChangeSet> for StateChangeSetView {
fn from(state_change_set: StateChangeSet) -> Self {
Self {
Expand Down Expand Up @@ -261,19 +338,19 @@ impl IndexerObjectStateView {
}

impl HumanReadableDisplay for IndexerObjectStateView {
fn to_human_readable_string(&self, verbose: bool) -> String {
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
let _ = verbose; // TODO: implement verbose string

format!(
r#"{}
objectId | {}
type | {}
owner | {}
owner(bitcoin) | {:?}
status | {}
tx_order | {}
state_index | {}
{}"#,
r#"{indent}{}
{indent} objectId | {}
{indent} type | {}
{indent} owner | {}
{indent} owner(bitcoin) | {:?}
{indent} status | {}
{indent} tx_order | {}
{indent} state_index | {}
{indent}{}"#,
"-".repeat(100),
self.metadata.id,
self.metadata.object_type,
Expand All @@ -283,6 +360,7 @@ impl HumanReadableDisplay for IndexerObjectStateView {
self.indexer_id.tx_order,
self.indexer_id.state_index,
"-".repeat(100),
indent = " ".repeat(indent),
)
}
}
Expand Down Expand Up @@ -396,37 +474,48 @@ impl From<AnnotatedState> for ObjectStateView {
}

impl HumanReadableDisplay for ObjectStateView {
fn to_human_readable_string(&self, verbose: bool) -> String {
let _ = verbose; // TODO: implement verbose string

format!(
r#"{}
objectId | {}
type | {}
owner | {}
owner(bitcoin) | {:?}
status | {}
{}"#,
"-".repeat(100),
self.metadata.id,
self.metadata.object_type,
self.metadata.owner,
self.metadata.owner_bitcoin_address,
human_readable_flag(self.metadata.flag),
"-".repeat(100),
)
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
self.metadata.to_human_readable_string(verbose, indent)
}
}

impl<T> HumanReadableDisplay for Vec<T>
where
T: HumanReadableDisplay,
{
fn to_human_readable_string(&self, verbose: bool) -> String {
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
let _ = verbose;
self.iter()
.map(|s| s.to_human_readable_string(verbose))
.map(|s| s.to_human_readable_string(verbose, indent))
.collect::<Vec<_>>()
.join("\n")
.join("\n\n")
}
}

fn parse_changed_objects(
changes: &Vec<ObjectChangeView>,
) -> (
Vec<ObjectMetaView>,
Vec<ObjectMetaView>,
Vec<ObjectMetaView>,
) {
let mut new_objs = vec![];
let mut modified_objs = vec![];
let mut deleted_objs = vec![];
for obj_change in changes {
let metadata = obj_change.metadata.clone();
debug_assert!(obj_change.value.is_some() || !obj_change.fields.is_empty());
match obj_change.value {
Some(OpView::New(_)) => new_objs.push(metadata),
Some(OpView::Modify(_)) => modified_objs.push(metadata),
Some(OpView::Delete) => deleted_objs.push(metadata),
None => modified_objs.push(metadata), // no value change, there must has field changes.
};
let (field_new_objs, field_modified_objs, field_deleted_objs) =
parse_changed_objects(&obj_change.fields);
new_objs.extend(field_new_objs);
modified_objs.extend(field_modified_objs);
deleted_objs.extend(field_deleted_objs);
}
(new_objs, modified_objs, deleted_objs)
}
7 changes: 4 additions & 3 deletions crates/rooch-rpc-api/src/jsonrpc_types/str_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,16 @@ impl std::fmt::Display for MoveStringView {

/// This trait is used to convert a struct to a human readable string
/// `verbose`: if true, the human readable string will be more detailed
/// `indent`: indentation for each line
pub trait HumanReadableDisplay {
fn to_human_readable_string(&self, verbose: bool) -> String;
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String;
}

impl<T> HumanReadableDisplay for StrView<T>
where
T: HumanReadableDisplay,
{
fn to_human_readable_string(&self, verbose: bool) -> String {
self.0.to_human_readable_string(verbose)
fn to_human_readable_string(&self, verbose: bool, indent: usize) -> String {
self.0.to_human_readable_string(verbose, indent)
}
}
25 changes: 11 additions & 14 deletions crates/rooch/src/commands/move_cli/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use moveos_verifier::build::run_verifier;
use moveos_verifier::verifier;
use rooch_key::key_derive::verify_password;
use rooch_key::keystore::account_keystore::AccountKeystore;
use rooch_rpc_api::jsonrpc_types::ExecuteTransactionResponseView;
use rooch_rpc_api::jsonrpc_types::{ExecuteTransactionResponseView, HumanReadableDisplay};
use rooch_types::address::RoochAddress;
use rooch_types::error::{RoochError, RoochResult};
use rooch_types::transaction::rooch::RoochTransaction;
Expand Down Expand Up @@ -235,19 +235,7 @@ impl Publish {

// print execution info
let exe_info = &txn_response.execution_info;
output.push_str(&format!(
r#"Execution info:
status: {:?}
gas used: {}
tx hash: {}
state root: {}
event root: {}"#,
exe_info.status,
exe_info.gas_used,
exe_info.tx_hash,
exe_info.state_root,
exe_info.event_root
));
output.push_str(&exe_info.to_human_readable_string(false, 0));

if let Some(txn_output) = &txn_response.output {
// print modules
Expand Down Expand Up @@ -306,6 +294,15 @@ impl Publish {
output.push_str(&format!("\n {}", module.short_str_lossless()));
}
};

// print objects changes
output.push_str("\n\n");
output.push_str(
txn_output
.changeset
.to_human_readable_string(false, 0)
.as_str(),
);
}

Ok(output)
Expand Down
Loading
Loading