Skip to content

Commit

Permalink
Bug: Tweak ZHTLC resync logic to fix iOS error
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlVS committed Oct 11, 2023
1 parent 7f193e7 commit 8df0d44
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 49 deletions.
15 changes: 14 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_bloc/flutter_bloc.dart' as real_bloc;
import 'package:komodo_dex/packages/z_coin_activation/bloc/z_coin_activation_bloc.dart';
import 'package:komodo_dex/packages/z_coin_activation/bloc/z_coin_activation_event.dart';
import 'package:komodo_dex/packages/z_coin_activation/bloc/z_coin_activation_state.dart';
import 'package:komodo_dex/packages/z_coin_activation/bloc/z_coin_notifications.dart';
import 'package:komodo_dex/packages/z_coin_activation/widgets/z_coin_status_list_tile.dart';
import 'package:komodo_dex/services/mm.dart';
import '../app_config/app_config.dart';
import '../blocs/authenticate_bloc.dart';
import '../blocs/coins_bloc.dart';
Expand Down Expand Up @@ -178,6 +180,8 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
super.initState();
_initCheckNetworkStatus();
WidgetsBinding.instance.addObserver(this);

MM.untilRpcIsUp().then((_) => _requestResync());
}

@override
Expand Down Expand Up @@ -207,11 +211,20 @@ class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
Log('main', 'lifecycle: resumed');
mainBloc.isInBackground = false;
lockService.lockSignal(context);
await mmSe.handleWakeUp();
await mmSe.handleWakeUp().whenComplete(() {
if (mmSe.running) _requestResync();
});

break;
}
}

void _requestResync() {
context
.read<ZCoinActivationBloc>()
.add(ZCoinActivationRequested(resync: true));
}

