diff --git a/src/runtime/import/db.rs b/src/runtime/import/db.rs index e4735cfd1..7cf6e43e4 100644 --- a/src/runtime/import/db.rs +++ b/src/runtime/import/db.rs @@ -654,13 +654,13 @@ pub(crate) fn db_get(ctx: FunctionEnvMut, ptr: WasmPtr, ptr_len: u32) - } }; - // Return error if the data is empty + // Return special error if the data is empty let Some(return_data) = ret else { debug!( target: "runtime::db::db_get", "[WASM] [{}] db_get(): Return data is empty", cid, ); - return darkfi_sdk::error::INTERNAL_ERROR + return darkfi_sdk::error::DB_GET_EMPTY }; if return_data.len() > u32::MAX as usize { diff --git a/src/sdk/src/db.rs b/src/sdk/src/db.rs index cb8840172..937d5a70b 100644 --- a/src/sdk/src/db.rs +++ b/src/sdk/src/db.rs @@ -26,15 +26,6 @@ use super::{ pub type DbHandle = u32; -pub const DB_SUCCESS: i64 = 0; -pub const CALLER_ACCESS_DENIED: i64 = -1; -pub const DB_INIT_FAILED: i64 = -2; -pub const DB_LOOKUP_FAILED: i64 = -3; -pub const DB_GET_FAILED: i64 = -4; -pub const DB_CONTAINS_KEY_FAILED: i64 = -5; -pub const DB_SET_FAILED: i64 = -6; -pub const DB_DEL_FAILED: i64 = -7; - /// Create a new database instance for the given contract. /// This should be called in the `init_contract()` section to create any databases /// that the contract might need or use. @@ -50,11 +41,7 @@ pub fn db_init(contract_id: ContractId, db_name: &str) -> GenericResult return Err(ContractError::CallerAccessDenied), - DB_INIT_FAILED => return Err(ContractError::DbInitFailed), - _ => unimplemented!(), - } + return Err(ContractError::from(ret)) } Ok(ret as u32) @@ -72,11 +59,7 @@ pub fn db_lookup(contract_id: ContractId, db_name: &str) -> GenericResult return Err(ContractError::CallerAccessDenied), - DB_LOOKUP_FAILED => return Err(ContractError::DbLookupFailed), - _ => unimplemented!(), - } + return Err(ContractError::from(ret)) } Ok(ret as u32) @@ -113,12 +96,14 @@ pub fn db_contains_key(db_handle: DbHandle, key: &[u8]) -> GenericResult { let ret = unsafe { db_contains_key_(buf.as_ptr(), len as u32) }; + if ret < 0 { + return Err(ContractError::from(ret)) + } + match ret { - CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied), - DB_CONTAINS_KEY_FAILED => Err(ContractError::DbContainsKeyFailed), 0 => Ok(false), 1 => Ok(true), - _ => unimplemented!(), + _ => unreachable!(), } } @@ -136,12 +121,13 @@ pub fn db_set(db_handle: DbHandle, key: &[u8], value: &[u8]) -> GenericResult<() len += key.to_vec().encode(&mut buf)?; len += value.to_vec().encode(&mut buf)?; - match db_set_(buf.as_ptr(), len as u32) { - CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied), - DB_SET_FAILED => Err(ContractError::DbSetFailed), - DB_SUCCESS => Ok(()), - _ => unreachable!(), + let ret = db_set_(buf.as_ptr(), len as u32); + + if ret != crate::entrypoint::SUCCESS { + return Err(ContractError::from(ret)) } + + Ok(()) } } @@ -158,12 +144,13 @@ pub fn db_del(db_handle: DbHandle, key: &[u8]) -> GenericResult<()> { len += db_handle.encode(&mut buf)?; len += key.to_vec().encode(&mut buf)?; - match db_del_(buf.as_ptr(), len as u32) { - CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied), - DB_DEL_FAILED => Err(ContractError::DbDelFailed), - DB_SUCCESS => Ok(()), - _ => unreachable!(), + let ret = db_del_(buf.as_ptr(), len as u32); + + if ret != crate::entrypoint::SUCCESS { + return Err(ContractError::from(ret)) } + + Ok(()) } } @@ -174,12 +161,13 @@ pub fn zkas_db_set(bincode: &[u8]) -> GenericResult<()> { let mut buf = vec![]; len += bincode.to_vec().encode(&mut buf)?; - match zkas_db_set_(buf.as_ptr(), len as u32) { - CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied), - DB_SET_FAILED => Err(ContractError::DbSetFailed), - DB_SUCCESS => Ok(()), - _ => unreachable!(), + let ret = zkas_db_set_(buf.as_ptr(), len as u32); + + if ret != crate::entrypoint::SUCCESS { + return Err(ContractError::from(ret)) } + + Ok(()) } } diff --git a/src/sdk/src/error.rs b/src/sdk/src/error.rs index 2c9e98504..f3dd37d9a 100644 --- a/src/sdk/src/error.rs +++ b/src/sdk/src/error.rs @@ -69,6 +69,9 @@ pub enum ContractError { #[error("Db get failed")] DbGetFailed, + #[error("Db get empty")] + DbGetEmpty, + #[error("Db contains_key failed")] DbContainsKeyFailed, @@ -111,13 +114,14 @@ pub const DB_NOT_FOUND: i64 = to_builtin!(10); pub const DB_SET_FAILED: i64 = to_builtin!(11); pub const DB_LOOKUP_FAILED: i64 = to_builtin!(12); pub const DB_GET_FAILED: i64 = to_builtin!(13); -pub const DB_CONTAINS_KEY_FAILED: i64 = to_builtin!(14); -pub const INVALID_FUNCTION: i64 = to_builtin!(15); -pub const DB_DEL_FAILED: i64 = to_builtin!(16); -pub const SMT_INVALID_LEAF: i64 = to_builtin!(17); -pub const SMT_INVALID_PATH_NODES: i64 = to_builtin!(18); -pub const GET_SYSTEM_TIME_FAILED: i64 = to_builtin!(19); -pub const DATA_TOO_LARGE: i64 = to_builtin!(20); +pub const DB_GET_EMPTY: i64 = to_builtin!(14); +pub const DB_CONTAINS_KEY_FAILED: i64 = to_builtin!(15); +pub const INVALID_FUNCTION: i64 = to_builtin!(16); +pub const DB_DEL_FAILED: i64 = to_builtin!(17); +pub const SMT_INVALID_LEAF: i64 = to_builtin!(18); +pub const SMT_INVALID_PATH_NODES: i64 = to_builtin!(19); +pub const GET_SYSTEM_TIME_FAILED: i64 = to_builtin!(20); +pub const DATA_TOO_LARGE: i64 = to_builtin!(21); impl From for i64 { fn from(err: ContractError) -> Self { @@ -134,6 +138,7 @@ impl From for i64 { ContractError::DbSetFailed => DB_SET_FAILED, ContractError::DbLookupFailed => DB_LOOKUP_FAILED, ContractError::DbGetFailed => DB_GET_FAILED, + ContractError::DbGetEmpty => DB_GET_EMPTY, ContractError::DbContainsKeyFailed => DB_CONTAINS_KEY_FAILED, ContractError::InvalidFunction => INVALID_FUNCTION, ContractError::DbDelFailed => DB_DEL_FAILED, @@ -168,6 +173,7 @@ impl From for ContractError { DB_SET_FAILED => Self::DbSetFailed, DB_LOOKUP_FAILED => Self::DbLookupFailed, DB_GET_FAILED => Self::DbGetFailed, + DB_GET_EMPTY => Self::DbGetEmpty, DB_CONTAINS_KEY_FAILED => Self::DbContainsKeyFailed, INVALID_FUNCTION => Self::InvalidFunction, DB_DEL_FAILED => Self::DbDelFailed, diff --git a/src/sdk/src/merkle.rs b/src/sdk/src/merkle.rs index 105ba7108..09d6560b0 100644 --- a/src/sdk/src/merkle.rs +++ b/src/sdk/src/merkle.rs @@ -54,5 +54,5 @@ pub fn merkle_add( } extern "C" { - fn merkle_add_(ptr: *const u8, len: u32) -> i32; + fn merkle_add_(ptr: *const u8, len: u32) -> i64; } diff --git a/src/sdk/src/util.rs b/src/sdk/src/util.rs index a4e45abbf..69371c767 100644 --- a/src/sdk/src/util.rs +++ b/src/sdk/src/util.rs @@ -16,10 +16,7 @@ * along with this program. If not, see . */ -use super::{ - db::{CALLER_ACCESS_DENIED, DB_GET_FAILED}, - error::{ContractError, GenericResult}, -}; +use super::error::{ContractError, GenericResult}; /// Calls the `set_return_data` WASM function. Returns Ok(()) on success. /// Otherwise, convert the i64 error code into a [`ContractError`]. @@ -58,12 +55,12 @@ pub fn get_object_size(object_index: u32) -> i64 { pub(crate) fn parse_ret(ret: i64) -> GenericResult>> { // Negative values represent an error code. if ret < 0 { - match ret { - CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied), - DB_GET_FAILED => return Err(ContractError::DbGetFailed), - -127 => return Ok(None), - _ => unimplemented!(), + // However here on the special case, we'll return Ok(None) + if ret == crate::error::DB_GET_EMPTY { + return Ok(None) } + + return Err(ContractError::from(ret)) } // Ensure that the returned value fits into the u32 datatype.