Skip to content

Commit

Permalink
Free memory Rust -> Dart
Browse files Browse the repository at this point in the history
  • Loading branch information
hhanh00 committed Jun 23, 2022
1 parent 72d2d1d commit bbd43f0
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 28 deletions.
2 changes: 1 addition & 1 deletion lib/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class HomeInnerState extends State<HomeInnerPage> 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(
Expand Down
3 changes: 1 addition & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,7 @@ class ZWalletAppState extends State<ZWalletApp> {
if (recover) {
final f = await getRecoveryFile();
final backup = await f.readAsString();
final res = WarpApi.restoreFullBackup("", backup);
print("Recovery $res");
WarpApi.restoreFullBackup("", backup);
f.delete();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/reset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class _FullRestoreState extends State<FullRestorePage> {
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 {
Expand Down
3 changes: 3 additions & 0 deletions lib/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,8 @@ abstract class _SyncStatus with Store {
int latestHeight = 0;

bool accountRestored = false;

@observable
bool syncing = false;

bool isSynced() {
Expand Down Expand Up @@ -580,6 +582,7 @@ abstract class _SyncStatus with Store {

@action
Future<void> 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);
Expand Down
2 changes: 1 addition & 1 deletion native/zcash-sync
46 changes: 29 additions & 17 deletions packages/warp_api_ffi/lib/warp_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int8>());
Expand Down Expand Up @@ -97,7 +101,7 @@ class WarpApi {

static String newDiversifiedAddress() {
final address = warp_api_lib.new_diversified_address();
return address.cast<Utf8>().toDartString();
return convertCString(address);
}

static void setActiveAccount(int coin, int account) {
Expand Down Expand Up @@ -154,7 +158,8 @@ class WarpApi {
final res = warp_api_lib.prepare_multi_payment(
recipientsJson.toNativeUtf8().cast<Int8>(),
useTransparent ? 1 : 0, anchorOffset);
return res.cast<Utf8>().toDartString();
final json = convertCString(res);
return json;
}

static Future<String> signOnly(int coin, int account, String tx, void Function(int) f) async {
Expand All @@ -171,7 +176,7 @@ class WarpApi {

static String broadcast(String txStr) {
final res = warp_api_lib.broadcast_tx(txStr.toNativeUtf8().cast<Int8>());
return res.cast<Utf8>().toDartString();
return convertCString(res);
}

// static String ledgerSign(int coin, String txFilename) {
Expand Down Expand Up @@ -226,42 +231,42 @@ class WarpApi {
address.toNativeUtf8().cast<Int8>(),
amount,
memo.toNativeUtf8().cast<Int8>());
return uri.cast<Utf8>().toDartString();
return convertCString(uri);
}

static String parsePaymentURI(String uri) {
final json = warp_api_lib.parse_payment_uri(
uri.toNativeUtf8().cast<Int8>());
return json.cast<Utf8>().toDartString();
return convertCString(json);
}

static String generateEncKey() {
return warp_api_lib.generate_random_enc_key().cast<Utf8>().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<Int8>());
return backup.cast<Utf8>().toDartString();
return convertCString(backup);
}

static String restoreFullBackup(String key, String backup) {
final res = warp_api_lib.restore_full_backup(key.toNativeUtf8().cast<Int8>(), backup.toNativeUtf8().cast<Int8>());
return res.cast<Utf8>().toDartString();
static void restoreFullBackup(String key, String backup) {
warp_api_lib.restore_full_backup(key.toNativeUtf8().cast<Int8>(), backup.toNativeUtf8().cast<Int8>());
}

static List<String> splitData(int id, String data) {
final res = warp_api_lib.split_data(id, data.toNativeUtf8().cast<Int8>()).cast<Utf8>().toDartString();
final res = convertCString(warp_api_lib.split_data(id, data.toNativeUtf8().cast<Int8>()));
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<Int8>()).cast<Utf8>().toDartString();
return convertCString(warp_api_lib.merge_data(drop.toNativeUtf8().cast<Int8>()));
}

static String getTxSummary(String tx) {
return warp_api_lib.get_tx_summary(tx.toNativeUtf8().cast<Int8>()).cast<Utf8>().toDartString();
return convertCString(warp_api_lib.get_tx_summary(tx.toNativeUtf8().cast<Int8>()));
// TODO: Free
}

// // static void storeShareSecret(int coin, int account, String secret) {
Expand Down Expand Up @@ -334,14 +339,14 @@ String sendPaymentIsolateFn(PaymentParams params) {
params.anchorOffset,
params.useTransparent ? 1 : 0,
params.port.nativePort);
return txId.cast<Utf8>().toDartString();
return convertCString(txId);
}

String signOnlyIsolateFn(SignOnlyParams params) {
final txId = warp_api_lib.sign(
params.tx.toNativeUtf8().cast<Int8>(),
params.port.nativePort);
return txId.cast<Utf8>().toDartString();
return convertCString(txId);
}

int getLatestHeightIsolateFn(Null n) {
Expand All @@ -354,7 +359,7 @@ int mempoolSyncIsolateFn(Null n) {

String shieldTAddrIsolateFn(Null n) {
final txId = warp_api_lib.shield_taddr();
return txId.cast<Utf8>().toDartString();
return convertCString(txId);
}

int syncHistoricalPricesIsolateFn(SyncHistoricalPricesParams params) {
Expand All @@ -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<Utf8>().toDartString();
return convertCString(txId);
}

int getTBalanceIsolateFn(GetTBalanceParams params) {
Expand Down Expand Up @@ -441,3 +446,10 @@ class ScanTransparentAccountsParams {
final int gapLimit;
ScanTransparentAccountsParams(this.gapLimit);
}

String convertCString(Pointer<Int8> s) {
final str = s.cast<Utf8>().toDartString();
warp_api_lib.deallocate_str(s);
return str;
}

40 changes: 37 additions & 3 deletions packages/warp_api_ffi/lib/warp_api_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<ffi.Int8> s,
) {
return _deallocate_str(
s,
);
}

late final _deallocate_str_ptr =
_lookup<ffi.NativeFunction<_c_deallocate_str>>('deallocate_str');
late final _dart_deallocate_str _deallocate_str =
_deallocate_str_ptr.asFunction<_dart_deallocate_str>();

void dart_post_cobject(
ffi.Pointer<ffi.Void> ptr,
) {
Expand All @@ -49,6 +62,15 @@ class NativeLibrary {
late final _dart_get_error _get_error =
_get_error_ptr.asFunction<_dart_get_error>();

ffi.Pointer<ffi.Int8> get_error_msg() {
return _get_error_msg();
}

late final _get_error_msg_ptr =
_lookup<ffi.NativeFunction<_c_get_error_msg>>('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<ffi.Int8> db_path,
) {
Expand Down Expand Up @@ -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<ffi.Int8> restore_full_backup(
void restore_full_backup(
ffi.Pointer<ffi.Int8> key,
ffi.Pointer<ffi.Int8> backup,
) {
Expand Down Expand Up @@ -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<ffi.Int8> s,
);

typedef _dart_deallocate_str = void Function(
ffi.Pointer<ffi.Int8> s,
);

typedef _c_dart_post_cobject = ffi.Void Function(
ffi.Pointer<ffi.Void> ptr,
);
Expand All @@ -625,6 +655,10 @@ typedef _c_get_error = ffi.Int8 Function();

typedef _dart_get_error = int Function();

typedef _c_get_error_msg = ffi.Pointer<ffi.Int8> Function();

typedef _dart_get_error_msg = ffi.Pointer<ffi.Int8> Function();

typedef _c_init_wallet = ffi.Void Function(
ffi.Pointer<ffi.Int8> db_path,
);
Expand Down Expand Up @@ -935,12 +969,12 @@ typedef _dart_get_full_backup = ffi.Pointer<ffi.Int8> Function(
ffi.Pointer<ffi.Int8> key,
);

typedef _c_restore_full_backup = ffi.Pointer<ffi.Int8> Function(
typedef _c_restore_full_backup = ffi.Void Function(
ffi.Pointer<ffi.Int8> key,
ffi.Pointer<ffi.Int8> backup,
);

typedef _dart_restore_full_backup = ffi.Pointer<ffi.Int8> Function(
typedef _dart_restore_full_backup = void Function(
ffi.Pointer<ffi.Int8> key,
ffi.Pointer<ffi.Int8> backup,
);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -167,4 +167,4 @@ msix_config:
msix_version: 1.0.0.0
logo_path: assets\icon.png
store: true
capabilities: internetClient
capabilities: internetClient

0 comments on commit bbd43f0

Please sign in to comment.