@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [
Expand Down
20 changes: 5 additions & 15 deletions lib/packages/z_coin_activation/bloc/z_coin_activation_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ class ZCoinActivationApi {

Stream<ZCoinStatus> activateCoin(
String ticker, {
bool firstLaunch = false,
bool resync = false,
}) async* {
int coinTaskId = await getTaskId(ticker);
ZCoinStatus taskStatus;
if (!firstLaunch) {
if (!resync) {
final isAlreadyActivated = (await activatedZCoins()).contains(ticker);

taskStatus = coinTaskId == null
Expand Down Expand Up @@ -225,7 +225,7 @@ class ZCoinActivationApi {

ZCoinStatus lastEmittedStatus;

coinTaskId = await initiateActivation(ticker, noSyncParams: firstLaunch);
coinTaskId = await initiateActivation(ticker, noSyncParams: resync);

lastEmittedStatus = await activationTaskStatus(coinTaskId, ticker: ticker);

Expand Down Expand Up @@ -410,8 +410,8 @@ class ZCoinActivationApi {
Map<String, dynamic> responseBody,
String ticker,
}) async {
int _progress = 100;
String _messageDetails = '';
int _progress = 5;
String _messageDetails = 'Activating $ticker';
if (!responseBody.containsKey('result')) return null;

final result = responseBody['result'] is Map<String, dynamic>
Expand All @@ -420,13 +420,6 @@ class ZCoinActivationApi {
String status = result['status'];
dynamic details = result['details'];

// checkPointBlock will be removed
//Coin coin = coinsBloc.getKnownCoinByAbbr(ticker);
// int blockOffset = 0;
// if (coin.type == CoinType.zhtlc) {
// blockOffset = coin.protocol.protocolData.checkPointBlock?.height ?? 0;
// }

// use range from checkpoint block to present
if (status == 'Ok') {
if (details.containsKey('error')) {
Expand Down Expand Up @@ -487,9 +480,6 @@ class ZCoinActivationApi {
_messageDetails = isBuildingPhase
? 'Building $ticker wallet database'
: 'Updating $ticker blocks cache';
} else {
_progress = 5;
_messageDetails = 'Activating $ticker';
}

return ZCoinStatus(
Expand Down
38 changes: 21 additions & 17 deletions lib/packages/z_coin_activation/bloc/z_coin_activation_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,22 @@ class ZCoinActivationBloc
ZCoinActivationRequested event,
Emitter<ZCoinActivationState> emit,
) async {
final toActivate = await _repository.outstandingZCoinActivations();
final hasRequestedCoins =
(await _repository.getRequestedActivatedCoins()).isNotEmpty;

if (!hasRequestedCoins) {
emit(ZCoinActivationInitial());
return;
}

final isResync = event.resync;

final toActivate = isResync
? await _repository.getEnabledZCoins()
: await _repository.outstandingZCoinActivations();
final toActivateInitalCount = toActivate.length;

try {
final isAllCoinsEnabled = await _repository.isAllRequestedZCoinsEnabled();
if (isAllCoinsEnabled) {
add(ZCoinActivationStatusRequested());
return;
}

final zhtlcActivationPrefs = await loadZhtlcActivationPrefs();
SyncType zhtlcSyncType = zhtlcActivationPrefs['zhtlcSyncType'];

Expand All @@ -63,7 +69,9 @@ class ZCoinActivationBloc
),
);
await emit.forEach<ZCoinStatus>(
_repository.activateRequestedZCoins(),
event.resync
? _repository.resyncEnabledZCoins()
: _repository.activateRequestedZCoins(),
onData: (coinStatus) {
if (coinStatus.isFailed) {
return ZCoinActivationFailure(
Expand Down Expand Up @@ -108,7 +116,7 @@ class ZCoinActivationBloc
progress: shouldShowNewProgress ? overallProgress : lastProgress,
message: 'Activating ${coinStatus.coin}',
eta: eta,
startTime: previousInProgressState.startTime,
startTime: previousInProgressState?.startTime ?? DateTime.now(),
);
},
onError: (e, s) {
Expand Down Expand Up @@ -165,6 +173,8 @@ class ZCoinActivationBloc
try {
final isAllCoinsEnabled = await _repository.isAllRequestedZCoinsEnabled();

final mustResync = await isResyncing();

// TODO? Consider if better to base the "in progress" state on the
// API task status instead of the existing bloc state.
final isActivationInProgress = state is ZCoinActivationInProgess;
Expand All @@ -174,23 +184,17 @@ class ZCoinActivationBloc
}

ZCoinActivationState newState;
if (isAllCoinsEnabled) {
if (isAllCoinsEnabled && !mustResync) {
newState = isActivationInProgress
? ZCoinActivationSuccess()
: ZCoinActivationKnownState(isAllCoinsEnabled);
: ZCoinActivationKnownState(false);
} else if (isActivationInProgress) {
newState = state as ZCoinActivationInProgess;
} else {
newState = ZCoinActivationKnownState(isAllCoinsEnabled);
}

emit(newState);

if (!isAllCoinsEnabled && !isActivationInProgress) {
add(ZCoinActivationRequested());
} else {
_repository.willInitialize = false;
}
} catch (e) {
debugPrint('Failed to get activation status: $e');
emit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ abstract class ZCoinActivationEvent {
}

/// Activates any requested ZCoins not already activated
class ZCoinActivationRequested extends ZCoinActivationEvent {}
class ZCoinActivationRequested extends ZCoinActivationEvent {
const ZCoinActivationRequested({this.resync = false});

final bool resync;
}

/// Sets the list of requested ZCoins to activate.
/// Must call [ZCoinActivationRequested] to activate the coins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ class ZCoinActivationRepository with RequestedZCoinsStorage {
static Future<String> get taskIdKey async =>
'activationTaskId_${(await Db.getCurrentWallet()).id}';

bool willInitialize = true;
Stream<ZCoinStatus> resyncEnabledZCoins() async* {
final enabledZCoins = await getEnabledZCoins();

Stream<ZCoinStatus> _activateZCoins(List<String> zCoins) async* {
try {
bool firstLaunch = willInitialize;
willInitialize = false;
yield* _activateZCoins(enabledZCoins, resyncOnly: true);
}

Stream<ZCoinStatus> _activateZCoins(
List<String> zCoins, {
bool resyncOnly = false,
}) async* {
try {
if (zCoins.isEmpty) return;

while (zCoins.isNotEmpty) {
final currentCoinTicker = zCoins.first;
await for (final update
in api.activateCoin(currentCoinTicker, firstLaunch: firstLaunch)) {
in api.activateCoin(currentCoinTicker, resync: resyncOnly)) {
Log(
'ZCoinActivationRepository:activateZCoins',
'Update received: ${update.toJson()}',
Expand Down Expand Up @@ -94,8 +98,6 @@ class ZCoinActivationRepository with RequestedZCoinsStorage {
Future<List<String>> outstandingZCoinActivations() async {
final requestedCoins = (await getRequestedActivatedCoins()).toSet();

if (willInitialize) return requestedCoins.toList();

final activatedZCoins = (await getEnabledZCoins()).toSet();

final coinsAlreadyActivated = activatedZCoins.intersection(requestedCoins);
Expand Down
6 changes: 4 additions & 2 deletions lib/packages/z_coin_activation/models/z_coin_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ class ZCoinStatus {
final String message;
final double progress;

bool get isActivated => status == ActivationTaskStatus.active;
bool get isActivated =>
status == ActivationTaskStatus.active ||
message.contains('is activated already');

bool get isFailed => status == ActivationTaskStatus.failed;
bool get isFailed => status == ActivationTaskStatus.failed && !isActivated;

bool get isInProgress => status == ActivationTaskStatus.inProgress;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Future<void> _showInProgressDialog(BuildContext context) async {
final localisations = AppLocalizations.of(context);

final etaString = state?.eta?.inMinutes == null
? localisations.loading
? null
: '${state.eta.inMinutes}${localisations.minutes}';
return AlertDialog(
title: Text(
Expand All @@ -462,12 +462,14 @@ Future<void> _showInProgressDialog(BuildContext context) async {
],
Text(localisations.willTakeTime),
SizedBox(height: 16),
Text('${localisations.rewardsTableTime}: $etaString'),
SizedBox(height: 16),
if (etaString != null) ...[
Text('${localisations.rewardsTableTime}: $etaString'),
SizedBox(height: 16),
],
Text(
'${localisations.swapProgress}: ${(state.progress * 100).round()}%',
),
SizedBox(height: 4),
SizedBox(height: 8),
LinearProgressIndicator(value: state.progress),
],
),
Expand Down
1 change: 0 additions & 1 deletion lib/services/mm.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' show Response;
Expand Down

0 comments on commit 8df0d44

Please sign in to comment.