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

Defer network calls from init to fetchChainHeight or serverCanBatch #978

Merged
merged 2 commits into from
Sep 6, 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
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ mixin ElectrumXInterface<T extends ElectrumXCurrencyInterface>

Future<int> fetchChainHeight({int retries = 1}) async {
try {
// Ensure server version is initialized and genesis hash is checked.
if (_serverVersion == null) {
await _initializeServerVersionAndCheckGenesisHash();
}

return await ClientManager.sharedInstance.getChainHeightFor(
cryptoCurrency,
);
Expand Down Expand Up @@ -1803,6 +1808,21 @@ mixin ElectrumXInterface<T extends ElectrumXCurrencyInterface>

@override
Future<void> init() async {
try {
// Server features and genesis hash check deferred.
// See _initializeServerVersionAndCheckGenesisHash.

await super.init();
} catch (e, s) {
// do nothing, still allow user into wallet
Logging.instance.log(
"$runtimeType init() did not complete: $e\n$s",
level: LogLevel.Warning,
);
}
}

Future<void> _initializeServerVersionAndCheckGenesisHash() async {
try {
final features = await electrumXClient
.getServerFeatures()
Expand All @@ -1814,17 +1834,14 @@ mixin ElectrumXInterface<T extends ElectrumXCurrencyInterface>
_parseServerVersion(features["server_version"] as String);

if (cryptoCurrency.genesisHash != features['genesis_hash']) {
throw Exception("genesis hash does not match!");
throw Exception("Genesis hash does not match!");
}
} catch (e, s) {
// do nothing, still allow user into wallet
Logging.instance.log(
"$runtimeType init() did not complete: $e\n$s",
"$runtimeType _initializeServerVersionAndCheckGenesisHash() did not complete: $e\n$s",
level: LogLevel.Warning,
);
}

await super.init();
}

// ===========================================================================
Expand Down
Loading