This commit is contained in:
M 2020-11-03 18:57:36 +02:00
parent 5524efb44d
commit 76581505a0
3 changed files with 45 additions and 70 deletions

View file

@ -373,6 +373,7 @@ extern "C"
bool connect_to_node(char *error) bool connect_to_node(char *error)
{ {
nice(19);
bool is_connected = get_current_wallet()->connectToDaemon(); bool is_connected = get_current_wallet()->connectToDaemon();
if (!is_connected) if (!is_connected)
@ -385,6 +386,7 @@ extern "C"
bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error) bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error)
{ {
nice(19);
Monero::Wallet *wallet = get_current_wallet(); Monero::Wallet *wallet = get_current_wallet();
std::string _login = ""; std::string _login = "";
@ -494,6 +496,8 @@ extern "C"
return committed; return committed;
} }
// START
uint64_t get_node_height_or_update(uint64_t base_eight) uint64_t get_node_height_or_update(uint64_t base_eight)
{ {
if (m_cached_syncing_blockchain_height < base_eight) { if (m_cached_syncing_blockchain_height < base_eight) {
@ -578,6 +582,8 @@ extern "C"
get_current_wallet()->setListener(m_listener); get_current_wallet()->setListener(m_listener);
} }
// END
int64_t *subaddrress_get_all() int64_t *subaddrress_get_all()
{ {
std::vector<Monero::SubaddressRow *> _subaddresses = m_subaddress->getAll(); std::vector<Monero::SubaddressRow *> _subaddresses = m_subaddress->getAll();

View file

@ -212,19 +212,16 @@ String getPublicSpendKey() =>
convertUTF8ToString(pointer: getPublicSpendKeyNative()); convertUTF8ToString(pointer: getPublicSpendKeyNative());
class SyncListener { class SyncListener {
SyncListener({this.onNewBlock, this.onNeedToRefresh, this.onNewTransaction}); SyncListener({this.onNewBlock});
void Function(int, int, double) onNewBlock; void Function(int, int, double) onNewBlock;
void Function() onNeedToRefresh;
void Function() onNewTransaction;
Timer _updateSyncInfoTimer; Timer _updateSyncInfoTimer;
int _cachedBlockchainHeight = 0; int _cachedBlockchainHeight = 0;
int _lastKnownBlockHeight = 0; int _lastKnownBlockHeight = 0;
int _initialSyncHeight = 0;
Future<int> getNodeHeightOrUpdate(int baseHeight) async { Future<int> getNodeHeightOrUpdate(int baseHeight) async {
if (_cachedBlockchainHeight < baseHeight) { if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) {
_cachedBlockchainHeight = await getNodeHeight(); _cachedBlockchainHeight = await getNodeHeight();
} }
@ -234,50 +231,36 @@ class SyncListener {
void start() { void start() {
_cachedBlockchainHeight = 0; _cachedBlockchainHeight = 0;
_lastKnownBlockHeight = 0; _lastKnownBlockHeight = 0;
_initialSyncHeight = 0;
_updateSyncInfoTimer ??= _updateSyncInfoTimer ??=
Timer.periodic(Duration(milliseconds: 1200), (_) async { Timer.periodic(Duration(milliseconds: 1200), (_) async {
final syncHeight = getSyncingHeight(); var syncHeight = getSyncingHeight();
final needToRefresh = isNeededToRefresh();
final newTransactionExist = isNewTransactionExist(); if (syncHeight <= 0) {
syncHeight = getCurrentHeight();
}
final bchHeight = await getNodeHeightOrUpdate(syncHeight); final bchHeight = await getNodeHeightOrUpdate(syncHeight);
if (_lastKnownBlockHeight != syncHeight && syncHeight != null) { if (_lastKnownBlockHeight == syncHeight || syncHeight == null) {
if (_initialSyncHeight <= 0) { return;
_initialSyncHeight = syncHeight;
}
_lastKnownBlockHeight = syncHeight;
final line = bchHeight - _initialSyncHeight;
final diff = line - (bchHeight - syncHeight);
final ptc = diff <= 0 ? 0.0 : diff / line;
final left = bchHeight - syncHeight;
// 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
onNewBlock?.call(syncHeight, left, ptc);
} }
if (newTransactionExist) { _lastKnownBlockHeight = syncHeight;
onNewTransaction?.call(); final track = bchHeight - syncHeight;
} final diff = track - (bchHeight - syncHeight);
final ptc = diff <= 0 ? 0.0 : diff / track;
if (needToRefresh) { final left = bchHeight - syncHeight;
onNeedToRefresh?.call(); // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
} onNewBlock?.call(syncHeight, left, ptc);
}); });
} }
void stop() => _updateSyncInfoTimer?.cancel(); void stop() => _updateSyncInfoTimer?.cancel();
} }
SyncListener setListeners(void Function(int, int, double) onNewBlock, SyncListener setListeners(void Function(int, int, double) onNewBlock) {
void Function() onNeedToRefresh, void Function() onNewTransaction) { final listener = SyncListener(onNewBlock: onNewBlock);
final listener = SyncListener(
onNewBlock: onNewBlock,
onNeedToRefresh: onNeedToRefresh,
onNewTransaction: onNewTransaction);
setListenerNative(); setListenerNative();
return listener; return listener;
} }

View file

@ -95,7 +95,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
address = subaddress.address; address = subaddress.address;
_setListeners(); _setListeners();
await transactionHistory.update(); await transactionHistory.update();
print('walletInfo.isRecovery ${walletInfo.isRecovery}');
if (walletInfo.isRecovery) { if (walletInfo.isRecovery) {
monero_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery); monero_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery);
@ -233,8 +233,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
void _setListeners() { void _setListeners() {
_listener?.stop(); _listener?.stop();
_listener = monero_wallet.setListeners( _listener = monero_wallet.setListeners(_onNewBlock);
_onNewBlock, _onNeedToRefresh, _onNewTransaction);
_listener.start(); _listener.start();
} }
@ -293,43 +292,30 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
int _getUnlockedBalance() => int _getUnlockedBalance() =>
monero_wallet.getUnlockedBalance(accountIndex: account.id); monero_wallet.getUnlockedBalance(accountIndex: account.id);
void _onNewBlock(int height, int blocksLeft, double ptc) => int _lastAutosaveTimestamp = 0;
syncStatus = SyncingSyncStatus(blocksLeft, ptc); final int _autosaveInterval = 60000;
Future _onNeedToRefresh() async { Future<void> _autoSave() async {
if (syncStatus is FailedSyncStatus) { final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
final sum = _lastAutosaveTimestamp + _autosaveInterval;
if (_lastAutosaveTimestamp != 0 && sum < nowTimestamp) {
return; return;
} }
if (walletInfo.isRecovery) { _lastAutosaveTimestamp = nowTimestamp + _autosaveInterval;
_askForUpdateTransactionHistory(); await save();
_askForUpdateBalance();
}
final currentHeight = getCurrentHeight();
final nodeHeight = monero_wallet.getNodeHeightSync();
if (nodeHeight - currentHeight < moneroBlockSize) {
syncStatus = SyncedSyncStatus();
if (walletInfo.isRecovery) {
await setAsRecovered();
}
}
// if (walletInfo.isRecovery &&
// (nodeHeight - currentHeight < moneroBlockSize)) {
// await setAsRecovered();
// }
if (currentHeight - _cachedRefreshHeight > moneroBlockSize) {
_cachedRefreshHeight = currentHeight;
await save();
}
} }
void _onNewTransaction() { void _onNewBlock(int height, int blocksLeft, double ptc) async {
_askForUpdateBalance();
_askForUpdateTransactionHistory(); _askForUpdateTransactionHistory();
_askForUpdateBalance();
if (blocksLeft < moneroBlockSize) {
syncStatus = SyncedSyncStatus();
await _autoSave();
} else {
syncStatus = SyncingSyncStatus(blocksLeft, ptc);
}
} }
} }