From bbd43f0eee557542b7b84afa5e295af9117fb45c Mon Sep 17 00:00:00 2001 From: Hanh Date: Wed, 22 Jun 2022 21:04:45 +0800 Subject: [PATCH] Free memory Rust -> Dart --- lib/home.dart | 2 +- lib/main.dart | 3 +- lib/reset.dart | 4 +- lib/store.dart | 3 ++ native/zcash-sync | 2 +- packages/warp_api_ffi/lib/warp_api.dart | 46 ++++++++++++------- .../warp_api_ffi/lib/warp_api_generated.dart | 40 ++++++++++++++-- pubspec.yaml | 4 +- 8 files changed, 76 insertions(+), 28 deletions(-) diff --git a/lib/home.dart b/lib/home.dart index 75f28bc25..2f8fbb720 100644 --- a/lib/home.dart +++ b/lib/home.dart @@ -142,7 +142,7 @@ class HomeInnerState extends State with SingleTickerProviderState return [ PopupMenuItem(child: Text(s.accounts), value: "Accounts"), PopupMenuItem(child: Text(s.backup), value: "Backup"), - PopupMenuItem(child: Text(s.rescan), value: "Rescan"), + PopupMenuItem(child: Text(s.rescan), value: "Rescan", enabled: !syncStatus.syncing), if (!simpleMode) PopupMenuItem(child: PopupMenuButton( diff --git a/lib/main.dart b/lib/main.dart index 5d8d0e21d..eeba411ac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -331,8 +331,7 @@ class ZWalletAppState extends State { if (recover) { final f = await getRecoveryFile(); final backup = await f.readAsString(); - final res = WarpApi.restoreFullBackup("", backup); - print("Recovery $res"); + WarpApi.restoreFullBackup("", backup); f.delete(); } diff --git a/lib/reset.dart b/lib/reset.dart index 810ff9278..05e6bb0bc 100644 --- a/lib/reset.dart +++ b/lib/reset.dart @@ -145,8 +145,8 @@ class _FullRestoreState extends State { final key = controller.text; final res = WarpApi.restoreFullBackup(key, backup); - if (res.isNotEmpty) { - final snackBar = SnackBar(content: Text(res)); + if (WarpApi.getError()) { + final snackBar = SnackBar(content: Text(WarpApi.getErrorMessage())); rootScaffoldMessengerKey.currentState?.showSnackBar(snackBar); } else { diff --git a/lib/store.dart b/lib/store.dart index a390d3f49..0614b6bb5 100644 --- a/lib/store.dart +++ b/lib/store.dart @@ -507,6 +507,8 @@ abstract class _SyncStatus with Store { int latestHeight = 0; bool accountRestored = false; + + @observable bool syncing = false; bool isSynced() { @@ -580,6 +582,7 @@ abstract class _SyncStatus with Store { @action Future rescan(BuildContext context, int height) async { + if (syncing) return; eta.reset(); final snackBar = SnackBar(content: Text(S.of(context).rescanRequested(height))); rootScaffoldMessengerKey.currentState?.showSnackBar(snackBar); diff --git a/native/zcash-sync b/native/zcash-sync index b40449731..141f5efff 160000 --- a/native/zcash-sync +++ b/native/zcash-sync @@ -1 +1 @@ -Subproject commit b4044973154b914b729d097e295f5c606a4163de +Subproject commit 141f5efff32aa5f0e5f623b9bede7f1c19ac3060 diff --git a/packages/warp_api_ffi/lib/warp_api.dart b/packages/warp_api_ffi/lib/warp_api.dart index b17da683e..dca0dfaba 100644 --- a/packages/warp_api_ffi/lib/warp_api.dart +++ b/packages/warp_api_ffi/lib/warp_api.dart @@ -43,6 +43,10 @@ class WarpApi { return warp_api_lib.get_error() != 0; } + static String getErrorMessage() { + return convertCString(warp_api_lib.get_error_msg()); + } + static initWallet(String dbPath) { warp_api_lib.init_wallet( dbPath.toNativeUtf8().cast()); @@ -97,7 +101,7 @@ class WarpApi { static String newDiversifiedAddress() { final address = warp_api_lib.new_diversified_address(); - return address.cast().toDartString(); + return convertCString(address); } static void setActiveAccount(int coin, int account) { @@ -154,7 +158,8 @@ class WarpApi { final res = warp_api_lib.prepare_multi_payment( recipientsJson.toNativeUtf8().cast(), useTransparent ? 1 : 0, anchorOffset); - return res.cast().toDartString(); + final json = convertCString(res); + return json; } static Future signOnly(int coin, int account, String tx, void Function(int) f) async { @@ -171,7 +176,7 @@ class WarpApi { static String broadcast(String txStr) { final res = warp_api_lib.broadcast_tx(txStr.toNativeUtf8().cast()); - return res.cast().toDartString(); + return convertCString(res); } // static String ledgerSign(int coin, String txFilename) { @@ -226,42 +231,42 @@ class WarpApi { address.toNativeUtf8().cast(), amount, memo.toNativeUtf8().cast()); - return uri.cast().toDartString(); + return convertCString(uri); } static String parsePaymentURI(String uri) { final json = warp_api_lib.parse_payment_uri( uri.toNativeUtf8().cast()); - return json.cast().toDartString(); + return convertCString(json); } static String generateEncKey() { - return warp_api_lib.generate_random_enc_key().cast().toDartString(); + return convertCString(warp_api_lib.generate_random_enc_key()); } static String getFullBackup(String key) { final backup = warp_api_lib.get_full_backup(key.toNativeUtf8().cast()); - return backup.cast().toDartString(); + return convertCString(backup); } - static String restoreFullBackup(String key, String backup) { - final res = warp_api_lib.restore_full_backup(key.toNativeUtf8().cast(), backup.toNativeUtf8().cast()); - return res.cast().toDartString(); + static void restoreFullBackup(String key, String backup) { + warp_api_lib.restore_full_backup(key.toNativeUtf8().cast(), backup.toNativeUtf8().cast()); } static List splitData(int id, String data) { - final res = warp_api_lib.split_data(id, data.toNativeUtf8().cast()).cast().toDartString(); + final res = convertCString(warp_api_lib.split_data(id, data.toNativeUtf8().cast())); final jsonMap = jsonDecode(res); final raptorq = RaptorQDrops.fromJson(jsonMap); return raptorq.drops; } static String mergeData(String drop) { - return warp_api_lib.merge_data(drop.toNativeUtf8().cast()).cast().toDartString(); + return convertCString(warp_api_lib.merge_data(drop.toNativeUtf8().cast())); } static String getTxSummary(String tx) { - return warp_api_lib.get_tx_summary(tx.toNativeUtf8().cast()).cast().toDartString(); + return convertCString(warp_api_lib.get_tx_summary(tx.toNativeUtf8().cast())); + // TODO: Free } // // static void storeShareSecret(int coin, int account, String secret) { @@ -334,14 +339,14 @@ String sendPaymentIsolateFn(PaymentParams params) { params.anchorOffset, params.useTransparent ? 1 : 0, params.port.nativePort); - return txId.cast().toDartString(); + return convertCString(txId); } String signOnlyIsolateFn(SignOnlyParams params) { final txId = warp_api_lib.sign( params.tx.toNativeUtf8().cast(), params.port.nativePort); - return txId.cast().toDartString(); + return convertCString(txId); } int getLatestHeightIsolateFn(Null n) { @@ -354,7 +359,7 @@ int mempoolSyncIsolateFn(Null n) { String shieldTAddrIsolateFn(Null n) { final txId = warp_api_lib.shield_taddr(); - return txId.cast().toDartString(); + return convertCString(txId); } int syncHistoricalPricesIsolateFn(SyncHistoricalPricesParams params) { @@ -365,7 +370,7 @@ int syncHistoricalPricesIsolateFn(SyncHistoricalPricesParams params) { String commitUnsavedContactsIsolateFn(CommitContactsParams params) { final txId = warp_api_lib.commit_unsaved_contacts(params.anchorOffset); - return txId.cast().toDartString(); + return convertCString(txId); } int getTBalanceIsolateFn(GetTBalanceParams params) { @@ -441,3 +446,10 @@ class ScanTransparentAccountsParams { final int gapLimit; ScanTransparentAccountsParams(this.gapLimit); } + +String convertCString(Pointer s) { + final str = s.cast().toDartString(); + warp_api_lib.deallocate_str(s); + return str; +} + diff --git a/packages/warp_api_ffi/lib/warp_api_generated.dart b/packages/warp_api_ffi/lib/warp_api_generated.dart index de07b07e0..136c70680 100644 --- a/packages/warp_api_ffi/lib/warp_api_generated.dart +++ b/packages/warp_api_ffi/lib/warp_api_generated.dart @@ -27,6 +27,19 @@ class NativeLibrary { late final _dart_dummy_export _dummy_export = _dummy_export_ptr.asFunction<_dart_dummy_export>(); + void deallocate_str( + ffi.Pointer s, + ) { + return _deallocate_str( + s, + ); + } + + late final _deallocate_str_ptr = + _lookup>('deallocate_str'); + late final _dart_deallocate_str _deallocate_str = + _deallocate_str_ptr.asFunction<_dart_deallocate_str>(); + void dart_post_cobject( ffi.Pointer ptr, ) { @@ -49,6 +62,15 @@ class NativeLibrary { late final _dart_get_error _get_error = _get_error_ptr.asFunction<_dart_get_error>(); + ffi.Pointer get_error_msg() { + return _get_error_msg(); + } + + late final _get_error_msg_ptr = + _lookup>('get_error_msg'); + late final _dart_get_error_msg _get_error_msg = + _get_error_msg_ptr.asFunction<_dart_get_error_msg>(); + void init_wallet( ffi.Pointer db_path, ) { @@ -551,7 +573,7 @@ class NativeLibrary { late final _dart_get_full_backup _get_full_backup = _get_full_backup_ptr.asFunction<_dart_get_full_backup>(); - ffi.Pointer restore_full_backup( + void restore_full_backup( ffi.Pointer key, ffi.Pointer backup, ) { @@ -613,6 +635,14 @@ typedef _c_dummy_export = ffi.Void Function(); typedef _dart_dummy_export = void Function(); +typedef _c_deallocate_str = ffi.Void Function( + ffi.Pointer s, +); + +typedef _dart_deallocate_str = void Function( + ffi.Pointer s, +); + typedef _c_dart_post_cobject = ffi.Void Function( ffi.Pointer ptr, ); @@ -625,6 +655,10 @@ typedef _c_get_error = ffi.Int8 Function(); typedef _dart_get_error = int Function(); +typedef _c_get_error_msg = ffi.Pointer Function(); + +typedef _dart_get_error_msg = ffi.Pointer Function(); + typedef _c_init_wallet = ffi.Void Function( ffi.Pointer db_path, ); @@ -935,12 +969,12 @@ typedef _dart_get_full_backup = ffi.Pointer Function( ffi.Pointer key, ); -typedef _c_restore_full_backup = ffi.Pointer Function( +typedef _c_restore_full_backup = ffi.Void Function( ffi.Pointer key, ffi.Pointer backup, ); -typedef _dart_restore_full_backup = ffi.Pointer Function( +typedef _dart_restore_full_backup = void Function( ffi.Pointer key, ffi.Pointer backup, ); diff --git a/pubspec.yaml b/pubspec.yaml index ea3eec25d..c43dd889f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.2.8+244 +version: 1.2.8+247 environment: sdk: ">=2.12.0 <3.0.0" @@ -167,4 +167,4 @@ msix_config: msix_version: 1.0.0.0 logo_path: assets\icon.png store: true - capabilities: internetClient \ No newline at end of file + capabilities: internetClient