Changed way for saving of monero wallet.

This commit is contained in:
M 2021-08-16 13:01:34 +03:00
parent c256d05029
commit ad95ae3232
3 changed files with 26 additions and 75 deletions

View file

@ -4,6 +4,7 @@
#include <functional>
#include <iostream>
#include <unistd.h>
#include <mutex>
#include "thread"
#include "CwWalletListener.h"
#if __APPLE__
@ -182,6 +183,8 @@ extern "C"
Monero::SubaddressAccount *m_account;
uint64_t m_last_known_wallet_height;
uint64_t m_cached_syncing_blockchain_height = 0;
std::mutex store_lock;
bool is_storing = false;
void change_current_wallet(Monero::Wallet *wallet)
{
@ -452,7 +455,15 @@ extern "C"
void store(char *path)
{
store_lock.lock();
if (is_storing) {
return;
}
is_storing = true;
get_current_wallet()->store(std::string(path));
is_storing = false;
store_lock.unlock();
}
bool transaction_create(char *address, char *payment_id, char *amount,

View file

@ -112,8 +112,6 @@ final rescanBlockchainAsyncNative = moneroApi
.lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
.asFunction<RescanBlockchainAsync>();
bool isStoring = false;
int getSyncingHeight() => getSyncingHeightNative();
bool isNeededToRefresh() => isNeededToRefreshNative() != 0;
@ -285,20 +283,7 @@ SyncListener setListeners(void Function(int, int, double) onNewBlock,
void onStartup() => onStartupNative();
void _storeSync(Object _) {
if (isStoring) {
return;
}
try {
isStoring = true;
storeSync();
isStoring = false;
} catch (e) {
isStoring = false;
rethrow;
}
}
void _storeSync(Object _) => storeSync();
bool _setupNodeSync(Map args) {
final address = args['address'] as String;

View file

@ -41,11 +41,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
balance = MoneroBalance(
fullBalance: monero_wallet.getFullBalance(accountIndex: 0),
unlockedBalance: monero_wallet.getFullBalance(accountIndex: 0));
_lastAutosaveTimestamp = 0;
_lastSaveTimestamp = 0;
_isSavingAfterSync = false;
_isSavingAfterNewTransaction = false;
_isTransactionUpdating = false;
_hasSyncAfterStartup = false;
walletAddresses = MoneroWalletAddresses(walletInfo);
_onAccountChangeReaction = reaction((_) => walletAddresses.account,
(Account account) {
@ -57,7 +54,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
});
}
static const int _autoAfterSyncSaveInterval = 60000;
static const int _autoSaveInterval = 30;
@override
MoneroWalletAddresses walletAddresses;
@ -82,11 +79,9 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
SyncListener _listener;
ReactionDisposer _onAccountChangeReaction;
int _lastAutosaveTimestamp;
bool _isSavingAfterSync;
bool _isSavingAfterNewTransaction;
bool _isTransactionUpdating;
int _lastSaveTimestamp;
bool _hasSyncAfterStartup;
Timer _autoSaveTimer;
Future<void> init() async {
await walletAddresses.init();
@ -105,12 +100,17 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
height: walletInfo.restoreHeight);
}
}
_autoSaveTimer = Timer.periodic(
Duration(seconds: _autoSaveInterval),
(_) async => await save());
}
@override
void close() {
_listener?.stop();
_onAccountChangeReaction?.reaction?.dispose();
_autoSaveTimer?.cancel();
}
@override
@ -240,15 +240,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
@override
Future<void> save() async {
await walletAddresses.updateAddressesInBox();
final now = DateTime.now().millisecondsSinceEpoch;
if (now - _lastSaveTimestamp < Duration(seconds: 10).inMilliseconds) {
return;
}
await backupWalletFiles(name);
_lastSaveTimestamp = now;
await monero_wallet.store();
}
@ -373,46 +365,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
int _getUnlockedBalance() =>
monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account.id);
Future<void> _afterSyncSave() async {
try {
if (_isSavingAfterSync) {
return;
}
_isSavingAfterSync = true;
final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
final sum = _lastAutosaveTimestamp + _autoAfterSyncSaveInterval;
if (_lastAutosaveTimestamp > 0 && sum < nowTimestamp) {
return;
}
await save();
_lastAutosaveTimestamp = nowTimestamp + _autoAfterSyncSaveInterval;
} catch (e) {
print(e.toString());
}
_isSavingAfterSync = false;
}
Future<void> _afterNewTransactionSave() async {
try {
if (_isSavingAfterNewTransaction) {
return;
}
_isSavingAfterNewTransaction = true;
await save();
} catch (e) {
print(e.toString());
}
_isSavingAfterNewTransaction = false;
}
void _onNewBlock(int height, int blocksLeft, double ptc) async {
try {
if (walletInfo.isRecovery) {
@ -426,7 +378,11 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
_askForUpdateBalance();
walletAddresses.accountList.update();
syncStatus = SyncedSyncStatus();
await _afterSyncSave();
if (!_hasSyncAfterStartup) {
_hasSyncAfterStartup = true;
await save();
}
if (walletInfo.isRecovery) {
await setAsRecovered();
@ -444,7 +400,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
await _askForUpdateTransactionHistory();
_askForUpdateBalance();
await Future<void>.delayed(Duration(seconds: 1));
await _afterNewTransactionSave();
} catch (e) {
print(e.toString());
}