Skip to content

Commit

Permalink
api
Browse files Browse the repository at this point in the history
  • Loading branch information
areshand committed Aug 25, 2023
1 parent 2a433ab commit de1632a
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 32 deletions.
20 changes: 12 additions & 8 deletions api/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -176,7 +176,10 @@ impl Context {
self.node_config.api.max_submit_transaction_batch_size
}

pub async fn submit_transaction(&self, txn: SignedTransaction) -> Result<SubmissionStatus> {
pub async fn submit_transaction(
&self,
txn: SignedTransaction,
) -> anyhow::Result<SubmissionStatus> {
let (req_sender, callback) = oneshot::channel();
self.mp_sender
.clone()
Expand Down Expand Up @@ -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<E: InternalError>(
Expand Down Expand Up @@ -292,7 +296,7 @@ impl Context {
.take(MAX_REQUEST_LIMIT as usize)
.collect::<Result<_>>()?;
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)
}
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)),
Expand Down
3 changes: 2 additions & 1 deletion consensus/src/consensusdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -203,6 +204,6 @@ impl ConsensusDB {
pub fn get_all_data<S: Schema>(&self) -> Result<Vec<(S::Key, S::Value)>, DbError> {
let mut iter = self.db.iter::<S>(ReadOptions::default())?;
iter.seek_to_first();
Ok(iter.collect::<Result<Vec<(S::Key, S::Value)>>>()?)
Ok(iter.collect::<Result<Vec<(S::Key, S::Value)>, AptosDbError>>()?)
}
}
2 changes: 1 addition & 1 deletion storage/aptosdb/src/db_debugger/checkpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<std::io::Error>::into)?;

// TODO(grao): Support sharded state merkle db and split_ledger_db here.
AptosDB::create_checkpoint(self.db_dir, self.output_dir, false, false)
Expand Down
11 changes: 9 additions & 2 deletions storage/aptosdb/src/db_debugger/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,6 +44,10 @@ impl AsRef<Path> for DbDir {

pub fn parse_nibble_path(src: &str) -> Result<NibblePath> {
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::<ParseIntError>::into)?,
))
})
.collect()
}
2 changes: 1 addition & 1 deletion storage/aptosdb/src/db_debugger/examine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

mod print_db_versions;

use anyhow::Result;
use crate::Result;

#[derive(clap::Subcommand)]
#[clap(about = "Examine databases.")]
Expand Down
3 changes: 1 addition & 2 deletions storage/aptosdb/src/db_debugger/examine/print_db_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions storage/aptosdb/src/db_debugger/ledger/check_range_proof.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion storage/aptosdb/src/db_debugger/ledger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.")]
Expand Down
6 changes: 4 additions & 2 deletions storage/aptosdb/src/db_debugger/state_tree/get_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion storage/aptosdb/src/db_debugger/state_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
4 changes: 2 additions & 2 deletions storage/aptosdb/src/db_debugger/truncate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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};
Expand Down
10 changes: 6 additions & 4 deletions storage/aptosdb/src/state_restore/restore_test.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down
3 changes: 2 additions & 1 deletion storage/schemadb/tests/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -178,7 +179,7 @@ fn collect_values<S: Schema>(db: &TestDB) -> Vec<(S::Key, S::Value)> {
.iter::<S>(Default::default())
.expect("Failed to create iterator.");
iter.seek_to_first();
iter.collect::<Result<Vec<_>>>().unwrap()
iter.collect::<Result<Vec<_>, AptosDbError>>().unwrap()
}

fn gen_expected_values(values: &[(u32, u32)]) -> Vec<(TestField, TestField)> {
Expand Down
12 changes: 12 additions & 0 deletions storage/storage-interface/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,15 @@ impl From<RecvError> for AptosDbError {
Self::Other(format!("{}", error))
}
}

impl From<std::io::Error> for AptosDbError {
fn from(error: std::io::Error) -> Self {
Self::Other(format!("{}", error))
}
}

impl From<std::num::ParseIntError> for AptosDbError {
fn from(error: std::num::ParseIntError) -> Self {
Self::Other(format!("{}", error))
}
}

0 comments on commit de1632a

Please sign in to comment.