|
|
@ -5,17 +5,19 @@ import config from '../config' |
|
|
|
import { mainLog, lndLog, lndLogGetLevel } from '../../utils/log' |
|
|
|
import { fetchBlockHeight } from './util' |
|
|
|
|
|
|
|
// Sync status is currenty pending.
|
|
|
|
const NEUTRINO_SYNC_STATUS_PENDING = 'chain-sync-pending' |
|
|
|
|
|
|
|
// Waiting for chain backend to finish synchronizing.
|
|
|
|
const NEUTRINO_SYNC_STATUS_WAITING = 'chain-sync-waiting' |
|
|
|
|
|
|
|
// Initial sync is currently in progress.
|
|
|
|
const NEUTRINO_SYNC_STATUS_IN_PROGRESS = 'chain-sync-started' |
|
|
|
|
|
|
|
// Initial sync has completed.
|
|
|
|
const NEUTRINO_SYNC_STATUS_COMPLETE = 'chain-sync-finished' |
|
|
|
// Sync statuses
|
|
|
|
const CHAIN_SYNC_PENDING = 'chain-sync-pending' |
|
|
|
const CHAIN_SYNC_WAITING = 'chain-sync-waiting' |
|
|
|
const CHAIN_SYNC_IN_PROGRESS = 'chain-sync-started' |
|
|
|
const CHAIN_SYNC_COMPLETE = 'chain-sync-finished' |
|
|
|
|
|
|
|
// Events
|
|
|
|
const ERROR = 'error' |
|
|
|
const CLOSE = 'close' |
|
|
|
const WALLET_UNLOCKER_GRPC_ACTIVE = 'wallet-unlocker-grpc-active' |
|
|
|
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. |
|
|
@ -27,9 +29,9 @@ class Neutrino extends EventEmitter { |
|
|
|
this.alias = alias |
|
|
|
this.autopilot = autopilot |
|
|
|
this.process = null |
|
|
|
this.grpcProxyStarted = false |
|
|
|
this.walletOpened = false |
|
|
|
this.chainSyncStatus = NEUTRINO_SYNC_STATUS_PENDING |
|
|
|
this.walletUnlockerGrpcActive = false |
|
|
|
this.lightningGrpcActive = false |
|
|
|
this.chainSyncStatus = CHAIN_SYNC_PENDING |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -56,9 +58,9 @@ class Neutrino extends EventEmitter { |
|
|
|
] |
|
|
|
|
|
|
|
this.process = spawn(lndConfig.lndPath, neutrinoArgs) |
|
|
|
.on('error', error => this.emit('error', error)) |
|
|
|
.on('error', error => this.emit(ERROR, error)) |
|
|
|
.on('close', code => { |
|
|
|
this.emit('close', code) |
|
|
|
this.emit(CLOSE, code) |
|
|
|
this.process = null |
|
|
|
}) |
|
|
|
|
|
|
@ -75,55 +77,55 @@ class Neutrino extends EventEmitter { |
|
|
|
lndLog[lndLogGetLevel(line)](line) |
|
|
|
} |
|
|
|
|
|
|
|
// gRPC started.
|
|
|
|
if (!this.grpcProxyStarted) { |
|
|
|
if (line.includes('gRPC proxy started') && line.includes('password')) { |
|
|
|
this.grpcProxyStarted = true |
|
|
|
this.emit('grpc-proxy-started') |
|
|
|
// password RPC server listening (wallet unlocker started).
|
|
|
|
if (!this.walletUnlockerGrpcActive && !this.lightningGrpcActive) { |
|
|
|
if (line.includes('RPC server listening on') && line.includes('password')) { |
|
|
|
this.walletUnlockerGrpcActive = true |
|
|
|
this.emit(WALLET_UNLOCKER_GRPC_ACTIVE) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Wallet opened.
|
|
|
|
if (!this.walletOpened) { |
|
|
|
if (line.includes('gRPC proxy started') && !line.includes('password')) { |
|
|
|
this.walletOpened = true |
|
|
|
this.emit('wallet-opened') |
|
|
|
// RPC server listening (wallet unlocked).
|
|
|
|
if (!this.lightningGrpcActive) { |
|
|
|
if (line.includes('RPC server listening on') && !line.includes('password')) { |
|
|
|
this.lightningGrpcActive = true |
|
|
|
this.emit(LIGHTNING_GRPC_ACTIVE) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// If the sync has already completed then we don't need to do anythibng else.
|
|
|
|
if (this.is(NEUTRINO_SYNC_STATUS_COMPLETE)) { |
|
|
|
// If the sync has already completed then we don't need to do anything else.
|
|
|
|
if (this.is(CHAIN_SYNC_COMPLETE)) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
// 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 ( |
|
|
|
line.includes('No sync peer candidates available') || |
|
|
|
line.includes('Unable to synchronize wallet to chain') || |
|
|
|
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.
|
|
|
|
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+)/) |
|
|
|
if (match) { |
|
|
|
// 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.
|
|
|
|
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
|
|
|
|
// 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.
|
|
|
|
fetchBlockHeight() |
|
|
|
.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.
|
|
|
|
.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.
|
|
|
|
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 match |
|
|
|
|
|
|
@ -144,13 +146,13 @@ class Neutrino extends EventEmitter { |
|
|
|
} |
|
|
|
|
|
|
|
if (height) { |
|
|
|
this.setState(NEUTRINO_SYNC_STATUS_IN_PROGRESS) |
|
|
|
this.emit('got-lnd-block-height', height) |
|
|
|
this.setState(CHAIN_SYNC_IN_PROGRESS) |
|
|
|
this.emit(GOT_LND_BLOCK_HEIGHT, height) |
|
|
|
} |
|
|
|
|
|
|
|
// Lnd syncing has completed.
|
|
|
|
if (line.includes('Chain backend is fully synced')) { |
|
|
|
this.setState(NEUTRINO_SYNC_STATUS_COMPLETE) |
|
|
|
this.setState(CHAIN_SYNC_COMPLETE) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|