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 6 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 {
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')
}

63
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
}

10
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,

4
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

6
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))
})

Loading…
Cancel
Save