From f5b1eff21b24ca10a349950b6f638a0d805c4eba Mon Sep 17 00:00:00 2001 From: webwarrior Date: Mon, 6 Feb 2023 14:40:48 +0100 Subject: [PATCH] Backend: check password using only one account Check password using only one account to speed up the process that otherwise could take too long, especially with the mobile frontends (and especially in DEBUG mode under an emulator). Co-authored-by: Andres G. Aragoneses --- src/GWallet.Backend/Account.fs | 46 +++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/GWallet.Backend/Account.fs b/src/GWallet.Backend/Account.fs index 8eba88f9c..0b86f5d6a 100644 --- a/src/GWallet.Backend/Account.fs +++ b/src/GWallet.Backend/Account.fs @@ -262,23 +262,35 @@ module Account = with | :? InvalidPassword -> false - match accountOpt with - | Some account -> async { return checkValidPasswordForSingleAccount account } - | _ -> - let checkJobs = - seq { - for account in GetAllActiveAccounts().OfType() do - yield async { - return checkValidPasswordForSingleAccount account - } - } - async { - let! aggregateSuccess = Async.Parallel checkJobs - if aggregateSuccess.All(fun success -> success = true) then - return true - else - return false - } + + let account = + match accountOpt with + | Some account -> account + | _ -> + // Because password check is expensive operation, only check it for one account. + // (Ether accounts take about 2 times longer to check, so rather use BTC/LTC) + let maybeBtcAccount = + GetAllActiveAccounts().OfType() + |> Seq.tryFind (fun acc -> (acc :> IAccount).Currency = Currency.BTC) + match maybeBtcAccount with + | Some btcAccount -> btcAccount + | None -> + let maybeLtcAccount = + GetAllActiveAccounts().OfType() + |> Seq.tryFind (fun acc -> (acc :> IAccount).Currency = Currency.LTC) + match maybeLtcAccount with + | Some ltcAccount -> ltcAccount + | None -> + let maybeSomeAccount = + GetAllActiveAccounts().OfType() + |> Seq.tryHead + match maybeSomeAccount with + | Some account -> account + | None -> + // the menu "Options" of GWallet.Frontend.Console shouldn't have shown this feature if no accounts + failwith "No accounts found to check valid password. Please report this bug" + + async { return checkValidPasswordForSingleAccount account } let private CreateArchivedAccount (currency: Currency) (unencryptedPrivateKey: string): ArchivedAccount = let fromUnencryptedPrivateKeyToPublicAddressFunc =