Skip to content

Commit

Permalink
return execution res at initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Jun 5, 2024
1 parent a049470 commit 0a40e6d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
14 changes: 7 additions & 7 deletions api/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ UnmanagedVector execute_view_function(vm_t *vm_ptr,
ByteSliceView view_function_payload,
UnmanagedVector *errmsg);

void initialize(vm_t *vm_ptr,
Db db,
GoApi api,
ByteSliceView env_payload,
ByteSliceView module_bundle_payload,
ByteSliceView allowed_publishers_payload,
UnmanagedVector *errmsg);
UnmanagedVector initialize(vm_t *vm_ptr,
Db db,
GoApi api,
ByteSliceView env_payload,
ByteSliceView module_bundle_payload,
ByteSliceView allowed_publishers_payload,
UnmanagedVector *errmsg);

UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, size_t length);

Expand Down
9 changes: 4 additions & 5 deletions api/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Initialize(
env []byte,
moduleBundle []byte,
allowedPublishers []byte,
) error {
) ([]byte, error) {
var err error

callID := startCall()
Expand All @@ -59,13 +59,12 @@ func Initialize(

errmsg := uninitializedUnmanagedVector()

_, err = C.initialize(vm.ptr, db, _api, e, mb, ap, &errmsg)
res, err := C.initialize(vm.ptr, db, _api, e, mb, ap, &errmsg)
if err != nil && err.(syscall.Errno) != C.ErrnoValue_Success {
// Depending on the nature of the error, `gasUsed` will either have a meaningful value, or just 0. │ struct ByteSliceView checksum,
return errorWithMessage(err, errmsg)
return nil, errorWithMessage(err, errmsg)
}

return err
return copyAndDestroyUnmanagedVector(res), err
}

// ExecuteContract call ffi(`execute_contract`) to execute
Expand Down
17 changes: 10 additions & 7 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,36 @@ func (vm *VM) Initialize(
env types.Env,
moduleBundle types.ModuleBundle,
allowedPublishers []types.AccountAddress,
) error {
) (types.ExecutionResult, error) {
envBz, err := env.BcsSerialize()
if err != nil {
return err
return types.ExecutionResult{}, err
}

moduleBundleBz, err := moduleBundle.BcsSerialize()
if err != nil {
return err
return types.ExecutionResult{}, err
}

allowedPublishersBz, err := types.SerializeAddressVector(allowedPublishers)
if err != nil {
return err
return types.ExecutionResult{}, err
}

err = api.Initialize(
res, err := api.Initialize(
vm.inner,
kvStore,
goApi,
envBz,
moduleBundleBz,
allowedPublishersBz,
)
if err != nil {
return types.ExecutionResult{}, err
}

return err
execRes, err := types.BcsDeserializeExecutionResult(res)
return execRes, err
}

// VM Destroyer
Expand Down Expand Up @@ -124,7 +128,6 @@ func (vm *VM) ExecuteEntryFunction(
sendersBz,
bz,
)

if err != nil {
return types.ExecutionResult{}, err
}
Expand Down
2 changes: 1 addition & 1 deletion lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func initializeVM(t *testing.T, isMinitia bool) (vm.VM, *api.Lookup) {
blockTime := uint64(time.Now().Unix())

vm := vm.NewVM(1000, 100)
err = vm.Initialize(
_, err = vm.Initialize(
kvStore,
api.NewEmptyMockAPI(blockTime),
types.Env{
Expand Down
14 changes: 7 additions & 7 deletions libmovevm/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ UnmanagedVector execute_view_function(vm_t *vm_ptr,
ByteSliceView view_function_payload,
UnmanagedVector *errmsg);

void initialize(vm_t *vm_ptr,
Db db,
GoApi api,
ByteSliceView env_payload,
ByteSliceView module_bundle_payload,
ByteSliceView allowed_publishers_payload,
UnmanagedVector *errmsg);
UnmanagedVector initialize(vm_t *vm_ptr,
Db db,
GoApi api,
ByteSliceView env_payload,
ByteSliceView module_bundle_payload,
ByteSliceView allowed_publishers_payload,
UnmanagedVector *errmsg);

UnmanagedVector new_unmanaged_vector(bool nil, const uint8_t *ptr, size_t length);

Expand Down
1 change: 1 addition & 0 deletions libmovevm/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod rust;

pub use go::GoError;

#[allow(unused_imports)]
pub use rust::{handle_c_error_binary, handle_c_error_default, RustError as Error};

#[cfg(test)]
Expand Down
6 changes: 3 additions & 3 deletions libmovevm/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::panic::{catch_unwind, AssertUnwindSafe};

use crate::args::VM_ARG;
use crate::error::handle_c_error_default;
use crate::error::{handle_c_error_binary, Error};
use crate::move_api::handler as api_handler;
use crate::{api::GoApi, vm, ByteSliceView, Db, UnmanagedVector};
Expand Down Expand Up @@ -58,7 +57,7 @@ pub extern "C" fn initialize(
module_bundle_payload: ByteSliceView,
allowed_publishers_payload: ByteSliceView,
errmsg: Option<&mut UnmanagedVector>,
) {
) -> UnmanagedVector {
let module_bundle: ModuleBundle =
bcs::from_bytes(module_bundle_payload.read().unwrap()).unwrap();
let env: Env = bcs::from_bytes(env_payload.read().unwrap()).unwrap();
Expand All @@ -73,7 +72,8 @@ pub extern "C" fn initialize(
None => Err(Error::unset_arg(VM_ARG)),
};

handle_c_error_default(res, errmsg)
let ret = handle_c_error_binary(res, errmsg);
UnmanagedVector::new(Some(ret))
}

// exported function to execute (an entrypoint of) contract
Expand Down
5 changes: 3 additions & 2 deletions libmovevm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn initialize_vm(
env: Env,
module_bundle: ModuleBundle,
allowed_publishers: Vec<AccountAddress>,
) -> Result<(), Error> {
) -> Result<Vec<u8>, Error> {
let mut storage = GoStorage::new(&db_handle);
let mut table_storage = GoTableStorage::new(&db_handle);

Expand All @@ -46,7 +46,8 @@ pub(crate) fn initialize_vm(
// write state change to storage
push_write_set(&mut storage, output.write_set())?;

Ok(())
let res = generate_result(output)?;
to_vec(&res)
}

pub(crate) fn execute_contract(
Expand Down

0 comments on commit 0a40e6d

Please sign in to comment.