From 45a3a9bba6f9c30d9bc15d8b15307e37b0958a5f Mon Sep 17 00:00:00 2001 From: Czarek Nakamoto Date: Sun, 15 Dec 2024 13:36:16 -0500 Subject: [PATCH] Improve multithread use of zano api --- cw_zano/lib/zano_wallet.dart | 5 ++-- cw_zano/lib/zano_wallet_api.dart | 45 +++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cw_zano/lib/zano_wallet.dart b/cw_zano/lib/zano_wallet.dart index 248d24413..02d63175c 100644 --- a/cw_zano/lib/zano_wallet.dart +++ b/cw_zano/lib/zano_wallet.dart @@ -418,13 +418,14 @@ abstract class ZanoWalletBase extends WalletBase updateTransactions() async { try { - print("isTransactionUpdating: $_isTransactionUpdating"); if (_isTransactionUpdating) { return; } _isTransactionUpdating = true; final transactions = await fetchTransactions(); - print("transactions: $transactions"); + if (transactions.length == transactionHistory.transactions.length) { + return; + } transactionHistory.clear(); transactionHistory.addMany(transactions); await transactionHistory.save(); diff --git a/cw_zano/lib/zano_wallet_api.dart b/cw_zano/lib/zano_wallet_api.dart index ff9c3c356..0c83c7722 100644 --- a/cw_zano/lib/zano_wallet_api.dart +++ b/cw_zano/lib/zano_wallet_api.dart @@ -67,7 +67,7 @@ mixin ZanoWalletApi { } Future getWalletInfo() async { - final json = zano.PlainWallet_getWalletInfo(hWallet); + final json = await _getWalletInfo(hWallet); final result = GetWalletInfoResult.fromJson(jsonDecode(json) as Map); _json('get_wallet_info', json); info('get_wallet_info got ${result.wi.balances.length} balances: ${result.wi.balances} seed: ${_shorten(result.wiExtended.seed)}'); @@ -75,7 +75,7 @@ mixin ZanoWalletApi { } Future getWalletStatus() async { - final json = zano.PlainWallet_getWalletStatus(hWallet); + final json = await _getWalletStatus(hWallet); if (json == Consts.errorWalletWrongId) { error('wrong wallet id'); throw ZanoWalletException('Wrong wallet id'); @@ -217,7 +217,7 @@ mixin ZanoWalletApi { final json = await invokeMethod('store', '{}'); final map = jsonDecode(json) as Map?; _checkForErrors(map); - return StoreResult.fromJson(map!['result']['result'] as Map); + return StoreResult.fromJson(map!['result'] as Map); } catch (e) { error('store $e'); return null; @@ -431,4 +431,43 @@ Map jsonDecode(String json) { String jsonEncode(Object? object) { return convert.jsonEncode(object); +} + +Future _getWalletStatus(int hWallet) async { + final jsonPtr = await Isolate.run(() async { + final lib = zanoapi.ZanoC(DynamicLibrary.open(zano.libPath)); + final status = lib.ZANO_PlainWallet_getWalletStatus( + hWallet, + ); + return status.address; + }); + String json = ""; + try { + final strPtr = Pointer.fromAddress(jsonPtr).cast(); + final str = strPtr.toDartString(); + zano.ZANO_free(strPtr.cast()); + json = str; + } catch (e) { + json = ""; + } + return json; +} +Future _getWalletInfo(int hWallet) async { + final jsonPtr = await Isolate.run(() async { + final lib = zanoapi.ZanoC(DynamicLibrary.open(zano.libPath)); + final status = lib.ZANO_PlainWallet_getWalletInfo( + hWallet, + ); + return status.address; + }); + String json = ""; + try { + final strPtr = Pointer.fromAddress(jsonPtr).cast(); + final str = strPtr.toDartString(); + zano.ZANO_free(strPtr.cast()); + json = str; + } catch (e) { + json = ""; + } + return json; } \ No newline at end of file