Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class CheckOutput {
return this.output
}

public checkLockHash = async (lockHashList: string[]): Promise<boolean | undefined> => {
public checkLockHash = (lockHashList: string[]): boolean => {
return lockHashList.includes(this.output.lockHash!)
}
}
22 changes: 10 additions & 12 deletions packages/neuron-wallet/src/services/sync/check-and-save/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class CheckTx {
}

public check = async (lockHashes: string[]): Promise<string[]> => {
const outputs: Cell[] = await this.filterOutputs(lockHashes)
const outputs: Cell[] = this.filterOutputs(lockHashes)
const inputAddresses = await this.filterInputs(lockHashes)

const outputAddresses: string[] = outputs.map(output => {
Expand All @@ -50,17 +50,15 @@ export default class CheckTx {
return false
}

public filterOutputs = async (lockHashes: string[]) => {
const cells: Cell[] = (await Promise.all(
this.tx.outputs!.map(async output => {
const checkOutput = new CheckOutput(output)
const result = await checkOutput.checkLockHash(lockHashes)
if (result) {
return output
}
return false
})
)).filter(cell => !!cell) as Cell[]
public filterOutputs = (lockHashes: string[]) => {
const cells: Cell[] = this.tx.outputs!.map(output => {
const checkOutput = new CheckOutput(output)
const result = checkOutput.checkLockHash(lockHashes)
if (result) {
return output
}
return false
}).filter(cell => !!cell) as Cell[]
return cells
}

Expand Down
14 changes: 11 additions & 3 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import CheckTx from 'services/sync/check-and-save/tx'
import { TransactionPersistor } from 'services/tx'
import LockUtils from 'models/lock-utils'
import { addressesUsedSubject } from './renderer-params'
import logger from 'utils/logger'

export default class GetBlocks {
private retryTime: number
Expand All @@ -34,18 +35,24 @@ export default class GetBlocks {
}

public getTipBlockNumber = async (): Promise<string> => {
const tip: string = await this.core.rpc.getTipBlockNumber()
return tip
return this.core.rpc.getTipBlockNumber()
}

public checkAndSave = async (blocks: Block[], lockHashes: string[]): Promise<void> => {
const cachedPreviousTxs = new Map()
for (const block of blocks) {
logger.debug(`checking block #${block.header.number}, ${block.transactions.length} txs`)
for (const tx of block.transactions) {
const checkTx = new CheckTx(tx, this.url)
const addresses = await checkTx.check(lockHashes)
if (addresses.length > 0) {
for (const input of tx.inputs!) {
const previousTxWithStatus = await this.getTransaction(input.previousOutput!.txHash)
const previousTxHash = input.previousOutput!.txHash
let previousTxWithStatus = cachedPreviousTxs.get(previousTxHash)
if (!previousTxWithStatus) {
previousTxWithStatus = await this.getTransaction(previousTxHash)
cachedPreviousTxs.set(previousTxHash, previousTxWithStatus)
}
const previousTx = TypeConvert.toTransaction(previousTxWithStatus.transaction)
const previousOutput = previousTx.outputs![+input.previousOutput!.index]
input.lock = previousOutput.lock
Expand All @@ -60,6 +67,7 @@ export default class GetBlocks {
}
}
}
cachedPreviousTxs.clear()
}

public retryGetBlock = async (num: string): Promise<Block> => {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class Queue {
const range = await this.rangeForCheck.getRange()
const rangeFirstBlockHeader: BlockHeader = range[0]
await this.currentBlockNumber.updateCurrent(BigInt(rangeFirstBlockHeader.number))
await this.rangeForCheck.clearRange()
this.rangeForCheck.clearRange()
await TransactionPersistor.deleteWhenFork(rangeFirstBlockHeader.number)
throw new Error(`chain forked: ${checkResult.type}`)
} else if (checkResult.type === CheckResultType.BlockHeadersNotMatch) {
Expand Down