diff --git a/api/src/context.rs b/api/src/context.rs index a622881d49f66..d2ff8f8c9db01 100644 --- a/api/src/context.rs +++ b/api/src/context.rs @@ -10,7 +10,7 @@ use crate::{ ForbiddenError, InternalError, NotFoundError, ServiceUnavailableError, StdApiError, }, }; -use anyhow::{bail, ensure, format_err, Context as AnyhowContext}; +use anyhow::{format_err, Context as AnyhowContext}; use aptos_api_types::{ AptosErrorCode, AsConverter, BcsBlock, GasEstimation, LedgerInfo, ResourceGroup, TransactionOnChainData, @@ -22,7 +22,7 @@ use aptos_logger::error; use aptos_mempool::{MempoolClientRequest, MempoolClientSender, SubmissionStatus}; use aptos_state_view::TStateView; use aptos_storage_interface::{ - db_ensure, + db_ensure, db_other_bail, errors::AptosDbError, state_view::{DbStateView, DbStateViewAtVersion, LatestDbStateCheckpointView}, DbReader, Order, MAX_REQUEST_LIMIT, @@ -176,7 +176,10 @@ impl Context { self.node_config.api.max_submit_transaction_batch_size } - pub async fn submit_transaction(&self, txn: SignedTransaction) -> Result { + pub async fn submit_transaction( + &self, + txn: SignedTransaction, + ) -> anyhow::Result { let (req_sender, callback) = oneshot::channel(); self.mp_sender .clone() @@ -264,6 +267,7 @@ impl Context { self.db .state_view_at_version(Some(version))? .get_state_value_bytes(state_key) + .map_err(Into::into) } pub fn get_state_value_poem( @@ -292,7 +296,7 @@ impl Context { .take(MAX_REQUEST_LIMIT as usize) .collect::>()?; if iter.next().transpose()?.is_some() { - bail!("Too many state items under account ({:?}).", address); + db_other_bail!("Too many state items under account ({:?}).", address); } Ok(kvs) } @@ -327,12 +331,12 @@ impl Context { Some(Ok((struct_tag, v.into_bytes()))) } Ok(Path::Code(_)) => None, - Err(e) => Some(Err(anyhow::Error::from(e))), + Err(e) => Some(Err(AptosDbError::from(e))), } } _ => { error!("storage prefix scan return inconsistent key ({:?}) with expected key prefix ({:?}).", k, StateKeyPrefix::from(address)); - Some(Err(format_err!( "storage prefix scan return inconsistent key ({:?})", k ))) + Some(Err(AptosDbError::Other( format!("storage prefix scan return inconsistent key ({:?})", k )))) } }, Err(e) => Some(Err(e)), @@ -398,12 +402,12 @@ impl Context { match Path::try_from(path.as_slice()) { Ok(Path::Code(module_id)) => Some(Ok((module_id, v.into_bytes()))), Ok(Path::Resource(_)) | Ok(Path::ResourceGroup(_)) => None, - Err(e) => Some(Err(anyhow::Error::from(e))), + Err(e) => Some(Err(AptosDbError::from(e))), } } _ => { error!("storage prefix scan return inconsistent key ({:?}) with expected key prefix ({:?}).", k, StateKeyPrefix::from(address)); - Some(Err(format_err!( "storage prefix scan return inconsistent key ({:?})", k ))) + Some(Err(AptosDbError::Other(format!( "storage prefix scan return inconsistent key ({:?})", k )))) } }, Err(e) => Some(Err(e)), diff --git a/consensus/src/consensusdb/mod.rs b/consensus/src/consensusdb/mod.rs index 4d2aea8b06bf3..9c2fcb01de665 100644 --- a/consensus/src/consensusdb/mod.rs +++ b/consensus/src/consensusdb/mod.rs @@ -14,6 +14,7 @@ use aptos_logger::prelude::*; use aptos_schemadb::{ schema::Schema, Options, ReadOptions, SchemaBatch, DB, DEFAULT_COLUMN_FAMILY_NAME, }; +use aptos_storage_interface::errors::AptosDbError; pub use schema::{ block::BlockSchema, dag::{CertifiedNodeSchema, DagVoteSchema, NodeSchema, OrderedAnchorIdSchema}, @@ -203,6 +204,6 @@ impl ConsensusDB { pub fn get_all_data(&self) -> Result, DbError> { let mut iter = self.db.iter::(ReadOptions::default())?; iter.seek_to_first(); - Ok(iter.collect::>>()?) + Ok(iter.collect::, AptosDbError>>()?) } } diff --git a/storage/aptosdb/src/db_debugger/checkpoint/mod.rs b/storage/aptosdb/src/db_debugger/checkpoint/mod.rs index b2fed6257bf1c..613d1ff29a221 100644 --- a/storage/aptosdb/src/db_debugger/checkpoint/mod.rs +++ b/storage/aptosdb/src/db_debugger/checkpoint/mod.rs @@ -19,7 +19,7 @@ pub struct Cmd { impl Cmd { pub fn run(self) -> Result<()> { ensure!(!self.output_dir.exists(), "Output dir already exists."); - fs::create_dir_all(&self.output_dir)?; + fs::create_dir_all(&self.output_dir).map_err(Into::::into)?; // TODO(grao): Support sharded state merkle db and split_ledger_db here. AptosDB::create_checkpoint(self.db_dir, self.output_dir, false, false) diff --git a/storage/aptosdb/src/db_debugger/common/mod.rs b/storage/aptosdb/src/db_debugger/common/mod.rs index 62ee3599987ff..94b705238d7e1 100644 --- a/storage/aptosdb/src/db_debugger/common/mod.rs +++ b/storage/aptosdb/src/db_debugger/common/mod.rs @@ -7,7 +7,10 @@ use crate::{ use aptos_config::config::RocksdbConfigs; use aptos_types::nibble::{nibble_path::NibblePath, Nibble}; use clap::Parser; -use std::path::{Path, PathBuf}; +use std::{ + num::ParseIntError, + path::{Path, PathBuf}, +}; pub const PAGE_SIZE: usize = 10; @@ -41,6 +44,10 @@ impl AsRef for DbDir { pub fn parse_nibble_path(src: &str) -> Result { src.chars() - .map(|c| Ok(Nibble::from(u8::from_str_radix(&c.to_string(), 16)?))) + .map(|c| { + Ok(Nibble::from( + u8::from_str_radix(&c.to_string(), 16).map_err(Into::::into)?, + )) + }) .collect() } diff --git a/storage/aptosdb/src/db_debugger/examine/mod.rs b/storage/aptosdb/src/db_debugger/examine/mod.rs index 8fe2a631a6c42..06b60d330b15e 100644 --- a/storage/aptosdb/src/db_debugger/examine/mod.rs +++ b/storage/aptosdb/src/db_debugger/examine/mod.rs @@ -3,7 +3,7 @@ mod print_db_versions; -use anyhow::Result; +use crate::Result; #[derive(clap::Subcommand)] #[clap(about = "Examine databases.")] diff --git a/storage/aptosdb/src/db_debugger/examine/print_db_versions.rs b/storage/aptosdb/src/db_debugger/examine/print_db_versions.rs index 0f477421114a4..ed77f1b7b59a9 100644 --- a/storage/aptosdb/src/db_debugger/examine/print_db_versions.rs +++ b/storage/aptosdb/src/db_debugger/examine/print_db_versions.rs @@ -17,9 +17,8 @@ use crate::{ get_overall_commit_progress, get_state_kv_commit_progress, get_state_merkle_commit_progress, }, - AptosDB, + AptosDB, Result, }; -use anyhow::Result; use aptos_config::config::RocksdbConfigs; use aptos_schemadb::{schema::Schema, ReadOptions, DB}; use aptos_types::transaction::Version; diff --git a/storage/aptosdb/src/db_debugger/ledger/check_range_proof.rs b/storage/aptosdb/src/db_debugger/ledger/check_range_proof.rs index 585e739bda2a5..8ae1ed4470f43 100644 --- a/storage/aptosdb/src/db_debugger/ledger/check_range_proof.rs +++ b/storage/aptosdb/src/db_debugger/ledger/check_range_proof.rs @@ -1,8 +1,10 @@ // Copyright © Aptos Foundation // SPDX-License-Identifier: Apache-2.0 -use crate::{db_debugger::common::DbDir, ledger_store::LedgerStore}; -use anyhow::{ensure, Result}; +use crate::{ + db_debugger::common::DbDir, db_ensure as ensure, ledger_store::LedgerStore, AptosDbError, + Result, +}; use aptos_crypto::hash::CryptoHash; use aptos_types::transaction::Version; use clap::Parser; diff --git a/storage/aptosdb/src/db_debugger/ledger/check_txn_info_hashes.rs b/storage/aptosdb/src/db_debugger/ledger/check_txn_info_hashes.rs index 9f522d9c3bb65..223f9fb944934 100644 --- a/storage/aptosdb/src/db_debugger/ledger/check_txn_info_hashes.rs +++ b/storage/aptosdb/src/db_debugger/ledger/check_txn_info_hashes.rs @@ -2,10 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 use crate::{ - db_debugger::common::DbDir, ledger_store::LedgerStore, - schema::transaction_accumulator::TransactionAccumulatorSchema, + db_debugger::common::DbDir, db_ensure as ensure, ledger_store::LedgerStore, + schema::transaction_accumulator::TransactionAccumulatorSchema, AptosDbError, Result, }; -use anyhow::{ensure, Result}; use aptos_crypto::hash::CryptoHash; use aptos_types::{proof::position::Position, transaction::Version}; use clap::Parser; diff --git a/storage/aptosdb/src/db_debugger/ledger/mod.rs b/storage/aptosdb/src/db_debugger/ledger/mod.rs index 273a94a284b63..9a474b9dfe569 100644 --- a/storage/aptosdb/src/db_debugger/ledger/mod.rs +++ b/storage/aptosdb/src/db_debugger/ledger/mod.rs @@ -4,7 +4,7 @@ mod check_range_proof; mod check_txn_info_hashes; -use anyhow::Result; +use crate::Result; #[derive(clap::Subcommand)] #[clap(about = "Check the ledger.")] diff --git a/storage/aptosdb/src/db_debugger/state_tree/get_path.rs b/storage/aptosdb/src/db_debugger/state_tree/get_path.rs index f3545f0f73b08..c5dbf9c71fbe6 100644 --- a/storage/aptosdb/src/db_debugger/state_tree/get_path.rs +++ b/storage/aptosdb/src/db_debugger/state_tree/get_path.rs @@ -3,9 +3,11 @@ use crate::{ db_debugger::common::{parse_nibble_path, DbDir}, + db_ensure as ensure, jellyfish_merkle_node::JellyfishMerkleNodeSchema, + Result, + AptosDbError, }; -use anyhow::{ensure, Result}; use aptos_crypto::HashValue; use aptos_jellyfish_merkle::node_type::{Child, Node, NodeKey, NodeType}; use aptos_types::{ @@ -30,7 +32,7 @@ pub struct Cmd { impl Cmd { pub fn run(self) -> Result<()> { - ensure!(self.before_version > 0); + ensure!(self.before_version > 0, "version must be greater than 0."); println!( "{}", format!( diff --git a/storage/aptosdb/src/db_debugger/state_tree/get_snapshots.rs b/storage/aptosdb/src/db_debugger/state_tree/get_snapshots.rs index 51ce612e1cfa7..b8799ca30cbb4 100644 --- a/storage/aptosdb/src/db_debugger/state_tree/get_snapshots.rs +++ b/storage/aptosdb/src/db_debugger/state_tree/get_snapshots.rs @@ -4,8 +4,8 @@ use crate::{ db_debugger::common::{DbDir, PAGE_SIZE}, jellyfish_merkle_node::JellyfishMerkleNodeSchema, + Result, }; -use anyhow::Result; use aptos_jellyfish_merkle::node_type::NodeKey; use aptos_types::transaction::Version; use clap::Parser; diff --git a/storage/aptosdb/src/db_debugger/state_tree/mod.rs b/storage/aptosdb/src/db_debugger/state_tree/mod.rs index 74e8107427af1..e1474df67037b 100644 --- a/storage/aptosdb/src/db_debugger/state_tree/mod.rs +++ b/storage/aptosdb/src/db_debugger/state_tree/mod.rs @@ -4,7 +4,7 @@ mod get_path; mod get_snapshots; -use anyhow::Result; +use crate::Result; /// Tool supports listing snapshots before version and printing node in merkel tree with version and nibble path #[derive(clap::Subcommand)] diff --git a/storage/aptosdb/src/db_debugger/truncate/mod.rs b/storage/aptosdb/src/db_debugger/truncate/mod.rs index 3b5cbf61e0919..3173404c5c0c9 100644 --- a/storage/aptosdb/src/db_debugger/truncate/mod.rs +++ b/storage/aptosdb/src/db_debugger/truncate/mod.rs @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 use crate::{ + db_ensure as ensure, jellyfish_merkle_node::JellyfishMerkleNodeSchema, schema::{ db_metadata::{DbMetadataKey, DbMetadataSchema, DbMetadataValue}, @@ -13,9 +14,8 @@ use crate::{ get_ledger_commit_progress, get_overall_commit_progress, get_state_kv_commit_progress, truncate_state_merkle_db, }, - AptosDB, StateStore, + AptosDB, AptosDbError, Result, StateStore, }; -use anyhow::{ensure, Result}; use aptos_config::config::RocksdbConfigs; use aptos_jellyfish_merkle::node_type::NodeKey; use aptos_schemadb::{ReadOptions, DB}; diff --git a/storage/aptosdb/src/state_restore/restore_test.rs b/storage/aptosdb/src/state_restore/restore_test.rs index 196d651606932..4d9bf8924a259 100644 --- a/storage/aptosdb/src/state_restore/restore_test.rs +++ b/storage/aptosdb/src/state_restore/restore_test.rs @@ -1,11 +1,13 @@ // Copyright © Aptos Foundation // SPDX-License-Identifier: Apache-2.0 -use crate::state_restore::{ - StateSnapshotProgress, StateSnapshotRestore, StateSnapshotRestoreMode, StateValueBatch, - StateValueWriter, +use crate::{ + state_restore::{ + StateSnapshotProgress, StateSnapshotRestore, StateSnapshotRestoreMode, StateValueBatch, + StateValueWriter, + }, + Result, }; -use anyhow::Result; use aptos_crypto::{hash::CryptoHash, HashValue}; use aptos_infallible::RwLock; use aptos_jellyfish_merkle::{ diff --git a/storage/schemadb/tests/db.rs b/storage/schemadb/tests/db.rs index 799543f4d4fd8..567ff5e13a11a 100644 --- a/storage/schemadb/tests/db.rs +++ b/storage/schemadb/tests/db.rs @@ -8,6 +8,7 @@ use aptos_schemadb::{ schema::{KeyCodec, Schema, ValueCodec}, ColumnFamilyName, SchemaBatch, DB, }; +use aptos_storage_interface::errors::AptosDbError; use byteorder::{LittleEndian, ReadBytesExt}; use rocksdb::DEFAULT_COLUMN_FAMILY_NAME; @@ -178,7 +179,7 @@ fn collect_values(db: &TestDB) -> Vec<(S::Key, S::Value)> { .iter::(Default::default()) .expect("Failed to create iterator."); iter.seek_to_first(); - iter.collect::>>().unwrap() + iter.collect::, AptosDbError>>().unwrap() } fn gen_expected_values(values: &[(u32, u32)]) -> Vec<(TestField, TestField)> { diff --git a/storage/storage-interface/src/errors.rs b/storage/storage-interface/src/errors.rs index eeff74cb50413..b71df5c4e90cd 100644 --- a/storage/storage-interface/src/errors.rs +++ b/storage/storage-interface/src/errors.rs @@ -45,3 +45,15 @@ impl From for AptosDbError { Self::Other(format!("{}", error)) } } + +impl From for AptosDbError { + fn from(error: std::io::Error) -> Self { + Self::Other(format!("{}", error)) + } +} + +impl From for AptosDbError { + fn from(error: std::num::ParseIntError) -> Self { + Self::Other(format!("{}", error)) + } +}