Browse Source

Make getAsyncAddress for limit CPU usage

master
Loëck Vézien 7 years ago
parent
commit
c177f6485d
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 24
      src/helpers/btc.js
  2. 26
      src/internals/accounts/sync.js

24
src/helpers/btc.js

@ -98,6 +98,9 @@ export async function getAccount({
}), }),
}) })
const getAsyncAddress = params =>
new Promise(resolve => setTimeout(() => resolve(getAddress(params)), 100))
const getLastAddress = (addresses, txs) => { const getLastAddress = (addresses, txs) => {
const txsAddresses = [...txs.inputs.map(tx => tx.prev_out.addr), ...txs.out.map(tx => tx.addr)] const txsAddresses = [...txs.inputs.map(tx => tx.prev_out.addr), ...txs.out.map(tx => tx.addr)]
const lastAddress = addresses.reverse().find(a => txsAddresses.includes(a.address)) || { const lastAddress = addresses.reverse().find(a => txsAddresses.includes(a.address)) || {
@ -110,14 +113,19 @@ export async function getAccount({
} }
const nextPath = (index = 0) => const nextPath = (index = 0) =>
Promise.all( Array.from(new Array(gapLimit).keys())
Array.from(new Array(gapLimit).keys()).map(v => .reduce(
Promise.all([ (promise, v) =>
getAddress({ type: 'external', index: v + index }), promise.then(async results => {
getAddress({ type: 'internal', index: v + index }), const result = await Promise.all([
]), getAsyncAddress({ type: 'external', index: v + index }),
), getAsyncAddress({ type: 'internal', index: v + index }),
).then(async results => { ])
return [...results, result]
}),
Promise.resolve([]),
)
.then(async results => {
const addresses = results.reduce((result, v) => [...result, ...v], []) const addresses = results.reduce((result, v) => [...result, ...v], [])
const listAddresses = addresses.map(a => a.address) const listAddresses = addresses.map(a => a.address)

26
src/internals/accounts/sync.js

@ -2,22 +2,30 @@
import { getAccount, getHDNode, networks } from 'helpers/btc' import { getAccount, getHDNode, networks } from 'helpers/btc'
export default (send: Function) => ({ const network = networks[1]
all: async ({ accounts }: { accounts: Array<Object> }) => {
const network = networks[1]
send('accounts.sync.progress', null, { kill: false }) function syncAccount({ id, ...currentAccount }) {
const syncAccount = ({ id, currentIndex }) => {
const hdnode = getHDNode({ xpub58: id, network }) const hdnode = getHDNode({ xpub58: id, network })
return getAccount({ currentIndex, hdnode, network, segwit: true }).then(account => ({ return getAccount({ hdnode, network, segwit: true, ...currentAccount }).then(account => ({
id, id,
...account, ...account,
})) }))
} }
export default (send: Function) => ({
all: async ({ accounts }: { accounts: Array<Object> }) => {
send('accounts.sync.progress', null, { kill: false })
try { try {
const result = await Promise.all(accounts.map(syncAccount)) const result = await accounts.reduce(
(promise, account) =>
promise.then(async results => {
const result = await syncAccount(account)
return [...results, result]
}),
Promise.resolve([]),
)
send('accounts.sync.success', result) send('accounts.sync.success', result)
} catch (err) { } catch (err) {
send('accounts.sync.fail', err.stack || err) send('accounts.sync.fail', err.stack || err)

Loading…
Cancel
Save