Skip to content

Commit

Permalink
Merge pull request #677 from shocknet/cleanup-ln-interface
Browse files Browse the repository at this point in the history
cleanup
  • Loading branch information
boufni95 authored Apr 26, 2024
2 parents eda5b2e + 97ec3ea commit 5f65a74
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 206 deletions.
46 changes: 1 addition & 45 deletions src/services/lnd/index.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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<void>
GetInfo(): Promise<NodeInfo>
Health(): Promise<void>
NewAddress(addressType: Types.AddressType): Promise<NewAddressResponse>
NewInvoice(value: number, memo: string, expiry: number): Promise<Invoice>
DecodeInvoice(paymentRequest: string): Promise<DecodedInvoice>
GetFeeLimitAmount(amount: number): number
GetMaxWithinLimit(amount: number): number
PayInvoice(invoice: string, amount: number, feeLimit: number): Promise<PaidInvoice>
EstimateChainFees(address: string, amount: number, targetConf: number): Promise<EstimateFeeResponse>
PayAddress(address: string, amount: number, satPerVByte: number, label?: string): Promise<SendCoinsResponse>
//OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string>
SetMockInvoiceAsPaid(invoice: string, amount: number): Promise<void>
ChannelBalance(): Promise<{ local: number, remote: number }>
GetTransactions(startHeight: number): Promise<TransactionDetails>
GetBalance(): Promise<BalanceInfo>
GetWalletBalance(): Promise<WalletBalanceResponse>
GetChannelBalance(): Promise<ChannelBalanceResponse>
ListClosedChannels(): Promise<ClosedChannelsResponse>
ListChannels(): Promise<ListChannelsResponse>
ListPendingChannels(): Promise<PendingChannelsResponse>
GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]>
GetAllPaidInvoices(max: number): Promise<ListInvoiceResponse>
GetAllPayments(max: number): Promise<ListPaymentsResponse>
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)
}
}
145 changes: 0 additions & 145 deletions src/services/lnd/mock.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/services/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,7 +26,7 @@ type UserOperationsSub = {

export default class {
storage: Storage
lnd: LightningHandler
lnd: LND
settings: MainSettings
userOperationsSub: UserOperationsSub | null = null
productManager: ProductManager
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/services/main/paymentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/services/main/sanityChecker.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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[] = []
Expand All @@ -22,7 +22,7 @@ export default class SanityChecker {
decrementEvents: Record<string, { userId: string, refund: number, failure: boolean }> = {}
log = getLogger({ appName: "SanityChecker" })
users: Record<string, { ts: number, updatedBalance: number }> = {}
constructor(storage: Storage, lnd: LightningHandler) {
constructor(storage: Storage, lnd: LND) {
this.storage = storage
this.lnd = lnd
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/main/watchdog.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/services/metrics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/tests/testBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5f65a74

Please sign in to comment.