Skip to content

Commit

Permalink
Check for local time discrepancy when entering the P2P swap tab (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
vilkris4 authored Jun 2, 2023
1 parent cbf878f commit db29982
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
29 changes: 29 additions & 0 deletions lib/utils/node_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:zenon_syrius_wallet_flutter/embedded_node/embedded_node.dart';
import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/model/model.dart';
import 'package:zenon_syrius_wallet_flutter/utils/constants.dart';
import 'package:zenon_syrius_wallet_flutter/utils/date_time_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:zenon_syrius_wallet_flutter/utils/notification_utils.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';
Expand Down Expand Up @@ -131,6 +132,34 @@ class NodeUtils {
}
}

static Future<void> checkForLocalTimeDiscrepancy(
String warningMessage) async {
const maxAllowedDiscrepancy = Duration(minutes: 5);
try {
final syncInfo = await zenon!.stats.syncInfo();
bool nodeIsSynced = kCurrentNode == kLocalhostDefaultNodeUrl
? (syncInfo.state == SyncState.syncDone ||
(syncInfo.targetHeight > 0 &&
syncInfo.currentHeight > 0 &&
(syncInfo.targetHeight - syncInfo.currentHeight) < 20))
: true;
if (nodeIsSynced) {
final frontierTime =
(await zenon!.ledger.getFrontierMomentum()).timestamp;
final timeDifference = (frontierTime - DateTimeUtils.unixTimeNow).abs();
if (timeDifference > maxAllowedDiscrepancy.inSeconds) {
NotificationUtils.sendNotificationError(
Exception('Local time discrepancy detected.'),
warningMessage,
);
}
}
} catch (e, stackTrace) {
Logger('NodeUtils')
.log(Level.WARNING, 'checkForLocalTimeDiscrepancy', e, stackTrace);
}
}

static void _initListenForUnreceivedAccountBlocks(Stream broadcaster) {
broadcaster.listen(
(event) {
Expand Down
17 changes: 15 additions & 2 deletions lib/widgets/tab_children_widgets/p2p_swap_tab_child.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ import 'package:zenon_syrius_wallet_flutter/widgets/modular_widgets/p2p_swap_wid
import 'package:zenon_syrius_wallet_flutter/widgets/modular_widgets/p2p_swap_widgets/p2p_swap_options_card.dart';
import 'package:zenon_syrius_wallet_flutter/widgets/widgets.dart';

class P2pSwapTabChild extends StatelessWidget {
class P2pSwapTabChild extends StatefulWidget {
final VoidCallback onStepperNotificationSeeMorePressed;

const P2pSwapTabChild({
required this.onStepperNotificationSeeMorePressed,
Key? key,
}) : super(key: key);

@override
State createState() => P2pSwapTabChildState();
}

class P2pSwapTabChildState extends State<P2pSwapTabChild> {
@override
void initState() {
super.initState();
NodeUtils.checkForLocalTimeDiscrepancy(
'''Local time discrepancy detected. Please confirm your operating '''
'''system's time is correct before conducting P2P swaps.''');
}

@override
Widget build(BuildContext context) {
return _getLayout(context);
Expand Down Expand Up @@ -45,7 +58,7 @@ class P2pSwapTabChild extends StatelessWidget {
child: Consumer<SelectedAddressNotifier>(
builder: (_, __, ___) => P2pSwapsCard(
onStepperNotificationSeeMorePressed:
onStepperNotificationSeeMorePressed,
widget.onStepperNotificationSeeMorePressed,
),
),
),
Expand Down

0 comments on commit db29982

Please sign in to comment.