Explorar o código

wasm: move all host imports into src/runtime/import/

x %!s(int64=3) %!d(string=hai) anos
pai
achega
888402d665

+ 9 - 7
src/runtime/chain_state.rs → src/runtime/import/chain_state.rs

@@ -20,13 +20,15 @@ use darkfi_sdk::crypto::{MerkleNode, Nullifier};
 use log::{debug, error};
 use wasmer::{AsStoreRef, FunctionEnvMut, WasmPtr};
 
-use super::{
-    memory::MemoryManipulation,
-    vm_runtime::{ContractSection, Env},
+use crate::{
+    node::state::ProgramState,
+    runtime::{
+        memory::MemoryManipulation,
+        vm_runtime::{ContractSection, Env},
+    },
 };
-use crate::node::state::ProgramState;
 
-pub(super) fn set_update(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) -> i32 {
+pub(crate) fn set_update(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) -> i32 {
     let env = ctx.data();
     match env.contract_section {
         ContractSection::Exec => {
@@ -67,7 +69,7 @@ pub(super) fn set_update(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u3
 
 /// Try to read a `Nullifier` from the given pointer and check if it's
 /// an existing nullifier in the blockchain state machine.
-pub(super) fn nullifier_exists(mut ctx: FunctionEnvMut<Env>, ptr: u32, len: u32) -> i32 {
+pub(crate) fn nullifier_exists(mut ctx: FunctionEnvMut<Env>, ptr: u32, len: u32) -> i32 {
     let env = ctx.data();
     match env.contract_section {
         ContractSection::Null => {
@@ -112,7 +114,7 @@ pub(super) fn nullifier_exists(mut ctx: FunctionEnvMut<Env>, ptr: u32, len: u32)
 
 /// Try to read a `MerkleNode` from the given pointer and check if it's
 /// a valid Merkle root in the chain's Merkle tree.
-pub(super) fn is_valid_merkle(mut ctx: FunctionEnvMut<Env>, ptr: u32, len: u32) -> i32 {
+pub(crate) fn is_valid_merkle(mut ctx: FunctionEnvMut<Env>, ptr: u32, len: u32) -> i32 {
     /*
     if let Some(bytes) = env.memory.get_ref().unwrap().read(ptr, len as usize) {
         debug!(target: "wasm_runtime::is_valid_merkle", "Read bytes: {:?}", bytes);

+ 5 - 0
src/runtime/import/mod.rs

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

+ 22 - 0
src/runtime/import/util.rs

@@ -0,0 +1,22 @@
+use log::{error, warn};
+use wasmer::{AsStoreRef, FunctionEnvMut, WasmPtr};
+
+use crate::runtime::{memory::MemoryManipulation, vm_runtime::Env};
+
+/// Host function for logging strings.
+/// This is injected into the runtime with wasmer's `imports!` macro.
+pub(crate) fn drk_log(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) {
+    let env = ctx.data();
+    let memory_view = env.memory_view(&ctx);
+
+    match ptr.read_utf8_string(&memory_view, len) {
+        Ok(msg) => {
+            let mut logs = env.logs.borrow_mut();
+            logs.push(msg);
+            std::mem::drop(logs);
+        }
+        Err(_) => {
+            error!(target: "wasm_runtime::drk_log", "Failed to read UTF-8 string from VM memory");
+        }
+    }
+}

+ 2 - 2
src/runtime/mod.rs

@@ -25,5 +25,5 @@ pub(crate) mod memory;
 /// Utility functions
 pub mod util;
 
-/// Host functions for querying blockchain state through `MemoryState`
-pub(crate) mod chain_state;
+/// Imported host functions
+pub(crate) mod import;

+ 0 - 23
src/runtime/util.rs

@@ -16,11 +16,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use log::{error, warn};
-use wasmer::{AsStoreRef, FunctionEnvMut, WasmPtr};
-
-use super::{memory::MemoryManipulation, vm_runtime::Env};
-
 /// Serialize contract payload to format accepted by the runtime entrypoint.
 /// We keep the same payload as a slice of bytes, and prepend it with a
 /// little-endian u64 to tell the payload's length.
@@ -33,21 +28,3 @@ pub fn serialize_payload(payload: &[u8]) -> Vec<u8> {
 
     out
 }
-
-/// Host function for logging strings.
-/// This is injected into the runtime with wasmer's `imports!` macro.
-pub(crate) fn drk_log(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u32) {
-    let env = ctx.data();
-    let memory_view = env.memory_view(&ctx);
-
-    match ptr.read_utf8_string(&memory_view, len) {
-        Ok(msg) => {
-            let mut logs = env.logs.borrow_mut();
-            logs.push(msg);
-            std::mem::drop(logs);
-        }
-        Err(_) => {
-            error!(target: "wasm_runtime::drk_log", "Failed to read UTF-8 string from VM memory");
-        }
-    }
-}

+ 7 - 6
src/runtime/vm_runtime.rs

@@ -34,9 +34,10 @@ use wasmer_middlewares::{
 };
 
 use super::{
-    chain_state::{is_valid_merkle, nullifier_exists, set_update},
+    import,
+    //chain_state::{is_valid_merkle, nullifier_exists, set_update},
     memory::MemoryManipulation,
-    util::{drk_log, serialize_payload},
+    util::serialize_payload,
 };
 use crate::{crypto::contract_id::ContractId, Error, Result};
 
@@ -144,25 +145,25 @@ impl Runtime {
                 "drk_log_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    drk_log,
+                    import::util::drk_log,
                 ),
 
                 "nullifier_exists_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    nullifier_exists,
+                    import::chain_state::nullifier_exists,
                 ),
 
                 "is_valid_merkle_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    is_valid_merkle,
+                    import::chain_state::is_valid_merkle,
                 ),
 
                 "set_update_" => Function::new_typed_with_env(
                     &mut store,
                     &ctx,
-                    set_update,
+                    import::chain_state::set_update,
                 ),
             }
         };