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