mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-04-28 03:00:18 -04:00
sdk: Apply relevant changes related to 3240221614
This commit is contained in:
@@ -654,13 +654,13 @@ pub(crate) fn db_get(ctx: FunctionEnvMut<Env>, ptr: WasmPtr<u8>, ptr_len: u32) -
|
||||
}
|
||||
};
|
||||
|
||||
// Return error if the data is empty
|
||||
// Return special error if the data is empty
|
||||
let Some(return_data) = ret else {
|
||||
debug!(
|
||||
target: "runtime::db::db_get",
|
||||
"[WASM] [{}] db_get(): Return data is empty", cid,
|
||||
);
|
||||
return darkfi_sdk::error::INTERNAL_ERROR
|
||||
return darkfi_sdk::error::DB_GET_EMPTY
|
||||
};
|
||||
|
||||
if return_data.len() > u32::MAX as usize {
|
||||
|
||||
@@ -26,15 +26,6 @@ use super::{
|
||||
|
||||
pub type DbHandle = u32;
|
||||
|
||||
pub const DB_SUCCESS: i64 = 0;
|
||||
pub const CALLER_ACCESS_DENIED: i64 = -1;
|
||||
pub const DB_INIT_FAILED: i64 = -2;
|
||||
pub const DB_LOOKUP_FAILED: i64 = -3;
|
||||
pub const DB_GET_FAILED: i64 = -4;
|
||||
pub const DB_CONTAINS_KEY_FAILED: i64 = -5;
|
||||
pub const DB_SET_FAILED: i64 = -6;
|
||||
pub const DB_DEL_FAILED: i64 = -7;
|
||||
|
||||
/// Create a new database instance for the given contract.
|
||||
/// This should be called in the `init_contract()` section to create any databases
|
||||
/// that the contract might need or use.
|
||||
@@ -50,11 +41,7 @@ pub fn db_init(contract_id: ContractId, db_name: &str) -> GenericResult<DbHandle
|
||||
let ret = db_init_(buf.as_ptr(), len as u32);
|
||||
|
||||
if ret < 0 {
|
||||
match ret {
|
||||
CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
|
||||
DB_INIT_FAILED => return Err(ContractError::DbInitFailed),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
Ok(ret as u32)
|
||||
@@ -72,11 +59,7 @@ pub fn db_lookup(contract_id: ContractId, db_name: &str) -> GenericResult<DbHand
|
||||
let ret = db_lookup_(buf.as_ptr(), len as u32);
|
||||
|
||||
if ret < 0 {
|
||||
match ret {
|
||||
CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
|
||||
DB_LOOKUP_FAILED => return Err(ContractError::DbLookupFailed),
|
||||
_ => unimplemented!(),
|
||||
}
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
Ok(ret as u32)
|
||||
@@ -113,12 +96,14 @@ pub fn db_contains_key(db_handle: DbHandle, key: &[u8]) -> GenericResult<bool> {
|
||||
|
||||
let ret = unsafe { db_contains_key_(buf.as_ptr(), len as u32) };
|
||||
|
||||
if ret < 0 {
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
match ret {
|
||||
CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied),
|
||||
DB_CONTAINS_KEY_FAILED => Err(ContractError::DbContainsKeyFailed),
|
||||
0 => Ok(false),
|
||||
1 => Ok(true),
|
||||
_ => unimplemented!(),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,12 +121,13 @@ pub fn db_set(db_handle: DbHandle, key: &[u8], value: &[u8]) -> GenericResult<()
|
||||
len += key.to_vec().encode(&mut buf)?;
|
||||
len += value.to_vec().encode(&mut buf)?;
|
||||
|
||||
match db_set_(buf.as_ptr(), len as u32) {
|
||||
CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied),
|
||||
DB_SET_FAILED => Err(ContractError::DbSetFailed),
|
||||
DB_SUCCESS => Ok(()),
|
||||
_ => unreachable!(),
|
||||
let ret = db_set_(buf.as_ptr(), len as u32);
|
||||
|
||||
if ret != crate::entrypoint::SUCCESS {
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,12 +144,13 @@ pub fn db_del(db_handle: DbHandle, key: &[u8]) -> GenericResult<()> {
|
||||
len += db_handle.encode(&mut buf)?;
|
||||
len += key.to_vec().encode(&mut buf)?;
|
||||
|
||||
match db_del_(buf.as_ptr(), len as u32) {
|
||||
CALLER_ACCESS_DENIED => Err(ContractError::CallerAccessDenied),
|
||||
DB_DEL_FAILED => Err(ContractError::DbDelFailed),
|
||||
DB_SUCCESS => Ok(()),
|
||||
_ => unreachable!(),
|
||||
let ret = db_del_(buf.as_ptr(), len as u32);
|
||||
|
||||
if ret != crate::entrypoint::SUCCESS {
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,12 +161,13 @@ pub fn zkas_db_set(bincode: &[u8]) -> GenericResult<()> {
|
||||
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!(),
|
||||
let ret = zkas_db_set_(buf.as_ptr(), len as u32);
|
||||
|
||||
if ret != crate::entrypoint::SUCCESS {
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,9 @@ pub enum ContractError {
|
||||
#[error("Db get failed")]
|
||||
DbGetFailed,
|
||||
|
||||
#[error("Db get empty")]
|
||||
DbGetEmpty,
|
||||
|
||||
#[error("Db contains_key failed")]
|
||||
DbContainsKeyFailed,
|
||||
|
||||
@@ -111,13 +114,14 @@ pub const DB_NOT_FOUND: i64 = to_builtin!(10);
|
||||
pub const DB_SET_FAILED: i64 = to_builtin!(11);
|
||||
pub const DB_LOOKUP_FAILED: i64 = to_builtin!(12);
|
||||
pub const DB_GET_FAILED: i64 = to_builtin!(13);
|
||||
pub const DB_CONTAINS_KEY_FAILED: i64 = to_builtin!(14);
|
||||
pub const INVALID_FUNCTION: i64 = to_builtin!(15);
|
||||
pub const DB_DEL_FAILED: i64 = to_builtin!(16);
|
||||
pub const SMT_INVALID_LEAF: i64 = to_builtin!(17);
|
||||
pub const SMT_INVALID_PATH_NODES: i64 = to_builtin!(18);
|
||||
pub const GET_SYSTEM_TIME_FAILED: i64 = to_builtin!(19);
|
||||
pub const DATA_TOO_LARGE: i64 = to_builtin!(20);
|
||||
pub const DB_GET_EMPTY: i64 = to_builtin!(14);
|
||||
pub const DB_CONTAINS_KEY_FAILED: i64 = to_builtin!(15);
|
||||
pub const INVALID_FUNCTION: i64 = to_builtin!(16);
|
||||
pub const DB_DEL_FAILED: i64 = to_builtin!(17);
|
||||
pub const SMT_INVALID_LEAF: i64 = to_builtin!(18);
|
||||
pub const SMT_INVALID_PATH_NODES: i64 = to_builtin!(19);
|
||||
pub const GET_SYSTEM_TIME_FAILED: i64 = to_builtin!(20);
|
||||
pub const DATA_TOO_LARGE: i64 = to_builtin!(21);
|
||||
|
||||
impl From<ContractError> for i64 {
|
||||
fn from(err: ContractError) -> Self {
|
||||
@@ -134,6 +138,7 @@ impl From<ContractError> for i64 {
|
||||
ContractError::DbSetFailed => DB_SET_FAILED,
|
||||
ContractError::DbLookupFailed => DB_LOOKUP_FAILED,
|
||||
ContractError::DbGetFailed => DB_GET_FAILED,
|
||||
ContractError::DbGetEmpty => DB_GET_EMPTY,
|
||||
ContractError::DbContainsKeyFailed => DB_CONTAINS_KEY_FAILED,
|
||||
ContractError::InvalidFunction => INVALID_FUNCTION,
|
||||
ContractError::DbDelFailed => DB_DEL_FAILED,
|
||||
@@ -168,6 +173,7 @@ impl From<i64> for ContractError {
|
||||
DB_SET_FAILED => Self::DbSetFailed,
|
||||
DB_LOOKUP_FAILED => Self::DbLookupFailed,
|
||||
DB_GET_FAILED => Self::DbGetFailed,
|
||||
DB_GET_EMPTY => Self::DbGetEmpty,
|
||||
DB_CONTAINS_KEY_FAILED => Self::DbContainsKeyFailed,
|
||||
INVALID_FUNCTION => Self::InvalidFunction,
|
||||
DB_DEL_FAILED => Self::DbDelFailed,
|
||||
|
||||
@@ -54,5 +54,5 @@ pub fn merkle_add(
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn merkle_add_(ptr: *const u8, len: u32) -> i32;
|
||||
fn merkle_add_(ptr: *const u8, len: u32) -> i64;
|
||||
}
|
||||
|
||||
@@ -16,10 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use super::{
|
||||
db::{CALLER_ACCESS_DENIED, DB_GET_FAILED},
|
||||
error::{ContractError, GenericResult},
|
||||
};
|
||||
use super::error::{ContractError, GenericResult};
|
||||
|
||||
/// Calls the `set_return_data` WASM function. Returns Ok(()) on success.
|
||||
/// Otherwise, convert the i64 error code into a [`ContractError`].
|
||||
@@ -58,12 +55,12 @@ pub fn get_object_size(object_index: u32) -> i64 {
|
||||
pub(crate) fn parse_ret(ret: i64) -> GenericResult<Option<Vec<u8>>> {
|
||||
// Negative values represent an error code.
|
||||
if ret < 0 {
|
||||
match ret {
|
||||
CALLER_ACCESS_DENIED => return Err(ContractError::CallerAccessDenied),
|
||||
DB_GET_FAILED => return Err(ContractError::DbGetFailed),
|
||||
-127 => return Ok(None),
|
||||
_ => unimplemented!(),
|
||||
// However here on the special case, we'll return Ok(None)
|
||||
if ret == crate::error::DB_GET_EMPTY {
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
return Err(ContractError::from(ret))
|
||||
}
|
||||
|
||||
// Ensure that the returned value fits into the u32 datatype.
|
||||
|
||||
Reference in New Issue
Block a user