Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh committed Nov 13, 2024
1 parent 965e008 commit 79100c2
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions ironfish/src/wallet/scanner/walletScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { HeadValue } from '../walletdb/headValue'
import { Config } from '../../fileStores'
import { Logger } from '../../logger'
import { Mutex } from '../../mutex'
import { BlockHeader, Transaction } from '../../primitives'
import { BlockHeader, GENESIS_BLOCK_SEQUENCE, Transaction } from '../../primitives'
import { BufferUtils, HashUtils } from '../../utils'
import { WorkerPool } from '../../workerPool'
import { ChainProcessorWithTransactions } from './chainProcessorWithTransactions'
Expand All @@ -33,7 +33,10 @@ export class WalletScanner {
* A snapshot of the accounts that have `scanningEnabled` set to true. Used
* to tell what accounts should be scanned, and from what block.
*/
private scanningAccounts = new Array<{ account: Account; scanFrom: HeadValue | null }>()
private scanningAccounts = new Array<{
account: Account
scanFrom: { head: HeadValue | null } | null
}>()

constructor(options: {
logger: Logger
Expand Down Expand Up @@ -183,7 +186,10 @@ export class WalletScanner {
for (const candidate of this.scanningAccounts) {
if (
!candidate.scanFrom ||
BufferUtils.equalsNullable(candidate.scanFrom.hash, blockHeader.previousBlockHash)
candidate.scanFrom.head?.hash.equals(blockHeader.previousBlockHash) ||
(blockHeader.sequence === GENESIS_BLOCK_SEQUENCE &&
candidate.scanFrom.head === null &&
candidate.account.createdAt === null)
) {
candidate.scanFrom = null

Expand All @@ -195,6 +201,12 @@ export class WalletScanner {
} else {
connectOnlyAccounts.push(candidate.account)
}
} else if (
candidate.scanFrom.head === null &&
candidate.account.createdAt &&
candidate.account.createdAt.sequence === blockHeader.sequence
) {
decryptAndConnectAccounts.push(candidate.account)
}
}

Expand Down Expand Up @@ -241,7 +253,7 @@ export class WalletScanner {
}

for (const account of this.scanningAccounts) {
if (account.scanFrom && BufferUtils.equalsNullable(account.scanFrom.hash, header.hash)) {
if (account.scanFrom?.head?.hash.equals(header.hash)) {
account.scanFrom = null
}
}
Expand Down Expand Up @@ -308,13 +320,18 @@ export class WalletScanner {
*/
private async refreshScanningAccounts(): Promise<void> {
this.scanningAccounts = (await this.getScanningAccountsWithHead()).map(
({ account, head }) => ({ account, scanFrom: head }),
({ account, head }) => ({ account, scanFrom: { head } }),
)
}

private getEarliestHead(): HeadValue | null {
let earliestHead = null
for (const { scanFrom: head } of this.scanningAccounts) {
for (const { scanFrom } of this.scanningAccounts) {
if (!scanFrom) {
continue
}

const head = scanFrom.head
if (!head) {
return null
}
Expand Down

0 comments on commit 79100c2

Please sign in to comment.