From 902422884a669f1ba9d99862c172701ced9f0016 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Thu, 19 Sep 2019 19:15:44 -0400 Subject: [PATCH] [cgo][libc] refs #105 Remove `Strings_Handle` --- include/skytypes.h | 4 - lib/cgo/api.client.go | 4 +- lib/cgo/cli.generate_addrs.go | 6 +- lib/cgo/libsky_handle.go | 18 +--- lib/cgo/libsky_handle_helper.go | 46 ---------- lib/cgo/params.distribution.go | 26 +++--- lib/cgo/tests/check_params.distribution.c | 101 +++++++++------------- 7 files changed, 57 insertions(+), 148 deletions(-) diff --git a/include/skytypes.h b/include/skytypes.h index 1f88b89e6..f24f23fa2 100644 --- a/include/skytypes.h +++ b/include/skytypes.h @@ -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 diff --git a/lib/cgo/api.client.go b/lib/cgo/api.client.go index dee5ff66d..de58701ed 100644 --- a/lib/cgo/api.client.go +++ b/lib/cgo/api.client.go @@ -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 @@ -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 } diff --git a/lib/cgo/cli.generate_addrs.go b/lib/cgo/cli.generate_addrs.go index 0c8b26852..a70e61526 100644 --- a/lib/cgo/cli.generate_addrs.go +++ b/lib/cgo/cli.generate_addrs.go @@ -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 } diff --git a/lib/cgo/libsky_handle.go b/lib/cgo/libsky_handle.go index 0ac1e10c2..de8157ba5 100644 --- a/lib/cgo/libsky_handle.go +++ b/lib/cgo/libsky_handle.go @@ -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) { @@ -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) { diff --git a/lib/cgo/libsky_handle_helper.go b/lib/cgo/libsky_handle_helper.go index 6ac09b95f..a89e27a15 100644 --- a/lib/cgo/libsky_handle_helper.go +++ b/lib/cgo/libsky_handle_helper.go @@ -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" @@ -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) diff --git a/lib/cgo/params.distribution.go b/lib/cgo/params.distribution.go index 928fba5c0..edd032b2c 100644 --- a/lib/cgo/params.distribution.go +++ b/lib/cgo/params.distribution.go @@ -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 } @@ -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 } diff --git a/lib/cgo/tests/check_params.distribution.c b/lib/cgo/tests/check_params.distribution.c index 5df7c7ed8..51f16001c 100644 --- a/lib/cgo/tests/check_params.distribution.c +++ b/lib/cgo/tests/check_params.distribution.c @@ -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