mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-05-05 02:52:15 +00:00
Changed way for saving of monero wallet.
This commit is contained in:
parent
c256d05029
commit
ad95ae3232
3 changed files with 26 additions and 75 deletions
|
@ -4,6 +4,7 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <mutex>
|
||||||
#include "thread"
|
#include "thread"
|
||||||
#include "CwWalletListener.h"
|
#include "CwWalletListener.h"
|
||||||
#if __APPLE__
|
#if __APPLE__
|
||||||
|
@ -182,6 +183,8 @@ extern "C"
|
||||||
Monero::SubaddressAccount *m_account;
|
Monero::SubaddressAccount *m_account;
|
||||||
uint64_t m_last_known_wallet_height;
|
uint64_t m_last_known_wallet_height;
|
||||||
uint64_t m_cached_syncing_blockchain_height = 0;
|
uint64_t m_cached_syncing_blockchain_height = 0;
|
||||||
|
std::mutex store_lock;
|
||||||
|
bool is_storing = false;
|
||||||
|
|
||||||
void change_current_wallet(Monero::Wallet *wallet)
|
void change_current_wallet(Monero::Wallet *wallet)
|
||||||
{
|
{
|
||||||
|
@ -452,7 +455,15 @@ extern "C"
|
||||||
|
|
||||||
void store(char *path)
|
void store(char *path)
|
||||||
{
|
{
|
||||||
|
store_lock.lock();
|
||||||
|
if (is_storing) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
is_storing = true;
|
||||||
get_current_wallet()->store(std::string(path));
|
get_current_wallet()->store(std::string(path));
|
||||||
|
is_storing = false;
|
||||||
|
store_lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool transaction_create(char *address, char *payment_id, char *amount,
|
bool transaction_create(char *address, char *payment_id, char *amount,
|
||||||
|
|
|
@ -112,8 +112,6 @@ final rescanBlockchainAsyncNative = moneroApi
|
||||||
.lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
|
.lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
|
||||||
.asFunction<RescanBlockchainAsync>();
|
.asFunction<RescanBlockchainAsync>();
|
||||||
|
|
||||||
bool isStoring = false;
|
|
||||||
|
|
||||||
int getSyncingHeight() => getSyncingHeightNative();
|
int getSyncingHeight() => getSyncingHeightNative();
|
||||||
|
|
||||||
bool isNeededToRefresh() => isNeededToRefreshNative() != 0;
|
bool isNeededToRefresh() => isNeededToRefreshNative() != 0;
|
||||||
|
@ -285,20 +283,7 @@ SyncListener setListeners(void Function(int, int, double) onNewBlock,
|
||||||
|
|
||||||
void onStartup() => onStartupNative();
|
void onStartup() => onStartupNative();
|
||||||
|
|
||||||
void _storeSync(Object _) {
|
void _storeSync(Object _) => storeSync();
|
||||||
if (isStoring) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
isStoring = true;
|
|
||||||
storeSync();
|
|
||||||
isStoring = false;
|
|
||||||
} catch (e) {
|
|
||||||
isStoring = false;
|
|
||||||
rethrow;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool _setupNodeSync(Map args) {
|
bool _setupNodeSync(Map args) {
|
||||||
final address = args['address'] as String;
|
final address = args['address'] as String;
|
||||||
|
|
|
@ -41,11 +41,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
balance = MoneroBalance(
|
balance = MoneroBalance(
|
||||||
fullBalance: monero_wallet.getFullBalance(accountIndex: 0),
|
fullBalance: monero_wallet.getFullBalance(accountIndex: 0),
|
||||||
unlockedBalance: monero_wallet.getFullBalance(accountIndex: 0));
|
unlockedBalance: monero_wallet.getFullBalance(accountIndex: 0));
|
||||||
_lastAutosaveTimestamp = 0;
|
|
||||||
_lastSaveTimestamp = 0;
|
|
||||||
_isSavingAfterSync = false;
|
|
||||||
_isSavingAfterNewTransaction = false;
|
|
||||||
_isTransactionUpdating = false;
|
_isTransactionUpdating = false;
|
||||||
|
_hasSyncAfterStartup = false;
|
||||||
walletAddresses = MoneroWalletAddresses(walletInfo);
|
walletAddresses = MoneroWalletAddresses(walletInfo);
|
||||||
_onAccountChangeReaction = reaction((_) => walletAddresses.account,
|
_onAccountChangeReaction = reaction((_) => walletAddresses.account,
|
||||||
(Account account) {
|
(Account account) {
|
||||||
|
@ -57,7 +54,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static const int _autoAfterSyncSaveInterval = 60000;
|
static const int _autoSaveInterval = 30;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
MoneroWalletAddresses walletAddresses;
|
MoneroWalletAddresses walletAddresses;
|
||||||
|
@ -82,11 +79,9 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
|
|
||||||
SyncListener _listener;
|
SyncListener _listener;
|
||||||
ReactionDisposer _onAccountChangeReaction;
|
ReactionDisposer _onAccountChangeReaction;
|
||||||
int _lastAutosaveTimestamp;
|
|
||||||
bool _isSavingAfterSync;
|
|
||||||
bool _isSavingAfterNewTransaction;
|
|
||||||
bool _isTransactionUpdating;
|
bool _isTransactionUpdating;
|
||||||
int _lastSaveTimestamp;
|
bool _hasSyncAfterStartup;
|
||||||
|
Timer _autoSaveTimer;
|
||||||
|
|
||||||
Future<void> init() async {
|
Future<void> init() async {
|
||||||
await walletAddresses.init();
|
await walletAddresses.init();
|
||||||
|
@ -105,12 +100,17 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
height: walletInfo.restoreHeight);
|
height: walletInfo.restoreHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_autoSaveTimer = Timer.periodic(
|
||||||
|
Duration(seconds: _autoSaveInterval),
|
||||||
|
(_) async => await save());
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void close() {
|
void close() {
|
||||||
_listener?.stop();
|
_listener?.stop();
|
||||||
_onAccountChangeReaction?.reaction?.dispose();
|
_onAccountChangeReaction?.reaction?.dispose();
|
||||||
|
_autoSaveTimer?.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -240,15 +240,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
@override
|
@override
|
||||||
Future<void> save() async {
|
Future<void> save() async {
|
||||||
await walletAddresses.updateAddressesInBox();
|
await walletAddresses.updateAddressesInBox();
|
||||||
|
|
||||||
final now = DateTime.now().millisecondsSinceEpoch;
|
|
||||||
|
|
||||||
if (now - _lastSaveTimestamp < Duration(seconds: 10).inMilliseconds) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await backupWalletFiles(name);
|
await backupWalletFiles(name);
|
||||||
_lastSaveTimestamp = now;
|
|
||||||
await monero_wallet.store();
|
await monero_wallet.store();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,46 +365,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
int _getUnlockedBalance() =>
|
int _getUnlockedBalance() =>
|
||||||
monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account.id);
|
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 {
|
void _onNewBlock(int height, int blocksLeft, double ptc) async {
|
||||||
try {
|
try {
|
||||||
if (walletInfo.isRecovery) {
|
if (walletInfo.isRecovery) {
|
||||||
|
@ -426,7 +378,11 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
_askForUpdateBalance();
|
_askForUpdateBalance();
|
||||||
walletAddresses.accountList.update();
|
walletAddresses.accountList.update();
|
||||||
syncStatus = SyncedSyncStatus();
|
syncStatus = SyncedSyncStatus();
|
||||||
await _afterSyncSave();
|
|
||||||
|
if (!_hasSyncAfterStartup) {
|
||||||
|
_hasSyncAfterStartup = true;
|
||||||
|
await save();
|
||||||
|
}
|
||||||
|
|
||||||
if (walletInfo.isRecovery) {
|
if (walletInfo.isRecovery) {
|
||||||
await setAsRecovered();
|
await setAsRecovered();
|
||||||
|
@ -444,7 +400,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
|
||||||
await _askForUpdateTransactionHistory();
|
await _askForUpdateTransactionHistory();
|
||||||
_askForUpdateBalance();
|
_askForUpdateBalance();
|
||||||
await Future<void>.delayed(Duration(seconds: 1));
|
await Future<void>.delayed(Duration(seconds: 1));
|
||||||
await _afterNewTransactionSave();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print(e.toString());
|
print(e.toString());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue