Skip to content

Commit

Permalink
handle put and delete return as a status and remove the SmartPointer
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed May 1, 2024
1 parent 285db8c commit f7ef944
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 62 deletions.
1 change: 0 additions & 1 deletion x/programs/cmd/simulator/cmd/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ func programExecuteFunc(
) (ids.ID, []int64, uint64, error) {
// simulate create program transaction
programTxID, err := generateRandomID()

if err != nil {
return ids.Empty, nil, 0, err
}
Expand Down
43 changes: 1 addition & 42 deletions x/programs/program/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package program

import (
"errors"
"fmt"
"math"
"runtime"
Expand Down Expand Up @@ -158,37 +157,7 @@ func WriteBytes(m *Memory, buf []byte) (uint32, error) {
return offset, nil
}

// SmartPtr is an int64 where the first 4 bytes represent the length of the bytes
// and the following 4 bytes represent a pointer to WASM memory where the bytes are stored.
type SmartPtr int64

// Get returns the int64 value of [s].
func (s SmartPtr) Get() int64 {
return int64(s)
}

// Len returns the length of the bytes stored in memory by [s].
func (s SmartPtr) Len() uint32 {
return uint32(s >> 32)
}

// PtrOffset returns the offset of the bytes stored in memory by [s].
func (s SmartPtr) PtrOffset() uint32 {
return uint32(s)
}

// Bytes returns the bytes stored in memory by [s].
func (s SmartPtr) Bytes(memory *Memory) ([]byte, error) {
// read the range of PtrOffset + length from memory
bytes, err := memory.Range(s.PtrOffset(), s.Len())
if err != nil {
return nil, err
}

return bytes, nil
}

// AllocateBytes writes [bytes] to memory and returns the resulting SmartPtr.
// AllocateBytes writes [bytes] to memory and returns the resulting pointer.
func AllocateBytes(bytes []byte, memory *Memory) (uint32, error) {
ptr, err := WriteBytes(memory, bytes)
if err != nil {
Expand All @@ -197,13 +166,3 @@ func AllocateBytes(bytes []byte, memory *Memory) (uint32, error) {

return ptr, nil
}

// NewSmartPtr returns a SmartPtr from [ptr] and [byteLen].
func NewSmartPtr(ptr uint32, byteLen int) (SmartPtr, error) {
// ensure length of bytes is not greater than int32 to prevent overflow
if !EnsureIntToInt32(byteLen) {
return 0, errors.New("length of bytes is greater than int32")
}

return SmartPtr(int64(ptr)), nil
}
33 changes: 14 additions & 19 deletions x/programs/rust/wasmlanche-sdk/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ where
let val_bytes = if let Some(val) = self.cache.get(&key) {
val
} else {
let val_ptr = unsafe { host::get_bytes(&self.program, &key.clone().into())? };
let val = unsafe { host::get_bytes(&self.program, &key.clone().into())? };
let val_ptr = val as *const u8;
// TODO write a test for that
if val_ptr.is_null() {
return Err(Error::Read);
Expand Down Expand Up @@ -163,7 +164,7 @@ macro_rules! ffi_linker {
#[link(wasm_import_module = $mod)]
extern "C" {
#[link_name = $link]
fn ffi(caller: CPointer, key: CPointer) -> *const u8;
fn ffi(caller: CPointer, key: CPointer) -> i32;
}

let $caller = to_ffi_ptr($caller.id())?;
Expand All @@ -173,7 +174,7 @@ macro_rules! ffi_linker {
#[link(wasm_import_module = $mod)]
extern "C" {
#[link_name = $link]
fn ffi(caller: CPointer, key: CPointer, value: CPointer) -> *const u8;
fn ffi(caller: CPointer, key: CPointer, value: CPointer) -> i32;
}

let $caller = to_ffi_ptr($caller.id())?;
Expand Down Expand Up @@ -214,21 +215,18 @@ mod host {
where
V: BorshSerialize,
{
let ptr = call_host_fn! {
match call_host_fn! {
wasm_import_module = "state"
link_name = "put"
args = (caller, key, value)
};

if ptr.is_null() {
Err(Error::Write)
} else {
Ok(())
} {
0 => Ok(()),
_ => Err(Error::Write)
}
}

/// Gets the bytes associated with the key from the host.
pub(super) unsafe fn get_bytes(caller: &Program, key: &Key) -> Result<*const u8, Error> {
pub(super) unsafe fn get_bytes(caller: &Program, key: &Key) -> Result<i32, Error> {
Ok(call_host_fn! {
wasm_import_module = "state"
link_name = "get"
Expand All @@ -238,16 +236,13 @@ mod host {

/// Deletes the bytes at key ptr from the host storage
pub(super) unsafe fn delete_bytes(caller: &Program, key: &Key) -> Result<(), Error> {
let ptr = call_host_fn! {
wasm_import_module = "state"
match call_host_fn! {
wasm_import_module = "state"
link_name = "delete"
args = (caller, key)
};

if ptr.is_null() {
Err(Error::Delete)
} else {
Ok(())
} {
0 => Ok(()),
_ => Err(Error::Delete)
}
}
}

0 comments on commit f7ef944

Please sign in to comment.