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

[wallet bug fix] Interactive shell crashes when trying to query an account address with 'object' command #1082

Merged
merged 1 commit into from
Mar 26, 2022
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
11 changes: 11 additions & 0 deletions sui/src/unit_tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use move_core_types::identifier::Identifier;
use serde_json::{json, Value};
use tracing_test::traced_test;

use std::fmt::Write;
use sui::config::{
AccountConfig, AuthorityPrivateInfo, Config, GenesisConfig, NetworkConfig, ObjectConfig,
PersistedConfig, WalletConfig, AUTHORITIES_DB_NAME,
Expand Down Expand Up @@ -1027,3 +1028,13 @@ async fn test_native_transfer() -> Result<(), anyhow::Error> {
network.kill().await?;
Ok(())
}

#[test]
// Test for issue https://github.com/MystenLabs/sui/issues/1078
fn test_bug_1078() {
let read = WalletCommandResult::Object(ObjectRead::NotExists(ObjectID::random()));
let mut writer = String::new();
// fmt ObjectRead should not fail.
write!(writer, "{}", read).unwrap();
write!(writer, "{:?}", read).unwrap();
}
25 changes: 14 additions & 11 deletions sui/src/wallet_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use colored::Colorize;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::TypeTag;
use move_core_types::parser::parse_type_tag;
use serde::ser::Error;
use serde::Serialize;
use structopt::clap::AppSettings;
use structopt::StructOpt;
Expand Down Expand Up @@ -476,7 +475,7 @@ impl Display for WalletCommandResult {
write!(writer, "{}", response)?;
}
WalletCommandResult::Object(object_read) => {
let object = object_read.object().map_err(fmt::Error::custom)?;
let object = unwrap_err_to_string(|| Ok(object_read.object()?));
writeln!(writer, "{}", object)?;
}
WalletCommandResult::Call(cert, effects) => {
Expand Down Expand Up @@ -550,21 +549,25 @@ fn write_cert_and_effects(

impl Debug for WalletCommandResult {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let s = match self {
let s = unwrap_err_to_string(|| match self {
WalletCommandResult::Object(object_read) => {
let object = object_read.object().map_err(fmt::Error::custom)?;
let layout = object_read.layout().map_err(fmt::Error::custom)?;
object
.to_json(layout)
.map_err(fmt::Error::custom)?
.to_string()
let object = object_read.object()?;
let layout = object_read.layout()?;
Ok(object.to_json(layout)?.to_string())
}
_ => serde_json::to_string(self).map_err(fmt::Error::custom)?,
};
_ => Ok(serde_json::to_string(self)?),
});
write!(f, "{}", s)
}
}

fn unwrap_err_to_string<T: Display, F: FnOnce() -> Result<T, anyhow::Error>>(func: F) -> String {
match func() {
Ok(s) => format!("{}", s),
Err(err) => format!("{}", err).red().to_string(),
}
}

impl WalletCommandResult {
pub fn print(&self, pretty: bool) {
let line = if pretty {
Expand Down