From 2d883f8f3fadc5df3fa5a845cc9fe9bff3b49aa5 Mon Sep 17 00:00:00 2001 From: Czarek Nakamoto Date: Sat, 11 May 2024 22:27:34 +0200 Subject: [PATCH] store crash fix --- cw_monero/lib/api/wallet.dart | 69 +++++++++++++++++---------- cw_monero/lib/api/wallet_manager.dart | 6 ++- cw_monero/pubspec.yaml | 1 + scripts/prepare_moneroc.sh | 2 +- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/cw_monero/lib/api/wallet.dart b/cw_monero/lib/api/wallet.dart index 9e37268ad..63277e6a3 100644 --- a/cw_monero/lib/api/wallet.dart +++ b/cw_monero/lib/api/wallet.dart @@ -1,8 +1,11 @@ import 'dart:async'; +import 'dart:ffi'; +import 'dart:isolate'; import 'package:cw_monero/api/account_list.dart'; import 'package:cw_monero/api/exceptions/setup_wallet_exception.dart'; import 'package:monero/monero.dart' as monero; +import 'package:mutex/mutex.dart'; int getSyncingHeight() { // final height = monero.MONERO_cw_WalletListener_height(getWlptr()); @@ -10,6 +13,7 @@ int getSyncingHeight() { // print("height: $height / $h2"); return h2; } + bool isNeededToRefresh() { final ret = monero.MONERO_cw_WalletListener_isNeedToRefresh(getWlptr()); monero.MONERO_cw_WalletListener_resetNeedToRefresh(getWlptr()); @@ -21,11 +25,13 @@ bool isNewTransactionExist() { monero.MONERO_cw_WalletListener_resetIsNewTransactionExist(getWlptr()); return ret; } + String getFilename() => monero.Wallet_filename(wptr!); String getSeed() { // monero.Wallet_setCacheAttribute(wptr!, key: "cakewallet.seed", value: seed); - final cakepolyseed = monero.Wallet_getCacheAttribute(wptr!, key: "cakewallet.seed"); + final cakepolyseed = + monero.Wallet_getCacheAttribute(wptr!, key: "cakewallet.seed"); if (cakepolyseed != "") { return cakepolyseed; } @@ -37,11 +43,15 @@ String getSeed() { return legacy; } -String getAddress({int accountIndex = 0, int addressIndex = 1}) => monero.Wallet_address(wptr!, accountIndex: accountIndex, addressIndex: addressIndex); +String getAddress({int accountIndex = 0, int addressIndex = 1}) => + monero.Wallet_address(wptr!, + accountIndex: accountIndex, addressIndex: addressIndex); -int getFullBalance({int accountIndex = 0}) => monero.Wallet_balance(wptr!, accountIndex: accountIndex); +int getFullBalance({int accountIndex = 0}) => + monero.Wallet_balance(wptr!, accountIndex: accountIndex); -int getUnlockedBalance({int accountIndex = 0}) => monero.Wallet_unlockedBalance(wptr!, accountIndex: accountIndex); +int getUnlockedBalance({int accountIndex = 0}) => + monero.Wallet_unlockedBalance(wptr!, accountIndex: accountIndex); int getCurrentHeight() => monero.Wallet_blockChainHeight(wptr!); @@ -66,16 +76,14 @@ bool setupNodeSync( daemonPassword: $password ?? '' } '''); - monero.Wallet_init( - wptr!, - daemonAddress: address, - useSsl: useSSL, - proxyAddress: socksProxyAddress ?? '', - daemonUsername: login ?? '', - daemonPassword: password ?? '' - ); + monero.Wallet_init(wptr!, + daemonAddress: address, + useSsl: useSSL, + proxyAddress: socksProxyAddress ?? '', + daemonUsername: login ?? '', + daemonPassword: password ?? ''); // monero.Wallet_init3(wptr!, argv0: '', defaultLogBaseName: 'moneroc', console: true); - + final status = monero.Wallet_status(wptr!); if (status != 0) { @@ -96,18 +104,26 @@ Future connectToNode() async { return true; } -void setRefreshFromBlockHeight({required int height}) => monero.Wallet_setRefreshFromBlockHeight(wptr!, refresh_from_block_height: height); +void setRefreshFromBlockHeight({required int height}) => + monero.Wallet_setRefreshFromBlockHeight(wptr!, + refresh_from_block_height: height); -void setRecoveringFromSeed({required bool isRecovery}) => monero.Wallet_setRecoveringFromSeed(wptr!, recoveringFromSeed: isRecovery); +void setRecoveringFromSeed({required bool isRecovery}) => + monero.Wallet_setRecoveringFromSeed(wptr!, recoveringFromSeed: isRecovery); -void storeSync() { - monero.Wallet_store(wptr!); +final storeMutex = Mutex(); +void storeSync() async { + await storeMutex.acquire(); + final addr = wptr!.address; + Isolate.run(() { + monero.Wallet_store(Pointer.fromAddress(addr)); + }); + storeMutex.release(); } void setPasswordSync(String password) { monero.Wallet_setPassword(wptr!, password: password); - final status = monero.Wallet_status(wptr!); if (status == 0) { throw Exception(monero.Wallet_errorString(wptr!)); @@ -127,11 +143,10 @@ String getSecretSpendKey() => monero.Wallet_secretSpendKey(wptr!); String getPublicSpendKey() => monero.Wallet_publicSpendKey(wptr!); class SyncListener { - SyncListener(this.onNewBlock, this.onNewTransaction) - : _cachedBlockchainHeight = 0, - _lastKnownBlockHeight = 0, - _initialSyncHeight = 0; - + SyncListener(this.onNewBlock, this.onNewTransaction) + : _cachedBlockchainHeight = 0, + _lastKnownBlockHeight = 0, + _initialSyncHeight = 0; void Function(int, int, double) onNewBlock; void Function() onNewTransaction; @@ -236,7 +251,7 @@ Future setupNode( bool isLightWallet = false}) async => _setupNodeSync({ 'address': address, - 'login': login , + 'login': login, 'password': password, 'useSSL': useSSL, 'isLightWallet': isLightWallet, @@ -252,10 +267,12 @@ Future getNodeHeight() async => _getNodeHeight(0); void rescanBlockchainAsync() => monero.Wallet_rescanBlockchainAsync(wptr!); String getSubaddressLabel(int accountIndex, int addressIndex) { - return monero.Wallet_getSubaddressLabel(wptr!, accountIndex: accountIndex, addressIndex: addressIndex); + return monero.Wallet_getSubaddressLabel(wptr!, + accountIndex: accountIndex, addressIndex: addressIndex); } -Future setTrustedDaemon(bool trusted) async => monero.Wallet_setTrustedDaemon(wptr!, arg: trusted); +Future setTrustedDaemon(bool trusted) async => + monero.Wallet_setTrustedDaemon(wptr!, arg: trusted); Future trustedDaemon() async => monero.Wallet_trustedDaemon(wptr!); diff --git a/cw_monero/lib/api/wallet_manager.dart b/cw_monero/lib/api/wallet_manager.dart index 3d5d81c6d..f5e467f9e 100644 --- a/cw_monero/lib/api/wallet_manager.dart +++ b/cw_monero/lib/api/wallet_manager.dart @@ -1,4 +1,5 @@ import 'dart:ffi'; +import 'dart:isolate'; import 'package:cw_monero/api/account_list.dart'; import 'package:cw_monero/api/exceptions/wallet_creation_exception.dart'; @@ -190,7 +191,10 @@ void loadWallet( try { if (wptr == null || path != _lastOpenedWallet) { if (wptr != null) { - monero.Wallet_store(wptr!); + final addr = wptr!.address; + Isolate.run(() { + monero.Wallet_store(Pointer.fromAddress(addr)); + }); } wptr = monero.WalletManager_openWallet(wmPtr, path: path, password: password); diff --git a/cw_monero/pubspec.yaml b/cw_monero/pubspec.yaml index 811a8e80f..c31a0c0d1 100644 --- a/cw_monero/pubspec.yaml +++ b/cw_monero/pubspec.yaml @@ -26,6 +26,7 @@ dependencies: git: url: https://git.mrcyjanek.net/mrcyjanek/monero.dart ref: 6a17a405a1a260fa228b2f4fc94044088a4335ac + mutex: ^3.1.0 dev_dependencies: flutter_test: diff --git a/scripts/prepare_moneroc.sh b/scripts/prepare_moneroc.sh index 005db5f2d..015cf76f3 100755 --- a/scripts/prepare_moneroc.sh +++ b/scripts/prepare_moneroc.sh @@ -10,7 +10,7 @@ if [[ ! -d "monero_c" ]]; then git clone https://github.com/mrcyjanek/monero_c --branch rewrite-wip cd monero_c - git checkout 6781419926d38e045680035a6c2dfcfe32fa22a2 + git checkout 4a9142a36f1fca90372216f40e799fabb38e27d7 git reset --hard git submodule update --init --force --recursive ./apply_patches.sh monero