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 @@ -301,9 +301,6 @@ export default class LightConnector extends Connector<CKBComponents.Hash> {
}

async appendScript(scripts: AppendScript[]) {
if (!scripts.length) {
return
}
this.initSyncProgress(scripts)
}
}
8 changes: 6 additions & 2 deletions packages/neuron-wallet/src/block-sync-renderer/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import AssetAccountInfo from 'models/asset-account-info'
import { Address as AddressInterface } from "models/address"
import AddressParser from 'models/address-parser'
import Multisig from 'models/multisig'
import BlockHeader from 'models/chain/block-header'
import TxAddressFinder from './tx-address-finder'
import IndexerConnector from './indexer-connector'
import IndexerCacheService from './indexer-cache-service'
Expand Down Expand Up @@ -134,8 +135,11 @@ export default class Queue {
blockHashes.map(v => ['getHeader', v])
).exec()
headers.forEach((blockHeader, idx) => {
txs[idx].timestamp = blockHeader!.timestamp
txs[idx].blockNumber = blockHeader!.number
if (blockHeader) {
const header = BlockHeader.fromSDK(blockHeader)
txs[idx].timestamp = header.timestamp
txs[idx].blockNumber = header.number
}
})
return txs
}
Expand Down
7 changes: 4 additions & 3 deletions packages/neuron-wallet/src/database/chain/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { getConnection } from 'typeorm'
import MultisigOutputChangedSubject from 'models/subjects/multisig-output-db-changed-subject'
import SyncProgressService from 'services/sync-progress'
import InputEntity from './entities/input'
import OutputEntity from './entities/output'
import TransactionEntity from './entities/transaction'
import SyncInfoEntity from './entities/sync-info'
import IndexerTxHashCache from './entities/indexer-tx-hash-cache'
import MultisigOutput from './entities/multisig-output'
import SyncProgress from './entities/sync-progress'

