Browse Source

runtime/import: Make reads free

parazyd 1 year ago
parent
commit
a8e704a802
3 changed files with 20 additions and 18 deletions
  1. 13 14
      src/runtime/import/db.rs
  2. 5 2
      src/runtime/import/merkle.rs
  3. 2 2
      src/runtime/import/smt.rs

+ 13 - 14
src/runtime/import/db.rs

@@ -66,9 +66,9 @@ pub(crate) fn db_init(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length read from the memory slice.
+    // Subtract used gas.
     // TODO: There should probably be an additional fee to open a new sled tree.
     // TODO: There should probably be an additional fee to open a new sled tree.
-    env.subtract_gas(&mut store, ptr_len as u64);
+    env.subtract_gas(&mut store, 1);
 
 
     // This takes lock of the blockchain overlay reference in the wasm env
     // This takes lock of the blockchain overlay reference in the wasm env
     let contracts = &env.blockchain.lock().unwrap().contracts;
     let contracts = &env.blockchain.lock().unwrap().contracts;
@@ -223,8 +223,8 @@ pub(crate) fn db_lookup(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len:
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length read from the memory slice.
-    env.subtract_gas(&mut store, ptr_len as u64);
+    // Subtract used gas. Opening an existing db should be free (i.e. 1 gas unit).
+    env.subtract_gas(&mut store, 1);
 
 
     // Read memory location that contains the ContractId and DB name
     // Read memory location that contains the ContractId and DB name
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
@@ -343,7 +343,9 @@ pub(crate) fn db_set(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u3
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length hread from the memory slice.
+    // Subtract used gas. Here we count the bytes written into the database.
+    // TODO: We might want to count only the difference in size if we're replacing
+    // data and the new data is larger.
     env.subtract_gas(&mut store, ptr_len as u64);
     env.subtract_gas(&mut store, ptr_len as u64);
 
 
     // Ensure that it is possible to read from the memory that this function needs
     // Ensure that it is possible to read from the memory that this function needs
@@ -475,8 +477,8 @@ pub(crate) fn db_del(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u3
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length of the looked-up key.
-    env.subtract_gas(&mut store, ptr_len as u64);
+    // Subtract used gas. We make deletion free.
+    env.subtract_gas(&mut store, 1);
 
 
     // Ensure that it is possible to read from the memory that this function needs
     // Ensure that it is possible to read from the memory that this function needs
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
@@ -591,8 +593,8 @@ pub(crate) fn db_get(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u3
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length of the looked-up key.
-    env.subtract_gas(&mut store, ptr_len as u64);
+    // Subtract used gas. Reading is free.
+    env.subtract_gas(&mut store, 1);
 
 
     // Ensure that it is possible to read memory
     // Ensure that it is possible to read memory
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
@@ -728,8 +730,8 @@ pub(crate) fn db_contains_key(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, pt
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length of the looked-up key.
-    env.subtract_gas(&mut store, ptr_len as u64);
+    // Subtract used gas. Reading is free.
+    env.subtract_gas(&mut store, 1);
 
 
     // Ensure memory is readable
     // Ensure memory is readable
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
@@ -834,9 +836,6 @@ pub(crate) fn zkas_db_set(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_le
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length read from the memory slice.
-    env.subtract_gas(&mut store, ptr_len as u64);
-
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
 
 
     // Ensure that the memory is readable
     // Ensure that the memory is readable

+ 5 - 2
src/runtime/import/merkle.rs

@@ -50,9 +50,12 @@ pub(crate) fn merkle_add(mut ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, len: u3
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length read from the memory slice.
+    // Subtract used gas.
     // This makes calling the function which returns early have some (small) cost.
     // This makes calling the function which returns early have some (small) cost.
-    env.subtract_gas(&mut store, len as u64);
+    env.subtract_gas(&mut store, 1);
+
+    // Subtract written bytes as gas
+    env.subtract_gas(&mut store, 33 /* value_data.len() as u64 */);
 
 
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
     let Ok(mem_slice) = ptr.slice(&memory_view, len) else {
     let Ok(mem_slice) = ptr.slice(&memory_view, len) else {

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

@@ -115,9 +115,9 @@ pub(crate) fn sparse_merkle_insert_batch(
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
         return darkfi_sdk::error::CALLER_ACCESS_DENIED
     }
     }
 
 
-    // Subtract used gas. Here we count the length read from the memory slice.
+    // Subtract used gas.
     // This makes calling the function which returns early have some (small) cost.
     // This makes calling the function which returns early have some (small) cost.
-    env.subtract_gas(&mut store, len as u64);
+    env.subtract_gas(&mut store, 1);
 
 
     let memory_view = env.memory_view(&store);
     let memory_view = env.memory_view(&store);
     let Ok(mem_slice) = ptr.slice(&memory_view, len) else {
     let Ok(mem_slice) = ptr.slice(&memory_view, len) else {