diff --git a/app/components/Onboarding/Syncing.js b/app/components/Onboarding/Syncing.js index c70536ec..50c9fc24 100644 --- a/app/components/Onboarding/Syncing.js +++ b/app/components/Onboarding/Syncing.js @@ -9,9 +9,13 @@ import styles from './Syncing.scss' class Syncing extends Component { componentWillMount() { - const { fetchBlockHeight, newAddress } = this.props + const { fetchBlockHeight, blockHeight, newAddress } = this.props + + // If we don't already know the target block height, fetch it now. + if (!blockHeight) { + fetchBlockHeight() + } - fetchBlockHeight() newAddress('np2wkh') } diff --git a/app/lnd/lib/neutrino.js b/app/lnd/lib/neutrino.js index 46555d23..d4aa3fdf 100644 --- a/app/lnd/lib/neutrino.js +++ b/app/lnd/lib/neutrino.js @@ -10,6 +10,14 @@ class Neutrino extends EventEmitter { this.alias = alias this.autopilot = autopilot this.process = null + this.state = { + grpcProxyStarted: false, + walletOpened: false, + chainSyncStarted: false, + chainSyncFinished: false, + currentBlockHeight: null, + targetBlockHeight: null + } } start() { @@ -52,37 +60,58 @@ class Neutrino extends EventEmitter { } // gRPC started. - if (line.includes('gRPC proxy started') && line.includes('password')) { - this.emit('grpc-proxy-started') + if (!this.state.grpcProxyStarted) { + if (line.includes('gRPC proxy started') && line.includes('password')) { + this.state.grpcProxyStarted = true + this.emit('grpc-proxy-started') + } } // Wallet opened. - if (line.includes('gRPC proxy started') && !line.includes('password')) { - this.emit('wallet-opened') + if (!this.state.walletOpened) { + if (line.includes('gRPC proxy started') && !line.includes('password')) { + this.state.walletOpened = true + this.emit('wallet-opened') + } } // LND syncing has started. - if (line.includes('Waiting for chain backend to finish sync')) { - this.emit('chain-sync-started') + if (!this.state.chainSyncStarted) { + if (line.includes('Syncing to block height')) { + const height = line.match(/Syncing to block height (\d+)/)[1] + this.state.chainSyncStarted = true + this.state.targetBlockHeight = height + this.emit('chain-sync-started') + this.emit('got-final-block-height', height) + } } // LND syncing has completed. - if (line.includes('Chain backend is fully synced')) { - this.emit('chain-sync-finished') + if (!this.state.chainSyncFinished) { + if (line.includes('Chain backend is fully synced')) { + this.state.chainSyncFinished = true + this.emit('chain-sync-finished') + } } // Pass current block height progress to front end for loading state UX - if (line.includes('Rescanned through block')) { - const height = line.match(/Rescanned through block.+\(height (\d*)/)[1] - this.emit('got-block-height', height) - } else if (line.includes('Caught up to height')) { - const height = line.match(/Caught up to height (\d*)/)[1] - this.emit('got-block-height', height) - } else if (line.includes('in the last')) { - const height = line.match(/Processed \d* blocks? in the last.+\(height (\d*)/)[1] - this.emit('got-block-height', height) + if (this.state.chainSyncStarted) { + if (line.includes('Downloading headers for blocks')) { + const height = line.match(/Downloading headers for blocks (\d+) to \d+/)[1] + this.emit('got-current-block-height', height) + } else if (line.includes('Rescanned through block')) { + const height = line.match(/Rescanned through block.+\(height (\d+)/)[1] + this.emit('got-current-block-height', height) + } else if (line.includes('Caught up to height')) { + const height = line.match(/Caught up to height (\d+)/)[1] + this.emit('got-current-block-height', height) + } else if (line.includes('in the last')) { + const height = line.match(/Processed \d* blocks? in the last.+\(height (\d+)/)[1] + this.emit('got-current-block-height', height) + } } }) + return this.process } diff --git a/app/reducers/ipc.js b/app/reducers/ipc.js index 4775ae9c..5d4dc6f8 100644 --- a/app/reducers/ipc.js +++ b/app/reducers/ipc.js @@ -1,5 +1,12 @@ import createIpc from 'redux-electron-ipc' -import { lndSyncing, lndSynced, lndBlockHeight, grpcDisconnected, grpcConnected } from './lnd' +import { + lndSyncing, + lndSynced, + lndBlockHeightTarget, + lndBlockHeight, + grpcDisconnected, + grpcConnected +} from './lnd' import { receiveInfo } from './info' import { receiveAddress } from './address' import { receiveCryptocurrency } from './ticker' @@ -53,6 +60,7 @@ import { const ipc = createIpc({ lndSyncing, lndSynced, + lndBlockHeightTarget, lndBlockHeight, grpcDisconnected, grpcConnected, diff --git a/app/reducers/lnd.js b/app/reducers/lnd.js index f2163b82..d153f4da 100644 --- a/app/reducers/lnd.js +++ b/app/reducers/lnd.js @@ -52,6 +52,10 @@ export const lndBlockHeight = (event, height) => dispatch => { dispatch({ type: RECEIVE_BLOCK, lndBlockHeight: height }) } +export const lndBlockHeightTarget = (event, height) => dispatch => { + dispatch({ type: RECEIVE_BLOCK_HEIGHT, blockHeight: height }) +} + export function getBlockHeight() { return { type: GET_BLOCK_HEIGHT diff --git a/app/zap.js b/app/zap.js index 72ea6936..708459c6 100644 --- a/app/zap.js +++ b/app/zap.js @@ -170,7 +170,11 @@ class ZapController { this.sendMessage('lndSynced') }) - this.neutrino.on('got-block-height', height => { + this.neutrino.on('got-final-block-height', height => { + this.sendMessage('lndBlockHeightTarget', Number(height)) + }) + + this.neutrino.on('got-current-block-height', height => { this.sendMessage('lndBlockHeight', Number(height)) })