From db4c4fb8d1050ed22a0bc87b415cbab4058a353f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s?= <47506558+MegaRedHand@users.noreply.github.com> Date: Thu, 16 Feb 2023 15:39:30 -0300 Subject: [PATCH] chore(db): make database error codes signed (#1236) Co-authored-by: lambdaclass-user --- crates/interfaces/src/db.rs | 16 ++++++++-------- crates/storage/db/src/implementation/mdbx/mod.rs | 8 ++++---- crates/storage/libmdbx-rs/src/error.rs | 11 +++++------ 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/crates/interfaces/src/db.rs b/crates/interfaces/src/db.rs index 9c86b43f33..a6f4ddca2a 100644 --- a/crates/interfaces/src/db.rs +++ b/crates/interfaces/src/db.rs @@ -3,28 +3,28 @@ pub enum Error { /// Failed to open database. #[error("{0:?}")] - DatabaseLocation(u32), + DatabaseLocation(i32), /// Failed to create a table in database. #[error("Table Creating error code: {0:?}")] - TableCreation(u32), + TableCreation(i32), /// Failed to insert a value into a table. #[error("Database write error code: {0:?}")] - Write(u32), + Write(i32), /// Failed to get a value into a table. #[error("Database read error code: {0:?}")] - Read(u32), + Read(i32), /// Failed to delete a `(key, value)` pair into a table. #[error("Database delete error code: {0:?}")] - Delete(u32), + Delete(i32), /// Failed to commit transaction changes into the database. #[error("Database commit error code: {0:?}")] - Commit(u32), + Commit(i32), /// Failed to initiate a transaction. #[error("Initialization of transaction errored with code: {0:?}")] - InitTransaction(u32), + InitTransaction(i32), /// Failed to initiate a cursor. #[error("Initialization of cursor errored with code: {0:?}")] - InitCursor(u32), + InitCursor(i32), /// Failed to decode a key from a table. #[error("Error decoding value.")] DecodeError, diff --git a/crates/storage/db/src/implementation/mdbx/mod.rs b/crates/storage/db/src/implementation/mdbx/mod.rs index 935a9285f3..fc76e9a76b 100644 --- a/crates/storage/db/src/implementation/mdbx/mod.rs +++ b/crates/storage/db/src/implementation/mdbx/mod.rs @@ -378,7 +378,7 @@ mod tests { assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero())))); // INSERT (failure) - assert_eq!(cursor.insert(key_to_insert, H256::zero()), Err(Error::Write(4294936497))); + assert_eq!(cursor.insert(key_to_insert, H256::zero()), Err(Error::Write(-30799))); assert_eq!(cursor.current(), Ok(Some((key_to_insert, H256::zero())))); tx.commit().expect(ERROR_COMMIT); @@ -467,7 +467,7 @@ mod tests { let key_to_append = 2; let tx = db.tx_mut().expect(ERROR_INIT_TX); let mut cursor = tx.cursor_write::().unwrap(); - assert_eq!(cursor.append(key_to_append, H256::zero()), Err(Error::Write(4294936878))); + assert_eq!(cursor.append(key_to_append, H256::zero()), Err(Error::Write(-30418))); assert_eq!(cursor.current(), Ok(Some((5, H256::zero())))); // the end of table tx.commit().expect(ERROR_COMMIT); @@ -507,14 +507,14 @@ mod tests { transition_id, AccountBeforeTx { address: Address::from_low_u64_be(subkey_to_append), info: None } ), - Err(Error::Write(4294936878)) + Err(Error::Write(-30418)) ); assert_eq!( cursor.append( transition_id - 1, AccountBeforeTx { address: Address::from_low_u64_be(subkey_to_append), info: None } ), - Err(Error::Write(4294936878)) + Err(Error::Write(-30418)) ); assert_eq!( cursor.append( diff --git a/crates/storage/libmdbx-rs/src/error.rs b/crates/storage/libmdbx-rs/src/error.rs index 54ea5e446a..0bc8e17724 100644 --- a/crates/storage/libmdbx-rs/src/error.rs +++ b/crates/storage/libmdbx-rs/src/error.rs @@ -96,8 +96,8 @@ impl Error { } /// Converts an [Error] to the raw error code. - pub fn to_err_code(&self) -> u32 { - let err_code = match self { + pub fn to_err_code(&self) -> i32 { + match self { Error::KeyExist => ffi::MDBX_KEYEXIST, Error::NotFound => ffi::MDBX_NOTFOUND, Error::NoData => ffi::MDBX_ENODATA, @@ -129,12 +129,11 @@ impl Error { Error::BadSignature => ffi::MDBX_EBADSIGN, Error::Other(err_code) => *err_code, _ => unreachable!(), - }; - err_code as u32 + } } } -impl From for u32 { +impl From for i32 { fn from(value: Error) -> Self { value.to_err_code() } @@ -145,7 +144,7 @@ impl fmt::Display for Error { let value = match self { Self::DecodeErrorLenDiff => "Mismatched data length", _ => unsafe { - let err = ffi::mdbx_strerror(self.to_err_code() as i32); + let err = ffi::mdbx_strerror(self.to_err_code()); str::from_utf8_unchecked(CStr::from_ptr(err).to_bytes()) }, };