From 9cc694f9b0ee619ff058dcdfd976e0726226398d Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:01:55 -0700 Subject: [PATCH 1/8] feat: error reporting with eyre --- crates/cli/src/cache.rs | 53 +++++++++++----------- crates/cli/src/error.rs | 54 +++++------------------ crates/cli/src/handler.rs | 9 ++-- crates/cli/src/handler/contract_build.rs | 13 +++--- crates/cli/src/handler/contract_deploy.rs | 31 +++++++------ crates/cli/src/handler/dev.rs | 32 ++++++-------- crates/cli/src/handler/enclave_build.rs | 13 +++--- crates/cli/src/handler/enclave_start.rs | 46 +++++++------------ crates/cli/src/handler/handshake.rs | 27 ++++++------ crates/cli/src/handler/init.rs | 8 ++-- crates/cli/src/handler/utils/helpers.rs | 33 +++++++------- crates/cli/src/main.rs | 6 +-- crates/cli/src/request.rs | 29 ++++++------ crates/cli/src/request/contract_deploy.rs | 10 ++--- crates/cli/src/request/enclave_start.rs | 10 ++--- 15 files changed, 160 insertions(+), 214 deletions(-) diff --git a/crates/cli/src/cache.rs b/crates/cli/src/cache.rs index 71485024..e2bca288 100644 --- a/crates/cli/src/cache.rs +++ b/crates/cli/src/cache.rs @@ -8,7 +8,8 @@ use tokio::{ use tracing::debug; use xxhash_rust::xxh3::Xxh3; -use crate::{config::Config, error::Error}; +use color_eyre::{eyre::eyre, Report, Result}; +use crate::config::Config; const BUFFER_SIZE: usize = 16384; // 16 KB buffer type Hash = u64; @@ -22,7 +23,7 @@ struct DeployedContract { // Porcelain impl Config { - pub async fn contract_has_changed(&self, file: &Path) -> Result { + pub async fn contract_has_changed(&self, file: &Path) -> Result { let cur_hash: Hash = Self::gen_hash(file).await?; debug!("current file hash: {}", cur_hash); @@ -39,10 +40,10 @@ impl Config { } /// Return a hash of the given file's contents - pub async fn gen_hash(file: &Path) -> Result { + pub async fn gen_hash(file: &Path) -> Result { let file = File::open(file) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; + let mut reader = BufReader::new(file); let mut hasher = Xxh3::new(); @@ -51,8 +52,8 @@ impl Config { loop { let bytes_read = reader .read(&mut buffer) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; + if bytes_read == 0 { break; } @@ -65,7 +66,7 @@ impl Config { Ok(hash) } - pub async fn save_codeid_to_cache(&self, file: &Path, code_id: u64) -> Result<(), Error> { + pub async fn save_codeid_to_cache(&self, file: &Path, code_id: u64) -> Result<()> { let contract_hash = Self::gen_hash(file).await?; let dest = Self::to_cache_path(self, file)?; let deployed_contract = DeployedContract { @@ -76,7 +77,7 @@ impl Config { Self::write_to_cache(dest.as_path(), &deployed_contract).await } - pub async fn get_cached_codeid(&self, file: &Path) -> Result { + pub async fn get_cached_codeid(&self, file: &Path) -> Result { let cache_path = Self::to_cache_path(self, file)?; let code_id = Self::read_from_cache(cache_path.as_path()).await?.code_id; @@ -85,12 +86,13 @@ impl Config { // Plumbing - fn to_cache_path(&self, file: &Path) -> Result { + fn to_cache_path(&self, file: &Path) -> Result { // Get cache filepath (".quartz/cache/example.wasm.json") from "example.wasm" filepath let mut filename = file .file_name() - .ok_or(Error::PathNotFile(file.display().to_string()))? + .ok_or(eyre!("file at cache filepath does not exist {}", file.display()))? .to_os_string(); + filename.push(".json"); let cached_file_path = Self::cache_dir(self)?.join::(filename.into()); @@ -99,42 +101,42 @@ impl Config { } /// Retreive hash from cache file - async fn read_from_cache(cache_file: &Path) -> Result { + async fn read_from_cache(cache_file: &Path) -> Result { let content = tokio::fs::read_to_string(cache_file) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; - serde_json::from_str(&content).map_err(|e| Error::GenericErr(e.to_string())) + .await?; + + serde_json::from_str(&content).map_err(|e| eyre!(e)) } /// Write a given file's contents hash to a file in cache directory - async fn write_to_cache(cache_file: &Path, data: &DeployedContract) -> Result<(), Error> { - let content = serde_json::to_string(data).map_err(|e| Error::GenericErr(e.to_string()))?; + async fn write_to_cache(cache_file: &Path, data: &DeployedContract) -> Result<()> { + let content = serde_json::to_string(data)?; + tokio::fs::write(cache_file, content) .await - .map_err(|e| Error::GenericErr(e.to_string())) + .map_err(|e| eyre!(e)) } - pub fn cache_dir(&self) -> Result { + pub fn cache_dir(&self) -> Result { Ok(self.app_dir.join(".cache/")) } - pub fn build_log_dir(&self) -> Result { + pub fn build_log_dir(&self) -> Result { Ok(self.app_dir.join(".cache/log/")) } /// Creates the build log if it isn't created already, returns relative path from app_dir to log directory - pub async fn create_build_log(&self) -> Result { + pub async fn create_build_log(&self) -> Result { let log_dir = Self::build_log_dir(self)?; if !log_dir.exists() { tokio::fs::create_dir_all(&log_dir) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; } Ok(log_dir) } - pub async fn log_build(&self, is_enclave: bool) -> Result<(), Error> { + pub async fn log_build(&self, is_enclave: bool) -> Result<()> { let log_dir = Self::create_build_log(self).await?; let filename = match is_enclave { @@ -143,8 +145,7 @@ impl Config { }; tokio::fs::write(log_dir.join(filename), "test") - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; Ok(()) } diff --git a/crates/cli/src/error.rs b/crates/cli/src/error.rs index 96a57c2a..c9943cc9 100644 --- a/crates/cli/src/error.rs +++ b/crates/cli/src/error.rs @@ -9,50 +9,20 @@ pub enum Error { PathNotFile(String), /// unspecified error: {0} GenericErr(String), - /// IoError: {0} - IoError(String), + /// Cache error: {0} + Cache(String), + /// Config error: {0} + Config(String), /// TOML Error : {0} - TomlError(String), + TomlError(#[from] toml::de::Error), + /// TOML Error : {0} + TomlSerError(#[from] toml::ser::Error), /// Tendermint error: {0} - TendermintError(String), + TendermintError(#[from] tendermint::Error), /// Clearscreen error: {0} - ClearscreenError(String), + ClearscreenError(#[from] clearscreen::Error), /// JSON Error: {0} - JsonError(String), -} - -impl From for Error { - fn from(err: std::io::Error) -> Self { - Error::IoError(err.to_string()) - } -} - -impl From for Error { - fn from(err: toml::de::Error) -> Self { - Error::TomlError(err.to_string()) - } -} - -impl From for Error { - fn from(err: toml::ser::Error) -> Self { - Error::TomlError(err.to_string()) - } -} - -impl From for Error { - fn from(err: tendermint::Error) -> Self { - Error::TendermintError(err.to_string()) - } -} - -impl From for Error { - fn from(err: clearscreen::Error) -> Self { - Error::ClearscreenError(err.to_string()) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Self { - Error::JsonError(err.to_string()) - } + JsonError(#[from] serde_json::Error), + /// IO Error: {0} + IoError(#[from] std::io::Error), } diff --git a/crates/cli/src/handler.rs b/crates/cli/src/handler.rs index 7d5d882f..b5bd801a 100644 --- a/crates/cli/src/handler.rs +++ b/crates/cli/src/handler.rs @@ -1,6 +1,7 @@ use async_trait::async_trait; -use crate::{config::Config, error::Error, request::Request, response::Response}; +use crate::{config::Config, request::Request, response::Response}; +use color_eyre::{Result, Report}; pub mod utils; // commands @@ -14,24 +15,22 @@ pub mod init; #[async_trait] pub trait Handler { - type Error; type Response; async fn handle + Send>( self, config: C, - ) -> Result; + ) -> Result; } #[async_trait] impl Handler for Request { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { match self { Request::Init(request) => request.handle(config).await, Request::Handshake(request) => request.handle(config).await, diff --git a/crates/cli/src/handler/contract_build.rs b/crates/cli/src/handler/contract_build.rs index c059c9d1..985bac23 100644 --- a/crates/cli/src/handler/contract_build.rs +++ b/crates/cli/src/handler/contract_build.rs @@ -11,16 +11,16 @@ use crate::{ request::contract_build::ContractBuildRequest, response::{contract_build::ContractBuildResponse, Response}, }; +use color_eyre::{Result, Report, eyre::eyre}; #[async_trait] impl Handler for ContractBuildRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Contract Build".blue().bold()); @@ -47,14 +47,13 @@ impl Handler for ContractBuildRequest { info!("{}", "🚧 Building contract binary ...".green().bold()); let status = command - .status() - .map_err(|e| Error::GenericErr(e.to_string()))?; - + .status()?; + if !status.success() { - return Err(Error::GenericErr(format!( + return Err(eyre!( "Couldn't build contract. \n{:?}", status - ))); + )); } config.log_build(false).await?; diff --git a/crates/cli/src/handler/contract_deploy.rs b/crates/cli/src/handler/contract_deploy.rs index 35bcdf26..e5982c3d 100644 --- a/crates/cli/src/handler/contract_deploy.rs +++ b/crates/cli/src/handler/contract_deploy.rs @@ -2,11 +2,12 @@ use std::path::Path; use async_trait::async_trait; use cargo_metadata::MetadataCommand; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::Context, owo_colors::OwoColorize}; use cw_client::{CliClient, CwClient}; use serde_json::json; use tendermint_rpc::HttpClient; use tracing::{debug, info}; +use color_eyre::{Result, Report, eyre::eyre}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ @@ -19,24 +20,21 @@ use crate::{ #[async_trait] impl Handler for ContractDeployRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Contract Deploy".blue().bold()); // Get contract package name in snake_case let package_name = MetadataCommand::new() .manifest_path(&self.contract_manifest) - .exec() - .map_err(|e| Error::GenericErr(e.to_string()))? + .exec()? .root_package() - .ok_or("No root package found in the metadata") - .map_err(|e| Error::GenericErr(e.to_string()))? + .ok_or(eyre!("No root package found in the metadata"))? .name .clone() .replace('-', "_"); @@ -48,8 +46,7 @@ impl Handler for ContractDeployRequest { .with_extension("wasm"); let (code_id, contract_addr) = deploy(wasm_bin_path.as_path(), self, config) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; Ok(ContractDeployResponse { code_id, @@ -63,7 +60,7 @@ async fn deploy( wasm_bin_path: &Path, args: ContractDeployRequest, config: &Config, -) -> Result<(u64, String), anyhow::Error> { +) -> Result<(u64, String), Report> { let tmrpc_client = HttpClient::new(config.node_url.as_str())?; let cw_client = CliClient::neutrond(config.node_url.clone()); @@ -73,7 +70,8 @@ async fn deploy( &config.chain_id, &config.tx_sender, wasm_bin_path.display().to_string(), - )?)?; + ).map_err(|err| eyre!(Box::new(err)))?).wrap_err("Error calling deploy on cw client")?; + let res = block_tx_commit(&tmrpc_client, deploy_output.txhash).await?; // Find the 'code_id' attribute @@ -89,15 +87,15 @@ async fn deploy( .find(|attr| attr.key_str().unwrap_or("") == "code_id") }) .and_then(|attr| attr.value_str().ok().and_then(|v| v.parse().ok())) - .ok_or_else(|| anyhow::anyhow!("Failed to find code_id in the transaction result"))?; + .ok_or_else(|| eyre!("Failed to find code_id in the transaction result"))?; info!("Code ID: {}", code_id); - config.save_codeid_to_cache(wasm_bin_path, code_id).await?; + config.save_codeid_to_cache(wasm_bin_path, code_id).await.wrap_err("Error saving contract code id to cache")?; code_id } else { - config.get_cached_codeid(wasm_bin_path).await? + config.get_cached_codeid(wasm_bin_path).await.wrap_err("Error getting contract code id from cache")? }; info!("🚀 Communicating with Relay to Instantiate..."); @@ -115,7 +113,8 @@ async fn deploy( code_id, json!(init_msg), &format!("{} Contract #{}", args.label, code_id), - )?)?; + ).map_err(|err| eyre!(Box::new(err)))?)?; // TODO: change underlying error type to be eyre instead of anyhow + let res = block_tx_commit(&tmrpc_client, init_output.txhash).await?; // Find the '_contract_address' attribute @@ -132,7 +131,7 @@ async fn deploy( }) .and_then(|attr| attr.value_str().ok().and_then(|v| v.parse().ok())) .ok_or_else(|| { - anyhow::anyhow!("Failed to find contract_address in the transaction result") + eyre!("Failed to find contract_address in the transaction result") })?; info!("🚀 Successfully deployed and instantiated contract!"); diff --git a/crates/cli/src/handler/dev.rs b/crates/cli/src/handler/dev.rs index 8be483a6..ff3aa862 100644 --- a/crates/cli/src/handler/dev.rs +++ b/crates/cli/src/handler/dev.rs @@ -1,14 +1,13 @@ use std::{path::PathBuf, time::Duration}; use async_trait::async_trait; -use color_eyre::owo_colors::OwoColorize; -// todo get rid of this? -use miette::{IntoDiagnostic, Result}; +use color_eyre::{eyre::{eyre, Context}, owo_colors::OwoColorize}; use quartz_common::proto::core_client::CoreClient; use tokio::{sync::mpsc, time::sleep}; use tracing::{debug, info}; use watchexec::Watchexec; use watchexec_signals::Signal; +use color_eyre::{Result, Report}; use crate::{ error::Error, @@ -24,13 +23,12 @@ use crate::{ #[async_trait] impl Handler for DevRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref(); info!("\nPeforming Dev"); @@ -58,7 +56,7 @@ async fn dev_driver( mut rx: mpsc::Receiver, args: &DevRequest, config: Config, -) -> Result<(), Error> { +) -> Result<(), Report> { // State let mut first_enclave_message = true; let mut first_contract_message = true; @@ -88,7 +86,7 @@ async fn dev_driver( spawn_enclave_start(args, &config)?; // Deploy new contract and perform handshake - let res = deploy_and_handshake(None, args, &config).await; + let res = deploy_and_handshake(None, args, &config).await.wrap_err("Error running deploy and handshake in `quartz dev`"); // Save resulting contract address or shutdown and return error match res { @@ -134,7 +132,7 @@ async fn dev_driver( Err(e) => { eprintln!("Error restarting enclave and handshake"); - return Err(e); + return Err(eyre!(e)); } } } @@ -154,7 +152,7 @@ async fn dev_driver( Err(e) => { eprintln!("Error deploying contract and handshake:"); - return Err(e); + return Err(eyre!(e)); } } @@ -167,7 +165,7 @@ async fn dev_driver( } // Spawns enclve start in a separate task which runs in the background -fn spawn_enclave_start(args: &DevRequest, config: &Config) -> Result<(), Error> { +fn spawn_enclave_start(args: &DevRequest, config: &Config) -> Result<()> { // In separate process, launch the enclave let enclave_start = EnclaveStartRequest { unsafe_trust_latest: args.unsafe_trust_latest, @@ -182,8 +180,6 @@ fn spawn_enclave_start(args: &DevRequest, config: &Config) -> Result<(), Error> if let Err(e) = enclave_start.handle(config_cpy).await { eprintln!("Error running enclave start.\n {}", e); } - - Ok::<(), Error>(()) }); Ok(()) @@ -194,7 +190,7 @@ async fn deploy_and_handshake( contract: Option<&str>, args: &DevRequest, config: &Config, -) -> Result { +) -> Result { info!("Waiting for enclave start to deploy contract and handshake"); // Wait at most 60 seconds to connect to enclave @@ -210,9 +206,7 @@ async fn deploy_and_handshake( i -= 1; if i == 0 { - return Err(Error::GenericErr( - "Could not connect to enclave".to_string(), - )); + return Err(eyre!("Could not connect to enclave")); } } // Calls which interact with enclave @@ -244,7 +238,7 @@ async fn deploy_and_handshake( // Run handshake info!("Running handshake on contract `{}`", contract); let handshake = HandshakeRequest { - contract: wasmaddr_to_id(&contract).map_err(|_| Error::GenericErr(String::default()))?, + contract: wasmaddr_to_id(&contract)?, unsafe_trust_latest: args.unsafe_trust_latest, }; @@ -255,7 +249,7 @@ async fn deploy_and_handshake( info!("Handshake complete: {}", res.pub_key); } Err(e) => { - return Err(e); + return Err(eyre!(e)); } _ => unreachable!("Unexpected response variant"), }; @@ -304,7 +298,7 @@ async fn watcher(tx: mpsc::Sender, log_dir: PathBuf) -> Result<()> { wx.config.pathset([log_dir]); // Keep running until Watchexec quits - let _ = main.await.into_diagnostic()?; + let _ = main.await?; Ok(()) } diff --git a/crates/cli/src/handler/enclave_build.rs b/crates/cli/src/handler/enclave_build.rs index 3ba4ef54..ccf8e6d5 100644 --- a/crates/cli/src/handler/enclave_build.rs +++ b/crates/cli/src/handler/enclave_build.rs @@ -1,7 +1,8 @@ use async_trait::async_trait; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::eyre, owo_colors::OwoColorize}; use tokio::process::Command; use tracing::{debug, info}; +use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -13,13 +14,12 @@ use crate::{ #[async_trait] impl Handler for EnclaveBuildRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Enclave Build".blue().bold()); @@ -45,14 +45,13 @@ impl Handler for EnclaveBuildRequest { info!("{}", "🚧 Running build command ...".green().bold()); let status = command .status() - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .await?; if !status.success() { - return Err(Error::GenericErr(format!( + return Err(eyre!( "Couldn't build enclave. {:?}", status - ))); + )); } config.log_build(true).await?; diff --git a/crates/cli/src/handler/enclave_start.rs b/crates/cli/src/handler/enclave_start.rs index 053eff28..6d0406ec 100644 --- a/crates/cli/src/handler/enclave_start.rs +++ b/crates/cli/src/handler/enclave_start.rs @@ -2,13 +2,14 @@ use std::{fs, path::Path}; use async_trait::async_trait; use cargo_metadata::MetadataCommand; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::{eyre, Context}, owo_colors::OwoColorize}; use cosmrs::AccountId; use quartz_common::enclave::types::Fmspc; use reqwest::Url; use tendermint::chain::Id; use tokio::process::{Child, Command}; use tracing::{debug, info}; +use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -20,18 +21,17 @@ use crate::{ #[async_trait] impl Handler for EnclaveStartRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref().clone(); info!("{}", "\nPeforming Enclave Start".blue().bold()); // Get trusted height and hash - let (trusted_height, trusted_hash) = self.get_hash_height(&config)?; + let (trusted_height, trusted_hash) = self.get_hash_height(&config).wrap_err("Error getting trusted hash and height")?; write_cache_hash_height(trusted_height, trusted_hash, &config).await?; if config.mock_sgx { @@ -59,27 +59,19 @@ impl Handler for EnclaveStartRequest { handle_process(enclave_child).await?; } else { let Some(fmspc) = self.fmspc else { - return Err(Error::GenericErr( - "FMSPC is required if MOCK_SGX isn't set".to_string(), - )); + return Err(eyre!("FMSPC is required if MOCK_SGX isn't set")); }; let Some(tcbinfo_contract) = self.tcbinfo_contract else { - return Err(Error::GenericErr( - "tcbinfo_contract is required if MOCK_SGX isn't set".to_string(), - )); + return Err(eyre!("tcbinfo_contract is required if MOCK_SGX isn't set")); }; let Some(dcap_verifier_contract) = self.dcap_verifier_contract else { - return Err(Error::GenericErr( - "dcap_verifier_contract is required if MOCK_SGX isn't set".to_string(), - )); + return Err(eyre!("dcap_verifier_contract is required if MOCK_SGX isn't set")); }; if let Err(_) = std::env::var("ADMIN_SK") { - return Err(Error::GenericErr( - "ADMIN_SK environment variable is not set".to_string(), - )); + return Err(eyre!("ADMIN_SK environment variable is not set")); }; let enclave_dir = fs::canonicalize(config.app_dir.join("enclave"))?; @@ -119,17 +111,13 @@ impl Handler for EnclaveStartRequest { } } -async fn handle_process(mut child: Child) -> Result<(), Error> { +async fn handle_process(mut child: Child) -> Result<()> { let status = child .wait() - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; - + .await?; + if !status.success() { - return Err(Error::GenericErr(format!( - "Couldn't build enclave. {:?}", - status - ))); + return Err(eyre!("Couldn't build enclave. {:?}", status)); } Ok(()) } @@ -138,18 +126,17 @@ async fn create_mock_enclave_child( app_dir: &Path, release: bool, enclave_args: Vec, -) -> Result { +) -> Result { let enclave_dir = app_dir.join("enclave"); let target_dir = app_dir.join("target"); // Use the enclave package metadata to get the path to the program binary let package_name = MetadataCommand::new() .manifest_path(enclave_dir.join("Cargo.toml")) - .exec() - .map_err(|e| Error::GenericErr(e.to_string()))? + .exec()? .root_package() .ok_or("No root package found in the metadata") - .map_err(|e| Error::GenericErr(e.to_string()))? + .map_err(|e| eyre!(e))? .name .clone(); @@ -168,8 +155,7 @@ async fn create_mock_enclave_child( info!("{}", "🚧 Spawning enclave process ...".green().bold()); let child = command .kill_on_drop(true) - .spawn() - .map_err(|e| Error::GenericErr(e.to_string()))?; + .spawn()?; Ok(child) } diff --git a/crates/cli/src/handler/handshake.rs b/crates/cli/src/handler/handshake.rs index 5ea4bfc1..9bf0329b 100644 --- a/crates/cli/src/handler/handshake.rs +++ b/crates/cli/src/handler/handshake.rs @@ -1,12 +1,13 @@ use anyhow::anyhow; use async_trait::async_trait; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::eyre, owo_colors::OwoColorize}; use cw_client::{CliClient, CwClient}; use futures_util::stream::StreamExt; use quartz_tm_prover::{config::Config as TmProverConfig, prover::prove}; use serde_json::json; use tendermint_rpc::{query::EventType, HttpClient, SubscriptionClient, WebSocketClient}; use tracing::{debug, info}; +use color_eyre::{Result, Report}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ @@ -22,27 +23,25 @@ use crate::{ #[async_trait] impl Handler for HandshakeRequest { - type Error = Error; type Response = Response; async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref().clone(); info!("{}", "\nPeforming Handshake".blue().bold()); // TODO: may need to import verbosity here let pub_key = handshake(self, config) - .await - .map_err(|e| Error::GenericErr(e.to_string()))?; - + .await?; + Ok(HandshakeResponse { pub_key }.into()) } } -async fn handshake(args: HandshakeRequest, config: Config) -> Result { +async fn handshake(args: HandshakeRequest, config: Config) -> Result { let tmrpc_client = HttpClient::new(config.node_url.as_str())?; let cw_client = CliClient::neutrond(config.node_url.clone()); @@ -64,7 +63,8 @@ async fn handshake(args: HandshakeRequest, config: Config) -> Result Result Result Result Result Result<(), anyhow::Error> { +async fn two_block_waitoor(wsurl: &str) -> Result<()> { let (client, driver) = WebSocketClient::new(wsurl).await?; let driver_handle = tokio::spawn(async move { driver.run().await }); diff --git a/crates/cli/src/handler/init.rs b/crates/cli/src/handler/init.rs index 8e523560..e76d6361 100644 --- a/crates/cli/src/handler/init.rs +++ b/crates/cli/src/handler/init.rs @@ -2,9 +2,10 @@ use std::path::PathBuf; use async_trait::async_trait; use cargo_generate::{generate, GenerateArgs, TemplatePath, Vcs}; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::Context, owo_colors::OwoColorize}; use tokio::fs; use tracing::info; +use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -16,14 +17,13 @@ use crate::{ #[async_trait] impl Handler for InitRequest { - type Error = Error; type Response = Response; // TODO: Add non-template init method async fn handle + Send>( self, config: C, - ) -> Result { + ) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Init".blue().bold()); @@ -36,7 +36,7 @@ impl Handler for InitRequest { .expect("path already validated"); fs::create_dir_all(&parent) .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .wrap_err("Error creating directories to target app folder")?; let file_name = self .name diff --git a/crates/cli/src/handler/utils/helpers.rs b/crates/cli/src/handler/utils/helpers.rs index f98c37bc..19a7bfc5 100644 --- a/crates/cli/src/handler/utils/helpers.rs +++ b/crates/cli/src/handler/utils/helpers.rs @@ -1,6 +1,5 @@ use std::time::Duration; -use anyhow::anyhow; use cosmrs::{AccountId, ErrorReport}; use cw_client::{CliClient, CwClient}; use regex::Regex; @@ -12,16 +11,17 @@ use tendermint_rpc::{ }; use tokio::fs::{self}; use tracing::debug; +use color_eyre::{eyre::eyre, Report, Result, eyre::WrapErr}; -use crate::{config::Config, error::Error}; +use crate::config::Config; -pub fn wasmaddr_to_id(address_str: &str) -> Result { - let _ = bech32_decode(address_str).map_err(|e| anyhow!(e))?; - address_str.parse().map_err(|e: ErrorReport| anyhow!(e)) +pub fn wasmaddr_to_id(address_str: &str) -> Result { + let _ = bech32_decode(address_str).map_err(|e| eyre!(e))?; + address_str.parse().map_err(|e: ErrorReport| eyre!(e)) } // Note: time until tx commit is empiraclly 800ms on DO wasmd chain. -pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result { +pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result { let re = Regex::new(r"tx \([A-F0-9]{64}\) not found")?; tokio::time::sleep(Duration::from_millis(400)).await; @@ -35,7 +35,7 @@ pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result { if !re.is_match(subdetail.source.data().unwrap_or_default()) { - return Err(anyhow!( + return Err(eyre!( "Error querying for tx: {}", ErrorDetail::Response(subdetail) )); @@ -46,7 +46,7 @@ pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result { - return Err(anyhow!("Error querying for tx: {}", e.0)); + return Err(eyre!("Error querying for tx: {}", e.0)); } } } @@ -55,13 +55,14 @@ pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result Result<(Height, Hash), Error> { - let cw_client = CliClient::neutrond(node_url); +pub fn query_latest_height_hash(node_url: Url) -> Result<(Height, Hash)> { + let cw_client = CliClient::wasmd(node_url); let (trusted_height, trusted_hash) = cw_client .trusted_height_hash() - .map_err(|e| Error::GenericErr(e.to_string()))?; - + .map_err(|e| eyre!(e)) + .wrap_err("Could not query chain with cw client")?; + Ok(( trusted_height.try_into()?, trusted_hash.parse().expect("invalid hash from wasmd"), @@ -72,7 +73,7 @@ pub async fn write_cache_hash_height( trusted_height: Height, trusted_hash: Hash, config: &Config, -) -> Result<(), Error> { +) -> Result<()> { let height_path = config.cache_dir()?.join("trusted.height"); fs::write(height_path.as_path(), trusted_height.to_string()).await?; let hash_path = config.cache_dir()?.join("trusted.hash"); @@ -81,15 +82,15 @@ pub async fn write_cache_hash_height( Ok(()) } -pub async fn read_cached_hash_height(config: &Config) -> Result<(Height, Hash), Error> { +pub async fn read_cached_hash_height(config: &Config) -> Result<(Height, Hash)> { let height_path = config.cache_dir()?.join("trusted.height"); let hash_path = config.cache_dir()?.join("trusted.hash"); if !height_path.exists() { - return Err(Error::PathNotFile(height_path.display().to_string())); + return Err(eyre!("Could not read trusted height from cache: {}", height_path.display().to_string())); } if !hash_path.exists() { - return Err(Error::PathNotFile(hash_path.display().to_string())); + return Err(eyre!("Could not read trusted hash from cache: {}", hash_path.display().to_string())); } let trusted_height: Height = fs::read_to_string(height_path.as_path()).await?.parse()?; diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index e98f3074..7e06b8ae 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -25,7 +25,7 @@ use std::path::PathBuf; use clap::Parser; use cli::ToFigment; -use color_eyre::{eyre::Result, owo_colors::OwoColorize}; +use color_eyre::{eyre::{eyre, Result}, owo_colors::OwoColorize}; use config::Config; use figment::{ providers::{Env, Format, Serialized, Toml}, @@ -97,10 +97,10 @@ async fn main() -> Result<()> { Ok(()) } -fn check_path(path: &Option) -> Result<(), error::Error> { +fn check_path(path: &Option) -> Result<()> { if let Some(path) = path { if !path.is_dir() { - return Err(error::Error::PathNotDir(format!("{}", path.display()))); + return Err(eyre!("Path is not a directory: {}", path.display())); } } diff --git a/crates/cli/src/request.rs b/crates/cli/src/request.rs index 60ad6c1a..e6a76212 100644 --- a/crates/cli/src/request.rs +++ b/crates/cli/src/request.rs @@ -1,6 +1,7 @@ +use color_eyre::eyre::eyre; + use crate::{ cli::{Command, ContractCommand, EnclaveCommand}, - error::Error, request::{ contract_build::ContractBuildRequest, contract_deploy::ContractDeployRequest, dev::DevRequest, enclave_build::EnclaveBuildRequest, enclave_start::EnclaveStartRequest, @@ -8,6 +9,8 @@ use crate::{ }, }; +use color_eyre::{Result, Report}; + pub mod contract_build; pub mod contract_deploy; pub mod dev; @@ -28,7 +31,7 @@ pub enum Request { } impl TryFrom for Request { - type Error = Error; + type Error = Report; fn try_from(cmd: Command) -> Result { match cmd { @@ -43,8 +46,7 @@ impl TryFrom for Request { Command::Dev(args) => Ok(DevRequest { watch: args.watch, unsafe_trust_latest: args.unsafe_trust_latest, - init_msg: serde_json::from_str(&args.contract_deploy.init_msg) - .map_err(|e| Error::GenericErr(e.to_string()))?, + init_msg: serde_json::from_str(&args.contract_deploy.init_msg)?, label: args.contract_deploy.label, contract_manifest: args.contract_deploy.contract_manifest, release: args.enclave_build.release, @@ -58,20 +60,17 @@ impl TryFrom for Request { } impl TryFrom for Request { - type Error = Error; + type Error = Report; - fn try_from(cmd: ContractCommand) -> Result { + fn try_from(cmd: ContractCommand) -> Result { match cmd { ContractCommand::Deploy(args) => { if !args.contract_manifest.exists() { - return Err(Error::PathNotFile( - args.contract_manifest.display().to_string(), - )); + return Err(eyre!("The contract manifest file does not exist: {}", args.contract_manifest.display())); } Ok(ContractDeployRequest { - init_msg: serde_json::from_str(&args.init_msg) - .map_err(|e| Error::GenericErr(e.to_string()))?, + init_msg: serde_json::from_str(&args.init_msg)?, label: args.label, contract_manifest: args.contract_manifest, } @@ -79,9 +78,7 @@ impl TryFrom for Request { } ContractCommand::Build(args) => { if !args.contract_manifest.exists() { - return Err(Error::PathNotFile( - args.contract_manifest.display().to_string(), - )); + return Err(eyre!("The contract manifest file does not exist: {}", args.contract_manifest.display())); } Ok(ContractBuildRequest { @@ -94,9 +91,9 @@ impl TryFrom for Request { } impl TryFrom for Request { - type Error = Error; +type Error = Report; - fn try_from(cmd: EnclaveCommand) -> Result { + fn try_from(cmd: EnclaveCommand) -> Result { match cmd { EnclaveCommand::Build(_) => Ok(EnclaveBuildRequest {}.into()), EnclaveCommand::Start(args) => Ok(EnclaveStartRequest { diff --git a/crates/cli/src/request/contract_deploy.rs b/crates/cli/src/request/contract_deploy.rs index c727e67a..d4cdec81 100644 --- a/crates/cli/src/request/contract_deploy.rs +++ b/crates/cli/src/request/contract_deploy.rs @@ -1,8 +1,10 @@ use std::{collections::HashMap, path::PathBuf}; +use color_eyre::eyre::Context; use serde::{Deserialize, Serialize}; -use crate::{error::Error, request::Request}; +use crate::request::Request; +use color_eyre::Result; #[derive(Clone, Debug)] pub struct ContractDeployRequest { @@ -18,10 +20,8 @@ impl From for Request { } impl ContractDeployRequest { - pub fn checked_init(init_msg: String) -> Result { - let parsed: GenericQuartzInit = serde_json::from_str(&init_msg).map_err(|_| { - Error::GenericErr("Init message doesn't contain mandatory quartz field.".to_string()) - })?; + pub fn checked_init(init_msg: String) -> Result { + let parsed: GenericQuartzInit = serde_json::from_str(&init_msg).wrap_err("Init message doesn't contain mandatory quartz field")?; Ok(parsed) } diff --git a/crates/cli/src/request/enclave_start.rs b/crates/cli/src/request/enclave_start.rs index 2946b8a6..ea0ba72e 100644 --- a/crates/cli/src/request/enclave_start.rs +++ b/crates/cli/src/request/enclave_start.rs @@ -2,9 +2,10 @@ use cosmrs::AccountId; use quartz_common::enclave::types::Fmspc; use tendermint::{block::Height, Hash}; use tracing::debug; +use color_eyre::Result; use crate::{ - config::Config, error::Error, handler::utils::helpers::query_latest_height_hash, + config::Config, handler::utils::helpers::query_latest_height_hash, request::Request, }; @@ -24,7 +25,7 @@ impl From for Request { impl EnclaveStartRequest { /// Returns the trusted hash and height - pub fn get_hash_height(&self, config: &Config) -> Result<(Height, Hash), Error> { + pub fn get_hash_height(&self, config: &Config) -> Result<(Height, Hash)> { if self.unsafe_trust_latest || config.trusted_height == 0 || config.trusted_hash.is_empty() { debug!("querying latest trusted hash & height from node"); @@ -37,9 +38,8 @@ impl EnclaveStartRequest { config.trusted_height.try_into()?, config .trusted_hash - .parse() - .map_err(|_| Error::GenericErr("invalid hash".to_string()))?, - )) + .parse()? + )) } } } From a9439c473ff026159d25c6d4597bd0ce513b2e4c Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:02:17 -0700 Subject: [PATCH 2/8] replace cli cw client's anyhow with eyre --- Cargo.lock | 1 + crates/utils/cw-client/Cargo.toml | 3 ++- crates/utils/cw-client/src/cli.rs | 24 ++++++++++++------------ examples/transfers/enclave/Cargo.lock | 1 + 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9f20c7a5..1696e807 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1285,6 +1285,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "color-eyre", "cosmos-sdk-proto", "cosmrs", "hex", diff --git a/crates/utils/cw-client/Cargo.toml b/crates/utils/cw-client/Cargo.toml index 7e9226a1..2c4c5242 100644 --- a/crates/utils/cw-client/Cargo.toml +++ b/crates/utils/cw-client/Cargo.toml @@ -12,12 +12,13 @@ authors.workspace = true path = "src/lib.rs" [dependencies] +anyhow.workspace = true async-trait.workspace = true +color-eyre.workspace = true hex.workspace = true reqwest.workspace = true serde.workspace = true serde_json.workspace = true -anyhow.workspace = true tonic.workspace = true cosmrs = { workspace = true, default-features = false, features = ["cosmwasm"] } diff --git a/crates/utils/cw-client/src/cli.rs b/crates/utils/cw-client/src/cli.rs index 8aea0721..1cd469f0 100644 --- a/crates/utils/cw-client/src/cli.rs +++ b/crates/utils/cw-client/src/cli.rs @@ -1,6 +1,6 @@ use std::process::Command; -use anyhow::anyhow; +use color_eyre::{eyre::eyre, Report}; use cosmrs::{tendermint::chain::Id, AccountId}; use reqwest::Url; use serde::de::DeserializeOwned; @@ -66,7 +66,7 @@ impl CwClient for CliClient { type Query = serde_json::Value; type RawQuery = String; type ChainId = Id; - type Error = anyhow::Error; + type Error = Report; async fn query_smart( &self, @@ -83,11 +83,11 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } let query_result: R = serde_json::from_slice(&output.stdout) - .map_err(|e| anyhow!("Error deserializing: {}", e))?; + .map_err(|e| eyre!("Error deserializing: {}", e))?; Ok(query_result) } @@ -106,7 +106,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } let query_result: R = serde_json::from_slice(&output.stdout).unwrap_or_default(); @@ -123,7 +123,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } let query_result: R = serde_json::from_slice(&output.stdout).unwrap_or_default(); @@ -154,7 +154,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } // TODO: find the rust type for the tx output and return that @@ -182,7 +182,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } // TODO: find the rust type for the tx output and return that @@ -215,7 +215,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } // TODO: find the rust type for the tx output and return that @@ -229,7 +229,7 @@ impl CwClient for CliClient { let output = command.output()?; if !output.status.success() { - return Err(anyhow!("{:?}", output)); + return Err(eyre!("{:?}", output)); } let query_result: serde_json::Value = @@ -241,13 +241,13 @@ impl CwClient for CliClient { }; let trusted_height = query_result[sync_info]["latest_block_height"] .as_str() - .ok_or(anyhow!("Could not query height"))?; + .ok_or(eyre!("Could not query height"))?; let trusted_height = trusted_height.parse::()?; let trusted_hash = query_result[sync_info]["latest_block_hash"] .as_str() - .ok_or(anyhow!("Could not query height"))? + .ok_or(eyre!("Could not query height"))? .to_string(); Ok((trusted_height, trusted_hash)) diff --git a/examples/transfers/enclave/Cargo.lock b/examples/transfers/enclave/Cargo.lock index fd26df5a..8db89875 100644 --- a/examples/transfers/enclave/Cargo.lock +++ b/examples/transfers/enclave/Cargo.lock @@ -950,6 +950,7 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", + "color-eyre", "cosmos-sdk-proto", "cosmrs", "hex", From 5f9486c0ac9e616f4b0c3a131dbfaa9219ceb38f Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:17:02 -0700 Subject: [PATCH 3/8] better error handling between dev and other commands --- crates/cli/src/handler/contract_deploy.rs | 1 - crates/cli/src/handler/dev.rs | 44 +++++++++-------------- crates/utils/cw-client/src/cli.rs | 32 +++++++++++------ examples/transfers/enclave/src/main.rs | 2 +- 4 files changed, 39 insertions(+), 40 deletions(-) diff --git a/crates/cli/src/handler/contract_deploy.rs b/crates/cli/src/handler/contract_deploy.rs index e5982c3d..226d5a7b 100644 --- a/crates/cli/src/handler/contract_deploy.rs +++ b/crates/cli/src/handler/contract_deploy.rs @@ -12,7 +12,6 @@ use color_eyre::{Result, Report, eyre::eyre}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ config::Config, - error::Error, handler::{utils::relay::RelayMessage, Handler}, request::contract_deploy::ContractDeployRequest, response::{contract_deploy::ContractDeployResponse, Response}, diff --git a/crates/cli/src/handler/dev.rs b/crates/cli/src/handler/dev.rs index ff3aa862..1fae0695 100644 --- a/crates/cli/src/handler/dev.rs +++ b/crates/cli/src/handler/dev.rs @@ -10,7 +10,6 @@ use watchexec_signals::Signal; use color_eyre::{Result, Report}; use crate::{ - error::Error, handler::{utils::helpers::wasmaddr_to_id, Handler}, request::{ contract_build::ContractBuildRequest, contract_deploy::ContractDeployRequest, @@ -80,13 +79,13 @@ async fn dev_driver( let contract_build = ContractBuildRequest { contract_manifest: args.contract_manifest.clone(), }; - contract_build.handle(&config).await?; + contract_build.handle(&config).await.wrap_err("Could not run `contract build`")?; // Start enclave in background spawn_enclave_start(args, &config)?; // Deploy new contract and perform handshake - let res = deploy_and_handshake(None, args, &config).await.wrap_err("Error running deploy and handshake in `quartz dev`"); + let res = deploy_and_handshake(None, args, &config).await; // Save resulting contract address or shutdown and return error match res { @@ -97,9 +96,7 @@ async fn dev_driver( info!("{}", "Enclave is listening for requests...".green().bold()); } Err(e) => { - eprintln!("Error launching quartz app"); - - return Err(e); + return Err(e).wrap_err("Error initializing `quartz dev`"); } } } @@ -130,9 +127,7 @@ async fn dev_driver( info!("{}", "Enclave is listening for requests...".green().bold()); } Err(e) => { - eprintln!("Error restarting enclave and handshake"); - - return Err(eyre!(e)); + return Err(e).wrap_err("Error restarting enclave after rebuild"); } } } @@ -152,7 +147,7 @@ async fn dev_driver( Err(e) => { eprintln!("Error deploying contract and handshake:"); - return Err(eyre!(e)); + return Err(e).wrap_err("Error redeploying contract after rebuild"); } } @@ -178,7 +173,7 @@ fn spawn_enclave_start(args: &DevRequest, config: &Config) -> Result<()> { tokio::spawn(async move { if let Err(e) = enclave_start.handle(config_cpy).await { - eprintln!("Error running enclave start.\n {}", e); + eprintln!("Error running enclave start.\n {:?}", e); } }); @@ -225,13 +220,12 @@ async fn deploy_and_handshake( contract_manifest: args.contract_manifest.clone(), }; // Call handler - let cd_res = contract_deploy.handle(config).await; + let cd_res = contract_deploy.handle(config).await.wrap_err("Could not run `quartz contract deploy`")?; - // Return contract address or shutdown enclave & error - match cd_res { - Ok(Response::ContractDeploy(res)) => res.contract_addr, - Err(e) => return Err(e), - _ => unreachable!("Unexpected response variant"), + if let Response::ContractDeploy(res) = cd_res { + res.contract_addr + } else { + unreachable!("Unexpected response variant") } }; @@ -242,17 +236,11 @@ async fn deploy_and_handshake( unsafe_trust_latest: args.unsafe_trust_latest, }; - let h_res = handshake.handle(config).await; - - match h_res { - Ok(Response::Handshake(res)) => { - info!("Handshake complete: {}", res.pub_key); - } - Err(e) => { - return Err(eyre!(e)); - } - _ => unreachable!("Unexpected response variant"), - }; + let h_res = handshake.handle(config).await.wrap_err("Could not run `quartz handshake`")?; + println!("got here"); + if let Response::Handshake(res) = h_res { + info!("Handshake complete: {}", res.pub_key); + } Ok(contract) } diff --git a/crates/utils/cw-client/src/cli.rs b/crates/utils/cw-client/src/cli.rs index 1cd469f0..cc06d493 100644 --- a/crates/utils/cw-client/src/cli.rs +++ b/crates/utils/cw-client/src/cli.rs @@ -1,6 +1,6 @@ use std::process::Command; -use color_eyre::{eyre::eyre, Report}; +use color_eyre::{eyre::eyre, Report, Result, Help}; use cosmrs::{tendermint::chain::Id, AccountId}; use reqwest::Url; use serde::de::DeserializeOwned; @@ -55,8 +55,20 @@ impl CliClient { } } - fn new_command(&self) -> Command { - Command::new(self.kind.bin()) + fn new_command(&self) -> Result { + let bin = self.kind.bin(); + if !self.is_bin_available(&bin) { + return Err(eyre!("Binary '{}' not found in PATH", bin)).suggestion(format!("Have you installed {}? If so, check that it's in your PATH.", bin)); + } + + Ok(Command::new(self.kind.bin())) + } + fn is_bin_available(&self, bin: &str) -> bool { + Command::new("which") + .arg(bin) + .output() + .map(|output| output.status.success()) + .unwrap_or(false) } } @@ -73,7 +85,7 @@ impl CwClient for CliClient { contract: &Self::Address, query: Self::Query, ) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["query", "wasm"]) @@ -96,7 +108,7 @@ impl CwClient for CliClient { contract: &Self::Address, query: Self::RawQuery, ) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["query", "wasm"]) @@ -114,7 +126,7 @@ impl CwClient for CliClient { } fn query_tx(&self, txhash: &str) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["query", "tx"]) @@ -139,7 +151,7 @@ impl CwClient for CliClient { msg: M, fees: &str, ) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["--chain-id", chain_id.as_ref()]) @@ -167,7 +179,7 @@ impl CwClient for CliClient { sender: &str, wasm_path: M, ) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["tx", "wasm", "store", &wasm_path.to_string()]) @@ -197,7 +209,7 @@ impl CwClient for CliClient { init_msg: M, label: &str, ) -> Result { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command .args(["--node", self.url.as_str()]) .args(["tx", "wasm", "instantiate"]) @@ -223,7 +235,7 @@ impl CwClient for CliClient { } fn trusted_height_hash(&self) -> Result<(u64, String), Self::Error> { - let mut command = self.new_command(); + let mut command = self.new_command()?; let command = command.args(["--node", self.url.as_str()]).arg("status"); let output = command.output()?; diff --git a/examples/transfers/enclave/src/main.rs b/examples/transfers/enclave/src/main.rs index 20892f09..2547f113 100644 --- a/examples/transfers/enclave/src/main.rs +++ b/examples/transfers/enclave/src/main.rs @@ -41,7 +41,7 @@ use crate::wslistener::WsListener; async fn main() -> Result<(), Box> { let args = Cli::parse(); - let admin_sk = std::env::var("ADMIN_SK")?; + let admin_sk = std::env::var("ADMIN_SK").map_err(|_| anyhow::anyhow!("Admin secret key not found in env vars"))?; let light_client_opts = LightClientOpts::new( args.chain_id.clone(), From b0ad8797d08d45cdbbb37fcb7b3f5980891c05df Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:17:34 -0700 Subject: [PATCH 4/8] remove error file and all other instances --- crates/cli/src/cache.rs | 13 +++++---- crates/cli/src/error.rs | 28 ------------------ crates/cli/src/handler/contract_build.rs | 1 - crates/cli/src/handler/enclave_build.rs | 1 - crates/cli/src/handler/enclave_start.rs | 37 +++++++++--------------- crates/cli/src/handler/handshake.rs | 2 -- crates/cli/src/handler/init.rs | 1 - crates/cli/src/handler/utils/helpers.rs | 4 +-- crates/cli/src/handler/utils/relay.rs | 13 ++++----- crates/cli/src/main.rs | 1 - crates/cli/src/request.rs | 28 +++++++++++------- crates/cli/src/request/init.rs | 14 ++++----- 12 files changed, 52 insertions(+), 91 deletions(-) delete mode 100644 crates/cli/src/error.rs diff --git a/crates/cli/src/cache.rs b/crates/cli/src/cache.rs index e2bca288..e2704bf6 100644 --- a/crates/cli/src/cache.rs +++ b/crates/cli/src/cache.rs @@ -8,7 +8,7 @@ use tokio::{ use tracing::debug; use xxhash_rust::xxh3::Xxh3; -use color_eyre::{eyre::eyre, Report, Result}; +use color_eyre::{eyre::eyre, Result}; use crate::config::Config; const BUFFER_SIZE: usize = 16384; // 16 KB buffer @@ -116,13 +116,16 @@ impl Config { .await .map_err(|e| eyre!(e)) } - pub fn cache_dir(&self) -> Result { - Ok(self.app_dir.join(".cache/")) + let cache_dir = self.app_dir.join(".cache/"); + std::fs::create_dir_all(&cache_dir)?; + Ok(cache_dir) } pub fn build_log_dir(&self) -> Result { - Ok(self.app_dir.join(".cache/log/")) + let build_log_dir = self.app_dir.join(".cache/log/"); + std::fs::create_dir_all(&build_log_dir)?; + Ok(build_log_dir) } /// Creates the build log if it isn't created already, returns relative path from app_dir to log directory @@ -144,7 +147,7 @@ impl Config { false => "contract", }; - tokio::fs::write(log_dir.join(filename), "test") + tokio::fs::write(log_dir.join(filename), "log") .await?; Ok(()) diff --git a/crates/cli/src/error.rs b/crates/cli/src/error.rs deleted file mode 100644 index c9943cc9..00000000 --- a/crates/cli/src/error.rs +++ /dev/null @@ -1,28 +0,0 @@ -use displaydoc::Display; -use thiserror::Error; - -#[derive(Debug, Display, Error)] -pub enum Error { - /// Specified path `{0}` is not a directory - PathNotDir(String), - /// Specified file `{0}` does not exist - PathNotFile(String), - /// unspecified error: {0} - GenericErr(String), - /// Cache error: {0} - Cache(String), - /// Config error: {0} - Config(String), - /// TOML Error : {0} - TomlError(#[from] toml::de::Error), - /// TOML Error : {0} - TomlSerError(#[from] toml::ser::Error), - /// Tendermint error: {0} - TendermintError(#[from] tendermint::Error), - /// Clearscreen error: {0} - ClearscreenError(#[from] clearscreen::Error), - /// JSON Error: {0} - JsonError(#[from] serde_json::Error), - /// IO Error: {0} - IoError(#[from] std::io::Error), -} diff --git a/crates/cli/src/handler/contract_build.rs b/crates/cli/src/handler/contract_build.rs index 985bac23..3340a015 100644 --- a/crates/cli/src/handler/contract_build.rs +++ b/crates/cli/src/handler/contract_build.rs @@ -6,7 +6,6 @@ use tracing::{debug, info}; use crate::{ config::Config, - error::Error, handler::Handler, request::contract_build::ContractBuildRequest, response::{contract_build::ContractBuildResponse, Response}, diff --git a/crates/cli/src/handler/enclave_build.rs b/crates/cli/src/handler/enclave_build.rs index ccf8e6d5..3183662e 100644 --- a/crates/cli/src/handler/enclave_build.rs +++ b/crates/cli/src/handler/enclave_build.rs @@ -6,7 +6,6 @@ use color_eyre::{Result, Report}; use crate::{ config::Config, - error::Error, handler::Handler, request::enclave_build::EnclaveBuildRequest, response::{enclave_build::EnclaveBuildResponse, Response}, diff --git a/crates/cli/src/handler/enclave_start.rs b/crates/cli/src/handler/enclave_start.rs index 6d0406ec..bba22859 100644 --- a/crates/cli/src/handler/enclave_start.rs +++ b/crates/cli/src/handler/enclave_start.rs @@ -13,7 +13,6 @@ use color_eyre::{Result, Report}; use crate::{ config::Config, - error::Error, handler::{utils::helpers::write_cache_hash_height, Handler}, request::enclave_start::EnclaveStartRequest, response::{enclave_start::EnclaveStartResponse, Response}, @@ -160,18 +159,13 @@ async fn create_mock_enclave_child( Ok(child) } -async fn gramine_sgx_gen_private_key(enclave_dir: &Path) -> Result<(), Error> { +async fn gramine_sgx_gen_private_key(enclave_dir: &Path) -> Result<()> { // Launch the gramine-sgx-gen-private-key command Command::new("gramine-sgx-gen-private-key") .current_dir(enclave_dir) .output() .await - .map_err(|e| { - Error::GenericErr(format!( - "Failed to execute gramine-sgx-gen-private-key: {}", - e - )) - })?; + .map_err(|e| eyre!("Failed to execute gramine-sgx-gen-private-key: {}", e))?; // Continue regardless of error // > /dev/null 2>&1 || : # may fail @@ -191,7 +185,7 @@ async fn gramine_manifest( node_url: &Url, ws_url: &Url, grpc_url: &Url, -) -> Result<(), Error> { +) -> Result<()> { let host = target_lexicon::HOST; let arch_libdir = format!( "/lib/{}-{}-{}", @@ -200,7 +194,7 @@ async fn gramine_manifest( let ra_client_spid = "51CAF5A48B450D624AEFE3286D314894"; let home_dir = dirs::home_dir() - .ok_or(Error::GenericErr("home dir not set".to_string()))? + .ok_or_else(|| eyre!("Home directory not set"))? .display() .to_string(); @@ -229,19 +223,16 @@ async fn gramine_manifest( .current_dir(enclave_dir) .status() .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .map_err(|e| eyre!("Failed to execute gramine-manifest: {}", e))?; if !status.success() { - return Err(Error::GenericErr(format!( - "Couldn't run gramine manifest. {:?}", - status - ))); + return Err(eyre!("gramine-manifest command failed with status: {:?}", status)); } Ok(()) } -async fn gramine_sgx_sign(enclave_dir: &Path) -> Result<(), Error> { +async fn gramine_sgx_sign(enclave_dir: &Path) -> Result<()> { let status = Command::new("gramine-sgx-sign") .arg("--manifest") .arg("quartz.manifest") @@ -250,26 +241,24 @@ async fn gramine_sgx_sign(enclave_dir: &Path) -> Result<(), Error> { .current_dir(enclave_dir) .status() .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .map_err(|e| eyre!("Failed to execute gramine-sgx-sign: {}", e))?; if !status.success() { - return Err(Error::GenericErr(format!( - "gramine-sgx-sign command failed. {:?}", - status - ))); + return Err(eyre!("gramine-sgx-sign command failed with status: {:?}", status)); } Ok(()) } -async fn create_gramine_sgx_child(enclave_dir: &Path) -> Result { +async fn create_gramine_sgx_child(enclave_dir: &Path) -> Result { info!("🚧 Spawning enclave process ..."); let child = Command::new("gramine-sgx") .arg("./quartz") .kill_on_drop(true) .current_dir(enclave_dir) - .spawn()?; + .spawn() + .map_err(|e| eyre!("Failed to spawn gramine-sgx child process: {}", e))?; Ok(child) -} +} \ No newline at end of file diff --git a/crates/cli/src/handler/handshake.rs b/crates/cli/src/handler/handshake.rs index 9bf0329b..6edaf6bb 100644 --- a/crates/cli/src/handler/handshake.rs +++ b/crates/cli/src/handler/handshake.rs @@ -1,4 +1,3 @@ -use anyhow::anyhow; use async_trait::async_trait; use color_eyre::{eyre::eyre, owo_colors::OwoColorize}; use cw_client::{CliClient, CwClient}; @@ -12,7 +11,6 @@ use color_eyre::{Result, Report}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ config::Config, - error::Error, handler::{ utils::{helpers::read_cached_hash_height, relay::RelayMessage}, Handler, diff --git a/crates/cli/src/handler/init.rs b/crates/cli/src/handler/init.rs index e76d6361..ace1c6aa 100644 --- a/crates/cli/src/handler/init.rs +++ b/crates/cli/src/handler/init.rs @@ -9,7 +9,6 @@ use color_eyre::{Result, Report}; use crate::{ config::Config, - error::Error, handler::Handler, request::init::InitRequest, response::{init::InitResponse, Response}, diff --git a/crates/cli/src/handler/utils/helpers.rs b/crates/cli/src/handler/utils/helpers.rs index 19a7bfc5..654fed52 100644 --- a/crates/cli/src/handler/utils/helpers.rs +++ b/crates/cli/src/handler/utils/helpers.rs @@ -11,7 +11,7 @@ use tendermint_rpc::{ }; use tokio::fs::{self}; use tracing::debug; -use color_eyre::{eyre::eyre, Report, Result, eyre::WrapErr}; +use color_eyre::{eyre::eyre, Result, eyre::WrapErr}; use crate::config::Config; @@ -56,7 +56,7 @@ pub async fn block_tx_commit(client: &HttpClient, tx: Hash) -> Result Result<(Height, Hash)> { - let cw_client = CliClient::wasmd(node_url); + let cw_client = CliClient::neutrond(node_url); let (trusted_height, trusted_hash) = cw_client .trusted_height_hash() diff --git a/crates/cli/src/handler/utils/relay.rs b/crates/cli/src/handler/utils/relay.rs index 01011e3c..d7d47984 100644 --- a/crates/cli/src/handler/utils/relay.rs +++ b/crates/cli/src/handler/utils/relay.rs @@ -3,8 +3,7 @@ use quartz_common::proto::{ }; use quartz_tm_prover::config::ProofOutput; use serde_json::{json, Value as JsonValue}; - -use crate::error::Error; +use color_eyre::{Result, eyre::eyre}; #[derive(Debug)] pub enum RelayMessage { @@ -14,17 +13,17 @@ pub enum RelayMessage { } impl RelayMessage { - pub async fn run_relay(self, enclave_rpc: String) -> Result { + pub async fn run_relay(self, enclave_rpc: String) -> Result { // Query the gRPC quartz enclave service let mut qc_client = CoreClient::connect(enclave_rpc) .await - .map_err(|e| Error::GenericErr(e.to_string()))?; + .map_err(|e| eyre!("Failed to connect to the gRPC quartz enclave service: {}", e))?; let attested_msg = match self { RelayMessage::Instantiate { mut init_msg } => qc_client .instantiate(tonic::Request::new(InstantiateRequest {})) .await - .map_err(|e| Error::GenericErr(e.to_string())) + .map_err(|e| eyre!("Failed to instantiate via gRPC quartz enclave service: {}", e)) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| { init_msg["quartz"] = msg; @@ -33,7 +32,7 @@ impl RelayMessage { RelayMessage::SessionCreate => qc_client .session_create(tonic::Request::new(SessionCreateRequest {})) .await - .map_err(|e| Error::GenericErr(e.to_string())) + .map_err(|e| eyre!("Failed to create session via gRPC quartz enclave service: {}", e)) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| json!({ "quartz": {"session_create": msg}}).to_string())?, RelayMessage::SessionSetPubKey { proof } => qc_client @@ -41,7 +40,7 @@ impl RelayMessage { message: serde_json::to_string(&proof)?, }) .await - .map_err(|e| Error::GenericErr(e.to_string())) + .map_err(|e| eyre!("Failed to set public key via gRPC quartz enclave service: {}", e)) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| json!({ "quartz": {"session_set_pub_key": msg}}).to_string())?, }; diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 7e06b8ae..6d1e98dc 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -16,7 +16,6 @@ pub mod cache; pub mod cli; pub mod config; -pub mod error; pub mod handler; pub mod request; pub mod response; diff --git a/crates/cli/src/request.rs b/crates/cli/src/request.rs index e6a76212..9bd0d939 100644 --- a/crates/cli/src/request.rs +++ b/crates/cli/src/request.rs @@ -43,18 +43,24 @@ impl TryFrom for Request { .into()), Command::Contract { contract_command } => contract_command.try_into(), Command::Enclave { enclave_command } => enclave_command.try_into(), - Command::Dev(args) => Ok(DevRequest { - watch: args.watch, - unsafe_trust_latest: args.unsafe_trust_latest, - init_msg: serde_json::from_str(&args.contract_deploy.init_msg)?, - label: args.contract_deploy.label, - contract_manifest: args.contract_deploy.contract_manifest, - release: args.enclave_build.release, - fmspc: args.fmspc, - tcbinfo_contract: args.tcbinfo_contract, - dcap_verifier_contract: args.dcap_verifier_contract, + Command::Dev(args) => { + if !args.contract_deploy.contract_manifest.exists() { + return Err(eyre!("The contract manifest file does not exist: {}", args.contract_deploy.contract_manifest.display())); + } + + Ok(DevRequest { + watch: args.watch, + unsafe_trust_latest: args.unsafe_trust_latest, + contract_manifest: args.contract_deploy.contract_manifest, + init_msg: serde_json::from_str(&args.contract_deploy.init_msg)?, + label: args.contract_deploy.label, + release: args.enclave_build.release, + fmspc: args.fmspc, + tcbinfo_contract: args.tcbinfo_contract, + dcap_verifier_contract: args.dcap_verifier_contract, + } + .into()) } - .into()), } } } diff --git a/crates/cli/src/request/init.rs b/crates/cli/src/request/init.rs index 51f5ee95..990b3730 100644 --- a/crates/cli/src/request/init.rs +++ b/crates/cli/src/request/init.rs @@ -1,6 +1,7 @@ use std::path::PathBuf; -use crate::{error::Error, request::Request}; +use crate::request::Request; +use color_eyre::{Result, eyre::eyre, Report}; #[derive(Clone, Debug)] pub struct InitRequest { @@ -8,16 +9,13 @@ pub struct InitRequest { } impl TryFrom for Request { - type Error = Error; + type Error = Report; - fn try_from(request: InitRequest) -> Result { + fn try_from(request: InitRequest) -> Result { if request.name.extension().is_some() { - return Err(Error::PathNotDir(format!("{}", request.name.display()))); + return Err(eyre!("Path is not a directory: {}", request.name.display())); } else if request.name.exists() { - return Err(Error::GenericErr(format!( - "Directory already exists: {}", - request.name.display() - ))); + return Err(eyre!("Directory already exists: {}", request.name.display())); } Ok(Request::Init(request)) From e1a96e1eac21ca303549c8f45570e460338b26b5 Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:46:34 -0700 Subject: [PATCH 5/8] fmt --- crates/cli/src/cache.rs | 25 ++++----- crates/cli/src/handler.rs | 12 ++--- crates/cli/src/handler/contract_build.rs | 18 ++----- crates/cli/src/handler/contract_deploy.rs | 64 ++++++++++++++--------- crates/cli/src/handler/dev.rs | 27 ++++++---- crates/cli/src/handler/enclave_build.rs | 17 ++---- crates/cli/src/handler/enclave_start.rs | 44 +++++++++------- crates/cli/src/handler/handshake.rs | 17 +++--- crates/cli/src/handler/init.rs | 8 +-- crates/cli/src/handler/utils/helpers.rs | 17 ++++-- crates/cli/src/handler/utils/relay.rs | 32 +++++++++--- crates/cli/src/main.rs | 5 +- crates/cli/src/request.rs | 21 +++++--- crates/cli/src/request/contract_deploy.rs | 6 +-- crates/cli/src/request/enclave_start.rs | 13 ++--- crates/cli/src/request/init.rs | 8 ++- 16 files changed, 182 insertions(+), 152 deletions(-) diff --git a/crates/cli/src/cache.rs b/crates/cli/src/cache.rs index e2704bf6..1fde2843 100644 --- a/crates/cli/src/cache.rs +++ b/crates/cli/src/cache.rs @@ -1,5 +1,6 @@ use std::path::{Path, PathBuf}; +use color_eyre::{eyre::eyre, Result}; use serde::{Deserialize, Serialize}; use tokio::{ fs::File, @@ -8,7 +9,6 @@ use tokio::{ use tracing::debug; use xxhash_rust::xxh3::Xxh3; -use color_eyre::{eyre::eyre, Result}; use crate::config::Config; const BUFFER_SIZE: usize = 16384; // 16 KB buffer @@ -41,8 +41,7 @@ impl Config { /// Return a hash of the given file's contents pub async fn gen_hash(file: &Path) -> Result { - let file = File::open(file) - .await?; + let file = File::open(file).await?; let mut reader = BufReader::new(file); @@ -50,10 +49,8 @@ impl Config { let mut buffer = [0; BUFFER_SIZE]; loop { - let bytes_read = reader - .read(&mut buffer) - .await?; - + let bytes_read = reader.read(&mut buffer).await?; + if bytes_read == 0 { break; } @@ -90,7 +87,10 @@ impl Config { // Get cache filepath (".quartz/cache/example.wasm.json") from "example.wasm" filepath let mut filename = file .file_name() - .ok_or(eyre!("file at cache filepath does not exist {}", file.display()))? + .ok_or(eyre!( + "file at cache filepath does not exist {}", + file.display() + ))? .to_os_string(); filename.push(".json"); @@ -102,8 +102,7 @@ impl Config { /// Retreive hash from cache file async fn read_from_cache(cache_file: &Path) -> Result { - let content = tokio::fs::read_to_string(cache_file) - .await?; + let content = tokio::fs::read_to_string(cache_file).await?; serde_json::from_str(&content).map_err(|e| eyre!(e)) } @@ -132,8 +131,7 @@ impl Config { pub async fn create_build_log(&self) -> Result { let log_dir = Self::build_log_dir(self)?; if !log_dir.exists() { - tokio::fs::create_dir_all(&log_dir) - .await?; + tokio::fs::create_dir_all(&log_dir).await?; } Ok(log_dir) @@ -147,8 +145,7 @@ impl Config { false => "contract", }; - tokio::fs::write(log_dir.join(filename), "log") - .await?; + tokio::fs::write(log_dir.join(filename), "log").await?; Ok(()) } diff --git a/crates/cli/src/handler.rs b/crates/cli/src/handler.rs index b5bd801a..be800c96 100644 --- a/crates/cli/src/handler.rs +++ b/crates/cli/src/handler.rs @@ -1,7 +1,7 @@ use async_trait::async_trait; +use color_eyre::{Report, Result}; use crate::{config::Config, request::Request, response::Response}; -use color_eyre::{Result, Report}; pub mod utils; // commands @@ -17,20 +17,14 @@ pub mod init; pub trait Handler { type Response; - async fn handle + Send>( - self, - config: C, - ) -> Result; + async fn handle + Send>(self, config: C) -> Result; } #[async_trait] impl Handler for Request { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { match self { Request::Init(request) => request.handle(config).await, Request::Handshake(request) => request.handle(config).await, diff --git a/crates/cli/src/handler/contract_build.rs b/crates/cli/src/handler/contract_build.rs index 3340a015..123d733a 100644 --- a/crates/cli/src/handler/contract_build.rs +++ b/crates/cli/src/handler/contract_build.rs @@ -1,7 +1,7 @@ use std::process::Command; use async_trait::async_trait; -use color_eyre::owo_colors::OwoColorize; +use color_eyre::{eyre::eyre, owo_colors::OwoColorize, Report, Result}; use tracing::{debug, info}; use crate::{ @@ -10,16 +10,12 @@ use crate::{ request::contract_build::ContractBuildRequest, response::{contract_build::ContractBuildResponse, Response}, }; -use color_eyre::{Result, Report, eyre::eyre}; #[async_trait] impl Handler for ContractBuildRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Contract Build".blue().bold()); @@ -45,14 +41,10 @@ impl Handler for ContractBuildRequest { } info!("{}", "🚧 Building contract binary ...".green().bold()); - let status = command - .status()?; - + let status = command.status()?; + if !status.success() { - return Err(eyre!( - "Couldn't build contract. \n{:?}", - status - )); + return Err(eyre!("Couldn't build contract. \n{:?}", status)); } config.log_build(false).await?; diff --git a/crates/cli/src/handler/contract_deploy.rs b/crates/cli/src/handler/contract_deploy.rs index 226d5a7b..76ca6d6d 100644 --- a/crates/cli/src/handler/contract_deploy.rs +++ b/crates/cli/src/handler/contract_deploy.rs @@ -2,12 +2,15 @@ use std::path::Path; use async_trait::async_trait; use cargo_metadata::MetadataCommand; -use color_eyre::{eyre::Context, owo_colors::OwoColorize}; +use color_eyre::{ + eyre::{eyre, Context}, + owo_colors::OwoColorize, + Report, Result, +}; use cw_client::{CliClient, CwClient}; use serde_json::json; use tendermint_rpc::HttpClient; use tracing::{debug, info}; -use color_eyre::{Result, Report, eyre::eyre}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ @@ -21,10 +24,7 @@ use crate::{ impl Handler for ContractDeployRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Contract Deploy".blue().bold()); @@ -44,8 +44,7 @@ impl Handler for ContractDeployRequest { .join(package_name) .with_extension("wasm"); - let (code_id, contract_addr) = deploy(wasm_bin_path.as_path(), self, config) - .await?; + let (code_id, contract_addr) = deploy(wasm_bin_path.as_path(), self, config).await?; Ok(ContractDeployResponse { code_id, @@ -65,12 +64,17 @@ async fn deploy( info!("🚀 Deploying {} Contract", args.label); let code_id = if config.contract_has_changed(wasm_bin_path).await? { - let deploy_output: WasmdTxResponse = serde_json::from_str(&cw_client.deploy( - &config.chain_id, - &config.tx_sender, - wasm_bin_path.display().to_string(), - ).map_err(|err| eyre!(Box::new(err)))?).wrap_err("Error calling deploy on cw client")?; - + let deploy_output: WasmdTxResponse = serde_json::from_str( + &cw_client + .deploy( + &config.chain_id, + &config.tx_sender, + wasm_bin_path.display().to_string(), + ) + .map_err(|err| eyre!(Box::new(err)))?, + ) + .wrap_err("Error calling deploy on cw client")?; + let res = block_tx_commit(&tmrpc_client, deploy_output.txhash).await?; // Find the 'code_id' attribute @@ -90,11 +94,17 @@ async fn deploy( info!("Code ID: {}", code_id); - config.save_codeid_to_cache(wasm_bin_path, code_id).await.wrap_err("Error saving contract code id to cache")?; + config + .save_codeid_to_cache(wasm_bin_path, code_id) + .await + .wrap_err("Error saving contract code id to cache")?; code_id } else { - config.get_cached_codeid(wasm_bin_path).await.wrap_err("Error getting contract code id from cache")? + config + .get_cached_codeid(wasm_bin_path) + .await + .wrap_err("Error getting contract code id from cache")? }; info!("🚀 Communicating with Relay to Instantiate..."); @@ -106,13 +116,17 @@ async fn deploy( info!("🚀 Instantiating {}", args.label); - let init_output: WasmdTxResponse = serde_json::from_str(&cw_client.init( - &config.chain_id, - &config.tx_sender, - code_id, - json!(init_msg), - &format!("{} Contract #{}", args.label, code_id), - ).map_err(|err| eyre!(Box::new(err)))?)?; // TODO: change underlying error type to be eyre instead of anyhow + let init_output: WasmdTxResponse = serde_json::from_str( + &cw_client + .init( + &config.chain_id, + &config.tx_sender, + code_id, + json!(init_msg), + &format!("{} Contract #{}", args.label, code_id), + ) + .map_err(|err| eyre!(Box::new(err)))?, + )?; // TODO: change underlying error type to be eyre instead of anyhow let res = block_tx_commit(&tmrpc_client, init_output.txhash).await?; @@ -129,9 +143,7 @@ async fn deploy( .find(|attr| attr.key_str().unwrap_or("") == "_contract_address") }) .and_then(|attr| attr.value_str().ok().and_then(|v| v.parse().ok())) - .ok_or_else(|| { - eyre!("Failed to find contract_address in the transaction result") - })?; + .ok_or_else(|| eyre!("Failed to find contract_address in the transaction result"))?; info!("🚀 Successfully deployed and instantiated contract!"); info!("🆔 Code ID: {}", code_id); diff --git a/crates/cli/src/handler/dev.rs b/crates/cli/src/handler/dev.rs index 1fae0695..7caa2b09 100644 --- a/crates/cli/src/handler/dev.rs +++ b/crates/cli/src/handler/dev.rs @@ -1,13 +1,16 @@ use std::{path::PathBuf, time::Duration}; use async_trait::async_trait; -use color_eyre::{eyre::{eyre, Context}, owo_colors::OwoColorize}; +use color_eyre::{ + eyre::{eyre, Context}, + owo_colors::OwoColorize, + Report, Result, +}; use quartz_common::proto::core_client::CoreClient; use tokio::{sync::mpsc, time::sleep}; use tracing::{debug, info}; use watchexec::Watchexec; use watchexec_signals::Signal; -use color_eyre::{Result, Report}; use crate::{ handler::{utils::helpers::wasmaddr_to_id, Handler}, @@ -24,10 +27,7 @@ use crate::{ impl Handler for DevRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref(); info!("\nPeforming Dev"); @@ -79,7 +79,10 @@ async fn dev_driver( let contract_build = ContractBuildRequest { contract_manifest: args.contract_manifest.clone(), }; - contract_build.handle(&config).await.wrap_err("Could not run `contract build`")?; + contract_build + .handle(&config) + .await + .wrap_err("Could not run `contract build`")?; // Start enclave in background spawn_enclave_start(args, &config)?; @@ -220,7 +223,10 @@ async fn deploy_and_handshake( contract_manifest: args.contract_manifest.clone(), }; // Call handler - let cd_res = contract_deploy.handle(config).await.wrap_err("Could not run `quartz contract deploy`")?; + let cd_res = contract_deploy + .handle(config) + .await + .wrap_err("Could not run `quartz contract deploy`")?; if let Response::ContractDeploy(res) = cd_res { res.contract_addr @@ -236,7 +242,10 @@ async fn deploy_and_handshake( unsafe_trust_latest: args.unsafe_trust_latest, }; - let h_res = handshake.handle(config).await.wrap_err("Could not run `quartz handshake`")?; + let h_res = handshake + .handle(config) + .await + .wrap_err("Could not run `quartz handshake`")?; println!("got here"); if let Response::Handshake(res) = h_res { info!("Handshake complete: {}", res.pub_key); diff --git a/crates/cli/src/handler/enclave_build.rs b/crates/cli/src/handler/enclave_build.rs index 3183662e..36654722 100644 --- a/crates/cli/src/handler/enclave_build.rs +++ b/crates/cli/src/handler/enclave_build.rs @@ -1,8 +1,7 @@ use async_trait::async_trait; -use color_eyre::{eyre::eyre, owo_colors::OwoColorize}; +use color_eyre::{eyre::eyre, owo_colors::OwoColorize, Report, Result}; use tokio::process::Command; use tracing::{debug, info}; -use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -15,10 +14,7 @@ use crate::{ impl Handler for EnclaveBuildRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Enclave Build".blue().bold()); @@ -42,15 +38,10 @@ impl Handler for EnclaveBuildRequest { } info!("{}", "🚧 Running build command ...".green().bold()); - let status = command - .status() - .await?; + let status = command.status().await?; if !status.success() { - return Err(eyre!( - "Couldn't build enclave. {:?}", - status - )); + return Err(eyre!("Couldn't build enclave. {:?}", status)); } config.log_build(true).await?; diff --git a/crates/cli/src/handler/enclave_start.rs b/crates/cli/src/handler/enclave_start.rs index bba22859..617220a5 100644 --- a/crates/cli/src/handler/enclave_start.rs +++ b/crates/cli/src/handler/enclave_start.rs @@ -2,14 +2,17 @@ use std::{fs, path::Path}; use async_trait::async_trait; use cargo_metadata::MetadataCommand; -use color_eyre::{eyre::{eyre, Context}, owo_colors::OwoColorize}; +use color_eyre::{ + eyre::{eyre, Context}, + owo_colors::OwoColorize, + Report, Result, +}; use cosmrs::AccountId; use quartz_common::enclave::types::Fmspc; use reqwest::Url; use tendermint::chain::Id; use tokio::process::{Child, Command}; use tracing::{debug, info}; -use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -22,15 +25,14 @@ use crate::{ impl Handler for EnclaveStartRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref().clone(); info!("{}", "\nPeforming Enclave Start".blue().bold()); // Get trusted height and hash - let (trusted_height, trusted_hash) = self.get_hash_height(&config).wrap_err("Error getting trusted hash and height")?; + let (trusted_height, trusted_hash) = self + .get_hash_height(&config) + .wrap_err("Error getting trusted hash and height")?; write_cache_hash_height(trusted_height, trusted_hash, &config).await?; if config.mock_sgx { @@ -66,10 +68,12 @@ impl Handler for EnclaveStartRequest { }; let Some(dcap_verifier_contract) = self.dcap_verifier_contract else { - return Err(eyre!("dcap_verifier_contract is required if MOCK_SGX isn't set")); + return Err(eyre!( + "dcap_verifier_contract is required if MOCK_SGX isn't set" + )); }; - if let Err(_) = std::env::var("ADMIN_SK") { + if std::env::var("ADMIN_SK").is_err() { return Err(eyre!("ADMIN_SK environment variable is not set")); }; @@ -111,10 +115,8 @@ impl Handler for EnclaveStartRequest { } async fn handle_process(mut child: Child) -> Result<()> { - let status = child - .wait() - .await?; - + let status = child.wait().await?; + if !status.success() { return Err(eyre!("Couldn't build enclave. {:?}", status)); } @@ -152,9 +154,7 @@ async fn create_mock_enclave_child( debug!("Enclave Start Command: {:?}", command); info!("{}", "🚧 Spawning enclave process ...".green().bold()); - let child = command - .kill_on_drop(true) - .spawn()?; + let child = command.kill_on_drop(true).spawn()?; Ok(child) } @@ -226,7 +226,10 @@ async fn gramine_manifest( .map_err(|e| eyre!("Failed to execute gramine-manifest: {}", e))?; if !status.success() { - return Err(eyre!("gramine-manifest command failed with status: {:?}", status)); + return Err(eyre!( + "gramine-manifest command failed with status: {:?}", + status + )); } Ok(()) @@ -244,7 +247,10 @@ async fn gramine_sgx_sign(enclave_dir: &Path) -> Result<()> { .map_err(|e| eyre!("Failed to execute gramine-sgx-sign: {}", e))?; if !status.success() { - return Err(eyre!("gramine-sgx-sign command failed with status: {:?}", status)); + return Err(eyre!( + "gramine-sgx-sign command failed with status: {:?}", + status + )); } Ok(()) @@ -261,4 +267,4 @@ async fn create_gramine_sgx_child(enclave_dir: &Path) -> Result { .map_err(|e| eyre!("Failed to spawn gramine-sgx child process: {}", e))?; Ok(child) -} \ No newline at end of file +} diff --git a/crates/cli/src/handler/handshake.rs b/crates/cli/src/handler/handshake.rs index 6edaf6bb..c23b93c5 100644 --- a/crates/cli/src/handler/handshake.rs +++ b/crates/cli/src/handler/handshake.rs @@ -1,12 +1,11 @@ use async_trait::async_trait; -use color_eyre::{eyre::eyre, owo_colors::OwoColorize}; +use color_eyre::{eyre::eyre, owo_colors::OwoColorize, Report, Result}; use cw_client::{CliClient, CwClient}; use futures_util::stream::StreamExt; use quartz_tm_prover::{config::Config as TmProverConfig, prover::prove}; use serde_json::json; use tendermint_rpc::{query::EventType, HttpClient, SubscriptionClient, WebSocketClient}; use tracing::{debug, info}; -use color_eyre::{Result, Report}; use super::utils::{helpers::block_tx_commit, types::WasmdTxResponse}; use crate::{ @@ -23,18 +22,14 @@ use crate::{ impl Handler for HandshakeRequest { type Response = Response; - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref().clone(); info!("{}", "\nPeforming Handshake".blue().bold()); // TODO: may need to import verbosity here - let pub_key = handshake(self, config) - .await?; - + let pub_key = handshake(self, config).await?; + Ok(HandshakeResponse { pub_key }.into()) } } @@ -120,7 +115,9 @@ async fn handshake(args: HandshakeRequest, config: Config) -> Result { block_tx_commit(&tmrpc_client, output.txhash).await?; info!("SessionSetPubKey tx committed"); - let output: WasmdTxResponse = cw_client.query_tx(&output.txhash.to_string()).map_err(|err| eyre!(Box::new(err)))?; // todo change + let output: WasmdTxResponse = cw_client + .query_tx(&output.txhash.to_string()) + .map_err(|err| eyre!(Box::new(err)))?; // todo change let wasm_event = output .events diff --git a/crates/cli/src/handler/init.rs b/crates/cli/src/handler/init.rs index ace1c6aa..2aecb4a1 100644 --- a/crates/cli/src/handler/init.rs +++ b/crates/cli/src/handler/init.rs @@ -2,10 +2,9 @@ use std::path::PathBuf; use async_trait::async_trait; use cargo_generate::{generate, GenerateArgs, TemplatePath, Vcs}; -use color_eyre::{eyre::Context, owo_colors::OwoColorize}; +use color_eyre::{eyre::Context, owo_colors::OwoColorize, Report, Result}; use tokio::fs; use tracing::info; -use color_eyre::{Result, Report}; use crate::{ config::Config, @@ -19,10 +18,7 @@ impl Handler for InitRequest { type Response = Response; // TODO: Add non-template init method - async fn handle + Send>( - self, - config: C, - ) -> Result { + async fn handle + Send>(self, config: C) -> Result { let config = config.as_ref(); info!("{}", "\nPeforming Init".blue().bold()); diff --git a/crates/cli/src/handler/utils/helpers.rs b/crates/cli/src/handler/utils/helpers.rs index 654fed52..6e9a1272 100644 --- a/crates/cli/src/handler/utils/helpers.rs +++ b/crates/cli/src/handler/utils/helpers.rs @@ -1,5 +1,9 @@ use std::time::Duration; +use color_eyre::{ + eyre::{eyre, WrapErr}, + Result, +}; use cosmrs::{AccountId, ErrorReport}; use cw_client::{CliClient, CwClient}; use regex::Regex; @@ -11,7 +15,6 @@ use tendermint_rpc::{ }; use tokio::fs::{self}; use tracing::debug; -use color_eyre::{eyre::eyre, Result, eyre::WrapErr}; use crate::config::Config; @@ -62,7 +65,7 @@ pub fn query_latest_height_hash(node_url: Url) -> Result<(Height, Hash)> { .trusted_height_hash() .map_err(|e| eyre!(e)) .wrap_err("Could not query chain with cw client")?; - + Ok(( trusted_height.try_into()?, trusted_hash.parse().expect("invalid hash from wasmd"), @@ -87,10 +90,16 @@ pub async fn read_cached_hash_height(config: &Config) -> Result<(Height, Hash)> let hash_path = config.cache_dir()?.join("trusted.hash"); if !height_path.exists() { - return Err(eyre!("Could not read trusted height from cache: {}", height_path.display().to_string())); + return Err(eyre!( + "Could not read trusted height from cache: {}", + height_path.display().to_string() + )); } if !hash_path.exists() { - return Err(eyre!("Could not read trusted hash from cache: {}", hash_path.display().to_string())); + return Err(eyre!( + "Could not read trusted hash from cache: {}", + hash_path.display().to_string() + )); } let trusted_height: Height = fs::read_to_string(height_path.as_path()).await?.parse()?; diff --git a/crates/cli/src/handler/utils/relay.rs b/crates/cli/src/handler/utils/relay.rs index d7d47984..d536ae0c 100644 --- a/crates/cli/src/handler/utils/relay.rs +++ b/crates/cli/src/handler/utils/relay.rs @@ -1,9 +1,9 @@ +use color_eyre::{eyre::eyre, Result}; use quartz_common::proto::{ core_client::CoreClient, InstantiateRequest, SessionCreateRequest, SessionSetPubKeyRequest, }; use quartz_tm_prover::config::ProofOutput; use serde_json::{json, Value as JsonValue}; -use color_eyre::{Result, eyre::eyre}; #[derive(Debug)] pub enum RelayMessage { @@ -15,15 +15,23 @@ pub enum RelayMessage { impl RelayMessage { pub async fn run_relay(self, enclave_rpc: String) -> Result { // Query the gRPC quartz enclave service - let mut qc_client = CoreClient::connect(enclave_rpc) - .await - .map_err(|e| eyre!("Failed to connect to the gRPC quartz enclave service: {}", e))?; + let mut qc_client = CoreClient::connect(enclave_rpc).await.map_err(|e| { + eyre!( + "Failed to connect to the gRPC quartz enclave service: {}", + e + ) + })?; let attested_msg = match self { RelayMessage::Instantiate { mut init_msg } => qc_client .instantiate(tonic::Request::new(InstantiateRequest {})) .await - .map_err(|e| eyre!("Failed to instantiate via gRPC quartz enclave service: {}", e)) + .map_err(|e| { + eyre!( + "Failed to instantiate via gRPC quartz enclave service: {}", + e + ) + }) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| { init_msg["quartz"] = msg; @@ -32,7 +40,12 @@ impl RelayMessage { RelayMessage::SessionCreate => qc_client .session_create(tonic::Request::new(SessionCreateRequest {})) .await - .map_err(|e| eyre!("Failed to create session via gRPC quartz enclave service: {}", e)) + .map_err(|e| { + eyre!( + "Failed to create session via gRPC quartz enclave service: {}", + e + ) + }) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| json!({ "quartz": {"session_create": msg}}).to_string())?, RelayMessage::SessionSetPubKey { proof } => qc_client @@ -40,7 +53,12 @@ impl RelayMessage { message: serde_json::to_string(&proof)?, }) .await - .map_err(|e| eyre!("Failed to set public key via gRPC quartz enclave service: {}", e)) + .map_err(|e| { + eyre!( + "Failed to set public key via gRPC quartz enclave service: {}", + e + ) + }) .map(|res| serde_json::from_str::(&res.into_inner().message))? .map(|msg| json!({ "quartz": {"session_set_pub_key": msg}}).to_string())?, }; diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 6d1e98dc..3df25e7a 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -24,7 +24,10 @@ use std::path::PathBuf; use clap::Parser; use cli::ToFigment; -use color_eyre::{eyre::{eyre, Result}, owo_colors::OwoColorize}; +use color_eyre::{ + eyre::{eyre, Result}, + owo_colors::OwoColorize, +}; use config::Config; use figment::{ providers::{Env, Format, Serialized, Toml}, diff --git a/crates/cli/src/request.rs b/crates/cli/src/request.rs index 9bd0d939..17c98209 100644 --- a/crates/cli/src/request.rs +++ b/crates/cli/src/request.rs @@ -1,4 +1,4 @@ -use color_eyre::eyre::eyre; +use color_eyre::{eyre::eyre, Report, Result}; use crate::{ cli::{Command, ContractCommand, EnclaveCommand}, @@ -9,8 +9,6 @@ use crate::{ }, }; -use color_eyre::{Result, Report}; - pub mod contract_build; pub mod contract_deploy; pub mod dev; @@ -45,7 +43,10 @@ impl TryFrom for Request { Command::Enclave { enclave_command } => enclave_command.try_into(), Command::Dev(args) => { if !args.contract_deploy.contract_manifest.exists() { - return Err(eyre!("The contract manifest file does not exist: {}", args.contract_deploy.contract_manifest.display())); + return Err(eyre!( + "The contract manifest file does not exist: {}", + args.contract_deploy.contract_manifest.display() + )); } Ok(DevRequest { @@ -72,7 +73,10 @@ impl TryFrom for Request { match cmd { ContractCommand::Deploy(args) => { if !args.contract_manifest.exists() { - return Err(eyre!("The contract manifest file does not exist: {}", args.contract_manifest.display())); + return Err(eyre!( + "The contract manifest file does not exist: {}", + args.contract_manifest.display() + )); } Ok(ContractDeployRequest { @@ -84,7 +88,10 @@ impl TryFrom for Request { } ContractCommand::Build(args) => { if !args.contract_manifest.exists() { - return Err(eyre!("The contract manifest file does not exist: {}", args.contract_manifest.display())); + return Err(eyre!( + "The contract manifest file does not exist: {}", + args.contract_manifest.display() + )); } Ok(ContractBuildRequest { @@ -97,7 +104,7 @@ impl TryFrom for Request { } impl TryFrom for Request { -type Error = Report; + type Error = Report; fn try_from(cmd: EnclaveCommand) -> Result { match cmd { diff --git a/crates/cli/src/request/contract_deploy.rs b/crates/cli/src/request/contract_deploy.rs index d4cdec81..120de28e 100644 --- a/crates/cli/src/request/contract_deploy.rs +++ b/crates/cli/src/request/contract_deploy.rs @@ -1,10 +1,9 @@ use std::{collections::HashMap, path::PathBuf}; -use color_eyre::eyre::Context; +use color_eyre::{eyre::Context, Result}; use serde::{Deserialize, Serialize}; use crate::request::Request; -use color_eyre::Result; #[derive(Clone, Debug)] pub struct ContractDeployRequest { @@ -21,7 +20,8 @@ impl From for Request { impl ContractDeployRequest { pub fn checked_init(init_msg: String) -> Result { - let parsed: GenericQuartzInit = serde_json::from_str(&init_msg).wrap_err("Init message doesn't contain mandatory quartz field")?; + let parsed: GenericQuartzInit = serde_json::from_str(&init_msg) + .wrap_err("Init message doesn't contain mandatory quartz field")?; Ok(parsed) } diff --git a/crates/cli/src/request/enclave_start.rs b/crates/cli/src/request/enclave_start.rs index ea0ba72e..8f1584e1 100644 --- a/crates/cli/src/request/enclave_start.rs +++ b/crates/cli/src/request/enclave_start.rs @@ -1,13 +1,10 @@ +use color_eyre::Result; use cosmrs::AccountId; use quartz_common::enclave::types::Fmspc; use tendermint::{block::Height, Hash}; use tracing::debug; -use color_eyre::Result; -use crate::{ - config::Config, handler::utils::helpers::query_latest_height_hash, - request::Request, -}; +use crate::{config::Config, handler::utils::helpers::query_latest_height_hash, request::Request}; #[derive(Clone, Debug)] pub struct EnclaveStartRequest { @@ -36,10 +33,8 @@ impl EnclaveStartRequest { debug!("reusing config trusted hash & height"); Ok(( config.trusted_height.try_into()?, - config - .trusted_hash - .parse()? - )) + config.trusted_hash.parse()?, + )) } } } diff --git a/crates/cli/src/request/init.rs b/crates/cli/src/request/init.rs index 990b3730..e2603735 100644 --- a/crates/cli/src/request/init.rs +++ b/crates/cli/src/request/init.rs @@ -1,7 +1,8 @@ use std::path::PathBuf; +use color_eyre::{eyre::eyre, Report, Result}; + use crate::request::Request; -use color_eyre::{Result, eyre::eyre, Report}; #[derive(Clone, Debug)] pub struct InitRequest { @@ -15,7 +16,10 @@ impl TryFrom for Request { if request.name.extension().is_some() { return Err(eyre!("Path is not a directory: {}", request.name.display())); } else if request.name.exists() { - return Err(eyre!("Directory already exists: {}", request.name.display())); + return Err(eyre!( + "Directory already exists: {}", + request.name.display() + )); } Ok(Request::Init(request)) From 80d34a1396f6b6bd10245406291f101acaf9b92a Mon Sep 17 00:00:00 2001 From: Daniel Gushchyan <39884512+dangush@users.noreply.github.com> Date: Fri, 4 Oct 2024 14:47:41 -0700 Subject: [PATCH 6/8] fmt --- crates/utils/cw-client/src/cli.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/utils/cw-client/src/cli.rs b/crates/utils/cw-client/src/cli.rs index cc06d493..a352be46 100644 --- a/crates/utils/cw-client/src/cli.rs +++ b/crates/utils/cw-client/src/cli.rs @@ -1,6 +1,6 @@ use std::process::Command; -use color_eyre::{eyre::eyre, Report, Result, Help}; +use color_eyre::{eyre::eyre, Help, Report, Result}; use cosmrs::{tendermint::chain::Id, AccountId}; use reqwest::Url; use serde::de::DeserializeOwned; @@ -58,9 +58,12 @@ impl CliClient { fn new_command(&self) -> Result { let bin = self.kind.bin(); if !self.is_bin_available(&bin) { - return Err(eyre!("Binary '{}' not found in PATH", bin)).suggestion(format!("Have you installed {}? If so, check that it's in your PATH.", bin)); + return Err(eyre!("Binary '{}' not found in PATH", bin)).suggestion(format!( + "Have you installed {}? If so, check that it's in your PATH.", + bin + )); } - + Ok(Command::new(self.kind.bin())) } fn is_bin_available(&self, bin: &str) -> bool { From da3aab599f621371dc98309a0cfccd380966b7af Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Mon, 7 Oct 2024 12:45:34 +0200 Subject: [PATCH 7/8] Fix bad default URL --- crates/cli/src/config.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/src/config.rs b/crates/cli/src/config.rs index e43c108a..ecae1ed4 100644 --- a/crates/cli/src/config.rs +++ b/crates/cli/src/config.rs @@ -79,7 +79,7 @@ fn default_ws_url() -> Url { } fn default_grpc_url() -> Url { - "http://127.0.0.1:9090," + "http://127.0.0.1:9090" .parse() .expect("valid hardcoded URL") } From eff57a385c1815679e428b24056270d96212a61e Mon Sep 17 00:00:00 2001 From: hu55a1n1 Date: Mon, 7 Oct 2024 12:46:01 +0200 Subject: [PATCH 8/8] Minor cleanup/fmt --- crates/cli/src/handler/contract_deploy.rs | 18 +++++++----------- examples/transfers/enclave/src/main.rs | 3 ++- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/crates/cli/src/handler/contract_deploy.rs b/crates/cli/src/handler/contract_deploy.rs index 76ca6d6d..cca19ed1 100644 --- a/crates/cli/src/handler/contract_deploy.rs +++ b/crates/cli/src/handler/contract_deploy.rs @@ -116,17 +116,13 @@ async fn deploy( info!("🚀 Instantiating {}", args.label); - let init_output: WasmdTxResponse = serde_json::from_str( - &cw_client - .init( - &config.chain_id, - &config.tx_sender, - code_id, - json!(init_msg), - &format!("{} Contract #{}", args.label, code_id), - ) - .map_err(|err| eyre!(Box::new(err)))?, - )?; // TODO: change underlying error type to be eyre instead of anyhow + let init_output: WasmdTxResponse = serde_json::from_str(&cw_client.init( + &config.chain_id, + &config.tx_sender, + code_id, + json!(init_msg), + &format!("{} Contract #{}", args.label, code_id), + )?)?; let res = block_tx_commit(&tmrpc_client, init_output.txhash).await?; diff --git a/examples/transfers/enclave/src/main.rs b/examples/transfers/enclave/src/main.rs index 2547f113..d2504de9 100644 --- a/examples/transfers/enclave/src/main.rs +++ b/examples/transfers/enclave/src/main.rs @@ -41,7 +41,8 @@ use crate::wslistener::WsListener; async fn main() -> Result<(), Box> { let args = Cli::parse(); - let admin_sk = std::env::var("ADMIN_SK").map_err(|_| anyhow::anyhow!("Admin secret key not found in env vars"))?; + let admin_sk = std::env::var("ADMIN_SK") + .map_err(|_| anyhow::anyhow!("Admin secret key not found in env vars"))?; let light_client_opts = LightClientOpts::new( args.chain_id.clone(),