/*
* Clean local sqlite storage
*/
export const clean = async () => {
await Promise.all([
...[InputEntity, OutputEntity, TransactionEntity, IndexerTxHashCache, MultisigOutput, SyncProgress].map(entity => {
...[InputEntity, OutputEntity, TransactionEntity, IndexerTxHashCache, MultisigOutput].map(entity => {
return getConnection()
.getRepository(entity)
.clear()
})
}),
SyncProgressService.clearCurrentWalletProgress()
])
MultisigOutputChangedSubject.getSubject().next('reset')

Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class IndexerService {
}

if (!NodeService.getInstance().isCkbNodeExternal) {
await startMonitor('ckb')
await startMonitor('ckb', true)
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/neuron-wallet/src/services/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import NetworksService from './networks'
import Multisig from 'models/multisig'
import SyncProgress, { SyncAddressType } from 'database/chain/entities/sync-progress'
import { NetworkType } from 'models/network'
import WalletService from './wallets'

const max64Int = '0x' + 'f'.repeat(16)
export default class MultisigService {
Expand Down Expand Up @@ -334,9 +335,13 @@ export default class MultisigService {
}

static async getMultisigConfigForLight() {
const currentWallet = WalletService.getInstance().getCurrent()
const multisigConfigs = await getConnection()
.getRepository(MultisigConfig)
.createQueryBuilder()
.where({
walletId: currentWallet?.id
})
.getMany()
return multisigConfigs.map(v => ({
walletId: v.walletId,
Expand Down
7 changes: 7 additions & 0 deletions packages/neuron-wallet/src/services/sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,11 @@ export default class SyncProgressService {
.where({ hash: In(hashes) })
.getMany()
}

static async clearCurrentWalletProgress() {
const currentWallet = WalletService.getInstance().getCurrent()
await getConnection()
.getRepository(SyncProgress)
.delete({ walletId: currentWallet?.id })
}
}
8 changes: 5 additions & 3 deletions packages/neuron-wallet/src/utils/ckb-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const lightRPCProperties: Record<string, Omit<Parameters<CKBRPC['addMethod']>[0]
paramsFormatters: [paramsFormatter.toHash],
resultFormatters: (result: { status: 'fetched' | 'fetching' | 'added' | 'not_found', data?: RPC.TransactionWithStatus }) => {
if (result.status === 'fetched' && result.data) {return resultFormatter.toTransactionWithStatus(result.data)}
return null
return result
}
}
}
Expand All @@ -117,7 +117,7 @@ export class LightRPC extends Base {
) => Promise<{ lastCursor: HexString, txs: { txHash: HexString, txIndex: HexString, blockNumber: CKBComponents.BlockNumber }[]}>

getTransactionInLight: Base['getTransaction']
fetchTransaction: (hash: string) => Promise<CKBComponents.TransactionWithStatus | null>
fetchTransaction: (hash: string) => Promise<CKBComponents.TransactionWithStatus | { status: 'fetching' | 'added' | 'not_found' }>

getGenesisBlock: () => Promise<CKBComponents.Block>
exceptionMethods = ['getCurrentEpoch', 'getEpochByNumber', 'getBlockHash', 'getLiveCell']
Expand Down Expand Up @@ -151,7 +151,9 @@ export class LightRPC extends Base {
if (!tx?.transaction) {
tx = await CommonUtils.retry(3, 100, async () => {
const tmp = await this.fetchTransaction(hash)
if (tmp === null) {throw new Error('Not fetch the transaction current')}
if ('status' in tmp) {
throw new Error(`transaction ${hash} status: ${tmp.status}`)
}
return tmp
})
if (!tx) {throw new Error(`Fetch transaction tx failed, please try it later: ${hash}`)}
Expand Down
21 changes: 16 additions & 5 deletions packages/neuron-wallet/tests/block-sync-renderer/queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,23 @@ const generateFakeTx = (id: string, publicKeyHash: string = '0x') => {
lock: Script.fromObject({ hashType: ScriptHashType.Type, codeHash: '0x' + id.repeat(64), args: publicKeyHash })
})
]
fakeTx.blockNumber = '1'
fakeTx.blockNumber = '0x1'
fakeTx.timestamp = '0x1880a3fa5bc'
const fakeTxWithStatus = {
transaction: fakeTx,
txStatus: new TxStatus('0x' + id.repeat(64), TxStatusType.Committed)
}
return fakeTxWithStatus
}

const fakeBlockHeader = {
version: '0x0',
epoch: '0x0',
hash: `0x${'0'.repeat(64)}`,
parentHash: `0x${'0'.repeat(64)}`,
timestamp: '0x0',
number: '0x0',
}
describe('queue', () => {
let queue: Queue
const fakeNodeUrl = 'http://fakenode:8114'
Expand Down Expand Up @@ -216,7 +225,7 @@ describe('queue', () => {
stubbedGetTransactionFn.mockResolvedValue(fakeTxWithStatus1)
stubbedRPCCreateBatchRequestExecFn
.mockResolvedValueOnce(fakeTxs)
.mockResolvedValueOnce(fakeTxs.map(v => ({ timestamp: v.transaction.timestamp, number: v.transaction.blockNumber })))
.mockResolvedValueOnce(fakeTxs.map(v => ({ ...fakeBlockHeader, timestamp: v.transaction.timestamp, number: v.transaction.blockNumber })))
stubbedTransactionsSubject.next({ txHashes: fakeTxs.map(v => v.transaction.hash), params: fakeTxs[0].transaction.blockNumber })
})
describe('when saving transactions is succeeded', () => {
Expand All @@ -226,7 +235,8 @@ describe('queue', () => {
const lockHashes = ['0x1f2615a8dde4e28ca736ff763c2078aff990043f4cbf09eb4b3a58a140a0862d']
const tx = Transaction.fromSDK(fakeTxWithStatus2.transaction.toSDK())
tx.blockHash = fakeTxWithStatus2.txStatus.blockHash!
tx.blockNumber = fakeTxWithStatus2.transaction.blockNumber
tx.blockNumber = BigInt(fakeTxWithStatus2.transaction.blockNumber!).toString()
tx.timestamp = BigInt(fakeTxWithStatus2.transaction.timestamp!).toString()
expect(stubbedTxAddressFinderConstructor).toHaveBeenCalledWith(
lockHashes,
[new AssetAccountInfo().generateAnyoneCanPayScript(addressInfo.blake160).computeHash()],
Expand All @@ -238,7 +248,8 @@ describe('queue', () => {
for (const { transaction } of fakeTxs) {
const tx = Transaction.fromSDK(transaction.toSDK())
tx.blockHash = fakeTxWithStatus2.txStatus.blockHash!
tx.blockNumber = fakeTxWithStatus2.transaction.blockNumber
tx.blockNumber = BigInt(fakeTxWithStatus2.transaction.blockNumber!).toString()
tx.timestamp = BigInt(fakeTxWithStatus2.transaction.timestamp!).toString()
expect(stubbedSaveFetchFn).toHaveBeenCalledWith(tx)
}
})
Expand All @@ -260,7 +271,7 @@ describe('queue', () => {
stubbedSaveFetchFn.mockRejectedValueOnce(err)
stubbedRPCCreateBatchRequestExecFn
.mockResolvedValueOnce(fakeTxs)
.mockResolvedValueOnce(fakeTxs.map(v => ({ timestamp: v.transaction.timestamp, number: v.transaction.blockNumber })))
.mockResolvedValueOnce(fakeTxs.map(v => ({ ...fakeBlockHeader, timestamp: v.transaction.timestamp, number: v.transaction.blockNumber })))
stubbedTransactionsSubject.next({ txHashes: fakeTxs.map(v => v.transaction.hash), params: fakeTxs[0].transaction.blockNumber })
await flushPromises()
})
Expand Down
9 changes: 9 additions & 0 deletions packages/neuron-wallet/tests/controllers/multisig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ jest.mock('electron', () => ({
getFocusedWindow: jest.fn()
}
}))
jest.mock('services/wallets', () => ({
getInstance() {
return {
getCurrent() {
return jest.fn()
}
}
}
}))

jest.mock('../../src/services/multisig')
const MultiSigServiceMock = MultisigService as jest.MockedClass<typeof MultisigService>
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-wallet/tests/controllers/sync-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ jest.doMock('services/ckb-runner', () => ({
jest.mock('undici', () => ({
request: () => jest.fn()()
}))
jest.mock('services/multisig', () => ({
syncMultisigOutput: () => jest.fn()
}))

describe('SyncApiController', () => {
const emitter = new Emitter()
Expand Down
6 changes: 6 additions & 0 deletions packages/neuron-wallet/tests/services/light-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const spawnMock = jest.fn()
const loggerErrorMock = jest.fn()
const loggerInfoMock = jest.fn()
const transportsGetFileMock = jest.fn()
const cleanMock = jest.fn()

function resetMock() {
mockFn.mockReset()
Expand All @@ -38,6 +39,7 @@ function resetMock() {
loggerErrorMock.mockReset()
loggerInfoMock.mockReset()
transportsGetFileMock.mockReset()
cleanMock.mockReset()
}

jest.doMock('../../src/env', () => ({
Expand Down Expand Up @@ -67,6 +69,10 @@ jest.doMock('../../src/services/settings', () => ({
}
}))

jest.doMock('../../src/database/chain', () => ({
clean: cleanMock
}))

jest.doMock('process', () => ({
get platform() {
return platformMock()
Expand Down