From fa695dbfa8c7b04efe4e1ee49d217a9d48fbc8eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= <4142+huitseeker@users.noreply.github.com> Date: Tue, 9 Aug 2022 14:57:36 -0400 Subject: [PATCH] refactor: convert all instances of anyhow to eyre (#732) --- narwhal/.clippy.toml | 1 + narwhal/consensus/Cargo.toml | 3 ++- narwhal/consensus/benches/process_certificates.rs | 3 +-- narwhal/crypto/Cargo.toml | 1 - narwhal/crypto/src/bls12377/mod.rs | 4 ++-- narwhal/crypto/src/bls12381.rs | 4 ++-- narwhal/crypto/src/ed25519.rs | 4 ++-- narwhal/crypto/src/pubkey_bytes.rs | 4 ++-- narwhal/crypto/src/secp256k1.rs | 4 ++-- narwhal/network/Cargo.toml | 2 +- narwhal/network/src/bounded_executor.rs | 4 ++-- narwhal/network/src/lib.rs | 2 +- narwhal/network/src/primary.rs | 2 +- narwhal/network/src/worker.rs | 4 ++-- narwhal/node/Cargo.toml | 2 +- narwhal/node/src/benchmark_client.rs | 10 +++++----- narwhal/node/src/main.rs | 6 +++--- 17 files changed, 30 insertions(+), 30 deletions(-) diff --git a/narwhal/.clippy.toml b/narwhal/.clippy.toml index f6ec3b437858e..6b692e15d4b81 100644 --- a/narwhal/.clippy.toml +++ b/narwhal/.clippy.toml @@ -6,6 +6,7 @@ type-complexity-threshold = 10000 too-many-arguments-threshold = 15 disallowed-methods = [ + {path = "anyhow", reason = "we prefer to use eyre"}, # we use tracing with the log feature instead of the log crate. { path = "log::info", reason = "use tracing::info instead" }, { path = "log::debug", reason = "use tracing::debug instead" }, diff --git a/narwhal/consensus/Cargo.toml b/narwhal/consensus/Cargo.toml index c10ffaf8d7520..5146280ca4a40 100644 --- a/narwhal/consensus/Cargo.toml +++ b/narwhal/consensus/Cargo.toml @@ -36,11 +36,12 @@ indexmap = { version = "1.9.1", features = ["serde"] } test_utils = { path = "../test_utils" } [target.'cfg(unix)'.dev-dependencies] -pprof = { version = "0.10.0", features = ["criterion", "flamegraph"] } +pprof = { version = "0.10.0", features = ["criterion", "flamegraph"]} [features] default = ["rand"] benchmark = [] +pprof = [] [lib] bench = false diff --git a/narwhal/consensus/benches/process_certificates.rs b/narwhal/consensus/benches/process_certificates.rs index ad73699ba5220..2dad2468e15ab 100644 --- a/narwhal/consensus/benches/process_certificates.rs +++ b/narwhal/consensus/benches/process_certificates.rs @@ -1,6 +1,5 @@ // Copyright (c) 2022, Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use arc_swap::ArcSwap; use consensus::{ bullshark::Bullshark, consensus::{ConsensusProtocol, ConsensusState}, @@ -36,7 +35,7 @@ pub fn process_certificates(c: &mut Criterion) { .map(|x| x.digest()) .collect::>(); let (certificates, _next_parents) = make_optimal_certificates(1..=rounds, &genesis, &keys); - let committee = Arc::new(ArcSwap::from_pointee(mock_committee(&keys))); + let committee = mock_committee(&keys); let store_path = temp_dir(); let store = make_consensus_store(&store_path); diff --git a/narwhal/crypto/Cargo.toml b/narwhal/crypto/Cargo.toml index 4e95f612e4a84..0f9bab1f7872e 100644 --- a/narwhal/crypto/Cargo.toml +++ b/narwhal/crypto/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" publish = false [dependencies] -anyhow = { version = "1.0.59", features = ["backtrace"] } ark-bls12-377 = { version = "0.3.0", features = ["std"], optional = true } base64ct = { version = "1.5.1", features = ["alloc"] } ed25519-consensus = { version = "2.0.1", features = ["serde"] } diff --git a/narwhal/crypto/src/bls12377/mod.rs b/narwhal/crypto/src/bls12377/mod.rs index 8c5c72e5e6002..c28fabaf4953e 100644 --- a/narwhal/crypto/src/bls12377/mod.rs +++ b/narwhal/crypto/src/bls12377/mod.rs @@ -429,10 +429,10 @@ impl Signer for BLS12377KeyPair { } impl FromStr for BLS12377KeyPair { - type Err = anyhow::Error; + type Err = eyre::Report; fn from_str(s: &str) -> Result { - let kp = Self::decode_base64(s).map_err(|e| anyhow::anyhow!("{}", e.to_string()))?; + let kp = Self::decode_base64(s).map_err(|e| eyre::eyre!("{}", e.to_string()))?; Ok(kp) } } diff --git a/narwhal/crypto/src/bls12381.rs b/narwhal/crypto/src/bls12381.rs index 89b17f03840c9..41ded69946e01 100644 --- a/narwhal/crypto/src/bls12381.rs +++ b/narwhal/crypto/src/bls12381.rs @@ -437,10 +437,10 @@ impl Signer for BLS12381KeyPair { } impl FromStr for BLS12381KeyPair { - type Err = anyhow::Error; + type Err = eyre::Report; fn from_str(s: &str) -> Result { - let kp = Self::decode_base64(s).map_err(|e| anyhow::anyhow!("{}", e.to_string()))?; + let kp = Self::decode_base64(s).map_err(|e| eyre::eyre!("{}", e.to_string()))?; Ok(kp) } } diff --git a/narwhal/crypto/src/ed25519.rs b/narwhal/crypto/src/ed25519.rs index f8cac85f029b2..c7070e87c26d5 100644 --- a/narwhal/crypto/src/ed25519.rs +++ b/narwhal/crypto/src/ed25519.rs @@ -405,10 +405,10 @@ impl KeyPair for Ed25519KeyPair { } impl FromStr for Ed25519KeyPair { - type Err = anyhow::Error; + type Err = eyre::Report; fn from_str(s: &str) -> Result { - let kp = Self::decode_base64(s).map_err(|e| anyhow::anyhow!("{}", e.to_string()))?; + let kp = Self::decode_base64(s).map_err(|e| eyre::eyre!("{}", e.to_string()))?; Ok(kp) } } diff --git a/narwhal/crypto/src/pubkey_bytes.rs b/narwhal/crypto/src/pubkey_bytes.rs index 15b75c8799744..0faf26d5d00e9 100644 --- a/narwhal/crypto/src/pubkey_bytes.rs +++ b/narwhal/crypto/src/pubkey_bytes.rs @@ -68,12 +68,12 @@ impl Default for PublicKeyBytes { } impl FromStr for PublicKeyBytes { - type Err = anyhow::Error; + type Err = eyre::Report; fn from_str(s: &str) -> Result { let s = s.strip_prefix("0x").unwrap_or(s); let value = hex::decode(s)?; - Self::from_bytes(&value[..]).map_err(|_| anyhow::anyhow!("byte deserialization failed")) + Self::from_bytes(&value[..]).map_err(|_| eyre::eyre!("byte deserialization failed")) } } diff --git a/narwhal/crypto/src/secp256k1.rs b/narwhal/crypto/src/secp256k1.rs index ae5b4079bbbaa..36c1b3ea49527 100644 --- a/narwhal/crypto/src/secp256k1.rs +++ b/narwhal/crypto/src/secp256k1.rs @@ -376,10 +376,10 @@ impl KeyPair for Secp256k1KeyPair { } impl FromStr for Secp256k1KeyPair { - type Err = anyhow::Error; + type Err = eyre::Report; fn from_str(s: &str) -> Result { - let kp = Self::decode_base64(s).map_err(|e| anyhow::anyhow!("{}", e.to_string()))?; + let kp = Self::decode_base64(s).map_err(|e| eyre::eyre!("{}", e.to_string()))?; Ok(kp) } } diff --git a/narwhal/network/Cargo.toml b/narwhal/network/Cargo.toml index c4e3a91ae6377..5664a13177b2a 100644 --- a/narwhal/network/Cargo.toml +++ b/narwhal/network/Cargo.toml @@ -7,7 +7,6 @@ edition = "2021" publish = false [dependencies] -anyhow = "1.0.59" async-trait = "0.1.57" backoff = { version = "0.4.0", features = ["tokio"] } bytes = "1.2.1" @@ -26,6 +25,7 @@ types = { path = "../types" } mysten-network = { git = "https://github.com/mystenlabs/mysten-infra.git", rev = "d965a5a795dcdb4d1c7964acf556bc249fdc58aa" } serde = "1.0.142" workspace-hack = { version = "0.1", path = "../workspace-hack" } +eyre = "0.6.8" [dev-dependencies] bincode = "1.3.3" diff --git a/narwhal/network/src/bounded_executor.rs b/narwhal/network/src/bounded_executor.rs index 471ff02f8edff..93bb9d3e615fd 100644 --- a/narwhal/network/src/bounded_executor.rs +++ b/narwhal/network/src/bounded_executor.rs @@ -299,8 +299,8 @@ mod test { }) } - async fn always_failing() -> Result<(), backoff::Error> { - Err(Into::into(anyhow::anyhow!("oops"))) + async fn always_failing() -> Result<(), backoff::Error> { + Err(Into::into(eyre::eyre!("oops"))) } fn panic_after(d: Duration, f: F) -> T diff --git a/narwhal/network/src/lib.rs b/narwhal/network/src/lib.rs index 36b6f1744f23d..a91f8a3243a67 100644 --- a/narwhal/network/src/lib.rs +++ b/narwhal/network/src/lib.rs @@ -25,7 +25,7 @@ pub use crate::{ }; // the result of our network messages -pub type MessageResult = Result, anyhow::Error>; +pub type MessageResult = Result, eyre::Report>; /// This adapter will make a [`tokio::task::JoinHandle`] abort its handled task when the handle is dropped. #[derive(Debug)] diff --git a/narwhal/network/src/primary.rs b/narwhal/network/src/primary.rs index 02558004381de..914c8edfe1c67 100644 --- a/narwhal/network/src/primary.rs +++ b/narwhal/network/src/primary.rs @@ -156,7 +156,7 @@ impl ReliableNetwork for PrimaryNetwork { client.send_message(message).await.map_err(|e| { // this returns a backoff::Error::Transient // so that if tonic::Status is returned, we retry - Into::>::into(anyhow::Error::from(e)) + Into::>::into(eyre::Report::from(e)) }) } }; diff --git a/narwhal/network/src/worker.rs b/narwhal/network/src/worker.rs index 4f0f56efc2414..d2e62d872fe77 100644 --- a/narwhal/network/src/worker.rs +++ b/narwhal/network/src/worker.rs @@ -158,7 +158,7 @@ impl ReliableNetwork for WorkerNetwork { client.send_message(message).await.map_err(|e| { // this returns a backoff::Error::Transient // so that if tonic::Status is returned, we retry - Into::>::into(anyhow::Error::from(e)) + Into::>::into(eyre::Report::from(e)) }) } }; @@ -246,7 +246,7 @@ impl ReliableNetwork for WorkerToPrimaryNetwork { client.send_message(message).await.map_err(|e| { // this returns a backoff::Error::Transient // so that if tonic::Status is returned, we retry - Into::>::into(anyhow::Error::from(e)) + Into::>::into(eyre::Report::from(e)) }) } }; diff --git a/narwhal/node/Cargo.toml b/narwhal/node/Cargo.toml index 883ab137d3ce9..e2897f26c7cbf 100644 --- a/narwhal/node/Cargo.toml +++ b/narwhal/node/Cargo.toml @@ -6,7 +6,6 @@ authors = ["Mysten Labs "] edition = "2021" [dependencies] -anyhow = "1.0.59" arc-swap = { version = "1.5.1", features = ["serde"] } async-trait = "0.1.57" bincode = "1.3.3" @@ -40,6 +39,7 @@ prometheus = "0.13.1" types = { path = "../types" } worker = { path = "../worker" } workspace-hack = { version = "0.1", path = "../workspace-hack" } +eyre = "0.6.8" [dev-dependencies] ed25519-dalek = "1.0.1" diff --git a/narwhal/node/src/benchmark_client.rs b/narwhal/node/src/benchmark_client.rs index be0e9c1380116..67f782971f066 100644 --- a/narwhal/node/src/benchmark_client.rs +++ b/narwhal/node/src/benchmark_client.rs @@ -1,9 +1,9 @@ // Copyright (c) 2021, Facebook, Inc. and its affiliates // Copyright (c) 2022, Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -use anyhow::{Context, Result}; use bytes::{BufMut as _, BytesMut}; use clap::{crate_name, crate_version, App, AppSettings}; +use eyre::Context; use futures::{future::join_all, StreamExt}; use rand::Rng; use tokio::{ @@ -16,7 +16,7 @@ use types::{TransactionProto, TransactionsClient}; use url::Url; #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> Result<(), eyre::Report> { let matches = App::new(crate_name!()) .version(crate_version!()) .about("Benchmark client for Narwhal and Tusk.") @@ -105,7 +105,7 @@ struct Client { } impl Client { - pub async fn send(&self) -> Result<()> { + pub async fn send(&self) -> Result<(), eyre::Report> { // We are distributing the transactions that need to be sent // within a second to sub-buckets. The precision here represents // the number of such buckets within the period of 1 second. @@ -119,7 +119,7 @@ impl Client { let burst = self.rate / PRECISION; if burst == 0 { - return Err(anyhow::Error::msg(format!( + return Err(eyre::Report::msg(format!( "Transaction rate is too low, should be at least {} tx/s and multiples of {}", PRECISION, PRECISION ))); @@ -127,7 +127,7 @@ impl Client { // The transaction size must be at least 16 bytes to ensure all txs are different. if self.size < 9 { - return Err(anyhow::Error::msg( + return Err(eyre::Report::msg( "Transaction size must be at least 9 bytes", )); } diff --git a/narwhal/node/src/main.rs b/narwhal/node/src/main.rs index 813d785a5c8db..7db4025e58a73 100644 --- a/narwhal/node/src/main.rs +++ b/narwhal/node/src/main.rs @@ -8,12 +8,12 @@ rust_2021_compatibility )] -use anyhow::{Context, Result}; use arc_swap::ArcSwap; use clap::{crate_name, crate_version, App, AppSettings, ArgMatches, SubCommand}; use config::{Committee, Import, Parameters, WorkerId}; use crypto::{generate_production_keypair, traits::KeyPair as _, KeyPair}; use executor::{SerializedTransaction, SubscriberResult}; +use eyre::Context; use futures::future::join_all; use node::{ execution_state::SimpleExecutionState, @@ -33,7 +33,7 @@ use tracing_subscriber::filter::{EnvFilter, LevelFilter}; static ALLOC: dhat::Alloc = dhat::Alloc; #[tokio::main] -async fn main() -> Result<()> { +async fn main() -> Result<(), eyre::Report> { let matches = App::new(crate_name!()) .version(crate_version!()) .about("A research implementation of Narwhal and Tusk.") @@ -126,7 +126,7 @@ async fn main() -> Result<()> { } // Runs either a worker or a primary. -async fn run(matches: &ArgMatches<'_>) -> Result<()> { +async fn run(matches: &ArgMatches<'_>) -> Result<(), eyre::Report> { let key_file = matches.value_of("keys").unwrap(); let committee_file = matches.value_of("committee").unwrap(); let parameters_file = matches.value_of("parameters");