From 97ec3ea3bbb1c7cfb1f231d36b947ac109d8c743 Mon Sep 17 00:00:00 2001 From: hatim boufnichel Date: Fri, 26 Apr 2024 17:10:07 +0200 Subject: [PATCH] cleanup --- src/services/lnd/index.ts | 46 +-------- src/services/lnd/mock.ts | 145 ---------------------------- src/services/main/index.ts | 6 +- src/services/main/paymentManager.ts | 6 +- src/services/main/sanityChecker.ts | 6 +- src/services/main/watchdog.ts | 6 +- src/services/metrics/index.ts | 6 +- src/tests/testBase.ts | 1 - 8 files changed, 16 insertions(+), 206 deletions(-) delete mode 100644 src/services/lnd/mock.ts diff --git a/src/services/lnd/index.ts b/src/services/lnd/index.ts index ad18f505f..9b15a9220 100644 --- a/src/services/lnd/index.ts +++ b/src/services/lnd/index.ts @@ -1,10 +1,5 @@ -import * as Types from '../../../proto/autogenerated/ts/types.js' -import { GetInfoResponse, NewAddressResponse, AddInvoiceResponse, PayReq, Payment, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse, WalletBalanceResponse } from '../../../proto/lnd/lightning.js' import { EnvMustBeNonEmptyString, EnvMustBeInteger, EnvCanBeBoolean } from '../helpers/envParser.js' -import { AddressPaidCb, BalanceInfo, DecodedInvoice, HtlcCb, Invoice, InvoicePaidCb, LndSettings, NewBlockCb, NodeInfo, PaidInvoice } from './settings.js' -import LND from './lnd.js' -import MockLnd from './mock.js' -import { getLogger } from '../helpers/logger.js' +import { LndSettings } from './settings.js' export const LoadLndSettingsFromEnv = (): LndSettings => { const lndAddr = EnvMustBeNonEmptyString("LND_ADDRESS") const lndCertPath = EnvMustBeNonEmptyString("LND_CERT_PATH") @@ -14,42 +9,3 @@ export const LoadLndSettingsFromEnv = (): LndSettings => { const mockLnd = EnvCanBeBoolean("MOCK_LND") return { mainNode: { lndAddr, lndCertPath, lndMacaroonPath }, feeRateLimit, feeFixedLimit, mockLnd } } -export interface LightningHandler { - Stop(): void - Warmup(): Promise - GetInfo(): Promise - Health(): Promise - NewAddress(addressType: Types.AddressType): Promise - NewInvoice(value: number, memo: string, expiry: number): Promise - DecodeInvoice(paymentRequest: string): Promise - GetFeeLimitAmount(amount: number): number - GetMaxWithinLimit(amount: number): number - PayInvoice(invoice: string, amount: number, feeLimit: number): Promise - EstimateChainFees(address: string, amount: number, targetConf: number): Promise - PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise - //OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise - SetMockInvoiceAsPaid(invoice: string, amount: number): Promise - ChannelBalance(): Promise<{ local: number, remote: number }> - GetTransactions(startHeight: number): Promise - GetBalance(): Promise - GetWalletBalance(): Promise - GetChannelBalance(): Promise - ListClosedChannels(): Promise - ListChannels(): Promise - ListPendingChannels(): Promise - GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]> - GetAllPaidInvoices(max: number): Promise - GetAllPayments(max: number): Promise - LockOutgoingOperations(): void - UnlockOutgoingOperations(): void -} - -export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb): LightningHandler => { - if (settings.mockLnd) { - getLogger({})("registering mock lnd handler") - return new MockLnd(settings, addressPaidCb, invoicePaidCb, newBlockCb) - } else { - getLogger({})("registering prod lnd handler") - return new LND(settings, addressPaidCb, invoicePaidCb, newBlockCb, htlcCb) - } -} \ No newline at end of file diff --git a/src/services/lnd/mock.ts b/src/services/lnd/mock.ts deleted file mode 100644 index eb9a2daba..000000000 --- a/src/services/lnd/mock.ts +++ /dev/null @@ -1,145 +0,0 @@ -//const grpc = require('@grpc/grpc-js'); -import { credentials, Metadata } from '@grpc/grpc-js' -import { GrpcTransport } from "@protobuf-ts/grpc-transport"; -import fs from 'fs' -import crypto from 'crypto' -import * as Types from '../../../proto/autogenerated/ts/types.js' -import { LightningClient } from '../../../proto/lnd/lightning.client.js' -import { InvoicesClient } from '../../../proto/lnd/invoices.client.js' -import { RouterClient } from '../../../proto/lnd/router.client.js' -import { GetInfoResponse, AddressType, NewAddressResponse, AddInvoiceResponse, Invoice_InvoiceState, PayReq, Payment_PaymentStatus, Payment, PaymentFailureReason, SendCoinsResponse, EstimateFeeResponse, TransactionDetails, ClosedChannelsResponse, ListChannelsResponse, PendingChannelsResponse, ListInvoiceResponse, ListPaymentsResponse, ChannelBalanceResponse, WalletBalanceResponse } from '../../../proto/lnd/lightning.js' -import { OpenChannelReq } from './openChannelReq.js'; -import { AddInvoiceReq } from './addInvoiceReq.js'; -import { PayInvoiceReq } from './payInvoiceReq.js'; -import { SendCoinsReq } from './sendCoinsReq.js'; -import { LndSettings, AddressPaidCb, InvoicePaidCb, NodeInfo, Invoice, DecodedInvoice, PaidInvoice, NewBlockCb, BalanceInfo } from './settings.js'; -import { getLogger } from '../helpers/logger.js'; - -export default class { - invoicesAwaiting: Record = {} - settings: LndSettings - abortController = new AbortController() - addressPaidCb: AddressPaidCb - invoicePaidCb: InvoicePaidCb - constructor(settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb) { - this.settings = settings - this.addressPaidCb = addressPaidCb - this.invoicePaidCb = invoicePaidCb - } - - async SetMockInvoiceAsPaid(invoice: string, amount: number): Promise { - const decoded = await this.DecodeInvoice(invoice) - if (decoded.numSatoshis && amount) { - throw new Error("non zero amount provided to pay invoice but invoice has value already") - } - this.invoicePaidCb(invoice, decoded.numSatoshis || amount, false) - delete this.invoicesAwaiting[invoice] - } - GetChannelBalance(): Promise { - throw new Error("Method not implemented."); - } - Stop() { } - async Warmup() { } - - async GetWalletBalance(): Promise { throw new Error("ListClosedChannels disabled in mock mode") } - async ListClosedChannels(): Promise { throw new Error("ListClosedChannels disabled in mock mode") } - async ListChannels(): Promise { throw new Error("ListChannels disabled in mock mode") } - async ListPendingChannels(): Promise { throw new Error("ListPendingChannels disabled in mock mode") } - async GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]> { throw new Error("GetForwardingHistory disabled in mock mode") } - - async GetInfo(): Promise { - return { alias: "mock", syncedToChain: true, syncedToGraph: true, blockHeight: 1, blockHash: "", identityPubkey: "mock", uris: [] } - } - - async Health(): Promise { } - - async NewAddress(addressType: Types.AddressType): Promise { - throw new Error("NewAddress disabled in mock mode") - } - - async NewInvoice(value: number, memo: string, expiry: number): Promise { - const mockInvoice = "lnbcrtmockin" + crypto.randomBytes(32).toString('hex') - this.invoicesAwaiting[mockInvoice] = { value, memo, expiryUnix: expiry + Date.now() / 1000 } - return { payRequest: mockInvoice } - } - - async DecodeInvoice(paymentRequest: string): Promise { - if (paymentRequest.startsWith('lnbcrtmockout')) { - const amt = this.decodeOutboundInvoice(paymentRequest) - return { numSatoshis: amt, paymentHash: paymentRequest } - } - const i = this.invoicesAwaiting[paymentRequest] - if (!i) { - throw new Error("invoice not found") - } - return { numSatoshis: i.value, paymentHash: paymentRequest } - } - - GetFeeLimitAmount(amount: number): number { - return Math.ceil(amount * this.settings.feeRateLimit + this.settings.feeFixedLimit); - } - - GetMaxWithinLimit(amount: number): number { - return Math.max(0, Math.floor(amount * (1 - this.settings.feeRateLimit) - this.settings.feeFixedLimit)) - } - - decodeOutboundInvoice(invoice: string): number { - if (!invoice.startsWith('lnbcrtmockout')) { - throw new Error("invalid mock invoice provided for payment") - } - const amt = invoice.substring('lnbcrtmockout'.length).split("__")[0] - if (isNaN(+amt)) { - throw new Error("invalid mock invoice provided for payment") - } - return +amt - } - - async PayInvoice(invoice: string, amount: number, feeLimit: number): Promise { - const log = getLogger({}) - log('payng', invoice) - await new Promise(res => setTimeout(res, 200)) - const amt = this.decodeOutboundInvoice(invoice) - log('paid', invoice) - return { feeSat: 1, paymentPreimage: "all_good", valueSat: amt || amount } - } - - async ChannelBalance(): Promise<{ local: number, remote: number }> { - return { local: 100 * 1000 * 1000, remote: 100 * 1000 * 1000 } - } - - async EstimateChainFees(address: string, amount: number, targetConf: number): Promise { - throw new Error("EstimateChainFees disabled in mock mode") - } - - async PayAddress(address: string, amount: number, satPerVByte: number, label = ""): Promise { - throw new Error("PayAddress disabled in mock mode") - } - - - async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise { - throw new Error("OpenChannel disabled in mock mode") - } - - async GetTransactions(startHeight: number): Promise { - throw new Error("GetTransactions disabled in mock mode") - } - - GetBalance(): Promise { - throw new Error("GetBalance disabled in mock mode") - } - - async GetAllPaidInvoices(max: number): Promise { - throw new Error("not implemented") - } - async GetAllPayments(max: number): Promise { - throw new Error("not implemented") - } - LockOutgoingOperations() { - throw new Error("not implemented") - } - UnlockOutgoingOperations() { - throw new Error("not implemented") - } -} - - diff --git a/src/services/main/index.ts b/src/services/main/index.ts index bf0bb05c7..ef33a17c5 100644 --- a/src/services/main/index.ts +++ b/src/services/main/index.ts @@ -5,7 +5,7 @@ import ProductManager from './productManager.js' import ApplicationManager from './applicationManager.js' import PaymentManager, { PendingTx } from './paymentManager.js' import { MainSettings } from './settings.js' -import NewLightningHandler, { LightningHandler } from "../lnd/index.js" +import LND from "../lnd/lnd.js" import { AddressPaidCb, HtlcCb, InvoicePaidCb, NewBlockCb } from "../lnd/settings.js" import { getLogger, PubLogger } from "../helpers/logger.js" import AppUserManager from "./appUserManager.js" @@ -26,7 +26,7 @@ type UserOperationsSub = { export default class { storage: Storage - lnd: LightningHandler + lnd: LND settings: MainSettings userOperationsSub: UserOperationsSub | null = null productManager: ProductManager @@ -41,7 +41,7 @@ export default class { this.settings = settings this.storage = storage - this.lnd = NewLightningHandler(settings.lndSettings, this.addressPaidCb, this.invoicePaidCb, this.newBlockCb, this.htlcCb) + this.lnd = new LND(settings.lndSettings, this.addressPaidCb, this.invoicePaidCb, this.newBlockCb, this.htlcCb) this.metricsManager = new MetricsManager(this.storage, this.lnd) this.paymentManager = new PaymentManager(this.storage, this.lnd, this.settings, this.addressPaidCb, this.invoicePaidCb) diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index 9f462e3b6..7e629fb00 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -4,7 +4,7 @@ import Storage from '../storage/index.js' import * as Types from '../../../proto/autogenerated/ts/types.js' import { MainSettings } from './settings.js' import { InboundOptionals, defaultInvoiceExpiry } from '../storage/paymentStorage.js' -import { LightningHandler } from '../lnd/index.js' +import LND from '../lnd/lnd.js' import { Application } from '../storage/entity/Application.js' import { getLogger } from '../helpers/logger.js' import { UserReceivingAddress } from '../storage/entity/UserReceivingAddress.js' @@ -42,12 +42,12 @@ export default class { storage: Storage settings: MainSettings - lnd: LightningHandler + lnd: LND addressPaidCb: AddressPaidCb invoicePaidCb: InvoicePaidCb log = getLogger({ appName: "PaymentManager" }) watchDog: Watchdog - constructor(storage: Storage, lnd: LightningHandler, settings: MainSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb) { + constructor(storage: Storage, lnd: LND, settings: MainSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb) { this.storage = storage this.settings = settings this.lnd = lnd diff --git a/src/services/main/sanityChecker.ts b/src/services/main/sanityChecker.ts index c69516730..73d94d6f1 100644 --- a/src/services/main/sanityChecker.ts +++ b/src/services/main/sanityChecker.ts @@ -1,5 +1,5 @@ import Storage from '../storage/index.js' -import { LightningHandler } from "../lnd/index.js" +import LND from "../lnd/lnd.js" import { LoggedEvent } from '../storage/eventsLog.js' import { Invoice, Payment } from '../../../proto/lnd/lightning'; import { getLogger } from '../helpers/logger.js'; @@ -12,7 +12,7 @@ type Reason = UniqueDecrementReasons | UniqueIncrementReasons | CommonReasons const incrementTwiceAllowed = ['fees', 'ban'] export default class SanityChecker { storage: Storage - lnd: LightningHandler + lnd: LND events: LoggedEvent[] = [] invoices: Invoice[] = [] @@ -22,7 +22,7 @@ export default class SanityChecker { decrementEvents: Record = {} log = getLogger({ appName: "SanityChecker" }) users: Record = {} - constructor(storage: Storage, lnd: LightningHandler) { + constructor(storage: Storage, lnd: LND) { this.storage = storage this.lnd = lnd } diff --git a/src/services/main/watchdog.ts b/src/services/main/watchdog.ts index b367a24de..b1f2618ee 100644 --- a/src/services/main/watchdog.ts +++ b/src/services/main/watchdog.ts @@ -1,6 +1,6 @@ import { EnvCanBeInteger } from "../helpers/envParser.js"; import { getLogger } from "../helpers/logger.js"; -import { LightningHandler } from "../lnd/index.js"; +import LND from "../lnd/lnd.js"; import { ChannelBalance } from "../lnd/settings.js"; import Storage from '../storage/index.js' export type WatchdogSettings = { @@ -15,14 +15,14 @@ export class Watchdog { initialLndBalance: number; initialUsersBalance: number; - lnd: LightningHandler; + lnd: LND; settings: WatchdogSettings; storage: Storage; latestCheckStart = 0 log = getLogger({ appName: "watchdog" }) enabled = false interval: NodeJS.Timer; - constructor(settings: WatchdogSettings, lnd: LightningHandler, storage: Storage) { + constructor(settings: WatchdogSettings, lnd: LND, storage: Storage) { this.lnd = lnd; this.settings = settings; this.storage = storage; diff --git a/src/services/metrics/index.ts b/src/services/metrics/index.ts index 219e0cf29..0e1f95533 100644 --- a/src/services/metrics/index.ts +++ b/src/services/metrics/index.ts @@ -5,15 +5,15 @@ import { HtlcEvent, HtlcEvent_EventType } from '../../../proto/lnd/router.js' import { BalanceInfo } from '../lnd/settings.js' import { BalanceEvent } from '../storage/entity/BalanceEvent.js' import { ChannelBalanceEvent } from '../storage/entity/ChannelsBalanceEvent.js' -import { LightningHandler } from '../lnd/index.js' +import LND from '../lnd/lnd.js' import HtlcTracker from './htlcTracker.js' const maxEvents = 100_000 export default class Handler { storage: Storage - lnd: LightningHandler + lnd: LND htlcTracker: HtlcTracker metrics: Types.UsageMetric[] = [] - constructor(storage: Storage, lnd: LightningHandler) { + constructor(storage: Storage, lnd: LND) { this.storage = storage this.lnd = lnd this.htlcTracker = new HtlcTracker(this.storage) diff --git a/src/tests/testBase.ts b/src/tests/testBase.ts index a68d5376d..236de4c72 100644 --- a/src/tests/testBase.ts +++ b/src/tests/testBase.ts @@ -9,7 +9,6 @@ import chaiString from 'chai-string' import { defaultInvoiceExpiry } from '../services/storage/paymentStorage.js' import SanityChecker from '../services/main/sanityChecker.js' import LND from '../services/lnd/lnd.js' -import { LightningHandler } from '../services/lnd/index.js' chai.use(chaiString) export const expect = chai.expect export type Describe = (message: string, failure?: boolean) => void