Browse Source

feat: determine block sync progress early

Determine the current block height directly from the lnd log output and
pass the data into the app as soon as we have it. Do not call out to
block explorers if we don't need to.
renovate/lint-staged-8.x
Tom Kirkpatrick 7 years ago
parent
commit
1c8a2e9bf4
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 8
      app/components/Onboarding/Syncing.js
  2. 63
      app/lnd/lib/neutrino.js
  3. 10
      app/reducers/ipc.js
  4. 4
      app/reducers/lnd.js
  5. 6
      app/zap.js

8
app/components/Onboarding/Syncing.js

@ -9,9 +9,13 @@ import styles from './Syncing.scss'
class Syncing extends Component { class Syncing extends Component {
componentWillMount() { 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') newAddress('np2wkh')
} }

63
app/lnd/lib/neutrino.js

@ -10,6 +10,14 @@ class Neutrino extends EventEmitter {
this.alias = alias this.alias = alias
this.autopilot = autopilot this.autopilot = autopilot
this.process = null this.process = null
this.state = {
grpcProxyStarted: false,
walletOpened: false,
chainSyncStarted: false,
chainSyncFinished: false,
currentBlockHeight: null,
targetBlockHeight: null
}
} }
start() { start() {
@ -52,37 +60,58 @@ class Neutrino extends EventEmitter {
} }
// gRPC started. // gRPC started.
if (line.includes('gRPC proxy started') && line.includes('password')) { if (!this.state.grpcProxyStarted) {
this.emit('grpc-proxy-started') if (line.includes('gRPC proxy started') && line.includes('password')) {
this.state.grpcProxyStarted = true
this.emit('grpc-proxy-started')
}
} }
// Wallet opened. // Wallet opened.
if (line.includes('gRPC proxy started') && !line.includes('password')) { if (!this.state.walletOpened) {
this.emit('wallet-opened') if (line.includes('gRPC proxy started') && !line.includes('password')) {
this.state.walletOpened = true
this.emit('wallet-opened')
}
} }
// LND syncing has started. // LND syncing has started.
if (line.includes('Waiting for chain backend to finish sync')) { if (!this.state.chainSyncStarted) {
this.emit('chain-sync-started') 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. // LND syncing has completed.
if (line.includes('Chain backend is fully synced')) { if (!this.state.chainSyncFinished) {
this.emit('chain-sync-finished') 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 // Pass current block height progress to front end for loading state UX
if (line.includes('Rescanned through block')) { if (this.state.chainSyncStarted) {
const height = line.match(/Rescanned through block.+\(height (\d*)/)[1] if (line.includes('Downloading headers for blocks')) {
this.emit('got-block-height', height) const height = line.match(/Downloading headers for blocks (\d+) to \d+/)[1]
} else if (line.includes('Caught up to height')) { this.emit('got-current-block-height', height)
const height = line.match(/Caught up to height (\d*)/)[1] } else if (line.includes('Rescanned through block')) {
this.emit('got-block-height', height) const height = line.match(/Rescanned through block.+\(height (\d+)/)[1]
} else if (line.includes('in the last')) { this.emit('got-current-block-height', height)
const height = line.match(/Processed \d* blocks? in the last.+\(height (\d*)/)[1] } else if (line.includes('Caught up to height')) {
this.emit('got-block-height', 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 return this.process
} }

10
app/reducers/ipc.js

@ -1,5 +1,12 @@
import createIpc from 'redux-electron-ipc' 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 { receiveInfo } from './info'
import { receiveAddress } from './address' import { receiveAddress } from './address'
import { receiveCryptocurrency } from './ticker' import { receiveCryptocurrency } from './ticker'
@ -53,6 +60,7 @@ import {
const ipc = createIpc({ const ipc = createIpc({
lndSyncing, lndSyncing,
lndSynced, lndSynced,
lndBlockHeightTarget,
lndBlockHeight, lndBlockHeight,
grpcDisconnected, grpcDisconnected,
grpcConnected, grpcConnected,

4
app/reducers/lnd.js

@ -52,6 +52,10 @@ export const lndBlockHeight = (event, height) => dispatch => {
dispatch({ type: RECEIVE_BLOCK, lndBlockHeight: height }) dispatch({ type: RECEIVE_BLOCK, lndBlockHeight: height })
} }
export const lndBlockHeightTarget = (event, height) => dispatch => {
dispatch({ type: RECEIVE_BLOCK_HEIGHT, blockHeight: height })
}
export function getBlockHeight() { export function getBlockHeight() {
return { return {
type: GET_BLOCK_HEIGHT type: GET_BLOCK_HEIGHT

6
app/zap.js

@ -170,7 +170,11 @@ class ZapController {
this.sendMessage('lndSynced') 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)) this.sendMessage('lndBlockHeight', Number(height))
}) })

Loading…
Cancel
Save