Skip to content

Commit

Permalink
fix: cgo definision to include missing account_type (#28)
Browse files Browse the repository at this point in the history
* fix cgo definision to include account_type

* fmt
  • Loading branch information
beer-1 authored Apr 13, 2024
1 parent 3d5bbb6 commit 5779488
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
4 changes: 2 additions & 2 deletions api/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef GoError (*remove_db_fn)(db_t *ptr, U8SliceView key, UnmanagedVector *err
typedef GoError (*scan_db_fn)(db_t *ptr, U8SliceView prefix, U8SliceView start, U8SliceView end, int32_t order, GoIter *out, UnmanagedVector *errOut);
// and api
typedef GoError (*query_fn)(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
typedef GoError (*get_account_info_fn)(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, UnmanagedVector *errOut);
typedef GoError (*get_account_info_fn)(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, UnmanagedVector *errOut);
typedef GoError (*amount_to_share_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
typedef GoError (*share_to_amount_fn)(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
typedef GoError (*unbond_timestamp_fn)(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
Expand All @@ -28,7 +28,7 @@ GoError cDelete_cgo(db_t *ptr, U8SliceView key, UnmanagedVector *errOut);
GoError cScan_cgo(db_t *ptr, U8SliceView prefix, U8SliceView start, U8SliceView end, int32_t order, GoIter *out, UnmanagedVector *errOut);
// api
GoError cQuery_cgo(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, UnmanagedVector *errOut);
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, UnmanagedVector *errOut);
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
GoError cShareToAmount_cgo(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
GoError cUnbondTimestamp_cgo(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
Expand Down
12 changes: 6 additions & 6 deletions api/callbacks_cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package api
#include <stdio.h>
// imports (db)
GoError cSet(db_t *ptr, U8SliceView key, U8SliceView val, UnmanagedVector *errOut);
GoError cGet(db_t *ptr, U8SliceView key, UnmanagedVector *val, UnmanagedVector *errOut);
GoError cSet(db_t *ptr, U8SliceView key, U8SliceView val, UnmanagedVector *errOut);
GoError cDelete(db_t *ptr, U8SliceView key, UnmanagedVector *errOut);
GoError cScan(db_t *ptr, U8SliceView prefix, U8SliceView start, U8SliceView end, int32_t order, GoIter *out, UnmanagedVector *errOut);
// imports (api)
GoError cQuery(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut);
GoError cGetAccountInfo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, UnmanagedVector *errOut);
GoError cAmountToShare(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
GoError cShareToAmount(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
GoError cGetAccountInfo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, UnmanagedVector *errOut);
GoError cAmountToShare(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t amount, uint64_t *share, UnmanagedVector *errOut);
GoError cShareToAmount(api_t *ptr, U8SliceView validator, U8SliceView metadata, uint64_t share, uint64_t *amount, UnmanagedVector *errOut);
GoError cUnbondTimestamp(api_t *ptr, uint64_t *unbondTimestamp, UnmanagedVector *errOut);
GoError cGetPrice(api_t *ptr, U8SliceView pairId, UnmanagedVector *price, uint64_t *updatedAt, uint64_t *decimals, UnmanagedVector *errOut);
// imports (iterator)
Expand Down Expand Up @@ -42,8 +42,8 @@ GoError cNext_cgo(iterator_t *ptr, UnmanagedVector *key, UnmanagedVector *errOut
GoError cQuery_cgo(api_t *ptr, U8SliceView request, uint64_t gasBalance, UnmanagedVector *response, uint64_t *usedGas, UnmanagedVector *errOut) {
return cQuery(ptr, request, gasBalance, response, usedGas, errOut);
}
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, UnmanagedVector *errOut) {
return cGetAccountInfo(ptr, addr, found, account_number, sequence, errOut);
GoError cGetAccountInfo_cgo(api_t *ptr, U8SliceView addr, bool *found, uint64_t *account_number, uint64_t *sequence, uint8_t *account_type, UnmanagedVector *errOut) {
return cGetAccountInfo(ptr, addr, found, account_number, sequence, account_type, errOut);
}
GoError cAmountToShare_cgo(api_t *ptr, U8SliceView validator, U8SliceView coinType, uint64_t amount, uint64_t *share, UnmanagedVector *errOut) {
return cAmountToShare(ptr, validator, coinType, amount, share, errOut);
Expand Down
41 changes: 22 additions & 19 deletions libmovevm/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ impl AccountAPI for GoApi {
let go_error: GoError = (self.vtable.get_account_info)(
self.state,
addr,
&mut found,
&mut account_number,
&mut sequence,
&mut account_type,
&mut error_msg,
&mut found as *mut bool,
&mut account_number as *mut u64,
&mut sequence as *mut u64,
&mut account_type as *mut u8,
&mut error_msg as *mut UnmanagedVector,
)
.into();

Expand Down Expand Up @@ -138,8 +138,8 @@ impl StakingAPI for GoApi {
validator,
metadata,
amount,
&mut share,
&mut error_msg,
&mut share as *mut u64,
&mut error_msg as *mut UnmanagedVector,
)
.into();

Expand Down Expand Up @@ -174,8 +174,8 @@ impl StakingAPI for GoApi {
validator,
metadata,
share,
&mut amount,
&mut error_msg,
&mut amount as *mut u64,
&mut error_msg as *mut UnmanagedVector,
)
.into();

Expand All @@ -194,9 +194,12 @@ impl StakingAPI for GoApi {
let mut unbond_timestamp = 0_u64;
let mut error_msg = UnmanagedVector::default();

let go_error: GoError =
(self.vtable.unbond_timestamp)(self.state, &mut unbond_timestamp, &mut error_msg)
.into();
let go_error: GoError = (self.vtable.unbond_timestamp)(
self.state,
&mut unbond_timestamp as *mut u64,
&mut error_msg as *mut UnmanagedVector,
)
.into();

// return complete error message (reading from buffer for GoError::Other)
let default = || "Failed to convert share to amount".to_string();
Expand All @@ -221,10 +224,10 @@ impl OracleAPI for GoApi {
let go_error: GoError = (self.vtable.get_price)(
self.state,
pair_id,
&mut price,
&mut updated_at,
&mut decimals,
&mut error_msg,
&mut price as *mut UnmanagedVector,
&mut updated_at as *mut u64,
&mut decimals as *mut u64,
&mut error_msg as *mut UnmanagedVector,
)
.into();

Expand Down Expand Up @@ -257,9 +260,9 @@ impl QueryAPI for GoApi {
self.state,
request,
gas_balance,
&mut response,
&mut used_gas,
&mut error_msg,
&mut response as *mut UnmanagedVector,
&mut used_gas as *mut u64,
&mut error_msg as *mut UnmanagedVector,
)
.into();

Expand Down
9 changes: 1 addition & 8 deletions libmovevm/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,7 @@ pub extern "C" fn initialize(

let res = match to_vm(vm_ptr) {
Some(vm) => catch_unwind(AssertUnwindSafe(move || {
vm::initialize_vm(
vm,
db,
api,
env,
module_bundle,
allowed_publishers,
)
vm::initialize_vm(vm, db, api, env, module_bundle, allowed_publishers)
}))
.unwrap_or_else(|_| Err(Error::panic())),
None => Err(Error::unset_arg(VM_ARG)),
Expand Down
14 changes: 13 additions & 1 deletion libmovevm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ impl UnmanagedVector {
}
}

/// Creates a non-none UnmanagedVector with the given data.
pub fn some(data: impl Into<Vec<u8>>) -> Self {
Self::new(Some(data.into()))
}

/// Creates a none UnmanagedVector.
pub fn none() -> Self {
Self::new(None)
}

pub fn is_none(&self) -> bool {
self.is_none
}
Expand All @@ -238,7 +248,7 @@ impl UnmanagedVector {

impl Default for UnmanagedVector {
fn default() -> Self {
Self::new(None)
Self::none()
}
}

Expand All @@ -253,6 +263,8 @@ pub extern "C" fn new_unmanaged_vector(
} else if length == 0 {
UnmanagedVector::new(Some(Vec::new()))
} else {
// In slice::from_raw_parts, `data` must be non-null and aligned even for zero-length slices.
// For this reason we cover the length == 0 case separately above.
let external_memory = unsafe { slice::from_raw_parts(ptr, length) };
let copy = Vec::from(external_memory);
UnmanagedVector::new(Some(copy))
Expand Down

0 comments on commit 5779488

Please sign in to comment.