sdk: Implement zkas_db_set for use inside wasm.

This commit is contained in:
parazyd
2023-03-05 01:52:53 +01:00
parent 6fda2f9142
commit c5ad486a09
2 changed files with 24 additions and 0 deletions

View File

@@ -221,6 +221,12 @@ impl Runtime {
import::db::db_del,
),
"zkas_db_set_" => Function::new_typed_with_env(
&mut store,
&ctx,
import::db::zkas_db_set,
),
"put_object_bytes_" => Function::new_typed_with_env(
&mut store,
&ctx,

View File

@@ -185,6 +185,22 @@ pub fn db_del(db_handle: DbHandle, key: &[u8]) -> GenericResult<()> {
}
}
/// Only deploy() can call this.
pub fn zkas_db_set(bincode: &[u8]) -> GenericResult<()> {
unsafe {
let mut len = 0;
let mut buf = vec![];
len += bincode.to_vec().encode(&mut buf)?;
match zkas_db_set_(buf.as_ptr(), len as u32) {
CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied),
DB_SET_FAILED => Err(ContractError::DbSetFailed),
DB_SUCCESS => Ok(()),
_ => unreachable!(),
}
}
}
pub fn set_return_data(data: &[u8]) -> Result<(), ContractError> {
unsafe {
match set_return_data_(data.as_ptr(), data.len() as u32) {
@@ -218,4 +234,6 @@ extern "C" {
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 zkas_db_set_(ptr: *const u8, len: u32) -> i32;
}