Forráskód Böngészése

sdk: Apply relevant changes related to 3240221614727e7bb754de6b33397dc90a92ddee

parazyd 2 éve
szülő
commit
e340fa6824
5 módosított fájl, 47 hozzáadás és 56 törlés
  1. 2 2
      src/runtime/import/db.rs
  2. 25 37
      src/sdk/src/db.rs
  3. 13 7
      src/sdk/src/error.rs
  4. 1 1
      src/sdk/src/merkle.rs
  5. 6 9
      src/sdk/src/util.rs

+ 2 - 2
src/runtime/import/db.rs

@@ -654,13 +654,13 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
             }
             }
         };
         };
 
 
-    // Return error if the data is empty
+    // Return special error if the data is empty
     let Some(return_data) = ret else {
     let Some(return_data) = ret else {
         debug!(
         debug!(
             target: "runtime::db::db_get",
             target: "runtime::db::db_get",
             "[WASM] [{}] db_get(): Return data is empty", cid,
             "[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 {
     if return_data.len() > u32::MAX as usize {

+ 25 - 37
src/sdk/src/db.rs

@@ -26,15 +26,6 @@ use super::{
 
 
 pub type DbHandle = u32;
 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.
 /// Create a new database instance for the given contract.
 /// This should be called in the `init_contract()` section to create any databases
 /// This should be called in the `init_contract()` section to create any databases
 /// that the contract might need or use.
 /// that the contract might need or use.
@@ -50,11 +41,7 @@ pub fn db_init(contract_id: ContractId, db_name: &str) -> GenericResult<DbHandle
         let ret = db_init_(buf.as_ptr(), len as u32);
         let ret = db_init_(buf.as_ptr(), len as u32);
 
 
         if ret < 0 {
         if ret < 0 {
-            match ret {
-                CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
-                DB_INIT_FAILED => return Err(ContractError::DbInitFailed),
-                _ => unimplemented!(),
-            }
+            return Err(ContractError::from(ret))
         }
         }
 
 
         Ok(ret as u32)
         Ok(ret as u32)
@@ -72,11 +59,7 @@ pub fn db_lookup(contract_id: ContractId, db_name: &str) -> GenericResult<DbHand
         let ret = db_lookup_(buf.as_ptr(), len as u32);
         let ret = db_lookup_(buf.as_ptr(), len as u32);
 
 
         if ret < 0 {
         if ret < 0 {
-            match ret {
-                CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
-                DB_LOOKUP_FAILED => return Err(ContractError::DbLookupFailed),
-                _ => unimplemented!(),
-            }
+            return Err(ContractError::from(ret))
         }
         }
 
 
         Ok(ret as u32)
         Ok(ret as u32)
@@ -113,12 +96,14 @@ pub fn db_contains_key(db_handle: DbHandle, key: &[u8]) -> GenericResult<bool> {
 
 
     let ret = unsafe { db_contains_key_(buf.as_ptr(), len as u32) };
     let ret = unsafe { db_contains_key_(buf.as_ptr(), len as u32) };
 
 
+    if ret < 0 {
+        return Err(ContractError::from(ret))
+    }
+
     match ret {
     match ret {
-        CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied),
-        DB_CONTAINS_KEY_FAILED => Err(ContractError::DbContainsKeyFailed),
         0 => Ok(false),
         0 => Ok(false),
         1 => Ok(true),
         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 += key.to_vec().encode(&mut buf)?;
         len += value.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 += db_handle.encode(&mut buf)?;
         len += key.to_vec().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![];
         let mut buf = vec![];
         len += bincode.to_vec().encode(&mut buf)?;
         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(())
     }
     }
 }
 }
 
 

+ 13 - 7
src/sdk/src/error.rs

@@ -69,6 +69,9 @@ pub enum ContractError {
     #[error("Db get failed")]
     #[error("Db get failed")]
     DbGetFailed,
     DbGetFailed,
 
 
+    #[error("Db get empty")]
+    DbGetEmpty,
+
     #[error("Db contains_key failed")]
     #[error("Db contains_key failed")]
     DbContainsKeyFailed,
     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_SET_FAILED: i64 = to_builtin!(11);
 pub const DB_LOOKUP_FAILED: i64 = to_builtin!(12);
 pub const DB_LOOKUP_FAILED: i64 = to_builtin!(12);
 pub const DB_GET_FAILED: i64 = to_builtin!(13);
 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<ContractError> for i64 {
 impl From<ContractError> for i64 {
     fn from(err: ContractError) -> Self {
     fn from(err: ContractError) -> Self {
@@ -134,6 +138,7 @@ impl From<ContractError> for i64 {
             ContractError::DbSetFailed => DB_SET_FAILED,
             ContractError::DbSetFailed => DB_SET_FAILED,
             ContractError::DbLookupFailed => DB_LOOKUP_FAILED,
             ContractError::DbLookupFailed => DB_LOOKUP_FAILED,
             ContractError::DbGetFailed => DB_GET_FAILED,
             ContractError::DbGetFailed => DB_GET_FAILED,
+            ContractError::DbGetEmpty => DB_GET_EMPTY,
             ContractError::DbContainsKeyFailed => DB_CONTAINS_KEY_FAILED,
             ContractError::DbContainsKeyFailed => DB_CONTAINS_KEY_FAILED,
             ContractError::InvalidFunction => INVALID_FUNCTION,
             ContractError::InvalidFunction => INVALID_FUNCTION,
             ContractError::DbDelFailed => DB_DEL_FAILED,
             ContractError::DbDelFailed => DB_DEL_FAILED,
@@ -168,6 +173,7 @@ impl From<i64> for ContractError {
             DB_SET_FAILED => Self::DbSetFailed,
             DB_SET_FAILED => Self::DbSetFailed,
             DB_LOOKUP_FAILED => Self::DbLookupFailed,
             DB_LOOKUP_FAILED => Self::DbLookupFailed,
             DB_GET_FAILED => Self::DbGetFailed,
             DB_GET_FAILED => Self::DbGetFailed,
+            DB_GET_EMPTY => Self::DbGetEmpty,
             DB_CONTAINS_KEY_FAILED => Self::DbContainsKeyFailed,
             DB_CONTAINS_KEY_FAILED => Self::DbContainsKeyFailed,
             INVALID_FUNCTION => Self::InvalidFunction,
             INVALID_FUNCTION => Self::InvalidFunction,
             DB_DEL_FAILED => Self::DbDelFailed,
             DB_DEL_FAILED => Self::DbDelFailed,

+ 1 - 1
src/sdk/src/merkle.rs

@@ -54,5 +54,5 @@ pub fn merkle_add(
 }
 }
 
 
 extern "C" {
 extern "C" {
-    fn merkle_add_(ptr: *const u8, len: u32) -> i32;
+    fn merkle_add_(ptr: *const u8, len: u32) -> i64;
 }
 }

+ 6 - 9
src/sdk/src/util.rs

@@ -16,10 +16,7 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
 
 
-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.
 /// Calls the `set_return_data` WASM function. Returns Ok(()) on success.
 /// Otherwise, convert the i64 error code into a [`ContractError`].
 /// 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<Option<Vec<u8>>> {
 pub(crate) fn parse_ret(ret: i64) -> GenericResult<Option<Vec<u8>>> {
     // Negative values represent an error code.
     // Negative values represent an error code.
     if ret < 0 {
     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.
     // Ensure that the returned value fits into the u32 datatype.