Skip to content

Commit

Permalink
refactor: convert all instances of anyhow to eyre (MystenLabs#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
huitseeker authored Aug 9, 2022
1 parent b247710 commit fa695db
Show file tree
Hide file tree
Showing 17 changed files with 30 additions and 30 deletions.
1 change: 1 addition & 0 deletions narwhal/.clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
3 changes: 2 additions & 1 deletion narwhal/consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions narwhal/consensus/benches/process_certificates.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -36,7 +35,7 @@ pub fn process_certificates(c: &mut Criterion) {
.map(|x| x.digest())
.collect::<BTreeSet<_>>();
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);
Expand Down
1 change: 0 additions & 1 deletion narwhal/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
4 changes: 2 additions & 2 deletions narwhal/crypto/src/bls12377/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,10 @@ impl Signer<BLS12377Signature> for BLS12377KeyPair {
}

impl FromStr for BLS12377KeyPair {
type Err = anyhow::Error;
type Err = eyre::Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions narwhal/crypto/src/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ impl Signer<BLS12381Signature> for BLS12381KeyPair {
}

impl FromStr for BLS12381KeyPair {
type Err = anyhow::Error;
type Err = eyre::Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions narwhal/crypto/src/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions narwhal/crypto/src/pubkey_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ impl<T, const N: usize> Default for PublicKeyBytes<T, N> {
}

impl<T: VerifyingKey, const N: usize> FromStr for PublicKeyBytes<T, N> {
type Err = anyhow::Error;
type Err = eyre::Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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"))
}
}

Expand Down
4 changes: 2 additions & 2 deletions narwhal/crypto/src/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Err> {
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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion narwhal/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions narwhal/network/src/bounded_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ mod test {
})
}

async fn always_failing() -> Result<(), backoff::Error<anyhow::Error>> {
Err(Into::into(anyhow::anyhow!("oops")))
async fn always_failing() -> Result<(), backoff::Error<eyre::Report>> {
Err(Into::into(eyre::eyre!("oops")))
}

fn panic_after<T, F>(d: Duration, f: F) -> T
Expand Down
2 changes: 1 addition & 1 deletion narwhal/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use crate::{
};

// the result of our network messages
pub type MessageResult = Result<tonic::Response<types::Empty>, anyhow::Error>;
pub type MessageResult = Result<tonic::Response<types::Empty>, eyre::Report>;

/// This adapter will make a [`tokio::task::JoinHandle`] abort its handled task when the handle is dropped.
#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion narwhal/network/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<backoff::Error<anyhow::Error>>::into(anyhow::Error::from(e))
Into::<backoff::Error<eyre::Report>>::into(eyre::Report::from(e))
})
}
};
Expand Down
4 changes: 2 additions & 2 deletions narwhal/network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<backoff::Error<anyhow::Error>>::into(anyhow::Error::from(e))
Into::<backoff::Error<eyre::Report>>::into(eyre::Report::from(e))
})
}
};
Expand Down Expand Up @@ -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::<backoff::Error<anyhow::Error>>::into(anyhow::Error::from(e))
Into::<backoff::Error<eyre::Report>>::into(eyre::Report::from(e))
})
}
};
Expand Down
2 changes: 1 addition & 1 deletion narwhal/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ authors = ["Mysten Labs <[email protected]>"]
edition = "2021"

[dependencies]
anyhow = "1.0.59"
arc-swap = { version = "1.5.1", features = ["serde"] }
async-trait = "0.1.57"
bincode = "1.3.3"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions narwhal/node/src/benchmark_client.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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.")
Expand Down Expand Up @@ -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.
Expand All @@ -119,15 +119,15 @@ 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
)));
}

// 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",
));
}
Expand Down
6 changes: 3 additions & 3 deletions narwhal/node/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.")
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit fa695db

Please sign in to comment.