fix: date from height logic, status disconnected & chain tip get

This commit is contained in:
Rafael Saes 2024-05-14 14:27:47 -03:00
parent 3c041eed5b
commit e1acef2b92
3 changed files with 42 additions and 18 deletions

View file

@ -234,6 +234,11 @@ abstract class ElectrumWalletBase
return _currentChainTip ?? await electrumClient.getCurrentBlockChainTip() ?? 0;
}
Future<int> getUpdatedChainTip() async {
_currentChainTip = await electrumClient.getCurrentBlockChainTip();
return _currentChainTip ?? 0;
}
@override
BitcoinWalletKeys get keys =>
BitcoinWalletKeys(wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!);
@ -262,10 +267,10 @@ abstract class ElectrumWalletBase
@action
Future<void> _setListeners(int height, {int? chainTipParam, bool? doSingleScan}) async {
final chainTip = chainTipParam ?? await getCurrentChainTip();
final chainTip = chainTipParam ?? await getUpdatedChainTip();
if (chainTip == height) {
syncStatus = NotConnectedSyncStatus();
syncStatus = SyncedSyncStatus();
return;
}
@ -1744,6 +1749,11 @@ abstract class ElectrumWalletBase
Future<void> _setInitialHeight() async {
if (_chainTipUpdateSubject != null) return;
if ((_currentChainTip == null || _currentChainTip! == 0) && walletInfo.restoreHeight == 0) {
await getUpdatedChainTip();
await walletInfo.updateRestoreHeight(_currentChainTip!);
}
_chainTipUpdateSubject = electrumClient.chainTipSubscribe();
_chainTipUpdateSubject?.listen((e) async {
final event = e as Map<String, dynamic>;
@ -1751,10 +1761,6 @@ abstract class ElectrumWalletBase
_currentChainTip = height;
if (_currentChainTip != null && _currentChainTip! > 0 && walletInfo.restoreHeight == 0) {
await walletInfo.updateRestoreHeight(_currentChainTip!);
}
if (alwaysScan == true && syncStatus is SyncedSyncStatus) {
_setListeners(walletInfo.restoreHeight);
}
@ -1980,10 +1986,16 @@ Future<void> startRefresh(ScanData scanData) async {
);
if (tweakHeight >= scanData.chainTip || scanData.isSingleScan) {
scanData.sendPort.send(SyncResponse(
syncHeight,
SyncedTipSyncStatus(scanData.chainTip),
));
if (tweakHeight >= scanData.chainTip)
scanData.sendPort.send(SyncResponse(
syncHeight,
SyncedTipSyncStatus(scanData.chainTip),
));
if (scanData.isSingleScan) {
scanData.sendPort.send(SyncResponse(syncHeight, SyncedSyncStatus()));
}
await tweaksSubscription!.close();
await electrumClient.close();
}

View file

@ -268,19 +268,31 @@ int getBitcoinHeightByDate({required DateTime date}) {
String dateKey = '${date.year}-${date.month.toString().padLeft(2, '0')}';
final closestKey = bitcoinDates.keys
.firstWhere((key) => formatMapKey(key).isBefore(date), orElse: () => bitcoinDates.keys.last);
int startBlock = bitcoinDates[dateKey] ?? bitcoinDates[closestKey]!;
final beginningBlock = bitcoinDates[dateKey] ?? bitcoinDates[closestKey]!;
DateTime startOfMonth = DateTime(date.year, date.month);
int daysDifference = date.difference(startOfMonth).inDays;
final startOfMonth = DateTime(date.year, date.month);
final daysDifference = date.difference(startOfMonth).inDays;
// approximately 6 blocks per hour, 24 hours per day
int estimatedBlocksSinceStartOfMonth = (daysDifference * 24 * 6);
return startBlock + estimatedBlocksSinceStartOfMonth;
return beginningBlock + estimatedBlocksSinceStartOfMonth;
}
DateTime getDateByBitcoinHeight(int height) {
final date = bitcoinDates.entries
.lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.last);
return formatMapKey(date.key);
final closestEntry = bitcoinDates.entries
.lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.first);
final beginningBlock = closestEntry.value;
final startOfMonth = formatMapKey(closestEntry.key);
final blocksDifference = height - beginningBlock;
final hoursDifference = blocksDifference / 5.5;
final estimatedDate = startOfMonth.add(Duration(hours: hoursDifference.ceil()));
if (estimatedDate.isAfter(DateTime.now())) {
return DateTime.now();
}
return estimatedDate;
}

View file

@ -14,10 +14,10 @@ class SettingActions {
});
static List<SettingActions> all = [
silentPaymentsSettingAction,
connectionSettingAction,
walletSettingAction,
addressBookSettingAction,
silentPaymentsSettingAction,
securityBackupSettingAction,
privacySettingAction,
displaySettingAction,