Просмотр исходного кода

runtime/sdk: change return types from i32 to i64

y 2 лет назад
Родитель
Сommit
753d01b249
4 измененных файлов с 33 добавлено и 33 удалено
  1. 16 16
      src/runtime/import/db.rs
  2. 2 2
      src/runtime/import/util.rs
  3. 14 14
      src/sdk/src/db.rs
  4. 1 1
      src/sdk/src/util.rs

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

@@ -57,7 +57,7 @@ impl DbHandle {
 ///
 /// This function should **only** be allowed in `ContractSection::Deploy`, as that
 /// is called when a contract is being (re)deployed and databases have to be created.
-pub(crate) fn db_init(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn db_init(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
     let cid = &env.contract_id;
 
@@ -171,7 +171,7 @@ pub(crate) fn db_init(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32)
 /// Returns the index of the DbHandle in the db_handles Vector on success. Otherwise, returns
 /// a negative error value.
 /// This function can be called from any [`ContractSection`].
-pub(crate) fn db_lookup(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn db_lookup(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
     let cid = &env.contract_id;
 
@@ -254,7 +254,7 @@ pub(crate) fn db_lookup(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32
 
     // Make sure we don't duplicate the DbHandle in the vec
     if let Some(index) = db_handles.iter().position(|x| x == &db_handle) {
-        return index as i32
+        return index as i64
     }
 
     // Push the new DbHandle to the Vec of opened DbHandles
@@ -274,7 +274,7 @@ pub(crate) fn db_lookup(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32
 /// the key-value pair. The DbHandle must match the ContractId.
 /// This function can be called only from the Deploy or Update [`ContractSection`].
 /// Returns `0` on success, otherwise returns a (negative) error value.
-pub(crate) fn db_set(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn db_set(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
 
     if let Err(e) = acl_allow(env, &[ContractSection::Deploy, ContractSection::Update]) {
@@ -370,7 +370,7 @@ pub(crate) fn db_set(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
 /// Remove a key from the database.
 /// This function can be called only from the Deploy or Update [`ContractSection`].
 /// Returns `0` on success, otherwise returns a (negative) error value.
-pub(crate) fn db_del(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn db_del(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
 
     if let Err(e) = acl_allow(env, &[ContractSection::Deploy, ContractSection::Update]) {
@@ -458,7 +458,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
     {
         error!(target: "runtime::db::db_get", "[wasm-runtime] db_get ACL denied: {}", e);
         // TODO: FIXME: We have to fix up the errors used within runtime and the sdk
-        return CALLER_ACCESS_DENIED.into()
+        return CALLER_ACCESS_DENIED
     }
 
     // Ensure that it is possible to read memory
@@ -466,13 +466,13 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
 
     let Ok(mem_slice) = ptr.slice(&memory_view, ptr_len) else {
         error!(target: "runtime::db::db_get", "Failed to make slice from ptr");
-        return DB_GET_FAILED.into()
+        return DB_GET_FAILED
     };
 
     let mut buf = vec![0_u8; ptr_len as usize];
     if let Err(e) = mem_slice.read_slice(&mut buf) {
         error!(target: "runtime::db::db_get", "Failed to read from memory slice: {}", e);
-        return DB_GET_FAILED.into()
+        return DB_GET_FAILED
     };
 
     let mut buf_reader = Cursor::new(buf);
@@ -482,7 +482,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
         Ok(v) => v,
         Err(e) => {
             error!(target: "runtime::db::db_get", "Failed to decode DbHandle: {}", e);
-            return DB_GET_FAILED.into()
+            return DB_GET_FAILED
         }
     };
     let db_handle_index = db_handle_index as usize;
@@ -492,7 +492,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
         Ok(v) => v,
         Err(e) => {
             error!(target: "runtime::db::db_get", "Failed to decode key from vec: {}", e);
-            return DB_GET_FAILED.into()
+            return DB_GET_FAILED
         }
     };
 
@@ -500,7 +500,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
     // supplied.
     if buf_reader.position() != ptr_len as u64 {
         error!(target: "runtime::db::db_get", "[wasm-runtime] Trailing bytes in argument stream");
-        return DB_GET_FAILED.into()
+        return DB_GET_FAILED
     }
 
     let db_handles = env.db_handles.borrow();
@@ -508,7 +508,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
     // Ensure that the index is within bounds
     if db_handles.len() <= db_handle_index {
         error!(target: "runtime::db::db_get", "Requested DbHandle that is out of bounds");
-        return DB_GET_FAILED.into()
+        return DB_GET_FAILED
     }
 
     // Get DbHandle using db_handle_index
@@ -520,7 +520,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
             Ok(v) => v,
             Err(e) => {
                 error!(target: "runtime::db::db_get", "Internal error getting from tree: {}", e);
-                return DB_GET_FAILED.into()
+                return DB_GET_FAILED
             }
         };
 
@@ -543,7 +543,7 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
 /// This function can be called by any [`ContractSection`].
 /// Returns `1` if the key is found. Returns `0` if the key is not found and there are no errors.
 /// Otherwise, returns a (negative) error code.
-pub(crate) fn db_contains_key(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn db_contains_key(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
 
     if let Err(e) = acl_allow(
@@ -616,7 +616,7 @@ pub(crate) fn db_contains_key(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_le
     // Lookup key parameter in the database
     match env.blockchain.lock().unwrap().overlay.lock().unwrap().contains_key(&db_handle.tree, &key)
     {
-        Ok(v) => i32::from(v), // <- 0=false, 1=true
+        Ok(v) => i64::from(v), // <- 0=false, 1=true. Convert bool to i64.
         Err(e) => {
             error!(target: "runtime::db::db_contains_key", "[wasm-runtime] sled.tree.contains_key failed: {}", e);
             DB_CONTAINS_KEY_FAILED
@@ -627,7 +627,7 @@ pub(crate) fn db_contains_key(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_le
 /// Given a zkas circuit, create a VerifyingKey and insert them both into the db.
 /// This function can called only from the Deploy [`ContractSection`].
 /// Returns `0` on success, otherwise returns a (negative) error code.
-pub(crate) fn zkas_db_set(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i32 {
+pub(crate) fn zkas_db_set(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -> i64 {
     let env = ctx.data();
 
     if let Err(e) = acl_allow(env, &[ContractSection::Deploy]) {

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

@@ -194,14 +194,14 @@ pub(crate) fn get_slot(ctx: FunctionEnvMut<Env>, slot: u64) -> i64 {
         env.contract_section != ContractSection::Metadata
     {
         error!(target: "runtime::db::db_get_slot()", "db_get_slot called in unauthorized section");
-        return CALLER_ACCESS_DENIED.into()
+        return CALLER_ACCESS_DENIED
     }
 
     let ret = match env.blockchain.lock().unwrap().slots.get_by_id(slot) {
         Ok(v) => v,
         Err(e) => {
             error!(target: "runtime::db::db_get_slot()", "Internal error getting from slots tree: {}", e);
-            return DB_GET_FAILED.into()
+            return DB_GET_FAILED
         }
     };
 

+ 14 - 14
src/sdk/src/db.rs

@@ -26,14 +26,14 @@ use super::{
 
 pub type DbHandle = u32;
 
-pub const DB_SUCCESS: i32 = 0;
-pub const CALLER_ACCESS_DENIED: i32 = -1;
-pub const DB_INIT_FAILED: i32 = -2;
-pub const DB_LOOKUP_FAILED: i32 = -3;
-pub const DB_GET_FAILED: i32 = -4;
-pub const DB_CONTAINS_KEY_FAILED: i32 = -5;
-pub const DB_SET_FAILED: i32 = -6;
-pub const DB_DEL_FAILED: i32 = -7;
+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
@@ -184,12 +184,12 @@ pub fn zkas_db_set(bincode: &[u8]) -> GenericResult<()> {
 }
 
 extern "C" {
-    fn db_init_(ptr: *const u8, len: u32) -> i32;
-    fn db_lookup_(ptr: *const u8, len: u32) -> i32;
+    fn db_init_(ptr: *const u8, len: u32) -> i64;
+    fn db_lookup_(ptr: *const u8, len: u32) -> i64;
     fn db_get_(ptr: *const u8, len: u32) -> i64;
-    fn db_contains_key_(ptr: *const u8, len: u32) -> i32;
-    fn db_set_(ptr: *const u8, len: u32) -> i32;
-    fn db_del_(ptr: *const u8, len: u32) -> i32;
+    fn db_contains_key_(ptr: *const u8, len: u32) -> i64;
+    fn db_set_(ptr: *const u8, len: u32) -> i64;
+    fn db_del_(ptr: *const u8, len: u32) -> i64;
 
-    fn zkas_db_set_(ptr: *const u8, len: u32) -> i32;
+    fn zkas_db_set_(ptr: *const u8, len: u32) -> i64;
 }

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

@@ -45,7 +45,7 @@ pub fn get_object_size(object_index: u32) -> i64 {
 /// Auxiliary function to parse db_get and get_slot return value.
 pub(crate) fn parse_ret(ret: i64) -> GenericResult<Option<Vec<u8>>> {
     if ret < 0 {
-        match ret as i32 {
+        match ret {
             CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
             DB_GET_FAILED => return Err(ContractError::DbGetFailed),
             -127 => return Ok(None),