Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore connection on rejected chain identifier mismatch #82

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading