feat(@wallet): move timer to status-go

This commit is contained in:
Anthony Laibe 2022-12-01 10:19:54 +01:00 committed by Anthony Laibe
parent 1238e91b09
commit 1e086de456
6 changed files with 27 additions and 97 deletions

View File

@ -62,8 +62,6 @@ proc init*(self: Controller) =
let accounts = self.getWalletAccounts() let accounts = self.getWalletAccounts()
let addresses = accounts.map(account => account.address) let addresses = accounts.map(account => account.address)
self.delegate.setHistoryFetchState(addresses, false) self.delegate.setHistoryFetchState(addresses, false)
else:
echo "Unhandled wallet signal: ", data.eventType
self.events.on(SIGNAL_TRANSACTIONS_LOADED) do(e:Args): self.events.on(SIGNAL_TRANSACTIONS_LOADED) do(e:Args):
let args = TransactionsLoadedArgs(e) let args = TransactionsLoadedArgs(e)
@ -87,9 +85,6 @@ proc init*(self: Controller) =
proc checkPendingTransactions*(self: Controller): seq[TransactionDto] = proc checkPendingTransactions*(self: Controller): seq[TransactionDto] =
return self.transactionService.checkPendingTransactions() return self.transactionService.checkPendingTransactions()
proc checkRecentHistory*(self: Controller, calledFromTimerOrInit = false) =
self.walletAccountService.checkRecentHistory(calledFromTimerOrInit)
proc getWalletAccounts*(self: Controller): seq[WalletAccountDto] = proc getWalletAccounts*(self: Controller): seq[WalletAccountDto] =
self.walletAccountService.getWalletAccounts() self.walletAccountService.getWalletAccounts()

View File

