This commit is contained in:
Matthew Fosse 2024-09-16 13:46:48 -07:00
parent e51d6ea8c0
commit 35c4f3e2f1
3 changed files with 61 additions and 26 deletions

View file

@ -306,7 +306,9 @@ abstract class ElectrumWalletBase
bool? doSingleScan, bool? doSingleScan,
bool? usingSupportedNode, bool? usingSupportedNode,
}) async { }) async {
final chainTip = chainTipParam ?? await getUpdatedChainTip(); // final chainTip = chainTipParam ?? await getUpdatedChainTip();
final chainTip = 861586;
print("chainTip: $chainTip");
if (chainTip == height) { if (chainTip == height) {
syncStatus = SyncedSyncStatus(); syncStatus = SyncedSyncStatus();
@ -406,7 +408,10 @@ abstract class ElectrumWalletBase
nodeSupportsSilentPayments = false; nodeSupportsSilentPayments = false;
} }
print("sp sync: ${message.syncStatus.progress() * 100}%"); if (message.syncStatus is SyncingSyncStatus) {
print("sp sync: ${(message.syncStatus as SyncingSyncStatus).blocksLeft} blocks left");
}
syncStatus = message.syncStatus; syncStatus = message.syncStatus;
await walletInfo.updateRestoreHeight(message.height); await walletInfo.updateRestoreHeight(message.height);

View file

@ -35,6 +35,10 @@ const notificationChannelName = 'CAKE BACKGROUND SERVICE';
const notificationChannelDescription = 'Cake Wallet Background Service'; const notificationChannelDescription = 'Cake Wallet Background Service';
const DELAY_SECONDS_BEFORE_SYNC_START = 15; const DELAY_SECONDS_BEFORE_SYNC_START = 15;
const spNotificationId = 888;
const spNodeNotificationMessage = "Currently configured Bitcoin node does not support Silent Payments. skipping wallet";
void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async { void setNotificationStandby(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
flutterLocalNotificationsPlugin.cancelAll(); flutterLocalNotificationsPlugin.cancelAll();
flutterLocalNotificationsPlugin.show( flutterLocalNotificationsPlugin.show(
@ -69,6 +73,23 @@ void setNotificationReady(FlutterLocalNotificationsPlugin flutterLocalNotificati
); );
} }
void setSpNodeWarningNotification(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin) async {
flutterLocalNotificationsPlugin.cancelAll();
flutterLocalNotificationsPlugin.show(
notificationId,
initialNotificationTitle,
spNodeNotificationMessage,
const NotificationDetails(
android: AndroidNotificationDetails(
notificationChannelId,
notificationChannelName,
icon: 'ic_bg_service_small',
ongoing: true,
),
),
);
}
@pragma('vm:entry-point') @pragma('vm:entry-point')
Future<void> onStart(ServiceInstance service) async { Future<void> onStart(ServiceInstance service) async {
print("BACKGROUND SERVICE STARTED!"); print("BACKGROUND SERVICE STARTED!");
@ -152,19 +173,19 @@ Future<void> onStart(ServiceInstance service) async {
.toList(); .toList();
// we only need to sync the first litecoin wallet since they share the same collection of blocks // we only need to sync the first litecoin wallet since they share the same collection of blocks
if (litecoinWallets.isNotEmpty) { // if (litecoinWallets.isNotEmpty) {
try { // try {
final firstWallet = litecoinWallets.first; // final firstWallet = litecoinWallets.first;
final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name); // final wallet = await walletLoadingService.load(firstWallet.type, firstWallet.name);
final node = settingsStore.getCurrentNode(firstWallet.type); // final node = settingsStore.getCurrentNode(firstWallet.type);
await wallet.connectToNode(node: node); // await wallet.connectToNode(node: node);
await wallet.startSync(); // // calling start sync isn't necessary since it's called after connecting to the node
syncingWallets.add(wallet); // syncingWallets.add(wallet);
} catch (e) { // } catch (e) {
// couldn't connect to mwebd (most likely) // // couldn't connect to mwebd (most likely)
print("error syncing litecoin wallet: $e"); // print("error syncing litecoin wallet: $e");
} // }
} // }
final List<WalletListItem> bitcoinWallets = walletListViewModel.wallets final List<WalletListItem> bitcoinWallets = walletListViewModel.wallets
.where((element) => element.type == WalletType.bitcoin) .where((element) => element.type == WalletType.bitcoin)
@ -175,10 +196,18 @@ Future<void> onStart(ServiceInstance service) async {
final wallet = final wallet =
await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name); await walletLoadingService.load(bitcoinWallets[i].type, bitcoinWallets[i].name);
final node = settingsStore.getCurrentNode(bitcoinWallets[i].type); final node = settingsStore.getCurrentNode(bitcoinWallets[i].type);
await wallet.connectToNode(node: node);
await wallet.startSync(); // bool nodeSupportsSP = await (wallet as ElectrumWallet).getNodeSupportsSilentPayments();
// TODO: use proxy layer: // TODO: fix this:
await (wallet as ElectrumWallet).rescan(height: 1); bool nodeSupportsSP = node.uriRaw.contains("electrs");
if (!nodeSupportsSP) {
print("Configured node does not support silent payments, skipping wallet");
setSpNodeWarningNotification(flutterLocalNotificationsPlugin);
continue;
}
// await wallet.connectToNode(node: node);
// (wallet as ElectrumWallet).setSilentPaymentsScanning(true);
(wallet as ElectrumWallet).rescan(height: 1);
syncingWallets.add(wallet); syncingWallets.add(wallet);
} catch (e) { } catch (e) {
print("error syncing bitcoin wallet: $e"); print("error syncing bitcoin wallet: $e");
@ -361,17 +390,17 @@ class BackgroundTasks {
final SyncMode syncMode = settingsStore.currentSyncMode; final SyncMode syncMode = settingsStore.currentSyncMode;
final bool syncAll = settingsStore.currentSyncAll; final bool syncAll = settingsStore.currentSyncAll;
if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) {
bgService.invoke('stopService');
return;
}
if (settingsStore.showSyncNotification) { if (settingsStore.showSyncNotification) {
flutterLocalNotificationsPlugin flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>() .resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
?.requestNotificationsPermission(); ?.requestNotificationsPermission();
} }
if (syncMode.type == SyncType.disabled || !FeatureFlag.isBackgroundSyncEnabled) {
bgService.invoke('stopService');
return;
}
bgService.invoke('stopService'); bgService.invoke('stopService');
await initializeService(bgService, settingsStore.showSyncNotification); await initializeService(bgService, settingsStore.showSyncNotification);

View file

@ -22,5 +22,6 @@ Future<void> loadCurrentWallet({String? password}) async {
final wallet = await walletLoadingService.load(type, name, password: password); final wallet = await walletLoadingService.load(type, name, password: password);
await appStore.changeCurrentWallet(wallet); await appStore.changeCurrentWallet(wallet);
// TODO: re-enable (need to figure out how to prevent current wallet from being loaded in the background service!) // TODO: re-enable (need to figure out how to prevent current wallet from being loaded in the background service!)
// getIt.get<BackgroundTasks>().registerBackgroundService(); // STILL TODO:!
getIt.get<BackgroundTasks>().registerBackgroundService();
} }