Skip to content

Commit

Permalink
Backend: use lazy to only init once
Browse files Browse the repository at this point in the history
For performance reasons, we don't want to execute migrations
everytime we need to iterate over the accounts.
  • Loading branch information
knocte committed Feb 18, 2024
1 parent bfd55cb commit 0f4db61
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
25 changes: 18 additions & 7 deletions src/GWallet.Backend/Account.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ type UnhandledCurrencyServerException(currency: Currency,

module Account =

let private isInitialized (accounts: seq<IAccount>) = lazy(
Config.Init()

#if !NATIVE_SEGWIT
let _readonlyUtxoAccounts =
#else
let readOnlyUtxoAccounts =
#endif
accounts.Where(fun acc -> acc.Currency.IsUtxo()).OfType<ReadOnlyAccount>()
#if NATIVE_SEGWIT
UtxoCoin.Account.MigrateReadOnlyAccountsToNativeSegWit readonlyUtxoAccounts
#endif
()
)

let private GetShowableBalanceAndImminentPaymentInternal (account: IAccount)
(mode: ServerSelectionMode)
(cancelSourceOption: Option<CustomCancelSource>)
Expand Down Expand Up @@ -86,8 +101,6 @@ module Account =
failwith <| SPrintF1 "Currency (%A) not supported for this API" currency

let GetAllActiveAccounts(): seq<IAccount> =
Config.PropagateEthAccountInfoToMissingTokensAccounts()

let allCurrencies = Currency.GetAll()

// uncomment this block below, manually, if when testing you need to go back to test the WelcomePage.xaml
Expand All @@ -106,11 +119,9 @@ module Account =
}

let allAccounts = getAccountsOfKind [AccountKind.Normal; AccountKind.ReadOnly]
#if NATIVE_SEGWIT
let readonlyUtxoAccounts =
allAccounts.Where(fun acc -> acc.Currency.IsUtxo()).OfType<ReadOnlyAccount>()
UtxoCoin.Account.MigrateReadOnlyAccountsToNativeSegWit readonlyUtxoAccounts
#endif

let _checkInit: unit =
(isInitialized allAccounts).Value

allAccounts

Expand Down
35 changes: 16 additions & 19 deletions src/GWallet.Backend/Config.fs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,6 @@ module Config =
configDir.Create()
configDir

// In case a new token was added it will not have a config for an existing user
// we copy the eth configs to the new tokens config directory
let PropagateEthAccountInfoToMissingTokensAccounts() =
for accountKind in (AccountKind.All()) do
let ethConfigDir = GetConfigDir Currency.ETH accountKind
for token in Currency.GetAll() do
if token.IsEthToken() then
let tokenConfigDir = GetConfigDir token accountKind
for ethAccountFilePath in Directory.GetFiles ethConfigDir.FullName do
let newPath = ethAccountFilePath.Replace(ethConfigDir.FullName, tokenConfigDir.FullName)
if not (File.Exists newPath) then
File.Copy(ethAccountFilePath, newPath)

let GetAccountFilesWithCurrency (currency: Currency) (accountKind: AccountKind): seq<FileRepresentation> =
seq {
for filePath in Directory.GetFiles (GetConfigDir currency accountKind).FullName do
Expand Down Expand Up @@ -177,9 +164,19 @@ module Config =
let RemoveReadOnlyAccount (account: ReadOnlyAccount): unit =
RemoveAccount account

[<Literal>]
let private NoInitMsg = "This function never really existed, but it's here to warn you that yes, configuration needs to be initialized (e.g. for migrations) but it's done in Account.GetAllActiveAccounts() so that it's done by all frontends seamlessly (not explicitly)"

[<Obsolete "This function never really existed, but it's here to warn you that yes, configuration needs to be initialized (e.g. for migrations) but it's done in Account.GetAllActiveAccounts() so that it's done by all frontends seamlessly (not explicitly)">]
let Init() =
failwith NoInitMsg
/// NOTE: the real initialization happens in Account.fs , see isInitialized
let internal Init() =
// In case a new token was added it will not have a config for an existing user
// we copy the eth configs to the new tokens config directory
let propagateEthAccountInfoToMissingTokensAccounts() =
for accountKind in (AccountKind.All()) do
let ethConfigDir = GetConfigDir Currency.ETH accountKind
for token in Currency.GetAll() do
if token.IsEthToken() then
let tokenConfigDir = GetConfigDir token accountKind
for ethAccountFilePath in Directory.GetFiles ethConfigDir.FullName do
let newPath = ethAccountFilePath.Replace(ethConfigDir.FullName, tokenConfigDir.FullName)
if not (File.Exists newPath) then
File.Copy(ethAccountFilePath, newPath)

propagateEthAccountInfoToMissingTokensAccounts()

0 comments on commit 0f4db61

Please sign in to comment.