fix(worker): server capability for tx verbose

This commit is contained in:
Rafael Saes 2025-01-23 09:55:57 -03:00
parent 47386d2e6a
commit 3cc7fba130

View file

@ -345,18 +345,21 @@ class ElectrumWorker {
} }
if (transactionIdsForHeights.isNotEmpty) { if (transactionIdsForHeights.isNotEmpty) {
final transactionsVerbose = await _getBatchTransactionVerbose( Map<String, Map<String, dynamic>>? transactionsVerbose;
hashes: transactionIdsForHeights.keys.toList(), if (_serverCapability!.supportsTxVerbose) {
); transactionsVerbose = await _getBatchTransactionVerbose(
hashes: transactionIdsForHeights.keys.toList(),
);
}
Map<String, String> transactionHexes = {}; Map<String, String> transactionHexes = {};
if (transactionsVerbose.isEmpty) { if (transactionsVerbose?.isEmpty ?? true) {
transactionHexes = await _getBatchTransactionHex( transactionHexes = await _getBatchTransactionHex(
hashes: transactionIdsForHeights.keys.toList(), hashes: transactionIdsForHeights.keys.toList(),
); );
} else { } else {
transactionsVerbose.values.forEach((e) { transactionsVerbose!.values.forEach((e) {
transactionHexes[e['txid'] as String] = e['hex'] as String; transactionHexes[e['txid'] as String] = e['hex'] as String;
}); });
} }
@ -365,7 +368,7 @@ class ElectrumWorker {
final hash = transactionIdHeight.key; final hash = transactionIdHeight.key;
final hex = transactionIdHeight.value; final hex = transactionIdHeight.value;
final transactionVerbose = transactionsVerbose[hash]; final transactionVerbose = transactionsVerbose?[hash];
late ElectrumTransactionBundle txBundle; late ElectrumTransactionBundle txBundle;
@ -473,37 +476,40 @@ class ElectrumWorker {
if (transactionIdsForHeights.isNotEmpty) { if (transactionIdsForHeights.isNotEmpty) {
await Future.wait(transactionIdsForHeights.keys.toList().map((hash) async { await Future.wait(transactionIdsForHeights.keys.toList().map((hash) async {
final transactionVerbose = await _electrumClient!.request( late String txHex;
ElectrumRequestGetTransactionVerbose(transactionHash: hash), Map<String, dynamic>? txVerbose;
);
late String transactionHex; if (_serverCapability!.supportsTxVerbose) {
txVerbose = await _electrumClient!.request(
if (transactionVerbose.isEmpty) { ElectrumRequestGetTransactionVerbose(
transactionHex = await _electrumClient!.request(
ElectrumRequestGetTransactionHex(
transactionHash: hash, transactionHash: hash,
), ),
); );
}
if (txVerbose?.isEmpty ?? true) {
txHex = await _electrumClient!.request(
ElectrumRequestGetTransactionHex(transactionHash: hash),
);
} else { } else {
transactionHex = transactionVerbose['hex'] as String; txHex = txVerbose!['hex'] as String;
} }
late ElectrumTransactionBundle txBundle; late ElectrumTransactionBundle txBundle;
// this is the initial tx history update, so ins will be filled later one by one, // this is the initial tx history update, so ins will be filled later one by one,
// and time and confirmations will be updated if needed again // and time and confirmations will be updated if needed again
if (transactionVerbose.isNotEmpty) { if (txVerbose?.isNotEmpty ?? false) {
txBundle = ElectrumTransactionBundle( txBundle = ElectrumTransactionBundle(
BtcTransaction.fromRaw(transactionHex), BtcTransaction.fromRaw(txHex),
ins: [], ins: [],
time: transactionVerbose['time'] as int?, time: txVerbose!['time'] as int?,
confirmations: (transactionVerbose['confirmations'] as int?) ?? 1, confirmations: (txVerbose['confirmations'] as int?) ?? 1,
isDateValidated: (transactionVerbose['time'] as int?) != null, isDateValidated: (txVerbose['time'] as int?) != null,
); );
} else { } else {
txBundle = ElectrumTransactionBundle( txBundle = ElectrumTransactionBundle(
BtcTransaction.fromRaw(transactionHex), BtcTransaction.fromRaw(txHex),
ins: [], ins: [],
confirmations: 1, confirmations: 1,
); );
@ -629,18 +635,22 @@ class ElectrumWorker {
} }
if (transactionsByIds.isNotEmpty) { if (transactionsByIds.isNotEmpty) {
final transactionsVerbose = await _getBatchTransactionVerbose( Map<String, Map<String, dynamic>>? transactionsVerbose;
hashes: transactionsByIds.keys.toList(),
); if (_serverCapability!.supportsTxVerbose) {
transactionsVerbose = await _getBatchTransactionVerbose(
hashes: transactionsByIds.keys.toList(),
);
}
Map<String, String> transactionHexes = {}; Map<String, String> transactionHexes = {};
if (transactionsVerbose.isEmpty) { if (transactionsVerbose?.isEmpty ?? true) {
transactionHexes = await _getBatchTransactionHex( transactionHexes = await _getBatchTransactionHex(
hashes: transactionsByIds.keys.toList(), hashes: transactionsByIds.keys.toList(),
); );
} else { } else {
transactionsVerbose.values.forEach((e) { transactionsVerbose!.values.forEach((e) {
transactionHexes[e['txid'] as String] = e['hex'] as String; transactionHexes[e['txid'] as String] = e['hex'] as String;
}); });
} }
@ -649,7 +659,7 @@ class ElectrumWorker {
final hash = entry.key; final hash = entry.key;
final txToFetch = entry.value; final txToFetch = entry.value;
final storedTx = txToFetch.tx; final storedTx = txToFetch.tx;
final txVerbose = transactionsVerbose[hash]; final txVerbose = transactionsVerbose?[hash];
final txHex = transactionHexes[hash]!; final txHex = transactionHexes[hash]!;
final original = final original =
storedTx?.original ?? BtcTransaction.fromRaw((txVerbose?["hex"] as String?) ?? txHex); storedTx?.original ?? BtcTransaction.fromRaw((txVerbose?["hex"] as String?) ?? txHex);
@ -1088,16 +1098,20 @@ class ElectrumWorker {
} }
} else { } else {
await Future.wait(hashes.map((hash) async { await Future.wait(hashes.map((hash) async {
final history = await _electrumClient!.request( Map<String, dynamic>? txVerbose;
ElectrumRequestGetTransactionVerbose(transactionHash: hash),
);
txVerboseResults.add(history); if (_serverCapability!.supportsTxVerbose) {
txVerbose = await _electrumClient!.request(
ElectrumRequestGetTransactionVerbose(
transactionHash: hash,
),
);
}
if (history.isEmpty) { if (txVerbose?.isEmpty ?? true) {
emptyVerboseTxs.add(hash); emptyVerboseTxs.add(hash);
} else { } else {
transactionHexes.add(history['hex'] as String); transactionHexes.add(txVerbose!['hex'] as String);
} }
})); }));
} }
@ -1220,13 +1234,17 @@ class ElectrumWorker {
int? time; int? time;
DateResult? dates; DateResult? dates;
final transactionVerbose = await _electrumClient!.request( Map<String, dynamic>? transactionVerbose;
ElectrumRequestGetTransactionVerbose(transactionHash: hash), if (_serverCapability!.supportsTxVerbose) {
); transactionVerbose = await _electrumClient!.request(
ElectrumRequestGetTransactionVerbose(transactionHash: hash),
);
}
String transactionHex; String transactionHex;
if (transactionVerbose.isNotEmpty) { if (transactionVerbose?.isNotEmpty ?? false) {
transactionHex = transactionVerbose['hex'] as String; transactionHex = transactionVerbose!['hex'] as String;
time = transactionVerbose['time'] as int?; time = transactionVerbose['time'] as int?;
confirmations = transactionVerbose['confirmations'] as int?; confirmations = transactionVerbose['confirmations'] as int?;
} else { } else {