fix(@desktop/wallet): unnecessary locking removed from wallet account service

This commit is contained in:
Sale Djenic 2023-04-05 13:50:32 +02:00 committed by saledjenic
parent 7ebfcd0183
commit d3746b0195

View File

@ -1,5 +1,4 @@
import NimQml, Tables, json, sequtils, sugar, chronicles, strformat, stint, httpclient, net, strutils, os, times, algorithm
import locks
import web3/[ethtypes, conversions]
import ../settings/service as settings_service
@ -123,8 +122,7 @@ QtObject:
tokenService: token_service.Service
networkService: network_service.Service
processedKeyPair: KeyPairDto
walletAccountsLock: Lock
walletAccounts {.guard: walletAccountsLock.}: OrderedTable[string, WalletAccountDto]
walletAccounts: OrderedTable[string, WalletAccountDto]
# Forward declaration
proc buildAllTokens(self: Service, accounts: seq[string], store: bool)
@ -154,8 +152,6 @@ QtObject:
result.accountsService = accountsService
result.tokenService = tokenService
result.networkService = networkService
initLock(result.walletAccountsLock)
withLock result.walletAccountsLock:
result.walletAccounts = initOrderedTable[string, WalletAccountDto]()
proc fetchAccounts*(self: Service): seq[WalletAccountDto] =
@ -178,7 +174,6 @@ QtObject:
return
proc updateRelatedAccounts(self: Service, derivedFrom: string, allAccounts: seq[WalletAccountDto]) =
withLock self.walletAccountsLock:
for wAcc in self.walletAccounts.mvalues:
if not wAcc.derivedFrom.isEmptyOrWhitespace and
cmpIgnoreCase(wAcc.derivedFrom, derivedFrom) == 0:
@ -189,27 +184,23 @@ QtObject:
# updating related accounts for already added accounts
self.updateRelatedAccounts(account.derivedFrom, allAccounts)
# add new account to store
withLock self.walletAccountsLock:
self.walletAccounts[account.address] = account
proc getCachedValuesForAccount*(self: Service, address: string): (bool, bool) =
var areBalancesCached = false
var areMarketValuesCached = false
withLock self.walletAccountsLock:
if self.walletAccounts.hasKey(address):
areBalancesCached = self.walletAccounts[address].hasBalanceCache
areMarketValuesCached = self.walletAccounts[address].hasMarketValuesCache
return (areBalancesCached, areMarketValuesCached)
proc storeTokensForAccount*(self: Service, address: string, tokens: seq[WalletTokenDto], areBalancesCached: bool, areMarketValuesCached: bool) =
withLock self.walletAccountsLock:
if self.walletAccounts.hasKey(address):
self.walletAccounts[address].tokens = tokens
self.walletAccounts[address].hasBalanceCache = areBalancesCached
self.walletAccounts[address].hasMarketValuesCache = areMarketValuesCached
proc updateOrReplaceBalancesAndCollectibles*(self: Service, address: string, tokens: seq[WalletTokenDto], ignoreBalances: bool, ignoreMarketValues: bool): seq[WalletTokenDto] =
withLock self.walletAccountsLock:
if self.walletAccounts.hasKey(address):
if self.walletAccounts[address].tokens.len == 0 or (not ignoreBalances and not ignoreMarketValues):
return tokens
@ -224,31 +215,29 @@ QtObject:
updatedTokens[i].balancesPerChain = token.balancesPerChain
return updatedTokens
proc walletAccountsContainsAddress*(self: Service, address: string): bool =
return self.walletAccounts.hasKey(address)
proc removeAccount*(self: Service, address: string): WalletAccountDto =
result = WalletAccountDto()
withLock self.walletAccountsLock:
if not self.walletAccountsContainsAddress(address):
return
result = self.walletAccounts[address]
self.walletAccounts.del(address)
# updating related accounts for other accounts
let allAccounts = self.fetchAccounts()
self.updateRelatedAccounts(result.derivedFrom, allAccounts)
proc walletAccountsContainsAddress*(self: Service, address: string): bool =
withLock self.walletAccountsLock:
result = self.walletAccounts.hasKey(address)
proc getAccountByAddress*(self: Service, address: string): WalletAccountDto =
result = WalletAccountDto()
withLock self.walletAccountsLock:
if self.walletAccounts.hasKey(address):
if not self.walletAccountsContainsAddress(address):
return
result = self.walletAccounts[address]
proc getWalletAccounts*(self: Service): seq[WalletAccountDto] =
withLock self.walletAccountsLock:
result = toSeq(self.walletAccounts.values)
proc getAddresses*(self: Service): seq[string] =
withLock self.walletAccountsLock:
result = toSeq(self.walletAccounts.keys())
proc init*(self: Service) =
@ -526,8 +515,8 @@ QtObject:
self.events.emit(SIGNAL_WALLET_ACCOUNT_ADDRESS_DETAILS_FETCHED, data)
proc updateAssetsLoadingState(self: Service, wAddress: string, loading: bool) =
withLock self.walletAccountsLock:
if self.walletAccounts.hasKey(wAddress):
if not self.walletAccountsContainsAddress(wAddress):
return
self.walletAccounts[wAddress].assetsLoading = loading
proc checkIfBalancesHaveError*(self: Service, tokens: seq[WalletTokenDto]): bool =