Repository navigation
feat(wallet): sort address book by last used time - #6900
SeniorZhai wants to merge 2 commits into
Conversation
|
AI review done for Quality: The change is directionally sound and the earlier placeholder and token-metadata cases are addressed, but the backfill completion state is vulnerable to concurrent and later snapshot updates, and the SQL cutoff can lose valid high-precision history. I verified the relevant control flow and reproduced the SQLite precision case with a focused in-memory check.
|
There was a problem hiding this comment.
AI review · GPT + Grok · eb28942
Request changes. git diff --check passed, and focused SQLite checks verified the address migration in both flavors and reproduced the reported query behavior. The Android build and Room tests stopped during offline Gradle configuration because the Foojay resolver plugin was unavailable.
| @Query("UPDATE addresses SET last_used_at = :usedAt WHERE address_id = :addressId AND (last_used_at IS NULL OR last_used_at < :usedAt)") | ||
| suspend fun updateLastUsedAt(addressId: String, usedAt: Long) | ||
|
|
||
| @Query( |
There was a problem hiding this comment.
A successful send can leave a saved address at Never if the user pastes its destination before address sync inserts the row. markUsed updates zero rows in that case, and backfill skips post-cutoff withdrawals. Persist the successful use by chain, destination, and tag so address sync can apply it when the row arrives.
| val toAddress = toAddress | ||
| if (token != null && toAddress != null) { | ||
| MixinApplication.get().applicationScope.launch { | ||
| web3ViewModel.markAddressUsed(token.chainId, toAddress) |
There was a problem hiding this comment.
A Web3 send started from a tagged address-book entry loses the selected tag here. The click handler passes ARGS_TO_ADDRESS_TAG, but this call ultimately matches only an empty tag, leaving the selected entry unchanged and potentially promoting another entry with the same destination. Use the selected address ID or include its tag in the lookup.
| cutoff: Long, | ||
| ) { | ||
| addresses.forEach { address -> | ||
| val usedAt = safeSnapshotDao.snapshotItems(FilterParams(type = SnapshotType.withdrawal, address = address).buildQuery()) |
There was a problem hiding this comment.
Backfill can promote an address based on a rejected withdrawal. The send path writes a local snapshot with an empty withdrawal hash before submission and leaves it in place on error. This query accepts that placeholder as history. Require evidence of a successful withdrawal before using a snapshot to advance last_used_at.
| refreshSnapshots(startPosition) | ||
| // Withdrawals before the cutoff are history synced after an upgrade or a fresh install. | ||
| if (Instant.parse(startPosition).toEpochMilli() < assetRepo.addressLastUsedCutoff()) { | ||
| assetRepo.backfillAddressLastUsed() |
There was a problem hiding this comment.
Backfill can miss a historical withdrawal when its token metadata arrives later. This job queues token refresh asynchronously, but the backfill requires t.chain_id to match. Inserting the token makes the snapshot eligible without triggering another backfill, and subsequent snapshot refreshes can skip it once the offset passes the cutoff. Resolve missing metadata before backfill or retry the affected history when the token is inserted.
| response.data?.let { | ||
| addressDao.insertList(it) | ||
| assetRepo.saveAddresses(it) | ||
| assetRepo.backfillAddressLastUsed(it) |
There was a problem hiding this comment.
Opening the address book reruns an unbounded snapshot query for every returned address. Wallet resumes repeat the same scan when the latest snapshot predates the cutoff, since an empty refresh leaves the offset unchanged. SQLite confirms a snapshot scan per address. Track processed history and addresses so unchanged data skips backfill, and apply the cutoff in the query.
| ) { | ||
| addresses.forEach { address -> | ||
| val usedAt = safeSnapshotDao.snapshotItems(FilterParams(type = SnapshotType.withdrawal, address = address).buildQuery()) | ||
| .firstNotNullOfOrNull { snapshot -> Instant.parse(snapshot.createdAt).toEpochMilli().takeIf { it < cutoff } } |
There was a problem hiding this comment.
Taking the first row from a descending text sort can pick an older withdrawal within the same second. 00:00:00Z sorts ahead of 00:00:00.900Z, so the earlier use wins. Choose the maximum parsed timestamp before the cutoff or sort by a normalized timestamp in SQL.
There was a problem hiding this comment.
AI review · GPT · 1cdc8e7
Request changes. The incremental diff was checked with git diff --check, and focused source tracing plus SQLite reproduction confirmed three defects in backfill completion and cutoff handling. The broader Android/Room test suite was not run because the repository’s offline Gradle setup lacks the Foojay resolver dependency.
| val historyComplete = PropertyHelper.findValueByKey(PREF_ADDRESS_LAST_USED_HISTORY_SYNCED, false) && | ||
| (missingTokens.isEmpty() || addressDao.queryTexts(missingHistoryTokensQuery(cutoff)).isEmpty()) | ||
| if (historyComplete) { | ||
| PropertyHelper.updateKeyValue(PREF_ADDRESS_LAST_USED_BACKFILLED, (backfilled + pending.map { it.addressId }).joinToString(",")) |
There was a problem hiding this comment.
The backfill marker can be written from a stale scan when snapshot refresh runs concurrently. For example, this call can scan before a matching withdrawal is inserted, then RefreshSnapshotsJob inserts it and sets PREF_ADDRESS_LAST_USED_HISTORY_SYNCED before the check at lines 557–558. This call then records the address as backfilled with last_used_at still unset, and the snapshot job can subsequently skip it because the marker is already present. Serialize the history-sync/backfill transition or recheck and update each address after the history-sync marker is set.
| val historyComplete = PropertyHelper.findValueByKey(PREF_ADDRESS_LAST_USED_HISTORY_SYNCED, false) && | ||
| (missingTokens.isEmpty() || addressDao.queryTexts(missingHistoryTokensQuery(cutoff)).isEmpty()) | ||
| if (historyComplete) { | ||
| PropertyHelper.updateKeyValue(PREF_ADDRESS_LAST_USED_BACKFILLED, (backfilled + pending.map { it.addressId }).joinToString(",")) |
There was a problem hiding this comment.
An address can be permanently marked backfilled while its historical snapshot still has an empty withdrawal hash. The filter correctly skips that pending row, but this marker is not invalidated when refreshAndGetSnapshot() later replaces the row with the completed withdrawal, so future backfills never revisit the address and its last_used_at remains stale. Clear or version the address marker when a previously incomplete withdrawal gains its hash, or make backfill derive completion from current snapshot state.
| // A withdrawal hash proves the withdrawal went out; local placeholders written before submission carry none. | ||
| internal const val HISTORY_WITHDRAWAL_FILTER = | ||
| "s.withdrawal IS NOT NULL AND s.withdrawal != 'null' AND instr(s.withdrawal, '\"withdrawal_hash\":\"\"') = 0 " + | ||
| "AND s.type != 'pending' AND julianday(s.created_at) < julianday(?)" |
There was a problem hiding this comment.
The julianday prefilter loses sub-millisecond precision and can discard a withdrawal that is actually before the cutoff. For example, SQLite evaluates 2026-02-01T00:00:00.500900Z < 2026-02-01T00:00:00.501Z as false, while Instant places the row 0.1 ms before the cutoff. Use a lossless timestamp comparison or fetch a conservative candidate set and apply the cutoff only after parsing with Instant.
No description provided.