sdk: Revert architecture guards

This commit is contained in:
parazyd
2022-11-06 21:45:43 +01:00
parent 8c27c24473
commit c8bc9484bb
3 changed files with 65 additions and 30 deletions

View File

@@ -7,14 +7,16 @@ use super::{
};
pub type DbHandle = u32;
type TxHandle = u32;
/// Only deploy() can call this. Creates a new database instance for this contract.
///
/// ```ignore
/// ```
/// type DbHandle = u32;
/// db_init(db_name) -> DbHandle
/// ```
pub fn db_init(contract_id: ContractId, db_name: &str) -> GenericResult<DbHandle> {
#[cfg(target_arch = "wasm32")]
unsafe {
let mut len = 0;
let mut buf = vec![];
@@ -33,9 +35,13 @@ pub fn db_init(contract_id: ContractId, db_name: &str) -> GenericResult<DbHandle
return Ok(ret as u32)
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!()
}
pub fn db_lookup(contract_id: ContractId, db_name: &str) -> GenericResult<DbHandle> {
#[cfg(target_arch = "wasm32")]
unsafe {
let mut len = 0;
let mut buf = vec![];
@@ -54,45 +60,55 @@ pub fn db_lookup(contract_id: ContractId, db_name: &str) -> GenericResult<DbHand
return Ok(ret as u32)
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!()
}
/// Everyone can call this. Will read a key from the key-value store.
///
/// ```ignore
/// ```
/// value = db_get(db_handle, key);
/// ```
pub fn db_get(db_handle: DbHandle, key: &[u8]) -> GenericResult<Option<Vec<u8>>> {
let mut len = 0;
let mut buf = vec![];
len += db_handle.encode(&mut buf)?;
len += key.to_vec().encode(&mut buf)?;
#[cfg(target_arch = "wasm32")]
{
let mut len = 0;
let mut buf = vec![];
len += db_handle.encode(&mut buf)?;
len += key.to_vec().encode(&mut buf)?;
let ret = unsafe { db_get_(buf.as_ptr(), len as u32) };
let ret = unsafe { db_get_(buf.as_ptr(), len as u32) };
if ret < 0 {
match ret {
-1 => return Err(ContractError::CallerAccessDenied),
-2 => return Err(ContractError::DbGetFailed),
-3 => return Ok(None),
_ => unimplemented!(),
if ret < 0 {
match ret {
-1 => return Err(ContractError::CallerAccessDenied),
-2 => return Err(ContractError::DbGetFailed),
-3 => return Ok(None),
_ => unimplemented!(),
}
}
let obj = ret as u32;
let obj_size = get_object_size(obj);
let mut buf = vec![0u8; obj_size as usize];
get_object_bytes(&mut buf, obj);
Ok(Some(buf))
}
let obj = ret as u32;
let obj_size = get_object_size(obj);
let mut buf = vec![0u8; obj_size as usize];
get_object_bytes(&mut buf, obj);
Ok(Some(buf))
#[cfg(not(target_arch = "wasm32"))]
unimplemented!()
}
/// Only update() can call this. Set a value within the transaction.
///
/// ```ignore
/// ```
/// db_set(tx_handle, key, value);
/// ```
pub fn db_set(db_handle: DbHandle, key: &[u8], value: &[u8]) -> GenericResult<()> {
// Check entry for tx_handle is not None
#[cfg(target_arch = "wasm32")]
unsafe {
let mut len = 0;
let mut buf = vec![];
@@ -107,8 +123,12 @@ pub fn db_set(db_handle: DbHandle, key: &[u8], value: &[u8]) -> GenericResult<()
_ => unreachable!(),
}
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!()
}
#[cfg(target_arch = "wasm32")]
extern "C" {
fn db_init_(ptr: *const u8, len: u32) -> i32;
fn db_lookup_(ptr: *const u8, len: u32) -> i32;

View File

@@ -19,7 +19,6 @@
pub use incrementalmerkletree;
pub use pasta_curves as pasta;
#[cfg(target_arch = "wasm32")]
/// Database functions
pub mod db;
@@ -35,7 +34,6 @@ pub mod log;
/// Crypto-related definitions
pub mod crypto;
#[cfg(target_arch = "wasm32")]
/// Merkle
pub mod merkle;

View File

@@ -1,29 +1,46 @@
#[cfg(target_arch = "wasm32")]
use super::error::ContractError;
#[cfg(target_arch = "wasm32")]
pub fn set_return_data(data: &[u8]) -> Result<(), ContractError> {
#[cfg(target_arch = "wasm32")]
unsafe {
return match set_return_data_(data.as_ptr(), data.len() as u32) {
0 => Ok(()),
errcode => Err(ContractError::from(errcode)),
}
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!();
}
#[cfg(target_arch = "wasm32")]
pub fn put_object_bytes(data: &[u8]) -> i64 {
unsafe { return put_object_bytes_(data.as_ptr(), data.len() as u32) }
#[cfg(target_arch = "wasm32")]
unsafe {
return put_object_bytes_(data.as_ptr(), data.len() as u32)
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!();
}
#[cfg(target_arch = "wasm32")]
pub fn get_object_bytes(data: &mut [u8], object_index: u32) -> i64 {
unsafe { return get_object_bytes_(data.as_mut_ptr(), object_index as u32) }
#[cfg(target_arch = "wasm32")]
{
unsafe { return get_object_bytes_(data.as_mut_ptr(), object_index as u32) }
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!();
}
#[cfg(target_arch = "wasm32")]
pub fn get_object_size(object_index: u32) -> i64 {
unsafe { return get_object_size_(object_index as u32) }
#[cfg(target_arch = "wasm32")]
unsafe {
return get_object_size_(object_index as u32)
}
#[cfg(not(target_arch = "wasm32"))]
unimplemented!();
}
#[cfg(target_arch = "wasm32")]