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)
{
nice(19);
bool is_connected = get_current_wallet()->connectToDaemon();
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)
{
nice(19);
Monero::Wallet *wallet = get_current_wallet();
std::string _login = "";
@ -494,6 +496,8 @@ extern "C"
return committed;
}
// START
uint64_t get_node_height_or_update(uint64_t base_eight)
{
if (m_cached_syncing_blockchain_height < base_eight) {
@ -578,6 +582,8 @@ extern "C"
get_current_wallet()->setListener(m_listener);
}
// END
int64_t *subaddrress_get_all()
{
std::vector<Monero::SubaddressRow *> _subaddresses = m_subaddress->getAll();

View file

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

View file

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