Skip to content

Commit

Permalink
Restore connection on rejected chain identifier mismatch (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin authored Mar 13, 2024
1 parent febe6da commit b7b28d2
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions lib/widgets/modular_widgets/settings_widgets/node_management.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,42 +127,25 @@ class _NodeManagementState extends State<NodeManagement> {

try {
_confirmNodeButtonKey.currentState?.animateForward();
String url = _selectedNode == kEmbeddedNode
? kLocalhostDefaultNodeUrl
: _selectedNode!;
bool isConnectionEstablished =
await NodeUtils.establishConnectionToNode(url);
if (_selectedNode == kEmbeddedNode) {
// Check if node is already running
if (!isConnectionEstablished) {
// Initialize local full node
await Isolate.spawn(EmbeddedNode.runNode, [''],
onExit: sl<ReceivePort>(instanceName: 'embeddedStoppedPort')
.sendPort);
kEmbeddedNodeRunning = true;
// The node needs a couple of seconds to actually start
await Future.delayed(kEmbeddedConnectionDelay);
isConnectionEstablished =
await NodeUtils.establishConnectionToNode(url);
}
} else {
isConnectionEstablished =
await NodeUtils.establishConnectionToNode(url);
if (isConnectionEstablished) {
await NodeUtils.closeEmbeddedNode();
}
}
var isConnectionEstablished =
await _establishConnectionToNode(_selectedNode);
if (isConnectionEstablished) {
kNodeChainId = await NodeUtils.getNodeChainIdentifier();
await htlcSwapsService!.storeLastCheckedHtlcBlockHeight(0);
if (await _checkForChainIdMismatch()) {
await htlcSwapsService!.storeLastCheckedHtlcBlockHeight(0);
await sharedPrefsService!.put(
kSelectedNodeKey,
_selectedNode,
);
kCurrentNode = _selectedNode!;
_sendChangingNodeSuccessNotification();
widget.onNodeChangedCallback();
} else {
await _establishConnectionToNode(kCurrentNode);
kNodeChainId = await NodeUtils.getNodeChainIdentifier();
setState(() {
_selectedNode = kCurrentNode!;
});
}
} else {
throw 'Connection could not be established to $_selectedNode';
Expand All @@ -180,6 +163,33 @@ class _NodeManagementState extends State<NodeManagement> {
}
}

Future<bool> _establishConnectionToNode(String? url) async {
String targetUrl = url == kEmbeddedNode ? kLocalhostDefaultNodeUrl : url!;
bool isConnectionEstablished =
await NodeUtils.establishConnectionToNode(targetUrl);
if (url == kEmbeddedNode) {
// Check if node is already running
if (!isConnectionEstablished) {
// Initialize local full node
await Isolate.spawn(EmbeddedNode.runNode, [''],
onExit:
sl<ReceivePort>(instanceName: 'embeddedStoppedPort').sendPort);
kEmbeddedNodeRunning = true;
// The node needs a couple of seconds to actually start
await Future.delayed(kEmbeddedConnectionDelay);
isConnectionEstablished =
await NodeUtils.establishConnectionToNode(targetUrl);
}
} else {
isConnectionEstablished =
await NodeUtils.establishConnectionToNode(targetUrl);
if (isConnectionEstablished) {
await NodeUtils.closeEmbeddedNode();
}
}
return isConnectionEstablished;
}

Widget _getAddNodeExpandableChild() {
return Column(
children: [
Expand Down

0 comments on commit b7b28d2

Please sign in to comment.