From 9f93cb3f922d6238e9ac8bca8026487df0d1c6a5 Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 9 Apr 2024 11:29:43 +0000 Subject: [PATCH] several attempts to close wallet --- cw_zano/ios/Classes/zano_api.cpp | 4 ++-- cw_zano/lib/api/api_calls.dart | 7 ++++--- cw_zano/lib/zano_wallet_api.dart | 26 ++++++++++++-------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cw_zano/ios/Classes/zano_api.cpp b/cw_zano/ios/Classes/zano_api.cpp index 85b0f93df..8e15658fa 100644 --- a/cw_zano/ios/Classes/zano_api.cpp +++ b/cw_zano/ios/Classes/zano_api.cpp @@ -304,9 +304,9 @@ extern "C" return plain_wallet::is_wallet_exist(path); } - void close_wallet(uint64_t hwallet) + char *close_wallet(uint64_t hwallet) { - plain_wallet::close_wallet(hwallet); + return strdup(plain_wallet::close_wallet(hwallet).c_str()); } diff --git a/cw_zano/lib/api/api_calls.dart b/cw_zano/lib/api/api_calls.dart index 338d2e3a7..ac740a4cf 100644 --- a/cw_zano/lib/api/api_calls.dart +++ b/cw_zano/lib/api/api_calls.dart @@ -22,8 +22,9 @@ typedef _is_wallet_exist = Int8 Function(Pointer); typedef _IsWalletExist = int Function(Pointer); // void close_wallet(uint64_t hwallet) -typedef _close_wallet = Void Function(Int64); -typedef _closeWallet = void Function(int hWallet); +// char *close_wallet(uint64_t hwallet) +typedef _close_wallet = Pointer Function(Int64); +typedef _closeWallet = Pointer Function(int hWallet); // uint64_t get_current_tx_fee(uint64_t priority) typedef _get_current_tx_fee = Int64 Function(Int64); @@ -166,7 +167,7 @@ class ApiCalls { static final _closeWalletNative = zanoApi.lookup>('close_wallet').asFunction<_closeWallet>(); - static void closeWallet({required int hWallet}) => _closeWalletNative(hWallet); + static String closeWallet({required int hWallet}) => _performApiCall(() => _closeWalletNative(hWallet)); static final _getWalletInfoNative = zanoApi.lookup>('get_wallet_info').asFunction<_StringFunctionWithIntHWallet>(); diff --git a/cw_zano/lib/zano_wallet_api.dart b/cw_zano/lib/zano_wallet_api.dart index 5fdfcbc78..11690e212 100644 --- a/cw_zano/lib/zano_wallet_api.dart +++ b/cw_zano/lib/zano_wallet_api.dart @@ -24,16 +24,14 @@ import 'package:path_provider/path_provider.dart'; import 'api/model/store_result.dart'; -//enum _LogType { none, simple, json } mixin ZanoWalletApi { static const _defaultNodeUri = '195.201.107.230:33336'; static const _statusDelivered = 'delivered'; static const _maxAttempts = 10; - //static const _logType = _LogType.json; static const _logInfo = true; static const _logError = true; - static const _logJson = true; + static const _logJson = false; static const int _zanoMixinValue = 10; int _hWallet = 0; @@ -48,9 +46,10 @@ mixin ZanoWalletApi { void setPassword(String password) => ApiCalls.setPassword(hWallet: hWallet, password: password); - void closeWallet() { - _info('close_wallet $hWallet'); - ApiCalls.closeWallet(hWallet: hWallet); + void closeWallet([int? walletToClose]) { + _info('close_wallet ${walletToClose ?? hWallet}'); + final result = ApiCalls.closeWallet(hWallet: walletToClose ?? hWallet); + _info('close_wallet result $result'); } Future setupNode() async { @@ -301,7 +300,7 @@ mixin ZanoWalletApi { return result; } - Future loadWallet(String path, String password, [bool secondAttempt = false]) async { + Future loadWallet(String path, String password, [int attempt = 0]) async { _info('load_wallet path $path password ${_shorten(password)}'); await _writeLog('load_wallet', 'load_wallet path $path password ${_shorten(password)}'); final json = ApiCalls.loadWallet(path: path, password: password); @@ -311,13 +310,12 @@ mixin ZanoWalletApi { if (map?['error'] != null) { final code = map?['error']!['code'] ?? ''; final message = map?['error']!['message'] ?? ''; - if (code == Consts.errorAlreadyExists && !secondAttempt) { - // TODO: that's not the best solution! - // already connected to this wallet. closing and attempting to reopen - debugPrint('already connected. closing and reopen wallet'); - closeWallet(); - await Future.delayed(const Duration(milliseconds: 2000)); - return await loadWallet(path, password, true); + if (code == Consts.errorAlreadyExists && attempt < 5) { + // already connected to this wallet. closing and trying to reopen + _info('already connected. closing and reopen wallet (attempt $attempt)'); + closeWallet(attempt); + await Future.delayed(const Duration(milliseconds: 500)); + return await loadWallet(path, password, attempt + 1); } throw ZanoWalletException('Error loading wallet, $message ($code)'); }