Skip to content

StdResult and StdError as anyhow alias #1162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 17, 2023
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mithril-aggregator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = { workspace = true }
repository = { workspace = true }

[dependencies]
anyhow = "1.0.71"
async-trait = "0.1.52"
chrono = { version = "0.4", features = ["serde"] }
clap = { version = "4.0", features = ["derive", "env", "cargo"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ impl ArtifactBuilder<Beacon, Snapshot> for CardanoImmutableFilesFullArtifactBuil

#[cfg(test)]
mod tests {
use anyhow::anyhow;
use std::path::Path;
use tempfile::NamedTempFile;

use mithril_common::test_utils::fake_data;
use tempfile::NamedTempFile;

use super::*;

Expand Down Expand Up @@ -253,7 +254,7 @@ mod tests {
let mut snapshot_uploader = MockSnapshotUploader::new();
snapshot_uploader
.expect_upload_snapshot()
.return_once(|_| Err("an error".to_string()))
.return_once(|_| Err(anyhow!("an error")))
.once();

let cardano_immutable_files_full_artifact_builder =
Expand Down
26 changes: 8 additions & 18 deletions mithril-aggregator/src/commands/era_command.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use anyhow::anyhow;
use clap::{Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder};
use mithril_common::{
crypto_helper::{key_decode_hex, EraMarkersSigner},
entities::{Epoch, HexEncodedEraMarkersSecretKey},
StdResult,
};
use slog_scope::debug;
use std::error::Error;

use crate::tools::EraTools;

Expand All @@ -18,10 +19,7 @@ pub struct EraCommand {
}

impl EraCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
self.era_subcommand.execute(config_builder).await
}
}
Expand All @@ -37,10 +35,7 @@ pub enum EraSubCommand {
}

impl EraSubCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
match self {
Self::List(cmd) => cmd.execute(config_builder).await,
Self::GenerateTxDatum(cmd) => cmd.execute(config_builder).await,
Expand All @@ -57,10 +52,7 @@ pub struct ListEraSubCommand {
}

impl ListEraSubCommand {
pub async fn execute(
&self,
_config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
debug!("LIST ERA command");
let era_tools = EraTools::new();
let eras = era_tools.get_supported_eras_list()?;
Expand Down Expand Up @@ -93,14 +85,12 @@ pub struct GenerateTxDatumEraSubCommand {
}

impl GenerateTxDatumEraSubCommand {
pub async fn execute(
&self,
_config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
debug!("GENERATETXDATUM ERA command");
let era_tools = EraTools::new();

let era_markers_secret_key = key_decode_hex(&self.era_markers_secret_key)?;
let era_markers_secret_key = key_decode_hex(&self.era_markers_secret_key)
.map_err(|e| anyhow!(e).context("json hex decode of era markers secret key failure"))?;
let era_markers_signer = EraMarkersSigner::from_secret_key(era_markers_secret_key);
print!(
"{}",
Expand Down
63 changes: 24 additions & 39 deletions mithril-aggregator/src/commands/genesis_command.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use anyhow::{anyhow, Context};
use clap::{Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder};
use mithril_common::{
crypto_helper::{key_decode_hex, ProtocolGenesisSigner},
entities::HexEncodedGenesisSecretKey,
StdResult,
};
use slog_scope::debug;
use std::{error::Error, path::PathBuf};
use std::path::PathBuf;

use crate::{dependency_injection::DependenciesBuilder, tools::GenesisTools, Configuration};

Expand All @@ -18,10 +20,7 @@ pub struct GenesisCommand {
}

impl GenesisCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
self.genesis_subcommand.execute(config_builder).await
}
}
Expand All @@ -43,10 +42,7 @@ pub enum GenesisSubCommand {
}

impl GenesisSubCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
match self {
Self::Bootstrap(cmd) => cmd.execute(config_builder).await,
Self::Export(cmd) => cmd.execute(config_builder).await,
Expand All @@ -65,15 +61,12 @@ pub struct ExportGenesisSubCommand {
}

impl ExportGenesisSubCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
let config: Configuration = config_builder
.build()
.map_err(|e| format!("configuration build error: {e}"))?
.with_context(|| "configuration build error")?
.try_deserialize()
.map_err(|e| format!("configuration deserialize error: {e}"))?;
.with_context(|| "configuration deserialize error")?;
debug!("EXPORT GENESIS command"; "config" => format!("{config:?}"));
println!(
"Genesis export payload to sign to {}",
Expand All @@ -84,10 +77,10 @@ impl ExportGenesisSubCommand {

let genesis_tools = GenesisTools::from_dependencies(dependencies)
.await
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
.with_context(|| "genesis-tools: initialization error")?;
genesis_tools
.export_payload_to_sign(&self.target_path)
.map_err(|err| format!("genesis-tools: export error: {err}"))?;
.with_context(|| "genesis-tools: export error")?;
Ok(())
}
}
Expand All @@ -100,15 +93,12 @@ pub struct ImportGenesisSubCommand {
}

impl ImportGenesisSubCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
let config: Configuration = config_builder
.build()
.map_err(|e| format!("configuration build error: {e}"))?
.with_context(|| "configuration build error")?
.try_deserialize()
.map_err(|e| format!("configuration deserialize error: {e}"))?;
.with_context(|| "configuration deserialize error")?;
debug!("IMPORT GENESIS command"; "config" => format!("{config:?}"));
println!(
"Genesis import signed payload from {}",
Expand All @@ -119,11 +109,11 @@ impl ImportGenesisSubCommand {

let genesis_tools = GenesisTools::from_dependencies(dependencies)
.await
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
.with_context(|| "genesis-tools: initialization error")?;
genesis_tools
.import_payload_signature(&self.signed_payload_path)
.await
.map_err(|err| format!("genesis-tools: import error: {err}"))?;
.with_context(|| "genesis-tools: import error")?;
Ok(())
}
}
Expand All @@ -144,10 +134,7 @@ pub struct SignGenesisSubCommand {
}

impl SignGenesisSubCommand {
pub async fn execute(
&self,
_config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, _config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
debug!("SIGN GENESIS command");
println!(
"Genesis sign payload from {} to {}",
Expand All @@ -161,7 +148,7 @@ impl SignGenesisSubCommand {
&self.genesis_secret_key_path,
)
.await
.map_err(|err| format!("genesis-tools: sign error: {err}"))?;
.with_context(|| "genesis-tools: sign error")?;

Ok(())
}
Expand All @@ -174,29 +161,27 @@ pub struct BootstrapGenesisSubCommand {
}

impl BootstrapGenesisSubCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
let config: Configuration = config_builder
.build()
.map_err(|e| format!("configuration build error: {e}"))?
.with_context(|| "configuration build error")?
.try_deserialize()
.map_err(|e| format!("configuration deserialize error: {e}"))?;
.with_context(|| "configuration deserialize error")?;
debug!("BOOTSTRAP GENESIS command"; "config" => format!("{config:?}"));
println!("Genesis bootstrap for test only!");
let mut dependencies_builder = DependenciesBuilder::new(config.clone());
let dependencies = dependencies_builder.create_genesis_container().await?;

let genesis_tools = GenesisTools::from_dependencies(dependencies)
.await
.map_err(|err| format!("genesis-tools: initialization error: {err}"))?;
let genesis_secret_key = key_decode_hex(&self.genesis_secret_key)?;
.with_context(|| "genesis-tools: initialization error")?;
let genesis_secret_key = key_decode_hex(&self.genesis_secret_key)
.map_err(|e| anyhow!(e).context("json hex decode of genesis secret key failure"))?;
let genesis_signer = ProtocolGenesisSigner::from_secret_key(genesis_secret_key);
genesis_tools
.bootstrap_test_genesis_certificate(genesis_signer)
.await
.map_err(|err| format!("genesis-tools: bootstrap error: {err}"))?;
.with_context(|| "genesis-tools: bootstrap error")?;
Ok(())
}
}
10 changes: 4 additions & 6 deletions mithril-aggregator/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ mod tools_command;

use clap::{Parser, Subcommand};
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
use mithril_common::StdResult;
use slog::Level;
use slog_scope::debug;
use std::{error::Error, path::PathBuf};
use std::path::PathBuf;

use crate::DefaultConfiguration;

Expand All @@ -21,10 +22,7 @@ pub enum MainCommand {
}

impl MainCommand {
pub async fn execute(
&self,
config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
match self {
Self::Genesis(cmd) => cmd.execute(config_builder).await,
Self::Era(cmd) => cmd.execute(config_builder).await,
Expand Down Expand Up @@ -84,7 +82,7 @@ impl Source for MainOpts {

impl MainOpts {
/// execute command
pub async fn execute(&self) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self) -> StdResult<()> {
let config_file_path = self
.config_directory
.join(format!("{}.json", self.run_mode));
Expand Down
13 changes: 6 additions & 7 deletions mithril-aggregator/src/commands/serve_command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use anyhow::Context;
use clap::Parser;
use config::{builder::DefaultState, ConfigBuilder, Map, Source, Value, ValueKind};
use mithril_common::StdResult;
use slog_scope::{crit, debug, info};
use std::{error::Error, net::IpAddr, path::PathBuf};
use std::{net::IpAddr, path::PathBuf};
use tokio::{sync::oneshot, task::JoinSet};

use crate::{dependency_injection::DependenciesBuilder, Configuration};
Expand Down Expand Up @@ -71,16 +73,13 @@ impl Source for ServeCommand {
}

impl ServeCommand {
pub async fn execute(
&self,
mut config_builder: ConfigBuilder<DefaultState>,
) -> Result<(), Box<dyn Error>> {
pub async fn execute(&self, mut config_builder: ConfigBuilder<DefaultState>) -> StdResult<()> {
config_builder = config_builder.add_source(self.clone());
let config: Configuration = config_builder
.build()
.map_err(|e| format!("configuration build error: {e}"))?
.with_context(|| "configuration build error")?
.try_deserialize()
.map_err(|e| format!("configuration deserialize error: {e}"))?;
.with_context(|| "configuration deserialize error")?;
debug!("SERVE command"; "config" => format!("{config:?}"));
let mut dependencies_builder = DependenciesBuilder::new(config.clone());

Expand Down
Loading