several attempts to close wallet

This commit is contained in:
leo 2024-04-09 11:29:43 +00:00
parent 54a522ac0b
commit 9f93cb3f92
3 changed files with 18 additions and 19 deletions

View file

@ -304,9 +304,9 @@ extern "C"
return plain_wallet::is_wallet_exist(path); 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());
} }

View file

@ -22,8 +22,9 @@ typedef _is_wallet_exist = Int8 Function(Pointer<Utf8>);
typedef _IsWalletExist = int Function(Pointer<Utf8>); typedef _IsWalletExist = int Function(Pointer<Utf8>);
// void close_wallet(uint64_t hwallet) // void close_wallet(uint64_t hwallet)
typedef _close_wallet = Void Function(Int64); // char *close_wallet(uint64_t hwallet)
typedef _closeWallet = void Function(int hWallet); typedef _close_wallet = Pointer<Utf8> Function(Int64);
typedef _closeWallet = Pointer<Utf8> Function(int hWallet);
// uint64_t get_current_tx_fee(uint64_t priority) // uint64_t get_current_tx_fee(uint64_t priority)
typedef _get_current_tx_fee = Int64 Function(Int64); typedef _get_current_tx_fee = Int64 Function(Int64);
@ -166,7 +167,7 @@ class ApiCalls {
static final _closeWalletNative = zanoApi.lookup<NativeFunction<_close_wallet>>('close_wallet').asFunction<_closeWallet>(); static final _closeWalletNative = zanoApi.lookup<NativeFunction<_close_wallet>>('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<NativeFunction<_stringFunctionWithInt64>>('get_wallet_info').asFunction<_StringFunctionWithIntHWallet>(); static final _getWalletInfoNative = zanoApi.lookup<NativeFunction<_stringFunctionWithInt64>>('get_wallet_info').asFunction<_StringFunctionWithIntHWallet>();

View file

@ -24,16 +24,14 @@ import 'package:path_provider/path_provider.dart';
import 'api/model/store_result.dart'; import 'api/model/store_result.dart';
//enum _LogType { none, simple, json }
mixin ZanoWalletApi { mixin ZanoWalletApi {
static const _defaultNodeUri = '195.201.107.230:33336'; static const _defaultNodeUri = '195.201.107.230:33336';
static const _statusDelivered = 'delivered'; static const _statusDelivered = 'delivered';
static const _maxAttempts = 10; static const _maxAttempts = 10;
//static const _logType = _LogType.json;
static const _logInfo = true; static const _logInfo = true;
static const _logError = true; static const _logError = true;
static const _logJson = true; static const _logJson = false;
static const int _zanoMixinValue = 10; static const int _zanoMixinValue = 10;
int _hWallet = 0; int _hWallet = 0;
@ -48,9 +46,10 @@ mixin ZanoWalletApi {
void setPassword(String password) => ApiCalls.setPassword(hWallet: hWallet, password: password); void setPassword(String password) => ApiCalls.setPassword(hWallet: hWallet, password: password);
void closeWallet() { void closeWallet([int? walletToClose]) {
_info('close_wallet $hWallet'); _info('close_wallet ${walletToClose ?? hWallet}');
ApiCalls.closeWallet(hWallet: hWallet); final result = ApiCalls.closeWallet(hWallet: walletToClose ?? hWallet);
_info('close_wallet result $result');
} }
Future<bool> setupNode() async { Future<bool> setupNode() async {
@ -301,7 +300,7 @@ mixin ZanoWalletApi {
return result; return result;
} }
Future<CreateWalletResult> loadWallet(String path, String password, [bool secondAttempt = false]) async { Future<CreateWalletResult> loadWallet(String path, String password, [int attempt = 0]) async {
_info('load_wallet path $path password ${_shorten(password)}'); _info('load_wallet path $path password ${_shorten(password)}');
await _writeLog('load_wallet', '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); final json = ApiCalls.loadWallet(path: path, password: password);
@ -311,13 +310,12 @@ mixin ZanoWalletApi {
if (map?['error'] != null) { if (map?['error'] != null) {
final code = map?['error']!['code'] ?? ''; final code = map?['error']!['code'] ?? '';
final message = map?['error']!['message'] ?? ''; final message = map?['error']!['message'] ?? '';
if (code == Consts.errorAlreadyExists && !secondAttempt) { if (code == Consts.errorAlreadyExists && attempt < 5) {
// TODO: that's not the best solution! // already connected to this wallet. closing and trying to reopen
// already connected to this wallet. closing and attempting to reopen _info('already connected. closing and reopen wallet (attempt $attempt)');
debugPrint('already connected. closing and reopen wallet'); closeWallet(attempt);
closeWallet(); await Future.delayed(const Duration(milliseconds: 500));
await Future.delayed(const Duration(milliseconds: 2000)); return await loadWallet(path, password, attempt + 1);
return await loadWallet(path, password, true);
} }
throw ZanoWalletException('Error loading wallet, $message ($code)'); throw ZanoWalletException('Error loading wallet, $message ($code)');
} }