Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crypto_plugins/flutter_libmwc
Submodule flutter_libmwc updated 1 files
+13 −6 rust/src/lib.rs
188 changes: 97 additions & 91 deletions lib/electrumx_rpc/electrumx_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ class ElectrumXClient {
final Mutex _torConnectingLock = Mutex();
bool _requireMutex = false;

final _adapterMutex = Mutex();

ElectrumXClient({
required String host,
required int port,
Expand Down Expand Up @@ -219,111 +221,115 @@ class ElectrumXClient {
}

Future<void> checkElectrumAdapter() async {
({InternetAddress host, int port})? proxyInfo;

if (AppConfig.hasFeature(AppFeature.tor)) {
// If we're supposed to use Tor...
if (_prefs.useTor) {
// But Tor isn't running...
if (_torService.status != TorConnectionStatus.connected) {
// And the killswitch isn't set...
if (!_prefs.torKillSwitch) {
// Then we'll just proceed and connect to ElectrumX through
// clearnet at the bottom of this function.
Logging.instance.w(
"Tor preference set but Tor is not enabled, killswitch not set,"
" connecting to Electrum adapter through clearnet",
);
await _adapterMutex.protect(() async {
({InternetAddress host, int port})? proxyInfo;

if (AppConfig.hasFeature(AppFeature.tor)) {
// If we're supposed to use Tor...
if (_prefs.useTor) {
// But Tor isn't running...
if (_torService.status != TorConnectionStatus.connected) {
// And the killswitch isn't set...
if (!_prefs.torKillSwitch) {
// Then we'll just proceed and connect to ElectrumX through
// clearnet at the bottom of this function.
Logging.instance.w(
"Tor preference set but Tor is not enabled, killswitch not set,"
" connecting to Electrum adapter through clearnet",
);
} else {
// ... But if the killswitch is set, then we throw an exception.
throw Exception(
"Tor preference and killswitch set but Tor is not enabled, "
"not connecting to Electrum adapter",
);
// TODO [prio=low]: Try to start Tor.
}
} else {
// ... But if the killswitch is set, then we throw an exception.
throw Exception(
"Tor preference and killswitch set but Tor is not enabled, "
"not connecting to Electrum adapter",
// Get the proxy info from the TorService.
proxyInfo = _torService.getProxyInfo();
}

if (netType == TorPlainNetworkOption.clear) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
// TODO [prio=low]: Try to start Tor.
}
} else {
// Get the proxy info from the TorService.
proxyInfo = _torService.getProxyInfo();
}

if (netType == TorPlainNetworkOption.clear) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
}
} else {
if (netType == TorPlainNetworkOption.tor) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
if (netType == TorPlainNetworkOption.tor) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
}
}
}
}
// If the current ElectrumAdapterClient is closed, create a new one.
if (getElectrumAdapter() != null && getElectrumAdapter()!.peer.isClosed) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(cryptoCurrency: cryptoCurrency);
}

final String useHost;
final int usePort;
final bool useUseSSL;

if (currentFailoverIndex == -1) {
useHost = host;
usePort = port;
useUseSSL = useSSL;
} else {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(cryptoCurrency: cryptoCurrency);
useHost = _failovers[currentFailoverIndex].address;
usePort = _failovers[currentFailoverIndex].port;
useUseSSL = _failovers[currentFailoverIndex].useSSL;
}
// If the current ElectrumAdapterClient is closed, create a new one.
if (getElectrumAdapter() != null && getElectrumAdapter()!.peer.isClosed) {
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
}

_electrumAdapterChannel ??= await electrum_adapter.connect(
useHost,
port: usePort,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
aliveTimerDuration: connectionTimeoutForSpecialCaseJsonRPCClients,
acceptUnverified: false,
useSSL: useUseSSL,
proxyInfo: proxyInfo,
);
final String useHost;
final int usePort;
final bool useUseSSL;

if (getElectrumAdapter() == null) {
final ElectrumClient newClient;
if (cryptoCurrency is Firo) {
newClient = FiroElectrumClient(
_electrumAdapterChannel!,
useHost,
usePort,
useUseSSL,
proxyInfo,
);
if (currentFailoverIndex == -1) {
useHost = host;
usePort = port;
useUseSSL = useSSL;
} else {
newClient = ElectrumClient(
_electrumAdapterChannel!,
useHost,
usePort,
useUseSSL,
proxyInfo,
_electrumAdapterChannel = null;
await ClientManager.sharedInstance.remove(
cryptoCurrency: cryptoCurrency,
);
useHost = _failovers[currentFailoverIndex].address;
usePort = _failovers[currentFailoverIndex].port;
useUseSSL = _failovers[currentFailoverIndex].useSSL;
}

await newClient.request('server.version');

await ClientManager.sharedInstance.addClient(
newClient,
cryptoCurrency: cryptoCurrency,
netType: netType,
_electrumAdapterChannel ??= await electrum_adapter.connect(
useHost,
port: usePort,
connectionTimeout: connectionTimeoutForSpecialCaseJsonRPCClients,
aliveTimerDuration: connectionTimeoutForSpecialCaseJsonRPCClients,
acceptUnverified: false,
useSSL: useUseSSL,
proxyInfo: proxyInfo,
);
}

return;
if (getElectrumAdapter() == null) {
final ElectrumClient newClient;
if (cryptoCurrency is Firo) {
newClient = FiroElectrumClient(
_electrumAdapterChannel!,
useHost,
usePort,
useUseSSL,
proxyInfo,
);
} else {
newClient = ElectrumClient(
_electrumAdapterChannel!,
useHost,
usePort,
useUseSSL,
proxyInfo,
);
}

await newClient.request('server.version');

await ClientManager.sharedInstance.addClient(
newClient,
cryptoCurrency: cryptoCurrency,
netType: netType,
);
}
});
}

/// Send raw rpc command
Expand Down
Loading
Loading