From 53d4aa8a973fd58a348fae49143afba87818cb14 Mon Sep 17 00:00:00 2001 From: Konstantin Ullrich Date: Sat, 14 Jun 2025 00:13:07 +0200 Subject: [PATCH] feat(cw_tari): Enhance recovery process and callback handling --- cw_tari/lib/callback.dart | 182 +++++++++++++++++++++-------------- cw_tari/lib/tari_wallet.dart | 38 ++++++-- 2 files changed, 142 insertions(+), 78 deletions(-) diff --git a/cw_tari/lib/callback.dart b/cw_tari/lib/callback.dart index e01fad4c5..16fb14408 100644 --- a/cw_tari/lib/callback.dart +++ b/cw_tari/lib/callback.dart @@ -1,89 +1,112 @@ import 'dart:ffi'; import 'package:tari/src/generated_bindings_tari.freeze.g.dart'; -import 'package:tari/tari.dart'; +import 'package:ffi/ffi.dart'; +import 'package:tari/ffi.dart'; class CallbackPlaceholders { + static int _chainTipHeight = 0; + static int _scannedHeight = 0; + static int get chainTipHeight => _chainTipHeight; + static int get scannedHeight => _scannedHeight; // Placeholder for callback_received_transaction static void callbackReceivedTransaction(Pointer context, Pointer transaction) { - print('callbackReceivedTransaction called'); + // print('callbackReceivedTransaction called'); } // Placeholder for callback_received_transaction_reply static void callbackReceivedTransactionReply( Pointer context, Pointer transaction) { - print('callbackReceivedTransactionReply called'); + // print('callbackReceivedTransactionReply called'); } - static NativeCallable - get callbackReceivedTransactionReplyPtr => - NativeCallable.listener(callbackReceivedTransactionReply); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackReceivedTransactionReplyPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer)>( + callbackReceivedTransactionReply); // Placeholder for callback_received_finalized_transaction static void callbackReceivedFinalizedTransaction( Pointer context, Pointer transaction) { - print('callbackReceivedFinalizedTransaction called'); + // print('callbackReceivedFinalizedTransaction called'); } - static NativeCallable - get callbackReceivedFinalizedTransactionPtr => - NativeCallable.listener( - callbackReceivedFinalizedTransaction); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackReceivedFinalizedTransactionPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer)>( + callbackReceivedFinalizedTransaction); // Placeholder for callback_transaction_broadcast static void callbackTransactionBroadcast( Pointer context, Pointer transaction) { - print('callbackTransactionBroadcast called'); + // print('callbackTransactionBroadcast called'); } - static NativeCallable - get callbackTransactionBroadcastPtr => - NativeCallable.listener( - callbackTransactionBroadcast); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackTransactionBroadcastPtr => Pointer.fromFunction< + Void Function(Pointer, + Pointer)>(callbackTransactionBroadcast); // Placeholder for callback_transaction_mined static void callbackTransactionMined( Pointer context, Pointer transaction) { - print('callbackTransactionMined called'); + // print('callbackTransactionMined called'); } - static NativeCallable - get callbackTransactionMinedPtr => - NativeCallable.listener(callbackTransactionMined); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackTransactionMinedPtr => Pointer.fromFunction< + Void Function(Pointer, + Pointer)>(callbackTransactionMined); // Placeholder for callback_transaction_mined_unconfirmed static void callbackTransactionMinedUnconfirmed(Pointer context, Pointer transaction, int unconfirmed) { - print('callbackTransactionMinedUnconfirmed called'); + // print('callbackTransactionMinedUnconfirmed called'); } - static NativeCallable - get callbackTransactionMinedUnconfirmedPtr => - NativeCallable.listener( - callbackTransactionMinedUnconfirmed); + static Pointer< + NativeFunction< + Void Function( + Pointer, Pointer, Uint64)>> + get callbackTransactionMinedUnconfirmedPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer, + Uint64)>(callbackTransactionMinedUnconfirmed); // Placeholder for callback_faux_transaction_confirmed static void callbackFauxTransactionConfirmed( Pointer context, Pointer transaction) { - print('callbackFauxTransactionConfirmed called'); + // print('callbackFauxTransactionConfirmed called'); } - static NativeCallable - get callbackFauxTransactionConfirmedPtr => - NativeCallable.listener( - callbackFauxTransactionConfirmed); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackFauxTransactionConfirmedPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer)>( + callbackFauxTransactionConfirmed); // Placeholder for callback_faux_transaction_unconfirmed static void callbackFauxTransactionUnconfirmed(Pointer context, Pointer transaction, int unconfirmed) { - print('callbackFauxTransactionUnconfirmed called'); + // print('callbackFauxTransactionUnconfirmed called'); } - static NativeCallable - get callbackFauxTransactionUnconfirmedPtr => - NativeCallable.listener( - callbackFauxTransactionUnconfirmed); + static Pointer< + NativeFunction< + Void Function( + Pointer, Pointer, Uint64)>> + get callbackFauxTransactionUnconfirmedPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer, + Uint64)>(callbackFauxTransactionUnconfirmed); // Placeholder for callback_transaction_send_result static void callbackTransactionSendResult(Pointer context, int result, @@ -91,11 +114,14 @@ class CallbackPlaceholders { print('callbackTransactionSendResult called'); } - static NativeCallable< + static Pointer< + NativeFunction< + Void Function(Pointer, UnsignedLongLong, + Pointer)>> + get callbackTransactionSendResultPtr => Pointer.fromFunction< Void Function(Pointer, UnsignedLongLong, - Pointer)> get callbackTransactionSendResultPtr => - NativeCallable.listener( - callbackTransactionSendResult); + Pointer)>( + callbackTransactionSendResult); // Placeholder for callback_transaction_cancellation static void callbackTransactionCancellation(Pointer context, @@ -103,64 +129,70 @@ class CallbackPlaceholders { print('callbackTransactionCancellation called'); } - static NativeCallable< - Void Function(Pointer, Pointer, Uint64)> - get callbackTransactionCancellationPtr => - NativeCallable.listener( - callbackTransactionCancellation); + static Pointer< + NativeFunction< + Void Function( + Pointer, Pointer, Uint64)>> + get callbackTransactionCancellationPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer, + Uint64)>(callbackTransactionCancellation); // Placeholder for callback_txo_validation_complete static void callbackTxoValidationComplete( Pointer context, int txo, int validation) { - print('callbackTxoValidationComplete called'); + print('callbackTxoValidationComplete called $txo $validation'); } - static NativeCallable, Uint64, Uint64)> + static Pointer, Uint64, Uint64)>> get callbackTxoValidationCompletePtr => - NativeCallable.listener( + Pointer.fromFunction, Uint64, Uint64)>( callbackTxoValidationComplete); // Placeholder for callback_contacts_liveness_data_updated static void callbackContactsLivenessDataUpdated( Pointer context, Pointer data) { - print('callbackContactsLivenessDataUpdated called'); + // print('callbackContactsLivenessDataUpdated called'); } - static NativeCallable< - Void Function(Pointer, Pointer)> - get callbackContactsLivenessDataUpdatedPtr => - NativeCallable.listener( - callbackContactsLivenessDataUpdated); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackContactsLivenessDataUpdatedPtr => Pointer.fromFunction< + Void Function(Pointer, Pointer)>( + callbackContactsLivenessDataUpdated); // Placeholder for callback_balance_updated static void callbackBalanceUpdated( Pointer context, Pointer balance) { - print('callbackBalanceUpdated called'); + // print('callbackBalanceUpdated called'); } - static NativeCallable, Pointer)> - get callbackBalanceUpdatedPtr => - NativeCallable.listener(callbackBalanceUpdated); + static Pointer< + NativeFunction, Pointer)>> + get callbackBalanceUpdatedPtr => Pointer.fromFunction< + Void Function( + Pointer, Pointer)>(callbackBalanceUpdated); // Placeholder for callback_transaction_validation_complete static void callbackTransactionValidationComplete( Pointer context, int transaction, int validation) { - print('callbackTransactionValidationComplete called'); + // print( + // 'callbackTransactionValidationComplete called $transaction $validation'); } - static NativeCallable, Uint64, Uint64)> + static Pointer, Uint64, Uint64)>> get callbackTransactionValidationCompletePtr => - NativeCallable.listener( + Pointer.fromFunction, Uint64, Uint64)>( callbackTransactionValidationComplete); // Placeholder for callback_saf_messages_received static void callbackSafMessagesReceived(Pointer context) { - print('callbackSafMessagesReceived called'); + // print('callbackSafMessagesReceived called'); } - static NativeCallable)> + static Pointer)>> get callbackSafMessagesReceivedPtr => - NativeCallable.listener( + Pointer.fromFunction)>( callbackSafMessagesReceived); // Placeholder for callback_connectivity_status @@ -179,28 +211,34 @@ class CallbackPlaceholders { } } - static NativeCallable, Uint64)> + static Pointer, Uint64)>> get callbackConnectivityStatusPtr => - NativeCallable.listener( + Pointer.fromFunction, Uint64)>( callbackConnectivityStatus); // Placeholder for callback_wallet_scanned_height static void callbackWalletScannedHeight(Pointer context, int height) { - print('callbackWalletScannedHeight called'); + print('Scanned height: $height / ${_chainTipHeight}'); + _scannedHeight = height; } - static NativeCallable, Uint64)> + static Pointer, Uint64)>> get callbackWalletScannedHeightPtr => - NativeCallable.listener( + Pointer.fromFunction, Uint64)>( callbackWalletScannedHeight); // Placeholder for callback_base_node_state static void callbackBaseNodeState( Pointer context, Pointer state) { - print('callbackBaseNodeState called'); + final errorPtr = malloc(); + _chainTipHeight = lib.basenode_state_get_height_of_the_longest_chain(state, errorPtr); + print('Current chain tip height: $_chainTipHeight'); } - static NativeCallable, Pointer)> - get callbackBaseNodeStatePtr => - NativeCallable.listener(callbackBaseNodeState); + static Pointer< + NativeFunction< + Void Function(Pointer, Pointer)>> + get callbackBaseNodeStatePtr => Pointer.fromFunction< + Void Function(Pointer, + Pointer)>(callbackBaseNodeState); } diff --git a/cw_tari/lib/tari_wallet.dart b/cw_tari/lib/tari_wallet.dart index b3c73ac0c..fd14acffc 100644 --- a/cw_tari/lib/tari_wallet.dart +++ b/cw_tari/lib/tari_wallet.dart @@ -13,6 +13,7 @@ import 'package:cw_core/wallet_addresses.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_keys_file.dart'; +import 'package:cw_tari/callback.dart'; import 'package:cw_tari/pending_tari_transaction.dart'; import 'package:cw_tari/tari_balance.dart'; import 'package:cw_tari/tari_transaction_history.dart'; @@ -101,28 +102,53 @@ abstract class TariWalletBase _walletFfi.setBaseNode(); + await Future.delayed(Duration(seconds: 10)); // Give it time to connect to the base node + + var isRecovering = true; _walletFfi.startRecovery((_, event, arg1, arg2) { switch (event) { + case 0: + print("[Recovery] Connecting to base node..."); + break; + case 1: + print("[Recovery] Connection to base node established"); + break; case 2: - print("Connection to base node failed. Retry ${arg1}/${arg2}"); + print("[Recovery] Connection to base node failed. Retry ${arg1}/${arg2}"); break; case 3: - print("Scanning progress: ${arg1}/${arg2} blocks"); + print("[Recovery] Scanning progress: ${arg1}/${arg2} blocks"); + isRecovering = false; break; case 4: print( - "Recovery completed! Recovered ${arg1} UTXOs (${arg2} MicroMinotari)"); + "[Recovery] Recovery completed! Recovered ${arg1} UTXOs (${arg2} MicroMinotari)"); + isRecovering = false; break; case 5: - print("Scanning round failed. Retry ${arg1}/${arg2}"); + print("[Recovery] Scanning round failed. Retry ${arg1}/${arg2}"); break; case 6: - print("Recovery failed!"); + print("[Recovery] Recovery failed!"); + isRecovering = false; + break; + default: + print("[Recovery] Unknown event: $event ${arg1} ${arg2}"); break; } }); + await Future.delayed(Duration(seconds: 15)); // Give it time to scan the blocks - log(_walletFfi.isRecovering().toString()); + while (isRecovering) { + await Future.delayed(Duration(seconds: 5)); // Check every 5 seconds + final balance = _walletFfi.getBalance(); + print("Scanned height: ${CallbackPlaceholders.scannedHeight} / ${CallbackPlaceholders.chainTipHeight}"); + print( + "Balance: ${balance.available} ${balance.pendingIncoming} ${balance.pendingOutgoing} ${balance.timeLocked}"); + if (CallbackPlaceholders.scannedHeight >= CallbackPlaceholders.chainTipHeight && CallbackPlaceholders.chainTipHeight > 0) { + break; + } + } // syncStatus = SyncedSyncStatus(); } catch (e) {