Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revert: Remove cache block number and filter cell #2840

Closed
wants to merge 1 commit into from
Closed
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 @@ -4,15 +4,13 @@ import AddressMeta from '../../database/address/meta'
import IndexerTxHashCache from '../../database/chain/entities/indexer-tx-hash-cache'
import RpcService from '../../services/rpc-service'
import TransactionWithStatus from '../../models/chain/transaction-with-status'
import SyncInfoEntity from '../../database/chain/entities/sync-info'
import { TransactionCollector, CellCollector, Indexer as CkbIndexer } from '@ckb-lumos/ckb-indexer'

export default class IndexerCacheService {
private addressMetas: AddressMeta[]
private rpcService: RpcService
private walletId: string
private indexer: CkbIndexer
#cacheBlockNumberEntity?: SyncInfoEntity

constructor(walletId: string, addressMetas: AddressMeta[], rpcService: RpcService, indexer: CkbIndexer) {
for (const addressMeta of addressMetas) {
Expand All @@ -27,6 +25,16 @@ export default class IndexerCacheService {
this.indexer = indexer
}

private async countTxHashes(): Promise<number> {
return getConnection()
.getRepository(IndexerTxHashCache)
.createQueryBuilder()
.where({
walletId: this.walletId,
})
.getCount()
}

private async getTxHashes(): Promise<IndexerTxHashCache[]> {
return getConnection()
.getRepository(IndexerTxHashCache)
Expand Down Expand Up @@ -71,8 +79,6 @@ export default class IndexerCacheService {
}

private async fetchTxMapping(): Promise<Map<string, Array<{ address: string; lockHash: string }>>> {
const lastCacheBlockNumber = await this.getCachedBlockNumber()
const currentHeaderBlockNumber = await this.rpcService.getTipBlockNumber()
const mappingsByTxHash = new Map()
for (const addressMeta of this.addressMetas) {
const lockScripts = [
Expand All @@ -86,8 +92,6 @@ export default class IndexerCacheService {
this.indexer,
{
lock: lockScript,
fromBlock: lastCacheBlockNumber.value,
toBlock: currentHeaderBlockNumber,
},
this.rpcService.url,
{
Expand Down Expand Up @@ -129,8 +133,6 @@ export default class IndexerCacheService {
args: lockScript.args.slice(0, 42),
},
argsLen,
fromBlock: lastCacheBlockNumber.value,
toBlock: currentHeaderBlockNumber,
})

for await (const cell of cellCollector.collect()) {
Expand All @@ -147,34 +149,14 @@ export default class IndexerCacheService {
return mappingsByTxHash
}

private async getCachedBlockNumber() {
if (!this.#cacheBlockNumberEntity) {
this.#cacheBlockNumberEntity =
(await getConnection()
.getRepository(SyncInfoEntity)
.findOne({ name: SyncInfoEntity.getLastCachedKey(this.walletId) })) ??
SyncInfoEntity.fromObject({
name: SyncInfoEntity.getLastCachedKey(this.walletId),
value: '0x0',
})
}

return this.#cacheBlockNumberEntity
}

private async saveCacheBlockNumber(cacheBlockNumber: string) {
let cacheBlockNumberEntity = await this.getCachedBlockNumber()
cacheBlockNumberEntity.value = cacheBlockNumber
await getConnection().manager.save(cacheBlockNumberEntity)
}

public async upsertTxHashes(): Promise<string[]> {
const tipBlockNumber = await this.rpcService.getTipBlockNumber()
const mappingsByTxHash = await this.fetchTxMapping()

const fetchedTxHashes = [...mappingsByTxHash.keys()]
if (!fetchedTxHashes.length) {
await this.saveCacheBlockNumber(tipBlockNumber)
const fetchedTxHashCount = fetchedTxHashes.reduce((sum, txHash) => sum + mappingsByTxHash.get(txHash)!.length, 0)

const txCount = await this.countTxHashes()
if (fetchedTxHashCount === txCount) {
return []
}
const txMetasCaches = await this.getTxHashes()
Expand All @@ -185,7 +167,6 @@ export default class IndexerCacheService {
const newTxHashes = fetchedTxHashes.filter(hash => !cachedTxHashesSet.has(hash))

if (!newTxHashes.length) {
await this.saveCacheBlockNumber(tipBlockNumber)
return []
}

Expand Down Expand Up @@ -233,7 +214,6 @@ export default class IndexerCacheService {
}
}

await this.saveCacheBlockNumber(tipBlockNumber)
return newTxHashes
}

Expand Down
12 changes: 1 addition & 11 deletions packages/neuron-wallet/src/block-sync-renderer/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default class Queue {
#rpcService: RpcService
#indexerConnector: Connector | undefined
#checkAndSaveQueue: QueueObject<{ txHashes: CKBComponents.Hash[]; params: unknown }> | undefined
#lockArgsSet: Set<string> = new Set()

#multiSignBlake160s: string[]
#anyoneCanPayLockHashes: string[]
Expand All @@ -45,15 +44,6 @@ export default class Queue {
this.#lockHashes = AddressParser.batchToLockHash(this.#addresses.map(meta => meta.address))

const blake160s = this.#addresses.map(meta => meta.blake160)
this.#lockArgsSet = new Set(
blake160s
.map(blake160 => [
blake160,
Multisig.hash([blake160]),
SystemScriptInfo.generateSecpScript(blake160).computeHash().slice(0, 42),
])
.flat()
)
this.#multiSignBlake160s = blake160s.map(blake160 => Multisig.hash([blake160]))
this.#anyoneCanPayLockHashes = blake160s.map(b =>
this.#assetAccountInfo.generateAnyoneCanPayScript(b).computeHash()
Expand Down Expand Up @@ -226,7 +216,7 @@ export default class Queue {
}
}
}
await TransactionPersistor.saveFetchTx(tx, this.#lockArgsSet)
await TransactionPersistor.saveFetchTx(tx)
for (const info of anyoneCanPayInfos) {
await AssetAccountService.checkAndSaveAssetAccountWhenSync(info.tokenID, info.blake160)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default class AddressService {
await getConnection().manager.save(publicKeyInfo)

const addressMeta = AddressMeta.fromHdPublicKeyInfoModel(publicKeyInfo.toModel())
this.notifyAddressCreated([addressMeta], undefined)
AddressService.notifyAddressCreated([addressMeta], undefined)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generateAndSaveForPublicKey is a static function, and a static function calls it. If use this, when I start Neuron the first time without a database file, it throws an error that can not called with undefined.

}

// Generate both receiving and change addresses.
Expand Down
Loading