From c27d5a6da87d05a0d1700c174929ad0a9af1b154 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sat, 31 Oct 2020 13:32:14 -0300 Subject: [PATCH] Use Bisq's timer in API's 'unlockwallet timeout(s)' This change was requested in https://github.com/chimp1984/bisq/commit/961703ecea62df62946ab001ec28205662688ccd This replaces and closes PR https://github.com/bisq-network/bisq/pull/4558 --- .../bisq/core/api/CoreWalletsService.java | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/bisq/core/api/CoreWalletsService.java b/core/src/main/java/bisq/core/api/CoreWalletsService.java index 29394aad8ac..fc15ec5062a 100644 --- a/core/src/main/java/bisq/core/api/CoreWalletsService.java +++ b/core/src/main/java/bisq/core/api/CoreWalletsService.java @@ -23,6 +23,9 @@ import bisq.core.btc.wallet.BtcWalletService; import bisq.core.btc.wallet.WalletsManager; +import bisq.common.Timer; +import bisq.common.UserThread; + import org.bitcoinj.core.Address; import org.bitcoinj.core.TransactionConfidence; import org.bitcoinj.crypto.KeyCrypterScrypt; @@ -37,8 +40,6 @@ import java.util.List; import java.util.Optional; -import java.util.Timer; -import java.util.TimerTask; import java.util.function.Function; import java.util.stream.Collectors; @@ -57,7 +58,7 @@ class CoreWalletsService { private final BtcWalletService btcWalletService; @Nullable - private TimerTask lockTask; + private Timer lockTimer; @Nullable private KeyParameter tempAesKey; @@ -190,29 +191,22 @@ void unlockWallet(String password, long timeout) { if (!walletsManager.checkAESKey(tempAesKey)) throw new IllegalStateException("incorrect password"); - if (lockTask != null) { - // The user is overriding a prior unlock timeout. Cancel the existing - // lock TimerTask to prevent it from calling lockWallet() before or after the - // new timer task does. - lockTask.cancel(); - // Avoid the synchronized(lock) overhead of an unnecessary lockTask.cancel() - // call the next time 'unlockwallet' is called. - lockTask = null; + if (lockTimer != null) { + // The user has called unlockwallet again, before the prior unlockwallet + // timeout has expired. He's overriding it with a new timeout value. + // Remove the existing lock timer to prevent it from calling lockwallet + // before or after the new one does. + lockTimer.stop(); + lockTimer = null; } - lockTask = new TimerTask() { - @Override - public void run() { - if (tempAesKey != null) { - // Do not try to lock wallet after timeout if the user has already - // done so via 'lockwallet' - log.info("Locking wallet after {} second timeout expired.", timeout); - tempAesKey = null; - } + lockTimer = UserThread.runAfter(() -> { + if (tempAesKey != null) { + // The unlockwallet timeout has expired; re-lock the wallet. + log.info("Locking wallet after {} second timeout expired.", timeout); + tempAesKey = null; } - }; - Timer timer = new Timer("Lock Wallet Timer"); - timer.schedule(lockTask, SECONDS.toMillis(timeout)); + }, timeout, SECONDS); } // Provided for automated wallet protection method testing, despite the