no incremental check, optional env
This commit is contained in:
parent
34cebc800e
commit
ad599dd565
2 changed files with 23 additions and 29 deletions
|
|
@ -10,6 +10,17 @@ export const EnvMustBeInteger = (name: string): number => {
|
||||||
}
|
}
|
||||||
return +env
|
return +env
|
||||||
}
|
}
|
||||||
|
export const EnvCanBeInteger = (name: string, defaultValue = 0): number => {
|
||||||
|
const env = process.env[name]
|
||||||
|
if (!env) {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
const envNum = +env
|
||||||
|
if (isNaN(envNum) || !Number.isInteger(envNum)) {
|
||||||
|
throw new Error(`${name} ENV must be an integer number or nothing`);
|
||||||
|
}
|
||||||
|
return envNum
|
||||||
|
}
|
||||||
export const EnvCanBeBoolean = (name: string): boolean => {
|
export const EnvCanBeBoolean = (name: string): boolean => {
|
||||||
const env = process.env[name]
|
const env = process.env[name]
|
||||||
if (!env) return false
|
if (!env) return false
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,17 @@
|
||||||
import { EnvMustBeInteger } from "../helpers/envParser.js";
|
import { EnvCanBeInteger } from "../helpers/envParser.js";
|
||||||
import { getLogger } from "../helpers/logger.js";
|
import { getLogger } from "../helpers/logger.js";
|
||||||
import { LightningHandler } from "./index.js";
|
import { LightningHandler } from "./index.js";
|
||||||
export type WatchdogSettings = {
|
export type WatchdogSettings = {
|
||||||
maxDiffSats: number
|
maxDiffSats: number
|
||||||
maxUpdateDiffSats: number
|
|
||||||
}
|
}
|
||||||
export const LoadWatchdogSettingsFromEnv = (test = false): WatchdogSettings => {
|
export const LoadWatchdogSettingsFromEnv = (test = false): WatchdogSettings => {
|
||||||
return {
|
return {
|
||||||
maxDiffSats: EnvMustBeInteger("WATCHDOG_MAX_DIFF_SATS"),
|
maxDiffSats: EnvCanBeInteger("WATCHDOG_MAX_DIFF_SATS")
|
||||||
maxUpdateDiffSats: EnvMustBeInteger("WATCHDOG_MAX_UPDATE_DIFF_SATS")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class Watchdog {
|
export class Watchdog {
|
||||||
initialLndBalance: number;
|
initialLndBalance: number;
|
||||||
initialUsersBalance: number;
|
initialUsersBalance: number;
|
||||||
lastLndBalance: number;
|
|
||||||
lastUsersBalance: number;
|
|
||||||
lnd: LightningHandler;
|
lnd: LightningHandler;
|
||||||
settings: WatchdogSettings;
|
settings: WatchdogSettings;
|
||||||
log = getLogger({ appName: "watchdog" })
|
log = getLogger({ appName: "watchdog" })
|
||||||
|
|
@ -26,10 +22,7 @@ export class Watchdog {
|
||||||
|
|
||||||
SeedLndBalance = async (totalUsersBalance: number) => {
|
SeedLndBalance = async (totalUsersBalance: number) => {
|
||||||
this.initialLndBalance = await this.getTotalLndBalance()
|
this.initialLndBalance = await this.getTotalLndBalance()
|
||||||
this.lastLndBalance = this.initialLndBalance
|
|
||||||
|
|
||||||
this.initialUsersBalance = totalUsersBalance
|
this.initialUsersBalance = totalUsersBalance
|
||||||
this.lastUsersBalance = this.initialUsersBalance
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getTotalLndBalance = async () => {
|
getTotalLndBalance = async () => {
|
||||||
|
|
@ -37,16 +30,16 @@ export class Watchdog {
|
||||||
return confirmedBalance + channelsBalance.reduce((acc, { localBalanceSats }) => acc + localBalanceSats, 0)
|
return confirmedBalance + channelsBalance.reduce((acc, { localBalanceSats }) => acc + localBalanceSats, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkBalanceUpdate = (deltaLnd: number, deltaUsers: number, type: 'incremental' | 'absolute', threshold: number) => {
|
checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => {
|
||||||
this.log("LND balance update:", deltaLnd, "sats", type === 'incremental' ? "since last balance check" : "since app startup")
|
this.log("LND balance update:", deltaLnd, "sats since app startup")
|
||||||
this.log("Users balance update:", deltaUsers, "sats", type === 'incremental' ? "since last balance check" : "since app startup")
|
this.log("Users balance update:", deltaUsers, "sats since app startup")
|
||||||
|
|
||||||
const result = this.checkDeltas(deltaLnd, deltaUsers)
|
const result = this.checkDeltas(deltaLnd, deltaUsers)
|
||||||
switch (result.type) {
|
switch (result.type) {
|
||||||
case 'mismatch':
|
case 'mismatch':
|
||||||
if (deltaLnd < 0) {
|
if (deltaLnd < 0) {
|
||||||
this.log("WARNING! LND balance decreased while users balance increased creating a difference of", result.absoluteDiff, "sats")
|
this.log("WARNING! LND balance decreased while users balance increased creating a difference of", result.absoluteDiff, "sats")
|
||||||
if (result.absoluteDiff > threshold) {
|
if (result.absoluteDiff > this.settings.maxDiffSats) {
|
||||||
this.log("Difference is too big for an update, locking outgoing operations")
|
this.log("Difference is too big for an update, locking outgoing operations")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -58,7 +51,7 @@ export class Watchdog {
|
||||||
case 'negative':
|
case 'negative':
|
||||||
if (Math.abs(deltaLnd) > Math.abs(deltaUsers)) {
|
if (Math.abs(deltaLnd) > Math.abs(deltaUsers)) {
|
||||||
this.log("WARNING! LND balance decreased more than users balance with a difference of", result.absoluteDiff, "sats")
|
this.log("WARNING! LND balance decreased more than users balance with a difference of", result.absoluteDiff, "sats")
|
||||||
if (result.absoluteDiff > threshold) {
|
if (result.absoluteDiff > this.settings.maxDiffSats) {
|
||||||
this.log("Difference is too big for an update, locking outgoing operations")
|
this.log("Difference is too big for an update, locking outgoing operations")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -70,7 +63,7 @@ export class Watchdog {
|
||||||
case 'positive':
|
case 'positive':
|
||||||
if (deltaLnd < deltaUsers) {
|
if (deltaLnd < deltaUsers) {
|
||||||
this.log("WARNING! LND balance increased less than users balance with a difference of", result.absoluteDiff, "sats")
|
this.log("WARNING! LND balance increased less than users balance with a difference of", result.absoluteDiff, "sats")
|
||||||
if (result.absoluteDiff > threshold) {
|
if (result.absoluteDiff > this.settings.maxDiffSats) {
|
||||||
this.log("Difference is too big for an update, locking outgoing operations")
|
this.log("Difference is too big for an update, locking outgoing operations")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -85,24 +78,14 @@ export class Watchdog {
|
||||||
PaymentRequested = async (totalUsersBalance: number) => {
|
PaymentRequested = async (totalUsersBalance: number) => {
|
||||||
this.log("Payment requested, checking balance")
|
this.log("Payment requested, checking balance")
|
||||||
const totalLndBalance = await this.getTotalLndBalance()
|
const totalLndBalance = await this.getTotalLndBalance()
|
||||||
const IncDeltaLnd = totalLndBalance - this.lastLndBalance
|
const deltaLnd = totalLndBalance - this.initialLndBalance
|
||||||
const IncDeltaUsers = totalUsersBalance - this.lastUsersBalance
|
const deltaUsers = totalUsersBalance - this.initialUsersBalance
|
||||||
const denyIncremental = this.checkBalanceUpdate(IncDeltaLnd, IncDeltaUsers, 'incremental', this.settings.maxUpdateDiffSats)
|
const deny = this.checkBalanceUpdate(deltaLnd, deltaUsers)
|
||||||
if (denyIncremental) {
|
if (deny) {
|
||||||
this.log("Balance mismatch detected in incremental update, locking outgoing operations")
|
|
||||||
this.lnd.LockOutgoingOperations()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
const AbsDeltaLnd = totalLndBalance - this.initialLndBalance
|
|
||||||
const AbsDeltaUsers = totalUsersBalance - this.initialUsersBalance
|
|
||||||
const denyAbsolute = this.checkBalanceUpdate(AbsDeltaLnd, AbsDeltaUsers, 'absolute', this.settings.maxDiffSats)
|
|
||||||
if (denyAbsolute) {
|
|
||||||
this.log("Balance mismatch detected in absolute update, locking outgoing operations")
|
this.log("Balance mismatch detected in absolute update, locking outgoing operations")
|
||||||
this.lnd.LockOutgoingOperations()
|
this.lnd.LockOutgoingOperations()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
this.lastLndBalance = totalLndBalance
|
|
||||||
this.lastUsersBalance = totalUsersBalance
|
|
||||||
}
|
}
|
||||||
|
|
||||||
checkDeltas = (deltaLnd: number, deltaUsers: number): DeltaCheckResult => {
|
checkDeltas = (deltaLnd: number, deltaUsers: number): DeltaCheckResult => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue