no incremental check, optional env

This commit is contained in:
boufni95 2024-03-26 19:52:24 +01:00
parent 34cebc800e
commit ad599dd565
2 changed files with 23 additions and 29 deletions

View file

@ -10,6 +10,17 @@ export const EnvMustBeInteger = (name: string): number => {
}
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 => {
const env = process.env[name]
if (!env) return false

View file

@ -1,21 +1,17 @@
import { EnvMustBeInteger } from "../helpers/envParser.js";
import { EnvCanBeInteger } from "../helpers/envParser.js";
import { getLogger } from "../helpers/logger.js";
import { LightningHandler } from "./index.js";
export type WatchdogSettings = {
maxDiffSats: number
maxUpdateDiffSats: number
}
export const LoadWatchdogSettingsFromEnv = (test = false): WatchdogSettings => {
return {
maxDiffSats: EnvMustBeInteger("WATCHDOG_MAX_DIFF_SATS"),
maxUpdateDiffSats: EnvMustBeInteger("WATCHDOG_MAX_UPDATE_DIFF_SATS")
maxDiffSats: EnvCanBeInteger("WATCHDOG_MAX_DIFF_SATS")
}
}
export class Watchdog {
initialLndBalance: number;
initialUsersBalance: number;
lastLndBalance: number;
lastUsersBalance: number;
lnd: LightningHandler;
settings: WatchdogSettings;
log = getLogger({ appName: "watchdog" })
@ -26,10 +22,7 @@ export class Watchdog {
SeedLndBalance = async (totalUsersBalance: number) => {
this.initialLndBalance = await this.getTotalLndBalance()
this.lastLndBalance = this.initialLndBalance
this.initialUsersBalance = totalUsersBalance
this.lastUsersBalance = this.initialUsersBalance
}
getTotalLndBalance = async () => {
@ -37,16 +30,16 @@ export class Watchdog {
return confirmedBalance + channelsBalance.reduce((acc, { localBalanceSats }) => acc + localBalanceSats, 0)
}
checkBalanceUpdate = (deltaLnd: number, deltaUsers: number, type: 'incremental' | 'absolute', threshold: number) => {
this.log("LND balance update:", deltaLnd, "sats", type === 'incremental' ? "since last balance check" : "since app startup")
this.log("Users balance update:", deltaUsers, "sats", type === 'incremental' ? "since last balance check" : "since app startup")
checkBalanceUpdate = (deltaLnd: number, deltaUsers: number) => {
this.log("LND balance update:", deltaLnd, "sats since app startup")
this.log("Users balance update:", deltaUsers, "sats since app startup")
const result = this.checkDeltas(deltaLnd, deltaUsers)
switch (result.type) {
case 'mismatch':
if (deltaLnd < 0) {
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")
return true
}
@ -58,7 +51,7 @@ export class Watchdog {
case 'negative':
if (Math.abs(deltaLnd) > Math.abs(deltaUsers)) {
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")
return true
}
@ -70,7 +63,7 @@ export class Watchdog {
case 'positive':
if (deltaLnd < deltaUsers) {
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")
return true
}
@ -85,24 +78,14 @@ export class Watchdog {
PaymentRequested = async (totalUsersBalance: number) => {
this.log("Payment requested, checking balance")
const totalLndBalance = await this.getTotalLndBalance()
const IncDeltaLnd = totalLndBalance - this.lastLndBalance
const IncDeltaUsers = totalUsersBalance - this.lastUsersBalance
const denyIncremental = this.checkBalanceUpdate(IncDeltaLnd, IncDeltaUsers, 'incremental', this.settings.maxUpdateDiffSats)
if (denyIncremental) {
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) {
const deltaLnd = totalLndBalance - this.initialLndBalance
const deltaUsers = totalUsersBalance - this.initialUsersBalance
const deny = this.checkBalanceUpdate(deltaLnd, deltaUsers)
if (deny) {
this.log("Balance mismatch detected in absolute update, locking outgoing operations")
this.lnd.LockOutgoingOperations()
return
}
this.lastLndBalance = totalLndBalance
this.lastUsersBalance = totalUsersBalance
}
checkDeltas = (deltaLnd: number, deltaUsers: number): DeltaCheckResult => {