Browse Source

refactor(neutrino): consistent method/event naming

renovate/lint-staged-8.x
Tom Kirkpatrick 7 years ago
parent
commit
8b4c863579
No known key found for this signature in database GPG Key ID: 72203A8EC5967EA8
  1. 13
      app/containers/Root.js
  2. 78
      app/lnd/lib/neutrino.js
  3. 15
      app/reducers/ipc.js
  4. 14
      app/reducers/lnd.js
  5. 7
      app/reducers/onboarding.js
  6. 22
      app/zap.js

13
app/containers/Root.js

@ -206,22 +206,23 @@ const Root = ({
onboardingProps, onboardingProps,
syncingProps syncingProps
}) => { }) => {
// If we are syncing show the syncing screen // If we are onboarding show the onboarding screen.
if (!onboardingProps.onboarding.onboarded) { if (onboardingProps.onboarding.onboarding) {
return <Onboarding {...onboardingProps} /> return <Onboarding {...onboardingProps} />
} }
// If we are syncing show the syncing screen // If we are syncing show the syncing screen.
if ( if (
onboardingProps.onboarding.onboarded &&
lnd.lightningGrpcActive &&
onboardingProps.onboarding.connectionType === 'local' && onboardingProps.onboarding.connectionType === 'local' &&
lnd.grpcStarted &&
lnd.syncStatus !== 'complete' lnd.syncStatus !== 'complete'
) { ) {
return <Syncing {...syncingProps} /> return <Syncing {...syncingProps} />
} }
// Don't launch the app without gRPC connection // Don't launch the app without a connection to the lightning wallet gRPC interface.
if (!lnd.grpcStarted) { if (!lnd.lightningGrpcActive) {
return <LoadingBolt /> return <LoadingBolt />
} }

78
app/lnd/lib/neutrino.js

@ -5,17 +5,19 @@ import config from '../config'
import { mainLog, lndLog, lndLogGetLevel } from '../../utils/log' import { mainLog, lndLog, lndLogGetLevel } from '../../utils/log'
import { fetchBlockHeight } from './util' import { fetchBlockHeight } from './util'
// Sync status is currenty pending. // Sync statuses
const NEUTRINO_SYNC_STATUS_PENDING = 'chain-sync-pending' const CHAIN_SYNC_PENDING = 'chain-sync-pending'
const CHAIN_SYNC_WAITING = 'chain-sync-waiting'
// Waiting for chain backend to finish synchronizing. const CHAIN_SYNC_IN_PROGRESS = 'chain-sync-started'
const NEUTRINO_SYNC_STATUS_WAITING = 'chain-sync-waiting' const CHAIN_SYNC_COMPLETE = 'chain-sync-finished'
// Initial sync is currently in progress. // Events
const NEUTRINO_SYNC_STATUS_IN_PROGRESS = 'chain-sync-started' const ERROR = 'error'
const CLOSE = 'close'
// Initial sync has completed. const WALLET_UNLOCKER_GRPC_ACTIVE = 'wallet-unlocker-grpc-active'
const NEUTRINO_SYNC_STATUS_COMPLETE = 'chain-sync-finished' const LIGHTNING_GRPC_ACTIVE = 'lightning-grpc-active'
const GOT_CURRENT_BLOCK_HEIGHT = 'got-current-block-height'
const GOT_LND_BLOCK_HEIGHT = 'got-lnd-block-height'
/** /**
* Wrapper class for Lnd to run and monitor it in Neutrino mode. * Wrapper class for Lnd to run and monitor it in Neutrino mode.
@ -27,9 +29,9 @@ class Neutrino extends EventEmitter {
this.alias = alias this.alias = alias
this.autopilot = autopilot this.autopilot = autopilot
this.process = null this.process = null
this.grpcProxyStarted = false this.walletUnlockerGrpcActive = false
this.walletOpened = false this.lightningGrpcActive = false
this.chainSyncStatus = NEUTRINO_SYNC_STATUS_PENDING this.chainSyncStatus = CHAIN_SYNC_PENDING
} }
/** /**
@ -56,9 +58,9 @@ class Neutrino extends EventEmitter {
] ]
this.process = spawn(lndConfig.lndPath, neutrinoArgs) this.process = spawn(lndConfig.lndPath, neutrinoArgs)
.on('error', error => this.emit('error', error)) .on('error', error => this.emit(ERROR, error))
.on('close', code => { .on('close', code => {
this.emit('close', code) this.emit(CLOSE, code)
this.process = null this.process = null
}) })
@ -75,55 +77,55 @@ class Neutrino extends EventEmitter {
lndLog[lndLogGetLevel(line)](line) lndLog[lndLogGetLevel(line)](line)
} }
// gRPC started. // password RPC server listening (wallet unlocker started).
if (!this.grpcProxyStarted) { if (!this.walletUnlockerGrpcActive && !this.lightningGrpcActive) {
if (line.includes('gRPC proxy started') && line.includes('password')) { if (line.includes('RPC server listening on') && line.includes('password')) {
this.grpcProxyStarted = true this.walletUnlockerGrpcActive = true
this.emit('grpc-proxy-started') this.emit(WALLET_UNLOCKER_GRPC_ACTIVE)
} }
} }
// Wallet opened. // RPC server listening (wallet unlocked).
if (!this.walletOpened) { if (!this.lightningGrpcActive) {
if (line.includes('gRPC proxy started') && !line.includes('password')) { if (line.includes('RPC server listening on') && !line.includes('password')) {
this.walletOpened = true this.lightningGrpcActive = true
this.emit('wallet-opened') this.emit(LIGHTNING_GRPC_ACTIVE)
} }
} }
// If the sync has already completed then we don't need to do anythibng else. // If the sync has already completed then we don't need to do anything else.
if (this.is(NEUTRINO_SYNC_STATUS_COMPLETE)) { if (this.is(CHAIN_SYNC_COMPLETE)) {
return return
} }
// Lnd waiting for backend to finish syncing. // Lnd waiting for backend to finish syncing.
if (this.is(NEUTRINO_SYNC_STATUS_PENDING) || this.is(NEUTRINO_SYNC_STATUS_IN_PROGRESS)) { if (this.is(CHAIN_SYNC_PENDING) || this.is(CHAIN_SYNC_IN_PROGRESS)) {
if ( if (
line.includes('No sync peer candidates available') || line.includes('No sync peer candidates available') ||
line.includes('Unable to synchronize wallet to chain') || line.includes('Unable to synchronize wallet to chain') ||
line.includes('Waiting for chain backend to finish sync') line.includes('Waiting for chain backend to finish sync')
) { ) {
this.setState(NEUTRINO_SYNC_STATUS_WAITING) this.setState(CHAIN_SYNC_WAITING)
} }
} }
// Lnd syncing has started or resumed. // Lnd syncing has started or resumed.
if (this.is(NEUTRINO_SYNC_STATUS_PENDING) || this.is(NEUTRINO_SYNC_STATUS_WAITING)) { if (this.is(CHAIN_SYNC_PENDING) || this.is(CHAIN_SYNC_WAITING)) {
const match = line.match(/Syncing to block height (\d+)/) const match = line.match(/Syncing to block height (\d+)/)
if (match) { if (match) {
// Notify that chhain syncronisation has now started. // Notify that chhain syncronisation has now started.
this.setState(NEUTRINO_SYNC_STATUS_IN_PROGRESS) this.setState(CHAIN_SYNC_IN_PROGRESS)
// This is the latest block that BTCd is aware of. // This is the latest block that BTCd is aware of.
const btcdHeight = Number(match[1]) const btcdHeight = Number(match[1])
this.emit('got-current-block-height', btcdHeight) this.emit(GOT_CURRENT_BLOCK_HEIGHT, btcdHeight)
// The height returned from the LND log output may not be the actual current block height (this is the case // The height returned from the LND log output may not be the actual current block height (this is the case
// when BTCD is still in the middle of syncing the blockchain) so try to fetch thhe current height from from // when BTCD is still in the middle of syncing the blockchain) so try to fetch thhe current height from from
// some block explorers just incase. // some block explorers just incase.
fetchBlockHeight() fetchBlockHeight()
.then( .then(
height => (height > btcdHeight ? this.emit('got-current-block-height', height) : null) height => (height > btcdHeight ? this.emit(GOT_CURRENT_BLOCK_HEIGHT, height) : null)
) )
// If we were unable to fetch from bock explorers at least we already have what BTCd gave us so just warn. // If we were unable to fetch from bock explorers at least we already have what BTCd gave us so just warn.
.catch(err => mainLog.warn(`Unable to fetch block height: ${err.message}`)) .catch(err => mainLog.warn(`Unable to fetch block height: ${err.message}`))
@ -131,7 +133,7 @@ class Neutrino extends EventEmitter {
} }
// Lnd as received some updated block data. // Lnd as received some updated block data.
if (this.is(NEUTRINO_SYNC_STATUS_WAITING) || this.is(NEUTRINO_SYNC_STATUS_IN_PROGRESS)) { if (this.is(CHAIN_SYNC_WAITING) || this.is(CHAIN_SYNC_IN_PROGRESS)) {
let height let height
let match let match
@ -144,13 +146,13 @@ class Neutrino extends EventEmitter {
} }
if (height) { if (height) {
this.setState(NEUTRINO_SYNC_STATUS_IN_PROGRESS) this.setState(CHAIN_SYNC_IN_PROGRESS)
this.emit('got-lnd-block-height', height) this.emit(GOT_LND_BLOCK_HEIGHT, height)
} }
// Lnd syncing has completed. // Lnd syncing has completed.
if (line.includes('Chain backend is fully synced')) { if (line.includes('Chain backend is fully synced')) {
this.setState(NEUTRINO_SYNC_STATUS_COMPLETE) this.setState(CHAIN_SYNC_COMPLETE)
} }
} }
}) })

15
app/reducers/ipc.js

@ -1,11 +1,5 @@
import createIpc from 'redux-electron-ipc' import createIpc from 'redux-electron-ipc'
import { import { lndSyncStatus, currentBlockHeight, lndBlockHeight, lightningGrpcActive } from './lnd'
lndSyncStatus,
currentBlockHeight,
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'
@ -47,7 +41,7 @@ import { receiveDescribeNetwork, receiveQueryRoutes, receiveInvoiceAndQueryRoute
import { import {
startOnboarding, startOnboarding,
startLndError, startLndError,
walletUnlockerStarted, walletUnlockerGrpcActive,
receiveSeed, receiveSeed,
receiveSeedError, receiveSeedError,
successfullyCreatedWallet, successfullyCreatedWallet,
@ -60,8 +54,7 @@ const ipc = createIpc({
lndSyncStatus, lndSyncStatus,
currentBlockHeight, currentBlockHeight,
lndBlockHeight, lndBlockHeight,
grpcDisconnected, lightningGrpcActive,
grpcConnected,
receiveInfo, receiveInfo,
@ -115,7 +108,7 @@ const ipc = createIpc({
startOnboarding, startOnboarding,
startLndError, startLndError,
walletUnlockerStarted, walletUnlockerGrpcActive,
receiveSeed, receiveSeed,
receiveSeedError, receiveSeedError,
successfullyCreatedWallet, successfullyCreatedWallet,

14
app/reducers/lnd.js

@ -15,8 +15,7 @@ export const SET_SYNC_STATUS_COMPLETE = 'SET_SYNC_STATUS_COMPLETE'
export const RECEIVE_BLOCK_HEIGHT = 'RECEIVE_BLOCK_HEIGHT' export const RECEIVE_BLOCK_HEIGHT = 'RECEIVE_BLOCK_HEIGHT'
export const RECEIVE_BLOCK = 'RECEIVE_BLOCK' export const RECEIVE_BLOCK = 'RECEIVE_BLOCK'
export const GRPC_DISCONNECTED = 'GRPC_DISCONNECTED' export const SET_LIGHTNING_WALLET_ACTIVE = 'SET_LIGHTNING_WALLET_ACTIVE'
export const GRPC_CONNECTED = 'GRPC_CONNECTED'
// ------------------------------------ // ------------------------------------
// Actions // Actions
@ -60,11 +59,9 @@ export const lndSyncStatus = (event, status) => (dispatch, getState) => {
} }
} }
export const grpcDisconnected = () => dispatch => dispatch({ type: GRPC_DISCONNECTED }) export const lightningGrpcActive = () => dispatch => {
export const grpcConnected = () => dispatch => {
dispatch(fetchInfo()) dispatch(fetchInfo())
dispatch({ type: GRPC_CONNECTED }) dispatch({ type: SET_LIGHTNING_WALLET_ACTIVE })
} }
// Receive IPC event for LND streaming a line // Receive IPC event for LND streaming a line
@ -98,8 +95,7 @@ const ACTION_HANDLERS = {
}), }),
[RECEIVE_BLOCK]: (state, { lndBlockHeight }) => ({ ...state, lndBlockHeight }), [RECEIVE_BLOCK]: (state, { lndBlockHeight }) => ({ ...state, lndBlockHeight }),
[GRPC_DISCONNECTED]: state => ({ ...state, grpcStarted: false }), [SET_LIGHTNING_WALLET_ACTIVE]: state => ({ ...state, lightningGrpcActive: true })
[GRPC_CONNECTED]: state => ({ ...state, grpcStarted: true })
} }
// ------------------------------------ // ------------------------------------
@ -107,7 +103,7 @@ const ACTION_HANDLERS = {
// ------------------------------------ // ------------------------------------
const initialState = { const initialState = {
syncStatus: 'pending', syncStatus: 'pending',
grpcStarted: false, lightningGrpcActive: false,
blockHeight: 0, blockHeight: 0,
lndBlockHeight: 0 lndBlockHeight: 0
} }

7
app/reducers/onboarding.js

@ -250,7 +250,7 @@ export const startLndError = (event, errors) => (dispatch, getState) => {
} }
// Listener from after the LND walletUnlocker has started // Listener from after the LND walletUnlocker has started
export const walletUnlockerStarted = () => dispatch => { export const walletUnlockerGrpcActive = () => dispatch => {
dispatch({ type: LND_STARTED }) dispatch({ type: LND_STARTED })
ipcRenderer.send('walletUnlocker', { msg: 'genSeed' }) ipcRenderer.send('walletUnlocker', { msg: 'genSeed' })
} }
@ -333,8 +333,8 @@ const ACTION_HANDLERS = {
[CHANGE_STEP]: (state, { step }) => ({ ...state, step, previousStep: state.step }), [CHANGE_STEP]: (state, { step }) => ({ ...state, step, previousStep: state.step }),
[ONBOARDING_STARTED]: state => ({ ...state, onboarded: false }), [ONBOARDING_STARTED]: state => ({ ...state, onboarding: true, onboarded: false }),
[ONBOARDING_FINISHED]: state => ({ ...state, onboarded: true }), [ONBOARDING_FINISHED]: state => ({ ...state, onboarding: false, onboarded: true }),
[STARTING_LND]: state => ({ ...state, startingLnd: true }), [STARTING_LND]: state => ({ ...state, startingLnd: true }),
[LND_STARTED]: state => ({ ...state, startingLnd: false }), [LND_STARTED]: state => ({ ...state, startingLnd: false }),
@ -448,6 +448,7 @@ export { onboardingSelectors }
// Reducer // Reducer
// ------------------------------------ // ------------------------------------
const initialState = { const initialState = {
onboarding: false,
onboarded: false, onboarded: false,
step: 0.1, step: 0.1,
connectionType: store.get('type', 'local'), connectionType: store.get('type', 'local'),

22
app/zap.js

@ -81,7 +81,7 @@ class ZapController {
return setTimeout(async () => { return setTimeout(async () => {
if (mode === 'external') { if (mode === 'external') {
// If lnd is already running, create and subscribe to the Lightning grpc object. // If lnd is already running, create and subscribe to the Lightning grpc object.
await this.startGrpc() await this.startLightningWallet()
this.sendMessage('successfullyCreatedWallet') this.sendMessage('successfullyCreatedWallet')
} else { } else {
// Otherwise, start the onboarding process. // Otherwise, start the onboarding process.
@ -114,8 +114,8 @@ class ZapController {
/** /**
* Create and subscribe to the Lightning grpc object. * Create and subscribe to the Lightning grpc object.
*/ */
async startGrpc() { async startLightningWallet() {
mainLog.info('Starting gRPC...') mainLog.info('Starting lightning wallet...')
const { lndSubscribe, lndMethods } = await lnd.initLnd() const { lndSubscribe, lndMethods } = await lnd.initLnd()
// Subscribe to bi-directional streams // Subscribe to bi-directional streams
@ -126,7 +126,7 @@ class ZapController {
lndMethods(event, msg, data) lndMethods(event, msg, data)
}) })
this.sendMessage('grpcConnected') this.sendMessage('lightningGrpcActive')
} }
/** /**
@ -142,7 +142,7 @@ class ZapController {
walletUnlockerMethods(event, msg, data) walletUnlockerMethods(event, msg, data)
}) })
this.sendMessage('walletUnlockerStarted') this.sendMessage('walletUnlockerGrpcActive')
} catch (error) { } catch (error) {
dialog.showMessageBox({ dialog.showMessageBox({
type: 'error', type: 'error',
@ -174,14 +174,14 @@ class ZapController {
app.quit() app.quit()
}) })
this.neutrino.on('grpc-proxy-started', () => { this.neutrino.on('wallet-unlocker-grpc-active', () => {
mainLog.info('gRPC proxy started') mainLog.info('Wallet unlocker gRPC active')
this.startWalletUnlocker() this.startWalletUnlocker()
}) })
this.neutrino.on('wallet-opened', () => { this.neutrino.on('lightning-grpc-active', () => {
mainLog.info('Wallet opened') mainLog.info('Lightning gRPC active')
this.startGrpc() this.startLightningWallet()
}) })
this.neutrino.on('chain-sync-waiting', () => { this.neutrino.on('chain-sync-waiting', () => {
@ -243,7 +243,7 @@ class ZapController {
mainLog.info(' > host:', cleanOptions.host) mainLog.info(' > host:', cleanOptions.host)
mainLog.info(' > cert:', cleanOptions.cert) mainLog.info(' > cert:', cleanOptions.cert)
mainLog.info(' > macaroon:', cleanOptions.macaroon) mainLog.info(' > macaroon:', cleanOptions.macaroon)
this.startGrpc() this.startLightningWallet()
.then(() => this.sendMessage('successfullyCreatedWallet')) .then(() => this.sendMessage('successfullyCreatedWallet'))
.catch(e => { .catch(e => {
const errors = {} const errors = {}

Loading…
Cancel
Save