Skip to content

Commit

Permalink
[cgo][libc] refs skycoin#105 Remove Strings_Handle
Browse files Browse the repository at this point in the history
  • Loading branch information
Maykel Arias Torres committed Sep 19, 2019
1 parent 93cea3c commit 9024228
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 148 deletions.
4 changes: 0 additions & 4 deletions include/skytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ typedef Handle WalletResponse__Handle;
*/
typedef Handle CreateTransactionRequest__Handle;

/**
* String Slice Handle
*/
typedef Handle Strings__Handle;

/**
* Instances of Go `map` type, deal map[string] as handle
Expand Down
4 changes: 2 additions & 2 deletions lib/cgo/api.client.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func SKY_api_Client_Wallets(_c C.Client__Handle, _arg0 *C.Wallets__Handle) (____
}

//export SKY_api_Client_NewWalletAddress
func SKY_api_Client_NewWalletAddress(_c C.Client__Handle, _id string, _n int, _password string, _arg3 *C.Strings__Handle) (____error_code uint32) {
func SKY_api_Client_NewWalletAddress(_c C.Client__Handle, _id string, _n int, _password string, _arg3 *[]string) (____error_code uint32) {
c, okc := lookupClientHandle(_c)
if !okc {
____error_code = SKY_BAD_HANDLE
Expand All @@ -303,7 +303,7 @@ func SKY_api_Client_NewWalletAddress(_c C.Client__Handle, _id string, _n int, _p
__arg3, ____return_err := c.NewWalletAddress(id, n, password)
____error_code = libErrorCode(____return_err)
if ____return_err == nil {
*_arg3 = (C.Strings__Handle)(registerHandle(__arg3))
*_arg3 = __arg3
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions lib/cgo/cli.generate_addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func SKY_cli_FormatAddressesAsJoinedArray(_addrs []C.cipher__Address, _arg1 *str
}

//export SKY_cli_AddressesToStrings
func SKY_cli_AddressesToStrings(_addrs []C.cipher__Address, _arg1 *C.Strings__Handle) (____error_code uint32) {
func SKY_cli_AddressesToStrings(_addrs []C.cipher__Address, _arg1 *[]string) (____error_code uint32) {
addrs := *(*[]cipher.Address)(unsafe.Pointer(&_addrs))
__addrs := toAddresserArray(addrs)
__arg1 := cli.AddressesToStrings(__addrs)
*_arg1 = registerStringsHandle(__arg1)
*_arg1 = cli.AddressesToStrings(__addrs)

return
}
18 changes: 2 additions & 16 deletions lib/cgo/libsky_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,22 +550,8 @@ func lookupDistributionHandle(handle C.Distribution__Handle) (*params.Distributi
return nil, false
}

func registerStringsHandle(obj []string) C.Strings__Handle {
return (C.Strings__Handle)(registerHandle(obj))
}

func lookupStringsHandle(handle C.Strings__Handle) ([]string, bool) {
obj, ok := lookupHandle(C.Handle(handle))
if ok {
if obj, isOK := (obj).([]string); isOK {
return obj, true
}
}
return nil, false
}

func registerCreateWalletOptionsHandle(obj *api.CreateWalletOptions) C.CreateWalletOptions__Handle {
return (C.Strings__Handle)(registerHandle(obj))
return (C.CreateWalletOptions__Handle)(registerHandle(obj))
}

func lookupCreateWalletOptionsHandle(handle C.CreateWalletOptions__Handle) (*api.CreateWalletOptions, bool) {
Expand All @@ -579,7 +565,7 @@ func lookupCreateWalletOptionsHandle(handle C.CreateWalletOptions__Handle) (*api
}

func registerWalletRecoverRequestHandle(obj *api.WalletRecoverRequest) C.WalletRecoverRequest__Handle {
return (C.Strings__Handle)(registerHandle(obj))
return (C.CreateWalletOptions__Handle)(registerHandle(obj))
}

func lookupWalletRecoverRequestHandle(handle C.WalletRecoverRequest__Handle) (*api.WalletRecoverRequest, bool) {
Expand Down
46 changes: 0 additions & 46 deletions lib/cgo/libsky_handle_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import "C"
import (
"encoding/json"
"path/filepath"
"sort"

api "github.com/skycoin/skycoin/src/api"
"github.com/skycoin/skycoin/src/daemon"
Expand Down Expand Up @@ -120,51 +119,6 @@ func SKY_Handle_Connections_GetCount(handle C.Handle,
return SKY_BAD_HANDLE
}

//export SKY_Handle_Strings_GetCount
func SKY_Handle_Strings_GetCount(handle C.Strings__Handle, count *uint32) uint32 {
obj, ok := lookupStringsHandle(handle)
if ok {
*count = uint32(len(obj))
return SKY_OK
}
return SKY_BAD_HANDLE
}

//export SKY_Handle_Strings_Sort
func SKY_Handle_Strings_Sort(handle C.Strings__Handle) uint32 {
obj, ok := lookupStringsHandle(handle)
if ok {
sort.Strings(obj)
return SKY_OK

}
return SKY_BAD_HANDLE
}

//export SKY_Handle_Strings_GetAt
func SKY_Handle_Strings_GetAt(handle C.Strings__Handle, index int, str *string) uint32 {
obj, ok := lookupStringsHandle(handle)
if ok {

*str = obj[index]
return SKY_OK

}
return SKY_BAD_HANDLE
}

//nolint megacheck
//export SKY_Handle_Strings_SetAt
func SKY_Handle_Strings_SetAt(handle C.Strings__Handle, index int, str *string) uint32 {
obj, ok := lookupStringsHandle(handle)
if !ok {
return SKY_BAD_HANDLE
}
obj[index] = *str
handle = registerStringsHandle(obj)
return SKY_OK
}

//export SKY_api_Handle_Client_GetWalletDir
func SKY_api_Handle_Client_GetWalletDir(handle C.Client__Handle, walletDir *C.GoString_) uint32 {
client, ok := lookupClientHandle(handle)
Expand Down
26 changes: 10 additions & 16 deletions lib/cgo/params.distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,31 +112,25 @@ func SKY_params_Distribution_SetUnlockTimeInterval(_d C.Distribution__Handle, _a
}

//export SKY_params_Distribution_GetAddresses
func SKY_params_Distribution_GetAddresses(_d C.Distribution__Handle, _arg0 *C.Strings__Handle) (____error_code uint32) {
func SKY_params_Distribution_GetAddresses(_d C.Distribution__Handle, _arg0 *[]string) (____error_code uint32) {
d, ok := lookupDistributionHandle(_d)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
arg0 := d.Addresses
*_arg0 = registerStringsHandle(arg0)
*_arg0 = d.Addresses
return
}

// nolint megacheck
//export SKY_params_Distribution_SetAddresses
func SKY_params_Distribution_SetAddresses(_d C.Distribution__Handle, _arg0 *C.Strings__Handle) (____error_code uint32) {
func SKY_params_Distribution_SetAddresses(_d C.Distribution__Handle, _arg0 *[]string) (____error_code uint32) {
d, ok := lookupDistributionHandle(_d)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}
s, oks := lookupStringsHandle(*_arg0)
if !oks {
____error_code = SKY_BAD_HANDLE
return
}
d.Addresses = s
d.Addresses = *_arg0
_d = registerDistributionHandle(d)
return
}
Expand Down Expand Up @@ -167,28 +161,28 @@ func SKY_params_Distribution_AddressInitialBalance(_d C.Distribution__Handle, _a
}

//export SKY_params_Distribution_UnlockedAddresses
func SKY_params_Distribution_UnlockedAddresses(_d C.Distribution__Handle, _arg0 *C.Strings__Handle) (____error_code uint32) {
func SKY_params_Distribution_UnlockedAddresses(_d C.Distribution__Handle, _arg0 *[]string) (____error_code uint32) {
d, ok := lookupDistributionHandle(_d)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}

arg0 := d.UnlockedAddresses()
*_arg0 = registerStringsHandle(arg0)
*_arg0 = d.UnlockedAddresses()

return
}

//export SKY_params_Distribution_LockedAddresses
func SKY_params_Distribution_LockedAddresses(_d C.Distribution__Handle, _arg0 *C.Strings__Handle) (____error_code uint32) {
func SKY_params_Distribution_LockedAddresses(_d C.Distribution__Handle, _arg0 *[]string) (____error_code uint32) {
d, ok := lookupDistributionHandle(_d)
if !ok {
____error_code = SKY_BAD_HANDLE
return
}

arg0 := d.LockedAddresses()
*_arg0 = registerStringsHandle(arg0)
*_arg0 = d.LockedAddresses()

return
}

Expand Down
101 changes: 40 additions & 61 deletions lib/cgo/tests/check_params.distribution.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,65 @@

START_TEST(TestDistributionAddressArrays)
{
GoUint8_ bufferunlocked[1024];
GoUint8_ bufferlocked[1024];
Strings__Handle address = 0;
Strings__Handle unlocked = 0;
Strings__Handle locked = 0;
GoSlice all = {NULL, 0, 0};
GoSlice unlocked = {NULL, 0, 0};
GoSlice locked = {NULL, 0, 0};
Distribution__Handle dist = 0;
SKY_params_NewDistribution(&dist);
GoUint32_ err = SKY_params_Distribution_GetMainNetDistribution(&dist);
ck_assert_msg(err == SKY_OK, "%X", err);
err = SKY_params_Distribution_GetAddresses(dist, &address);
ck_assert_msg(err == SKY_OK, "%X", err);
GoUint32 count = 0;
err = SKY_Handle_Strings_GetCount(address, &count);
ck_assert_msg(err == SKY_OK, "%X", err);
ck_assert(count == 100);
GoUint8_ bufferAddr[1024];
GoString_ addr = {bufferAddr, 0};
GoUint32 err = SKY_params_Distribution_GetMainNetDistribution(&dist);
ck_assert_int_eq(err, SKY_OK);
err = SKY_params_Distribution_GetAddresses(dist, &all);
ck_assert_int_eq(err, SKY_OK);
ck_assert(all.len == 100);

// At the time of this writing, there should be 25 addresses in the
// unlocked pool and 75 in the locked pool.
err = SKY_params_Distribution_UnlockedAddresses(dist, &unlocked);
ck_assert_msg(err == SKY_OK, "%X", err);
GoUint32 countUnlock;
err = SKY_Handle_Strings_GetCount(unlocked, &countUnlock);
ck_assert_msg(err == SKY_OK, "%X", err);
ck_assert(countUnlock == 25);
ck_assert_int_eq(err, SKY_OK);
ck_assert(unlocked.len == 25);
err = SKY_params_Distribution_LockedAddresses(dist, &locked);
ck_assert_msg(err == SKY_OK, "%X", err);
GoUint32 countLock;
err = SKY_Handle_Strings_GetCount(locked, &countLock);
ck_assert_msg(err == SKY_OK, "%X", err);
ck_assert(countLock == 75);
ck_assert_int_eq(err, SKY_OK);
ck_assert(locked.len == 75);

int i, j, k;
GoString *iStr, *jStr, *kStr;
int notfound;

for (i = 0; i < countUnlock; ++i) {
GoUint8_ bufferi[1024];
GoString_ iStr = {bufferi, 0};
err = SKY_Handle_Strings_GetAt(unlocked, i, &iStr);
ck_assert_msg(err == SKY_OK, "%X", err);
for (i = 0, iStr = (GoString*)all.data; i < all.len; ++i, ++iStr) {
// Check no duplicate address in distribution addresses
for (j = i + 1, jStr = iStr + 1; j < all.len; ++j, ++jStr) {
ck_assert_str_ne((char*)iStr->p, (char*)jStr->p);
}
}

for (i = 0, iStr = (GoString*)unlocked.data; i < unlocked.len; ++i, ++iStr) {
// Check no duplicate address in unlocked addresses
for (j = i + 1; j < countUnlock; ++j) {
GoUint8_ bufferj[1024];
GoString_ jStr = {bufferj, 0};
err = SKY_Handle_Strings_GetAt(unlocked, j, &jStr);
ck_assert_msg(err == SKY_OK, "%X", err);
ck_assert_str_ne((char*)iStr.p, (char*)jStr.p);
for (j = i + 1, jStr = iStr + 1; j < unlocked.len; ++j, ++jStr) {
ck_assert_str_ne((char*)iStr->p, (char*)jStr->p);
}
GoUint8_ bufferAll[1024];
GoString_ kStr = {bufferAll, 0};
err = SKY_Handle_Strings_GetAt(address, i, &kStr);
ck_assert_msg(err == SKY_OK, "%X", err);

// Check unlocked address in set of all addresses
for (k = 0, notfound = 1; notfound && (k < count); ++k) {
notfound = strcmp(iStr.p, kStr.p);
for (k = 0, notfound = 1, kStr = (GoString*)all.data; notfound && (k < all.len); ++k, ++kStr) {
notfound = strcmp(iStr->p, kStr->p);
}
ck_assert(notfound == 0);
}

for (i = 0; i < countLock; ++i) {
GoUint8_ bufferi[1024];
GoString_ iStr = {bufferi, 0};
err = SKY_Handle_Strings_GetAt(locked, i, &iStr);
ck_assert_msg(err == SKY_OK, "%X", err);
for (i = 0, iStr = (GoString*)locked.data; i < locked.len; ++i, ++iStr) {
// Check no duplicate address in locked addresses
for (j = i + 1; j < countLock; ++j) {
GoUint8_ bufferj[1024];
GoString_ jStr = {bufferj, 0};
err = SKY_Handle_Strings_GetAt(locked, j, &jStr);
ck_assert_msg(err == SKY_OK, "%X", err);
ck_assert_str_ne((char*)iStr.p, (char*)jStr.p);
for (j = i + 1, jStr = iStr + 1; j < locked.len; ++j, ++jStr) {
ck_assert_str_ne((char*)iStr->p, (char*)jStr->p);
}

// Check locked address in set of all addresses
for (k = 0, notfound = 1; notfound && (k < count); ++k) {
GoUint8_ bufferAll[1024];
GoString_ kStr = {bufferAll, 0};
err = SKY_Handle_Strings_GetAt(address, k, &kStr);
ck_assert_msg(err == SKY_OK, "%X", err);
notfound = strcmp(iStr.p, kStr.p);
for (k = 0, notfound = 1, kStr = (GoString*)all.data; notfound && k < all.len; ++k, ++kStr) {
notfound = strcmp(iStr->p, kStr->p);
}
ck_assert(notfound == 0);

// Check locked address not in set of unlocked addresses
for (k = 0, notfound = 1, kStr = (GoString*)unlocked.data; notfound && k < unlocked.len; ++k, ++kStr) {
// ck_assert_str_ne((char *)iStr->p, (char *)jStr->p);
}
ck_assert_int_eq(notfound, 0);
}
}
END_TEST
Expand Down

0 comments on commit 9024228

Please sign in to comment.