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

wasm: clean up the db interface

x 3 лет назад
Родитель
Сommit
98921f7cc8

+ 6 - 18
src/runtime/import/chain_state.rs

@@ -34,30 +34,18 @@ pub(crate) fn set_update(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u3
         ContractSection::Exec => {
             let memory_view = env.memory_view(&ctx);
 
-            // FIXME: make me preettty!
-            let slice = ptr.slice(&memory_view, len);
-            if slice.is_err() {
+            let Ok(slice) = ptr.slice(&memory_view, len) else {
                 return -2
-            }
-            let slice = slice.unwrap();
-
-            // FIXME: make me double pretty
-            // before:
-            //let update_data = slice.read_to_vec();
-            //if update_data.is_err() {
-            //    return -2;
-            //}
-            //let update_data = update_data.unwrap();
+            };
 
-            // after:
             let Ok(update_data) = slice.read_to_vec() else {
                 return -2;
             };
-            //
 
-            // FIXME: Shouldn't assert here, but rather return an error.
-            // An assert would make the host panic.
-            assert!(env.contract_update.take().is_none());
+            // This function should only ever be called once on the runtime.
+            if !env.contract_update.take().is_none() {
+                return -3
+            }
             let func_id = update_data[0];
             let update_data = &update_data[1..];
             env.contract_update.set(Some((func_id, update_data.to_vec())));

+ 4 - 1
src/runtime/import/mod.rs

@@ -1,5 +1,8 @@
+/// Host functions for interacting with db backend
+pub(crate) mod db;
+
 /// Host functions for querying blockchain state through `MemoryState`
 pub(crate) mod chain_state;
-///
+
 /// Host functions for utilities
 pub(crate) mod util;

+ 56 - 26
src/runtime/vm_runtime.rs

@@ -141,32 +141,62 @@ impl Runtime {
         );
 
         let imports = imports! {
-            "env" => {
-                "drk_log_" => Function::new_typed_with_env(
-                    &mut store,
-                    &ctx,
-                    import::util::drk_log,
-                ),
-
-                "nullifier_exists_" => Function::new_typed_with_env(
-                    &mut store,
-                    &ctx,
-                    import::chain_state::nullifier_exists,
-                ),
-
-                "is_valid_merkle_" => Function::new_typed_with_env(
-                    &mut store,
-                    &ctx,
-                    import::chain_state::is_valid_merkle,
-                ),
-
-                "set_update_" => Function::new_typed_with_env(
-                    &mut store,
-                    &ctx,
-                    import::chain_state::set_update,
-                ),
-            }
-        };
+                "env" => {
+                    "drk_log_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::util::drk_log,
+                    ),
+
+                    "nullifier_exists_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::chain_state::nullifier_exists,
+                    ),
+
+                    "is_valid_merkle_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::chain_state::is_valid_merkle,
+                    ),
+
+                    "set_update_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::chain_state::set_update,
+                    ),
+
+                    "db_init_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::db::db_init,
+                    ),
+
+                    "db_get_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::db::db_get,
+                    ),
+
+                    "db_begin_tx_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::db::db_begin_tx,
+                    ),
+
+                    "db_set_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::db::db_set,
+                    ),
+
+                    "db_end_tx_" => Function::new_typed_with_env(
+                        &mut store,
+                        &ctx,
+                        import::db::db_end_tx,
+                    ),
+                }
+            };
 
         debug!(target: "wasm_runtime::new", "Instantiating module");
         let instance = Instance::new(&mut store, &module, &imports)?;

+ 6 - 0
src/sdk/src/error.rs

@@ -43,6 +43,9 @@ pub enum ContractError {
 
     #[error("Error checking merkle root validity")]
     ValidMerkleCheck,
+
+    #[error("Update already set")]
+    UpdateAlreadySet,
 }
 
 /// Builtin return values occupy the upper 32 bits
@@ -59,6 +62,7 @@ pub const SET_UPDATE_ERROR: u64 = to_builtin!(3);
 pub const IO_ERROR: u64 = to_builtin!(4);
 pub const NULLIFIER_EXIST_CHECK: u64 = to_builtin!(5);
 pub const VALID_MERKLE_CHECK: u64 = to_builtin!(6);
+pub const UPDATE_ALREADY_SET: u64 = to_builtin!(7);
 
 impl From<ContractError> for u64 {
     fn from(err: ContractError) -> Self {
@@ -68,6 +72,7 @@ impl From<ContractError> for u64 {
             ContractError::SetUpdateError => SET_UPDATE_ERROR,
             ContractError::NullifierExistCheck => NULLIFIER_EXIST_CHECK,
             ContractError::ValidMerkleCheck => VALID_MERKLE_CHECK,
+            ContractError::UpdateAlreadySet => UPDATE_ALREADY_SET,
             ContractError::Custom(error) => {
                 if error == 0 {
                     CUSTOM_ZERO
@@ -88,6 +93,7 @@ impl From<u64> for ContractError {
             IO_ERROR => Self::IoError("Unknown".to_string()),
             NULLIFIER_EXIST_CHECK => Self::NullifierExistCheck,
             VALID_MERKLE_CHECK => Self::ValidMerkleCheck,
+            UPDATE_ALREADY_SET => Self::UpdateAlreadySet,
             _ => Self::Custom(error as u32),
         }
     }

+ 1 - 0
src/sdk/src/state.rs

@@ -27,6 +27,7 @@ pub fn set_update(update_data: &[u8]) -> Result<(), ContractError> {
         return match set_update_(update_data.as_ptr(), update_data.len() as u32) {
             0 => Ok(()),
             -1 => Err(ContractError::SetUpdateError),
+            -2 => Err(ContractError::UpdateAlreadySet),
             _ => unreachable!(),
         }
     }