@ -16,9 +16,6 @@ method load*(self: AccessInterface) {.base.} =
method isLoaded*(self: AccessInterface): bool {.base.} = method isLoaded*(self: AccessInterface): bool {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method checkRecentHistory*(self: AccessInterface, calledFromTimerOrInit: bool) {.base.} =
raise newException(ValueError, "No implementation available")
method switchAccount*(self: AccessInterface, accountIndex: int) {.base.} = method switchAccount*(self: AccessInterface, accountIndex: int) {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")

View File

@ -58,7 +58,6 @@ method isLoaded*(self: Module): bool =
return self.moduleLoaded return self.moduleLoaded
method viewDidLoad*(self: Module) = method viewDidLoad*(self: Module) =
self.controller.checkRecentHistory(calledFromTimerOrInit = true)
let accounts = self.getWalletAccounts() let accounts = self.getWalletAccounts()
self.moduleLoaded = true self.moduleLoaded = true

View File

@ -95,10 +95,6 @@ type KeycardActivityArgs* = ref object of Args
keycardNewName*: string keycardNewName*: string
keyPair*: KeyPairDto keyPair*: KeyPairDto
const CheckBalanceSlotExecuteIntervalInSeconds = 15 * 60 # 15 mins
const CheckBalanceTimerIntervalInMilliseconds = 5000 # 5 sec
const CheckHistoryIntervalInMilliseconds = 20 * 60 * 1000 # 20 mins
include async_tasks include async_tasks
include ../../common/json_utils include ../../common/json_utils
@ -120,9 +116,9 @@ QtObject:
isHistoryFetchTimerAlreadyRunning: bool isHistoryFetchTimerAlreadyRunning: bool
# Forward declaration # Forward declaration
proc buildAllTokens(self: Service, calledFromTimerOrInit = false) proc buildAllTokens(self: Service)
proc startBuildingTokensTimer(self: Service, resetTimeToNow = true) proc checkRecentHistory*(self: Service)
proc startFetchingHistoryTimer(self: Service, resetTimeToNow = true) proc startWallet(self: Service)
proc delete*(self: Service) = proc delete*(self: Service) =
self.closingApp = true self.closingApp = true
@ -184,7 +180,9 @@ QtObject:
account.relatedAccounts = accounts.filter(x => not account.derivedFrom.isEmptyOrWhitespace and (cmpIgnoreCase(x.derivedFrom, account.derivedFrom) == 0)) account.relatedAccounts = accounts.filter(x => not account.derivedFrom.isEmptyOrWhitespace and (cmpIgnoreCase(x.derivedFrom, account.derivedFrom) == 0))
self.walletAccounts[account.address] = account self.walletAccounts[account.address] = account
self.buildAllTokens(true) self.buildAllTokens()
self.checkRecentHistory()
self.startWallet()
except Exception as e: except Exception as e:
let errDesription = e.msg let errDesription = e.msg
error "error: ", errDesription error "error: ", errDesription
@ -200,17 +198,10 @@ QtObject:
self.events.on(SignalType.Wallet.event) do(e:Args): self.events.on(SignalType.Wallet.event) do(e:Args):
var data = WalletSignal(e) var data = WalletSignal(e)
case data.eventType: case data.eventType:
of "recent-history-ready": of "wallet-tick-reload":
# run timer again... self.checkRecentHistory()
self.startFetchingHistoryTimer() self.buildAllTokens()
of "non-archival-node-detected":
# run timer again...
self.startFetchingHistoryTimer()
of "fetching-history-error":
# run timer again...
self.startFetchingHistoryTimer()
else:
echo "Unhandled wallet signal: ", data.eventType
proc getAccountByAddress*(self: Service, address: string): WalletAccountDto = proc getAccountByAddress*(self: Service, address: string): WalletAccountDto =
if not self.walletAccounts.hasKey(address): if not self.walletAccounts.hasKey(address):
@ -231,13 +222,15 @@ QtObject:
if(accounts[i].address == address): if(accounts[i].address == address):
return i return i
proc checkRecentHistory*(self: Service, calledFromTimerOrInit = false) = proc startWallet(self: Service) =
if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return
# Since we don't have a way to re-run TimerTaskArg (to stop it and run again), we introduced some flags which will discard backend.startWallet()
# just ignore buildAllTokens in case that proc is called by some action in the time window between two successive calls
# initiated by TimerTaskArg. proc checkRecentHistory*(self: Service) =
if not calledFromTimerOrInit: if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
self.ignoreTimeInitiatedHistoryFetchBuild = true return
try: try:
let addresses = self.getWalletAccounts().map(a => a.address) let addresses = self.getWalletAccounts().map(a => a.address)
@ -450,32 +443,6 @@ QtObject:
data.error = e.msg data.error = e.msg
self.events.emit(SIGNAL_WALLET_ACCOUNT_DERIVED_ADDRESS_DETAILS_FETCHED, data) self.events.emit(SIGNAL_WALLET_ACCOUNT_DERIVED_ADDRESS_DETAILS_FETCHED, data)
proc onStartBuildingTokensTimer*(self: Service, response: string) {.slot.} =
if ((now().toTime().toUnix() - self.timerStartTimeInSeconds) < CheckBalanceSlotExecuteIntervalInSeconds):
self.startBuildingTokensTimer(resetTimeToNow = false)
return
if self.ignoreTimeInitiatedTokensBuild:
self.ignoreTimeInitiatedTokensBuild = false
return
self.buildAllTokens(true)
proc startBuildingTokensTimer(self: Service, resetTimeToNow = true) =
if(self.closingApp):
return
if (resetTimeToNow):
self.timerStartTimeInSeconds = now().toTime().toUnix()
let arg = TimerTaskArg(
tptr: cast[ByteAddress](timerTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onStartBuildingTokensTimer",
timeoutInMilliseconds: CheckBalanceTimerIntervalInMilliseconds
)
self.threadpool.start(arg)
proc onAllTokensBuilt*(self: Service, response: string) {.slot.} = proc onAllTokensBuilt*(self: Service, response: string) {.slot.} =
try: try:
let responseObj = response.parseJson let responseObj = response.parseJson
@ -495,19 +462,10 @@ QtObject:
except Exception as e: except Exception as e:
error "error: ", procName="onAllTokensBuilt", errName = e.name, errDesription = e.msg error "error: ", procName="onAllTokensBuilt", errName = e.name, errDesription = e.msg
# run timer again... proc buildAllTokens(self: Service) =
self.startBuildingTokensTimer() if(not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
proc buildAllTokens(self: Service, calledFromTimerOrInit = false) =
if(self.closingApp or not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled()):
return return
# Since we don't have a way to re-run TimerTaskArg (to stop it and run again), we introduced some flags which will
# just ignore buildAllTokens in case that proc is called by some action in the time window between two successive calls
# initiated by TimerTaskArg.
if not calledFromTimerOrInit:
self.ignoreTimeInitiatedTokensBuild = true
let arg = BuildTokensTaskArg( let arg = BuildTokensTaskArg(
tptr: cast[ByteAddress](prepareTokensTask), tptr: cast[ByteAddress](prepareTokensTask),
vptr: cast[ByteAddress](self.vptr), vptr: cast[ByteAddress](self.vptr),
@ -517,6 +475,8 @@ QtObject:
proc onIsWalletEnabledChanged*(self: Service) {.slot.} = proc onIsWalletEnabledChanged*(self: Service) {.slot.} =
self.buildAllTokens() self.buildAllTokens()
self.checkRecentHistory()
self.startWallet()
proc getNetworkCurrencyBalance*(self: Service, network: NetworkDto): float64 = proc getNetworkCurrencyBalance*(self: Service, network: NetworkDto): float64 =
for walletAccount in toSeq(self.walletAccounts.values): for walletAccount in toSeq(self.walletAccounts.values):
@ -652,27 +612,3 @@ QtObject:
except Exception as e: except Exception as e:
error "error: ", procName="deleteKeycard", errName = e.name, errDesription = e.msg error "error: ", procName="deleteKeycard", errName = e.name, errDesription = e.msg
return "error: " & e.msg return "error: " & e.msg
proc onStartHistoryFetchingTimer*(self: Service, response: string) {.slot.} =
self.isHistoryFetchTimerAlreadyRunning = false
if self.ignoreTimeInitiatedHistoryFetchBuild:
self.ignoreTimeInitiatedHistoryFetchBuild = false
return
self.checkRecentHistory(true)
proc startFetchingHistoryTimer(self: Service, resetTimeToNow = true) =
if(self.closingApp or
not singletonInstance.localAccountSensitiveSettings.getIsWalletEnabled() or
self.isHistoryFetchTimerAlreadyRunning):
return
# TODO move this to status-go, because the 20 minutes timer leaves the app hanging when trying to leave
# self.isHistoryFetchTimerAlreadyRunning = true
# let arg = TimerTaskArg(
# tptr: cast[ByteAddress](timerTask),
# vptr: cast[ByteAddress](self.vptr),
# slot: "onStartHistoryFetchingTimer",
# timeoutInMilliseconds: CheckHistoryIntervalInMilliseconds
# )
# self.threadpool.start(arg)

View File

@ -91,6 +91,9 @@ rpc(getPendingTransactionsByChainIDs, "wallet"):
rpc(getWalletToken, "wallet"): rpc(getWalletToken, "wallet"):
discard discard
rpc(startWallet, "wallet"):
discard
rpc(getTransactionEstimatedTime, "wallet"): rpc(getTransactionEstimatedTime, "wallet"):
chainId: int chainId: int
maxFeePerGas: float maxFeePerGas: float

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 1501c4071734de0730849dd9af02f803c9d0da70 Subproject commit c735e2a6bbef9874173378121316798d3d6fe981