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
2 changes: 1 addition & 1 deletion packages/neuron-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"last 2 chrome versions"
],
"dependencies": {
"@nervosnetwork/ckb-sdk-core": "0.19.0",
"@nervosnetwork/ckb-sdk-core": "0.19.1",
"@uifabric/experiments": "7.10.0",
"@uifabric/styling": "7.4.0",
"canvg": "2.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
]
},
"dependencies": {
"@nervosnetwork/ckb-sdk-core": "0.19.0",
"@nervosnetwork/ckb-sdk-utils": "0.19.0",
"@nervosnetwork/ckb-sdk-core": "0.19.1",
"@nervosnetwork/ckb-sdk-utils": "0.19.1",
"bn.js": "4.11.8",
"chalk": "2.4.2",
"electron-log": "3.0.7",
Expand All @@ -51,7 +51,7 @@
"uuid": "3.3.2"
},
"devDependencies": {
"@nervosnetwork/ckb-types": "0.19.0",
"@nervosnetwork/ckb-types": "0.19.1",
"@types/electron-devtools-installer": "2.2.0",
"@types/elliptic": "6.4.8",
"@types/sqlite3": "3.1.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/neuron-wallet/src/services/indexer/indexer-rpc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { generateCore } from 'services/sdk-core'

export default class IndexerRPC {
private core: Core

constructor(url: string) {
this.core = new Core(url)
this.core = generateCore(url)
}

public deindexLockHash = async (lockHash: string) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default class IndexerQueue {
// this.lockHashes = lockHashes
this.lockHashInfos = lockHashInfos
this.indexerRPC = new IndexerRPC(url)
this.getBlocksService = new GetBlocks()
this.getBlocksService = new GetBlocks(url)
this.blockNumberService = new BlockNumber()
this.tipNumberListener = tipNumberSubject.subscribe(async (num: string) => {
if (num) {
Expand Down
10 changes: 9 additions & 1 deletion packages/neuron-wallet/src/services/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { interval, BehaviorSubject, merge } from 'rxjs'
import { distinctUntilChanged, sampleTime, flatMap, delay, retry, debounceTime } from 'rxjs/operators'
import https from 'https'
import http from 'http'
import { ShouldBeTypeOf } from 'exceptions'
import { ConnectionStatusSubject } from 'models/subjects/node'
import { CurrentNetworkIDSubject } from 'models/subjects/networks'
Expand Down Expand Up @@ -51,7 +53,13 @@ class NodeService {
if (!url.startsWith('http')) {
throw new Error('Protocol of url should be specified')
}
this.core.setNode({ url })
if (url.startsWith('https')) {
const httpsAgent = new https.Agent({ keepAlive: true })
this.core.setNode({ url, httpsAgent })
} else {
const httpAgent = new http.Agent({ keepAlive: true })
this.core.setNode({ url, httpAgent })
}
this.tipNumberSubject.next('0')
this.connectionStatusSubject.next(false)
return this.core
Expand Down
19 changes: 19 additions & 0 deletions packages/neuron-wallet/src/services/sdk-core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import https from 'https'
import http from 'http'

export const generateCore = (url: string): Core => {
const core = new Core(url)
if (url.startsWith('https')) {
const httpsAgent = new https.Agent({ keepAlive: true })
core.rpc.setNode({ url, httpsAgent })
} else {
const httpAgent = new http.Agent({ keepAlive: true })
core.rpc.setNode({ url, httpAgent })
}
return core
}

export default {
generateCore,
}
17 changes: 14 additions & 3 deletions packages/neuron-wallet/src/services/sync/block-listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ export default class BlockListener {
private currentBlockNumber: BlockNumber
private tipNumberSubject: BehaviorSubject<string | undefined>
private tipNumberListener: Subscription | undefined
private url: string

constructor(
url: string,
lockHashes: string[],
tipNumberSubject: BehaviorSubject<string | undefined> = NodeService.getInstance().tipNumberSubject
) {
this.url = url
this.lockHashes = lockHashes
this.currentBlockNumber = new BlockNumber()
this.rangeForCheck = new RangeForCheck()
this.rangeForCheck = new RangeForCheck(url)
this.tipNumberSubject = tipNumberSubject
}

Expand Down Expand Up @@ -51,10 +54,17 @@ export default class BlockListener {
}

try {
const getBlocksService = new GetBlocks()
const getBlocksService = new GetBlocks(this.url)
const currentTip = await getBlocksService.getTipBlockNumber()
const startBlockNumber = await this.getStartBlockNumber()
this.queue = new Queue(this.lockHashes, startBlockNumber, currentTip, this.currentBlockNumber, this.rangeForCheck)
this.queue = new Queue(
this.url,
this.lockHashes,
startBlockNumber,
currentTip,
this.currentBlockNumber,
this.rangeForCheck
)
} catch (err) {
logger.error(`BlockListener start error:`, err)
}
Expand Down Expand Up @@ -119,6 +129,7 @@ export default class BlockListener {
} else {
const startBlockNumber: string = await this.getStartBlockNumber()
this.queue = new Queue(
this.url,
this.lockHashes,
startBlockNumber,
endBlockNumber,
Expand Down
31 changes: 12 additions & 19 deletions packages/neuron-wallet/src/services/sync/get-blocks.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import { generateCore } from 'services/sdk-core'

import { Block, BlockHeader } from 'types/cell-types'
import TypeConvert from 'types/type-convert'
import { NetworkWithID } from 'services/networks'
import CheckAndSave from './check-and-save'
import Utils from './utils'

import { networkSwitchSubject } from './renderer-params'

let core: Core
networkSwitchSubject.subscribe((network: NetworkWithID | undefined) => {
if (network) {
core = new Core(network.remote)
}
})

export default class GetBlocks {
private retryTime: number
private retryInterval: number
private core: Core

constructor(retryTime: number = 3, retryInterval: number = 100) {
constructor(url: string, retryTime: number = 3, retryInterval: number = 100) {
this.retryTime = retryTime
this.retryInterval = retryInterval
this.core = generateCore(url)
}

public getRangeBlocks = async (blockNumbers: string[]): Promise<Block[]> => {
Expand All @@ -35,7 +28,7 @@ export default class GetBlocks {
}

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

Expand All @@ -48,31 +41,31 @@ export default class GetBlocks {

public retryGetBlock = async (num: string): Promise<Block> => {
const block: Block = await Utils.retry(this.retryTime, this.retryInterval, async () => {
const b: Block = await GetBlocks.getBlockByNumber(num)
const b: Block = await this.getBlockByNumber(num)
return b
})

return block
}

public getTransaction = async (hash: string): Promise<CKBComponents.TransactionWithStatus> => {
const tx = await core.rpc.getTransaction(hash)
const tx = await this.core.rpc.getTransaction(hash)
return tx
}

public getHeader = async (hash: string): Promise<BlockHeader> => {
const result = await core.rpc.getHeader(hash)
const result = await this.core.rpc.getHeader(hash)
return TypeConvert.toBlockHeader(result)
}

public static getBlockByNumber = async (num: string): Promise<Block> => {
const block = await core.rpc.getBlockByNumber(num)
public getBlockByNumber = async (num: string): Promise<Block> => {
const block = await this.core.rpc.getBlockByNumber(num)
return TypeConvert.toBlock(block)
}

public static genesisBlockHash = async (): Promise<string> => {
public genesisBlockHash = async (): Promise<string> => {
const hash: string = await Utils.retry(3, 100, async () => {
const h: string = await core.rpc.getBlockHash('0')
const h: string = await this.core.rpc.getBlockHash('0')
return h
})

Expand Down
5 changes: 3 additions & 2 deletions packages/neuron-wallet/src/services/sync/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ export default class Queue {
private yieldTime = 1

constructor(
url: string,
lockHashes: string[],
startBlockNumber: string,
endBlockNumber: string,
currentBlockNumber: BlockNumber = new BlockNumber(),
rangeForCheck: RangeForCheck = new RangeForCheck(),
rangeForCheck: RangeForCheck = new RangeForCheck(url),
start: boolean = true
) {
this.lockHashes = lockHashes
this.getBlocksService = new GetBlocks()
this.getBlocksService = new GetBlocks(url)
this.startBlockNumber = BigInt(startBlockNumber)
this.endBlockNumber = BigInt(endBlockNumber)
this.rangeForCheck = rangeForCheck
Expand Down
7 changes: 6 additions & 1 deletion packages/neuron-wallet/src/services/sync/range-for-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export enum CheckResultType {
export default class RangeForCheck {
private range: BlockHeader[] = []
private checkSize = 12
private url: string

constructor(url: string) {
this.url = url
}

public getRange = async (): Promise<BlockHeader[]> => {
if (this.range.length <= 0) {
Expand All @@ -26,7 +31,7 @@ export default class RangeForCheck {
const realStartBlockNumber: bigint = startBlockNumber > BigInt(0) ? startBlockNumber : BigInt(0)
const blockNumbers = Utils.range(realStartBlockNumber.toString(), currentBlockNumber.toString())

const getBlocksService = new GetBlocks()
const getBlocksService = new GetBlocks(this.url)
const blocks: Block[] = await getBlocksService.getRangeBlocks(blockNumbers)
const blockHeaders: BlockHeader[] = blocks.map((block: Block) => {
return block.header
Expand Down
6 changes: 3 additions & 3 deletions packages/neuron-wallet/src/startup/sync-block-task/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const loadAddressesAndConvert = async (): Promise<string[]> => {

// call this after network switched
let blockListener: BlockListener | undefined
export const switchNetwork = async () => {
export const switchNetwork = async (url: string) => {
// stop all blocks service
if (blockListener) {
await blockListener.stopAndWait()
Expand All @@ -37,7 +37,7 @@ export const switchNetwork = async () => {
// load lockHashes
const lockHashes: string[] = await loadAddressesAndConvert()
// start sync blocks service
blockListener = new BlockListener(lockHashes, nodeService.tipNumberSubject)
blockListener = new BlockListener(url, lockHashes, nodeService.tipNumberSubject)

// listen to address created
addressCreatedSubject.subscribe(async (addresses: Address[]) => {
Expand Down Expand Up @@ -70,7 +70,7 @@ export const switchNetwork = async () => {
}
// wait former queue to be drained
const hashes: string[] = await loadAddressesAndConvert()
blockListener = new BlockListener(hashes, nodeService.tipNumberSubject)
blockListener = new BlockListener(url, hashes, nodeService.tipNumberSubject)
await blockListener.start(true)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/startup/sync-block-task/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const run = async () => {
if (indexerEnabled) {
await indexerSwitchNetwork(network.remote)
} else {
await syncSwitchNetwork()
await syncSwitchNetwork(network.remote)
}
}
})
Expand Down
15 changes: 6 additions & 9 deletions packages/neuron-wallet/src/utils/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import Core from '@nervosnetwork/ckb-sdk-core'
import NodeService from 'services/node'
import { blake2b, PERSONAL, hexToBytes } from '@nervosnetwork/ckb-sdk-utils'

export default class Blake2b {
private blake2b: any
private core: Core

constructor() {
this.core = NodeService.getInstance().core
this.blake2b = this.core.utils.blake2b(32, null, null, this.core.utils.PERSONAL)
this.blake2b = blake2b(32, null, null, PERSONAL)
}

public update = (message: string): void => {
const msg = this.core.utils.hexToBytes(message.replace(/0x/, ''))
const msg = hexToBytes(message.replace(/0x/, ''))
this.blake2b.update(msg)
}

Expand All @@ -20,8 +17,8 @@ export default class Blake2b {
}

public static digest = (message: string): string => {
const blake2b = new Blake2b()
blake2b.update(message)
return blake2b.digest()
const blake2bHash = new Blake2b()
blake2bHash.update(message)
return blake2bHash.digest()
}
}
Loading