fix watchdog
This commit is contained in:
parent
c2542c1ef9
commit
a42446d45a
2 changed files with 27 additions and 11 deletions
|
|
@ -24,7 +24,7 @@ export class LiquidityProvider {
|
||||||
invoicePaidCb: InvoicePaidCb
|
invoicePaidCb: InvoicePaidCb
|
||||||
connecting = false
|
connecting = false
|
||||||
readyInterval: NodeJS.Timeout
|
readyInterval: NodeJS.Timeout
|
||||||
queue: ((usable: boolean) => void)[] = []
|
queue: ((state: 'ready') => void)[] = []
|
||||||
// make the sub process accept client
|
// make the sub process accept client
|
||||||
constructor(pubDestination: string, invoicePaidCb: InvoicePaidCb) {
|
constructor(pubDestination: string, invoicePaidCb: InvoicePaidCb) {
|
||||||
if (!pubDestination) {
|
if (!pubDestination) {
|
||||||
|
|
@ -51,14 +51,14 @@ export class LiquidityProvider {
|
||||||
return this.pubDestination
|
return this.pubDestination
|
||||||
}
|
}
|
||||||
|
|
||||||
AwaitProviderReady = async (): Promise<boolean> => {
|
AwaitProviderReady = async (): Promise<'inactive' | 'ready'> => {
|
||||||
if (!this.pubDestination) {
|
if (!this.pubDestination) {
|
||||||
return false
|
return 'inactive'
|
||||||
}
|
}
|
||||||
if (this.latestMaxWithdrawable !== null) {
|
if (this.latestMaxWithdrawable !== null) {
|
||||||
return true
|
return 'ready'
|
||||||
}
|
}
|
||||||
return new Promise<boolean>(res => {
|
return new Promise<'ready'>(res => {
|
||||||
this.queue.push(res)
|
this.queue.push(res)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +74,7 @@ export class LiquidityProvider {
|
||||||
if (this.latestMaxWithdrawable === null) {
|
if (this.latestMaxWithdrawable === null) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.queue.forEach(q => q(true))
|
this.queue.forEach(q => q('ready'))
|
||||||
this.log("subbing to user operations")
|
this.log("subbing to user operations")
|
||||||
this.client.GetLiveUserOperations(res => {
|
this.client.GetLiveUserOperations(res => {
|
||||||
console.log("got user operation", res)
|
console.log("got user operation", res)
|
||||||
|
|
|
||||||
|
|
@ -41,11 +41,27 @@ export class Watchdog {
|
||||||
clearInterval(this.interval)
|
clearInterval(this.interval)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Start = async () => {
|
Start = async () => {
|
||||||
|
const result = await Promise.race([
|
||||||
|
this.liquidProvider.AwaitProviderReady(),
|
||||||
|
new Promise<'failed'>((res, rej) => {
|
||||||
|
setTimeout(() => {
|
||||||
|
this.log("Provider did not become ready in time, starting without it")
|
||||||
|
res('failed')
|
||||||
|
}, 5000)
|
||||||
|
})
|
||||||
|
])
|
||||||
|
|
||||||
|
let providerBalance = 0
|
||||||
|
if (result === 'ready') {
|
||||||
|
providerBalance = await this.liquidProvider.GetLatestBalance()
|
||||||
|
}
|
||||||
|
await this.StartWatching(providerBalance)
|
||||||
|
}
|
||||||
|
StartWatching = async (providerBalance: number) => {
|
||||||
this.startedAtUnix = Math.floor(Date.now() / 1000)
|
this.startedAtUnix = Math.floor(Date.now() / 1000)
|
||||||
const totalUsersBalance = await this.storage.paymentStorage.GetTotalUsersBalance()
|
const totalUsersBalance = await this.storage.paymentStorage.GetTotalUsersBalance()
|
||||||
this.initialLndBalance = await this.getTotalLndBalance(totalUsersBalance)
|
this.initialLndBalance = await this.getTotalLndBalance(totalUsersBalance, providerBalance)
|
||||||
this.initialUsersBalance = totalUsersBalance
|
this.initialUsersBalance = totalUsersBalance
|
||||||
const fwEvents = await this.lnd.GetForwardingHistory(0, this.startedAtUnix)
|
const fwEvents = await this.lnd.GetForwardingHistory(0, this.startedAtUnix)
|
||||||
this.latestIndexOffset = fwEvents.lastOffsetIndex
|
this.latestIndexOffset = fwEvents.lastOffsetIndex
|
||||||
|
|
@ -72,14 +88,13 @@ export class Watchdog {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
getTotalLndBalance = async (usersTotal: number) => {
|
getTotalLndBalance = async (usersTotal: number, providerBalance: number) => {
|
||||||
const walletBalance = await this.lnd.GetWalletBalance()
|
const walletBalance = await this.lnd.GetWalletBalance()
|
||||||
this.log(Number(walletBalance.confirmedBalance), "sats in chain wallet")
|
this.log(Number(walletBalance.confirmedBalance), "sats in chain wallet")
|
||||||
const channelsBalance = await this.lnd.GetChannelBalance()
|
const channelsBalance = await this.lnd.GetChannelBalance()
|
||||||
getLogger({ component: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal, f: this.accumulatedHtlcFees })
|
getLogger({ component: "debugLndBalancev3" })({ w: walletBalance, c: channelsBalance, u: usersTotal, f: this.accumulatedHtlcFees })
|
||||||
const totalLightningBalanceMsats = (channelsBalance.localBalance?.msat || 0n) + (channelsBalance.unsettledLocalBalance?.msat || 0n)
|
const totalLightningBalanceMsats = (channelsBalance.localBalance?.msat || 0n) + (channelsBalance.unsettledLocalBalance?.msat || 0n)
|
||||||
const totalLightningBalance = Math.ceil(Number(totalLightningBalanceMsats) / 1000)
|
const totalLightningBalance = Math.ceil(Number(totalLightningBalanceMsats) / 1000)
|
||||||
const providerBalance = await this.liquidProvider.GetLatestBalance()
|
|
||||||
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance
|
return Number(walletBalance.confirmedBalance) + totalLightningBalance + providerBalance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -138,7 +153,8 @@ export class Watchdog {
|
||||||
this.latestCheckStart = Date.now()
|
this.latestCheckStart = Date.now()
|
||||||
await this.updateAccumulatedHtlcFees()
|
await this.updateAccumulatedHtlcFees()
|
||||||
const totalUsersBalance = await this.storage.paymentStorage.GetTotalUsersBalance()
|
const totalUsersBalance = await this.storage.paymentStorage.GetTotalUsersBalance()
|
||||||
const totalLndBalance = await this.getTotalLndBalance(totalUsersBalance)
|
const providerBalance = await this.liquidProvider.GetLatestBalance()
|
||||||
|
const totalLndBalance = await this.getTotalLndBalance(totalUsersBalance, providerBalance)
|
||||||
const deltaLnd = totalLndBalance - (this.initialLndBalance + this.accumulatedHtlcFees)
|
const deltaLnd = totalLndBalance - (this.initialLndBalance + this.accumulatedHtlcFees)
|
||||||
const deltaUsers = totalUsersBalance - this.initialUsersBalance
|
const deltaUsers = totalUsersBalance - this.initialUsersBalance
|
||||||
const deny = this.checkBalanceUpdate(deltaLnd, deltaUsers)
|
const deny = this.checkBalanceUpdate(deltaLnd, deltaUsers)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue