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; return _currentChainTip ?? await electrumClient.getCurrentBlockChainTip() ?? 0;
} }
Future<int> getUpdatedChainTip() async {
_currentChainTip = await electrumClient.getCurrentBlockChainTip();
return _currentChainTip ?? 0;
}
@override @override
BitcoinWalletKeys get keys => BitcoinWalletKeys get keys =>
BitcoinWalletKeys(wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!); BitcoinWalletKeys(wif: hd.wif!, privateKey: hd.privKey!, publicKey: hd.pubKey!);
@ -262,10 +267,10 @@ abstract class ElectrumWalletBase
@action @action
Future<void> _setListeners(int height, {int? chainTipParam, bool? doSingleScan}) async { Future<void> _setListeners(int height, {int? chainTipParam, bool? doSingleScan}) async {
final chainTip = chainTipParam ?? await getCurrentChainTip(); final chainTip = chainTipParam ?? await getUpdatedChainTip();
if (chainTip == height) { if (chainTip == height) {
syncStatus = NotConnectedSyncStatus(); syncStatus = SyncedSyncStatus();
return; return;
} }
@ -1744,6 +1749,11 @@ abstract class ElectrumWalletBase
Future<void> _setInitialHeight() async { Future<void> _setInitialHeight() async {
if (_chainTipUpdateSubject != null) return; if (_chainTipUpdateSubject != null) return;
if ((_currentChainTip == null || _currentChainTip! == 0) && walletInfo.restoreHeight == 0) {
await getUpdatedChainTip();
await walletInfo.updateRestoreHeight(_currentChainTip!);
}
_chainTipUpdateSubject = electrumClient.chainTipSubscribe(); _chainTipUpdateSubject = electrumClient.chainTipSubscribe();
_chainTipUpdateSubject?.listen((e) async { _chainTipUpdateSubject?.listen((e) async {
final event = e as Map<String, dynamic>; final event = e as Map<String, dynamic>;
@ -1751,10 +1761,6 @@ abstract class ElectrumWalletBase
_currentChainTip = height; _currentChainTip = height;
if (_currentChainTip != null && _currentChainTip! > 0 && walletInfo.restoreHeight == 0) {
await walletInfo.updateRestoreHeight(_currentChainTip!);
}
if (alwaysScan == true && syncStatus is SyncedSyncStatus) { if (alwaysScan == true && syncStatus is SyncedSyncStatus) {
_setListeners(walletInfo.restoreHeight); _setListeners(walletInfo.restoreHeight);
} }
@ -1980,10 +1986,16 @@ Future<void> startRefresh(ScanData scanData) async {
); );
if (tweakHeight >= scanData.chainTip || scanData.isSingleScan) { if (tweakHeight >= scanData.chainTip || scanData.isSingleScan) {
scanData.sendPort.send(SyncResponse( if (tweakHeight >= scanData.chainTip)
syncHeight, scanData.sendPort.send(SyncResponse(
SyncedTipSyncStatus(scanData.chainTip), syncHeight,
)); SyncedTipSyncStatus(scanData.chainTip),
));
if (scanData.isSingleScan) {
scanData.sendPort.send(SyncResponse(syncHeight, SyncedSyncStatus()));
}
await tweaksSubscription!.close(); await tweaksSubscription!.close();
await electrumClient.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')}'; String dateKey = '${date.year}-${date.month.toString().padLeft(2, '0')}';
final closestKey = bitcoinDates.keys final closestKey = bitcoinDates.keys
.firstWhere((key) => formatMapKey(key).isBefore(date), orElse: () => bitcoinDates.keys.last); .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); final startOfMonth = DateTime(date.year, date.month);
int daysDifference = date.difference(startOfMonth).inDays; final daysDifference = date.difference(startOfMonth).inDays;
// approximately 6 blocks per hour, 24 hours per day // approximately 6 blocks per hour, 24 hours per day
int estimatedBlocksSinceStartOfMonth = (daysDifference * 24 * 6); int estimatedBlocksSinceStartOfMonth = (daysDifference * 24 * 6);
return startBlock + estimatedBlocksSinceStartOfMonth; return beginningBlock + estimatedBlocksSinceStartOfMonth;
} }
DateTime getDateByBitcoinHeight(int height) { DateTime getDateByBitcoinHeight(int height) {
final date = bitcoinDates.entries final closestEntry = bitcoinDates.entries
.lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.last); .lastWhere((entry) => entry.value >= height, orElse: () => bitcoinDates.entries.first);
return formatMapKey(date.key); 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 = [ static List<SettingActions> all = [
silentPaymentsSettingAction,
connectionSettingAction, connectionSettingAction,
walletSettingAction, walletSettingAction,
addressBookSettingAction, addressBookSettingAction,
silentPaymentsSettingAction,
securityBackupSettingAction, securityBackupSettingAction,
privacySettingAction, privacySettingAction,
displaySettingAction, displaySettingAction,