Merge branch 'subscribe_adapter' into testing

This commit is contained in:
sneurlax 2024-02-15 18:08:31 -06:00
commit 0bb564bbc2
3 changed files with 920 additions and 911 deletions

View file

@ -256,7 +256,7 @@ class ElectrumXClient {
}
}
Future<void> _checkElectrumAdapter() async {
Future<void> checkElectrumAdapter() async {
({InternetAddress host, int port})? proxyInfo;
// If we're supposed to use Tor...
@ -370,9 +370,9 @@ class ElectrumXClient {
if (_requireMutex) {
await _torConnectingLock
.protect(() async => await _checkElectrumAdapter());
.protect(() async => await checkElectrumAdapter());
} else {
await _checkElectrumAdapter();
await checkElectrumAdapter();
}
try {
@ -450,9 +450,9 @@ class ElectrumXClient {
if (_requireMutex) {
await _torConnectingLock
.protect(() async => await _checkElectrumAdapter());
.protect(() async => await checkElectrumAdapter());
} else {
await _checkElectrumAdapter();
await checkElectrumAdapter();
}
try {
@ -814,7 +814,7 @@ class ElectrumXClient {
}) async {
Logging.instance.log("attempting to fetch blockchain.transaction.get...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
dynamic response = await _electrumAdapterClient!.getTransaction(txHash);
Logging.instance.log("Fetching blockchain.transaction.get finished",
level: LogLevel.Info);
@ -847,7 +847,7 @@ class ElectrumXClient {
}) async {
Logging.instance.log("attempting to fetch lelantus.getanonymityset...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
Map<String, dynamic> response =
await (_electrumAdapterClient as FiroElectrumClient)!
.getLelantusAnonymitySet(groupId: groupId, blockHash: blockhash);
@ -866,7 +866,7 @@ class ElectrumXClient {
}) async {
Logging.instance.log("attempting to fetch lelantus.getmintmetadata...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
dynamic response = await (_electrumAdapterClient as FiroElectrumClient)!
.getLelantusMintData(mints: mints);
Logging.instance.log("Fetching lelantus.getmintmetadata finished",
@ -882,7 +882,7 @@ class ElectrumXClient {
}) async {
Logging.instance.log("attempting to fetch lelantus.getusedcoinserials...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
int retryCount = 3;
dynamic response;
@ -906,7 +906,7 @@ class ElectrumXClient {
Future<int> getLelantusLatestCoinId({String? requestID}) async {
Logging.instance.log("attempting to fetch lelantus.getlatestcoinid...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
int response =
await (_electrumAdapterClient as FiroElectrumClient).getLatestCoinId();
Logging.instance.log("Fetching lelantus.getlatestcoinid finished",
@ -937,7 +937,7 @@ class ElectrumXClient {
try {
Logging.instance.log("attempting to fetch spark.getsparkanonymityset...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
Map<String, dynamic> response =
await (_electrumAdapterClient as FiroElectrumClient)
.getSparkAnonymitySet(
@ -960,7 +960,7 @@ class ElectrumXClient {
// Use electrum_adapter package's getSparkUsedCoinsTags method.
Logging.instance.log("attempting to fetch spark.getusedcoinstags...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
Map<String, dynamic> response =
await (_electrumAdapterClient as FiroElectrumClient)
.getUsedCoinsTags(startNumber: startNumber);
@ -993,7 +993,7 @@ class ElectrumXClient {
try {
Logging.instance.log("attempting to fetch spark.getsparkmintmetadata...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
List<dynamic> response =
await (_electrumAdapterClient as FiroElectrumClient)
.getSparkMintMetaData(sparkCoinHashes: sparkCoinHashes);
@ -1015,7 +1015,7 @@ class ElectrumXClient {
try {
Logging.instance.log("attempting to fetch spark.getsparklatestcoinid...",
level: LogLevel.Info);
await _checkElectrumAdapter();
await checkElectrumAdapter();
int response = await (_electrumAdapterClient as FiroElectrumClient)
.getSparkLatestCoinId();
Logging.instance.log("Fetching spark.getsparklatestcoinid finished",
@ -1037,7 +1037,7 @@ class ElectrumXClient {
/// "rate": 1000,
/// }
Future<Map<String, dynamic>> getFeeRate({String? requestID}) async {
await _checkElectrumAdapter();
await checkElectrumAdapter();
return await _electrumAdapterClient!.getFeeRate();
}

File diff suppressed because it is too large Load diff

View file

@ -11,7 +11,6 @@ import 'package:mutex/mutex.dart';
import 'package:stackwallet/electrumx_rpc/cached_electrumx_client.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_chain_height_service.dart';
import 'package:stackwallet/electrumx_rpc/electrumx_client.dart';
import 'package:stackwallet/electrumx_rpc/subscribable_electrumx_client.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/v2/input_v2.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/v2/output_v2.dart';
import 'package:stackwallet/models/isar/models/blockchain_data/v2/transaction_v2.dart';
@ -34,17 +33,23 @@ import 'package:stackwallet/wallets/wallet/intermediate/bip39_hd_wallet.dart';
import 'package:stackwallet/wallets/wallet/wallet_mixin_interfaces/paynym_interface.dart';
import 'package:stream_channel/stream_channel.dart';
import '../../../services/event_bus/events/global/tor_connection_status_changed_event.dart';
import '../../../services/event_bus/events/global/tor_status_changed_event.dart';
mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
late ElectrumXClient electrumXClient;
late StreamChannel electrumAdapterChannel;
late ElectrumClient electrumAdapterClient;
late CachedElectrumXClient electrumXCachedClient;
late SubscribableElectrumXClient subscribableElectrumXClient;
// late SubscribableElectrumXClient subscribableElectrumXClient;
int? get maximumFeerate => null;
int? _latestHeight;
StreamSubscription<TorPreferenceChangedEvent>? _torPreferenceListener;
StreamSubscription<TorConnectionStatusChangedEvent>? _torStatusListener;
static const _kServerBatchCutoffVersion = [1, 6];
List<int>? _serverVersion;
bool get serverCanBatch {
@ -811,6 +816,9 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
Future<int> fetchChainHeight() async {
try {
// _checkChainHeightSubscription();
// TODO above. Make sure that the subscription/stream is alive.
// Don't set a stream subscription if one already exists.
await _manageChainHeightSubscription();
@ -862,36 +870,36 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
}
}
Future<void> _createSubscription() async {
final completer = Completer<int>();
ElectrumxChainHeightService.completers[cryptoCurrency.coin] = completer;
await electrumXClient.checkElectrumAdapter();
// TODO [prio=extreme]: Does this update anything in this file?? Thinking no.
// Make sure we only complete once.
final isFirstResponse = _latestHeight == null;
final stream = electrumAdapterClient.subscribeHeaders();
// Subscribe to block headers.
final subscription = subscribableElectrumXClient.subscribeToBlockHeaders();
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] =
stream.asBroadcastStream().listen((response) {
final int chainHeight = response.height;
// print("Current chain height: $chainHeight");
// Set stream subscription.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin] =
subscription.responseStream.asBroadcastStream().listen((event) {
final response = event;
if (response != null &&
response is Map &&
response.containsKey('height')) {
final int chainHeight = response['height'] as int;
// print("Current chain height: $chainHeight");
_latestHeight = chainHeight;
_latestHeight = chainHeight;
if (isFirstResponse) {
// If the completer is not completed, complete it.
if (!ElectrumxChainHeightService
.completers[cryptoCurrency.coin]!.isCompleted) {
// Complete the completer, returning the chain height.
ElectrumxChainHeightService.completers[cryptoCurrency.coin]!
.complete(chainHeight);
if (isFirstResponse && !completer.isCompleted) {
// Return the chain height.
completer.complete(chainHeight);
}
});
} else {
// Don't set a stream subscription if one already exists.
// Check if the stream subscription is paused.
if (ElectrumxChainHeightService
.subscriptions[cryptoCurrency.coin]!.isPaused) {
// If it's paused, resume it.
ElectrumxChainHeightService.subscriptions[cryptoCurrency.coin]!
.resume();
}
if (_latestHeight != null) {
return _latestHeight!;
}
} else {
Logging.instance.log(
@ -998,13 +1006,14 @@ mixin ElectrumXInterface<T extends Bip39HDCurrency> on Bip39HDWallet<T> {
electrumAdapterClient: electrumAdapterClient,
electrumAdapterUpdateCallback: updateClient,
);
subscribableElectrumXClient = SubscribableElectrumXClient.from(
node: newNode,
prefs: prefs,
failovers: failovers,
);
await subscribableElectrumXClient.connect(
host: newNode.address, port: newNode.port);
// Replaced using electrum_adapters' SubscribableClient in fetchChainHeight.
// subscribableElectrumXClient = SubscribableElectrumXClient.from(
// node: newNode,
// prefs: prefs,
// failovers: failovers,
// );
// await subscribableElectrumXClient.connect(
// host: newNode.address, port: newNode.port);
}
//============================================================================