Skip to content

Commit

Permalink
runtime/import: Make reads free
Browse files Browse the repository at this point in the history
  • Loading branch information
parazyd committed Feb 8, 2025
1 parent 0bf59aa commit a8e704a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
27 changes: 13 additions & 14 deletions src/runtime/import/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// 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.
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
let contracts = &env.blockchain.lock().unwrap().contracts;
Expand Down Expand Up @@ -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
}

// 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
let memory_view = env.memory_view(&store);
Expand Down Expand Up @@ -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
}

// 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);

// Ensure that it is possible to read from the memory that this function needs
Expand Down Expand Up @@ -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
}

// 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
let memory_view = env.memory_view(&store);
Expand Down Expand Up @@ -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
}

// 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
let memory_view = env.memory_view(&store);
Expand Down Expand Up @@ -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
}

// 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
let memory_view = env.memory_view(&store);
Expand Down Expand Up @@ -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
}

// 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);

// Ensure that the memory is readable
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/import/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

// 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.
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 Ok(mem_slice) = ptr.slice(&memory_view, len) else {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/import/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ pub(crate) fn sparse_merkle_insert_batch(
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.
env.subtract_gas(&mut store, len as u64);
env.subtract_gas(&mut store, 1);

let memory_view = env.memory_view(&store);
let Ok(mem_slice) = ptr.slice(&memory_view, len) else {
Expand Down

0 comments on commit a8e704a

Please sign in to comment.