mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: rewrite all error messages for consistency (#5176)
Co-authored-by: Roman Krasiuk <rokrassyuk@gmail.com>
This commit is contained in:
@ -46,15 +46,11 @@ impl<'a, E: EnvironmentKind> DatabaseGAT<'a> for Env<E> {
|
||||
|
||||
impl<E: EnvironmentKind> Database for Env<E> {
|
||||
fn tx(&self) -> Result<<Self as DatabaseGAT<'_>>::TX, DatabaseError> {
|
||||
Ok(Tx::new(
|
||||
self.inner.begin_ro_txn().map_err(|e| DatabaseError::InitTransaction(e.into()))?,
|
||||
))
|
||||
Ok(Tx::new(self.inner.begin_ro_txn().map_err(|e| DatabaseError::InitTx(e.into()))?))
|
||||
}
|
||||
|
||||
fn tx_mut(&self) -> Result<<Self as DatabaseGAT<'_>>::TXMut, DatabaseError> {
|
||||
Ok(Tx::new(
|
||||
self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTransaction(e.into()))?,
|
||||
))
|
||||
Ok(Tx::new(self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?))
|
||||
}
|
||||
}
|
||||
|
||||
@ -120,15 +116,14 @@ impl<E: EnvironmentKind> Env<E> {
|
||||
}
|
||||
}
|
||||
|
||||
let env =
|
||||
Env { inner: inner_env.open(path).map_err(|e| DatabaseError::FailedToOpen(e.into()))? };
|
||||
let env = Env { inner: inner_env.open(path).map_err(|e| DatabaseError::Open(e.into()))? };
|
||||
|
||||
Ok(env)
|
||||
}
|
||||
|
||||
/// Creates all the defined tables, if necessary.
|
||||
pub fn create_tables(&self) -> Result<(), DatabaseError> {
|
||||
let tx = self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTransaction(e.into()))?;
|
||||
let tx = self.inner.begin_rw_txn().map_err(|e| DatabaseError::InitTx(e.into()))?;
|
||||
|
||||
for table in Tables::ALL {
|
||||
let flags = match table.table_type() {
|
||||
@ -137,7 +132,7 @@ impl<E: EnvironmentKind> Env<E> {
|
||||
};
|
||||
|
||||
tx.create_db(Some(table.name()), flags)
|
||||
.map_err(|e| DatabaseError::TableCreation(e.into()))?;
|
||||
.map_err(|e| DatabaseError::CreateTable(e.into()))?;
|
||||
}
|
||||
|
||||
tx.commit().map_err(|e| DatabaseError::Commit(e.into()))?;
|
||||
|
||||
@ -31,8 +31,7 @@ where
|
||||
T: ScaleValue + parity_scale_codec::Decode + Sync + Send + std::fmt::Debug,
|
||||
{
|
||||
fn decompress<B: AsRef<[u8]>>(value: B) -> Result<T, DatabaseError> {
|
||||
parity_scale_codec::Decode::decode(&mut value.as_ref())
|
||||
.map_err(|_| DatabaseError::DecodeError)
|
||||
parity_scale_codec::Decode::decode(&mut value.as_ref()).map_err(|_| DatabaseError::Decode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -187,29 +187,22 @@ tables!([
|
||||
(PruneCheckpoints, TableType::Table)
|
||||
]);
|
||||
|
||||
#[macro_export]
|
||||
/// Macro to declare key value table.
|
||||
#[macro_export]
|
||||
macro_rules! table {
|
||||
($(#[$docs:meta])+ ( $table_name:ident ) $key:ty | $value:ty) => {
|
||||
$(#[$docs])+
|
||||
///
|
||||
#[doc = concat!("Takes [`", stringify!($key), "`] as a key and returns [`", stringify!($value), "`]")]
|
||||
#[doc = concat!("Takes [`", stringify!($key), "`] as a key and returns [`", stringify!($value), "`].")]
|
||||
#[derive(Clone, Copy, Debug, Default)]
|
||||
pub struct $table_name;
|
||||
|
||||
impl $crate::table::Table for $table_name {
|
||||
const NAME: &'static str = $table_name::const_name();
|
||||
const NAME: &'static str = stringify!($table_name);
|
||||
type Key = $key;
|
||||
type Value = $value;
|
||||
}
|
||||
|
||||
impl $table_name {
|
||||
#[doc=concat!("Return ", stringify!($table_name), " as it is present inside the database.")]
|
||||
pub const fn const_name() -> &'static str {
|
||||
stringify!($table_name)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for $table_name {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", stringify!($table_name))
|
||||
@ -225,7 +218,7 @@ macro_rules! dupsort {
|
||||
table!(
|
||||
$(#[$docs])+
|
||||
///
|
||||
#[doc = concat!("`DUPSORT` table with subkey being: [`", stringify!($subkey), "`].")]
|
||||
#[doc = concat!("`DUPSORT` table with subkey being: [`", stringify!($subkey), "`]")]
|
||||
( $table_name ) $key | $value
|
||||
);
|
||||
impl DupSort for $table_name {
|
||||
@ -430,37 +423,36 @@ pub type StageId = String;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::*;
|
||||
|
||||
const TABLES: [(TableType, &str); NUM_TABLES] = [
|
||||
(TableType::Table, CanonicalHeaders::const_name()),
|
||||
(TableType::Table, HeaderTD::const_name()),
|
||||
(TableType::Table, HeaderNumbers::const_name()),
|
||||
(TableType::Table, Headers::const_name()),
|
||||
(TableType::Table, BlockBodyIndices::const_name()),
|
||||
(TableType::Table, BlockOmmers::const_name()),
|
||||
(TableType::Table, BlockWithdrawals::const_name()),
|
||||
(TableType::Table, TransactionBlock::const_name()),
|
||||
(TableType::Table, Transactions::const_name()),
|
||||
(TableType::Table, TxHashNumber::const_name()),
|
||||
(TableType::Table, Receipts::const_name()),
|
||||
(TableType::Table, PlainAccountState::const_name()),
|
||||
(TableType::DupSort, PlainStorageState::const_name()),
|
||||
(TableType::Table, Bytecodes::const_name()),
|
||||
(TableType::Table, AccountHistory::const_name()),
|
||||
(TableType::Table, StorageHistory::const_name()),
|
||||
(TableType::DupSort, AccountChangeSet::const_name()),
|
||||
(TableType::DupSort, StorageChangeSet::const_name()),
|
||||
(TableType::Table, HashedAccount::const_name()),
|
||||
(TableType::DupSort, HashedStorage::const_name()),
|
||||
(TableType::Table, AccountsTrie::const_name()),
|
||||
(TableType::DupSort, StoragesTrie::const_name()),
|
||||
(TableType::Table, TxSenders::const_name()),
|
||||
(TableType::Table, SyncStage::const_name()),
|
||||
(TableType::Table, SyncStageProgress::const_name()),
|
||||
(TableType::Table, PruneCheckpoints::const_name()),
|
||||
(TableType::Table, CanonicalHeaders::NAME),
|
||||
(TableType::Table, HeaderTD::NAME),
|
||||
(TableType::Table, HeaderNumbers::NAME),
|
||||
(TableType::Table, Headers::NAME),
|
||||
(TableType::Table, BlockBodyIndices::NAME),
|
||||
(TableType::Table, BlockOmmers::NAME),
|
||||
(TableType::Table, BlockWithdrawals::NAME),
|
||||
(TableType::Table, TransactionBlock::NAME),
|
||||
(TableType::Table, Transactions::NAME),
|
||||
(TableType::Table, TxHashNumber::NAME),
|
||||
(TableType::Table, Receipts::NAME),
|
||||
(TableType::Table, PlainAccountState::NAME),
|
||||
(TableType::DupSort, PlainStorageState::NAME),
|
||||
(TableType::Table, Bytecodes::NAME),
|
||||
(TableType::Table, AccountHistory::NAME),
|
||||
(TableType::Table, StorageHistory::NAME),
|
||||
(TableType::DupSort, AccountChangeSet::NAME),
|
||||
(TableType::DupSort, StorageChangeSet::NAME),
|
||||
(TableType::Table, HashedAccount::NAME),
|
||||
(TableType::DupSort, HashedStorage::NAME),
|
||||
(TableType::Table, AccountsTrie::NAME),
|
||||
(TableType::DupSort, StoragesTrie::NAME),
|
||||
(TableType::Table, TxSenders::NAME),
|
||||
(TableType::Table, SyncStage::NAME),
|
||||
(TableType::Table, SyncStageProgress::NAME),
|
||||
(TableType::Table, PruneCheckpoints::NAME),
|
||||
];
|
||||
|
||||
#[test]
|
||||
|
||||
@ -116,8 +116,7 @@ impl Encode for BlockNumberAddress {
|
||||
impl Decode for BlockNumberAddress {
|
||||
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
|
||||
let value = value.as_ref();
|
||||
let num =
|
||||
u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::DecodeError)?);
|
||||
let num = u64::from_be_bytes(value[..8].try_into().map_err(|_| DatabaseError::Decode)?);
|
||||
let hash = Address::from_slice(&value[8..]);
|
||||
|
||||
Ok(BlockNumberAddress((num, hash)))
|
||||
|
||||
@ -19,6 +19,6 @@ impl Compress for IntegerList {
|
||||
|
||||
impl Decompress for IntegerList {
|
||||
fn decompress<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
|
||||
IntegerList::from_bytes(value.as_ref()).map_err(|_| DatabaseError::DecodeError)
|
||||
IntegerList::from_bytes(value.as_ref()).map_err(|_| DatabaseError::Decode)
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,8 +23,7 @@ pub use sharded_key::ShardedKey;
|
||||
macro_rules! impl_uints {
|
||||
($($name:tt),+) => {
|
||||
$(
|
||||
impl Encode for $name
|
||||
{
|
||||
impl Encode for $name {
|
||||
type Encoded = [u8; std::mem::size_of::<$name>()];
|
||||
|
||||
fn encode(self) -> Self::Encoded {
|
||||
@ -32,12 +31,11 @@ macro_rules! impl_uints {
|
||||
}
|
||||
}
|
||||
|
||||
impl Decode for $name
|
||||
{
|
||||
impl Decode for $name {
|
||||
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, $crate::DatabaseError> {
|
||||
Ok(
|
||||
$name::from_be_bytes(
|
||||
value.as_ref().try_into().map_err(|_| $crate::DatabaseError::DecodeError)?
|
||||
value.as_ref().try_into().map_err(|_| $crate::DatabaseError::Decode)?
|
||||
)
|
||||
)
|
||||
}
|
||||
@ -50,6 +48,7 @@ impl_uints!(u64, u32, u16, u8);
|
||||
|
||||
impl Encode for Vec<u8> {
|
||||
type Encoded = Vec<u8>;
|
||||
|
||||
fn encode(self) -> Self::Encoded {
|
||||
self
|
||||
}
|
||||
@ -63,6 +62,7 @@ impl Decode for Vec<u8> {
|
||||
|
||||
impl Encode for Address {
|
||||
type Encoded = [u8; 20];
|
||||
|
||||
fn encode(self) -> Self::Encoded {
|
||||
self.0 .0
|
||||
}
|
||||
@ -76,6 +76,7 @@ impl Decode for Address {
|
||||
|
||||
impl Encode for B256 {
|
||||
type Encoded = [u8; 32];
|
||||
|
||||
fn encode(self) -> Self::Encoded {
|
||||
self.0
|
||||
}
|
||||
@ -83,12 +84,13 @@ impl Encode for B256 {
|
||||
|
||||
impl Decode for B256 {
|
||||
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
|
||||
Ok(B256::from_slice(value.as_ref()))
|
||||
Ok(B256::new(value.as_ref().try_into().map_err(|_| DatabaseError::Decode)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Encode for String {
|
||||
type Encoded = Vec<u8>;
|
||||
|
||||
fn encode(self) -> Self::Encoded {
|
||||
self.into_bytes()
|
||||
}
|
||||
@ -96,7 +98,7 @@ impl Encode for String {
|
||||
|
||||
impl Decode for String {
|
||||
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DatabaseError> {
|
||||
String::from_utf8(value.as_ref().to_vec()).map_err(|_| DatabaseError::DecodeError)
|
||||
String::from_utf8(value.as_ref().to_vec()).map_err(|_| DatabaseError::Decode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -69,7 +69,7 @@ where
|
||||
let tx_num_index = value.len() - 8;
|
||||
|
||||
let highest_tx_number = u64::from_be_bytes(
|
||||
value[tx_num_index..].try_into().map_err(|_| DatabaseError::DecodeError)?,
|
||||
value[tx_num_index..].try_into().map_err(|_| DatabaseError::Decode)?,
|
||||
);
|
||||
let key = T::decode(&value[..tx_num_index])?;
|
||||
|
||||
|
||||
@ -63,7 +63,7 @@ impl Decode for StorageShardedKey {
|
||||
let tx_num_index = value.len() - 8;
|
||||
|
||||
let highest_tx_number = u64::from_be_bytes(
|
||||
value[tx_num_index..].try_into().map_err(|_| DatabaseError::DecodeError)?,
|
||||
value[tx_num_index..].try_into().map_err(|_| DatabaseError::Decode)?,
|
||||
);
|
||||
let address = Address::decode(&value[..20])?;
|
||||
let storage_key = B256::decode(&value[20..52])?;
|
||||
|
||||
@ -15,14 +15,13 @@ pub const DB_VERSION: u64 = 1;
|
||||
#[allow(missing_docs)]
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum DatabaseVersionError {
|
||||
#[error("Unable to determine the version of the database, file is missing.")]
|
||||
#[error("unable to determine the version of the database, file is missing")]
|
||||
MissingFile,
|
||||
#[error("Unable to determine the version of the database, file is malformed.")]
|
||||
#[error("unable to determine the version of the database, file is malformed")]
|
||||
MalformedFile,
|
||||
#[error(
|
||||
"Breaking database change detected. \
|
||||
Your database version (v{version}) is incompatible with the latest database version (v{}).",
|
||||
DB_VERSION.to_string()
|
||||
"breaking database change detected: your database version (v{version}) \
|
||||
is incompatible with the latest database version (v{DB_VERSION})"
|
||||
)]
|
||||
VersionMismatch { version: u64 },
|
||||
#[error("IO error occurred while reading {path}: {err}")]
|
||||
|
||||
Reference in New Issue
Block a user