Merge pull request #643 from shocknet/htlc_tracker

Htlc tracker
This commit is contained in:
Justin (shocknet) 2024-03-01 15:45:29 -05:00 committed by GitHub
commit 446d3e889b
16 changed files with 4239 additions and 3857 deletions

View file

@ -1,12 +1,10 @@
import { DataSource } from "typeorm" import { DataSource } from "typeorm"
import { BalanceEvent } from "./build/src/services/storage/entity/BalanceEvent.js" import { ChannelRouting } from "./build/src/services/storage/entity/ChannelRouting.js"
import { ChannelBalanceEvent } from "./build/src/services/storage/entity/ChannelsBalanceEvent.js"
import { RoutingEvent } from "./build/src/services/storage/entity/RoutingEvent.js"
export default new DataSource({ export default new DataSource({
type: "sqlite", type: "sqlite",
database: "metrics.sqlite", database: "metrics.sqlite",
entities: [ RoutingEvent, BalanceEvent, ChannelBalanceEvent], entities: [ChannelRouting],
}); });

View file

@ -444,105 +444,51 @@ The nostr server will send back a message response, and inside the body there wi
## Messages ## Messages
### The content of requests and response from the methods ### The content of requests and response from the methods
### AppUser ### ChannelRouting
- __identifier__: _string_ - __channel_id__: _string_
- __info__: _[UserInfo](#UserInfo)_ - __send_errors__: _number_
- __max_withdrawable__: _number_ - __receive_errors__: _number_
- __forward_errors_as_input__: _number_
- __forward_errors_as_output__: _number_
- __missed_forward_fee_as_input__: _number_
- __missed_forward_fee_as_output__: _number_
- __forward_fee_as_input__: _number_
- __forward_fee_as_output__: _number_
- __events_number__: _number_
### SendAppUserToAppUserPaymentRequest ### AddAppInvoiceRequest
- __from_user_identifier__: _string_ - __payer_identifier__: _string_
- __to_user_identifier__: _string_ - __http_callback_url__: _string_
- __amount__: _number_ - __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### NewAddressRequest ### GetAppUserLNURLInfoRequest
- __addressType__: _[AddressType](#AddressType)_
### LndMetrics
- __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### LnurlLinkResponse
- __lnurl__: _string_
- __k1__: _string_
### GetUserOperationsRequest
- __latestIncomingInvoice__: _number_
- __latestOutgoingInvoice__: _number_
- __latestIncomingTx__: _number_
- __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
- __max_size__: _number_
### UserOperations
- __fromIndex__: _number_
- __toIndex__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### AddProductRequest
- __name__: _string_
- __price_sats__: _number_
### PayAppUserInvoiceRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
- __invoice__: _string_ - __base_url_override__: _string_
- __amount__: _number_
### PayInvoiceResponse
- __preimage__: _string_
- __amount_paid__: _number_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### ChainBalanceEvent
- __block_height__: _number_
- __confirmed_balance__: _number_
- __unconfirmed_balance__: _number_
- __total_balance__: _number_
### SetMockInvoiceAsPaidRequest ### SetMockInvoiceAsPaidRequest
- __invoice__: _string_ - __invoice__: _string_
- __amount__: _number_ - __amount__: _number_
### Application
- __name__: _string_
- __id__: _string_
- __balance__: _number_
- __npub__: _string_
### OpenChannel
- __channel_id__: _string_
- __capacity__: _number_
- __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### LndMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
### AddAppUserRequest ### AddAppUserRequest
- __identifier__: _string_ - __identifier__: _string_
- __fail_if_exists__: _boolean_ - __fail_if_exists__: _boolean_
- __balance__: _number_ - __balance__: _number_
### PayInvoiceRequest ### NewInvoiceResponse
- __invoice__: _string_ - __invoice__: _string_
- __amount__: _number_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### UsersInfo
- __total__: _number_
- __no_balance__: _number_
- __negative_balance__: _number_
- __always_been_inactive__: _number_
- __balance_avg__: _number_
- __balance_median__: _number_
### NewInvoiceRequest
- __amountSats__: _number_
- __memo__: _string_
### RoutingEvent ### RoutingEvent
- __incoming_channel_id__: _number_ - __incoming_channel_id__: _number_
@ -558,13 +504,34 @@ The nostr server will send back a message response, and inside the body there wi
- __offchain__: _boolean_ - __offchain__: _boolean_
- __forward_fail_event__: _boolean_ - __forward_fail_event__: _boolean_
### AddAppInvoiceRequest ### AuthApp
- __payer_identifier__: _string_ - __app__: _[Application](#Application)_
- __http_callback_url__: _string_ - __auth_token__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### GetAppUserRequest ### DecodeInvoiceResponse
- __amount__: _number_
### EncryptionExchangeRequest
- __publicKey__: _string_
- __deviceId__: _string_
### AppUser
- __identifier__: _string_
- __info__: _[UserInfo](#UserInfo)_
- __max_withdrawable__: _number_
### SetMockAppUserBalanceRequest
- __user_identifier__: _string_ - __user_identifier__: _string_
- __amount__: _number_
### NewAddressRequest
- __addressType__: _[AddressType](#AddressType)_
### OpenChannelRequest
- __destination__: _string_
- __fundingAmount__: _number_
- __pushAmount__: _number_
- __closeAddress__: _string_
### UserOperation ### UserOperation
- __paidAtUnix__: _number_ - __paidAtUnix__: _number_
@ -579,49 +546,38 @@ The nostr server will send back a message response, and inside the body there wi
- __tx_hash__: _string_ - __tx_hash__: _string_
- __internal__: _boolean_ - __internal__: _boolean_
### LndNodeMetrics ### AddProductRequest
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_ - __name__: _string_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_ - __price_sats__: _number_
- __routing_events__: ARRAY of: _[RoutingEvent](#RoutingEvent)_
- __offline_channels__: _number_
- __online_channels__: _number_
- __pending_channels__: _number_
- __closing_channels__: _number_
- __open_channels__: ARRAY of: _[OpenChannel](#OpenChannel)_
- __closed_channels__: ARRAY of: _[ClosedChannel](#ClosedChannel)_
### HandleLnurlPayResponse ### AddAppUserInvoiceRequest
- __pr__: _string_ - __receiver_identifier__: _string_
- __routes__: ARRAY of: _[Empty](#Empty)_ - __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### UsageMetric ### LndGetInfoRequest
- __processed_at_ms__: _number_ - __nodeId__: _number_
- __parsed_in_nano__: _number_
- __auth_in_nano__: _number_
- __validate_in_nano__: _number_
- __handle_in_nano__: _number_
- __rpc_name__: _string_
- __batch__: _boolean_
- __nostr__: _boolean_
- __batch_size__: _number_
### PayInvoiceResponse ### GetAppUserRequest
- __preimage__: _string_ - __user_identifier__: _string_
- __amount_paid__: _number_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### RelaysMigration ### PayAppUserInvoiceRequest
- __relays__: ARRAY of: _string_ - __user_identifier__: _string_
- __invoice__: _string_
- __amount__: _number_
### Empty ### SetMockAppBalanceRequest
- __amount__: _number_
### ChannelBalanceEvent ### GetUserOperationsRequest
- __block_height__: _number_ - __latestIncomingInvoice__: _number_
- __channel_id__: _string_ - __latestOutgoingInvoice__: _number_
- __local_balance_sats__: _number_ - __latestIncomingTx__: _number_
- __remote_balance_sats__: _number_ - __latestOutgoingTx__: _number_
- __latestIncomingUserToUserPayment__: _number_
- __latestOutgoingUserToUserPayment__: _number_
- __max_size__: _number_
### AppMetrics ### AppMetrics
- __app__: _[Application](#Application)_ - __app__: _[Application](#Application)_
@ -634,40 +590,165 @@ The nostr server will send back a message response, and inside the body there wi
- __total_fees__: _number_ - __total_fees__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_ - __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### GetAppUserLNURLInfoRequest ### LndGetInfoResponse
- __user_identifier__: _string_ - __alias__: _string_
- __base_url_override__: _string_
### SendAppUserToAppPaymentRequest
- __from_user_identifier__: _string_
- __amount__: _number_
### PayInvoiceRequest
- __invoice__: _string_
- __amount__: _number_
### LiveUserOperation
- __operation__: _[UserOperation](#UserOperation)_
### MigrationUpdate
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional
### OpenChannel
- __channel_id__: _string_
- __capacity__: _number_
- __active__: _boolean_
- __lifetime__: _number_
- __local_balance__: _number_
- __remote_balance__: _number_
### DecodeInvoiceRequest ### DecodeInvoiceRequest
- __invoice__: _string_ - __invoice__: _string_
### ClosureMigration ### UserInfo
- __closes_at_unix__: _number_ - __userId__: _string_
- __balance__: _number_
- __max_withdrawable__: _number_
- __user_identifier__: _string_
### LndGetInfoRequest ### UsageMetric
- __nodeId__: _number_ - __processed_at_ms__: _number_
- __parsed_in_nano__: _number_
- __auth_in_nano__: _number_
- __validate_in_nano__: _number_
- __handle_in_nano__: _number_
- __rpc_name__: _string_
- __batch__: _boolean_
- __nostr__: _boolean_
- __batch_size__: _number_
### ChainBalanceEvent ### ChannelBalanceEvent
- __block_height__: _number_ - __block_height__: _number_
- __confirmed_balance__: _number_ - __channel_id__: _string_
- __unconfirmed_balance__: _number_ - __local_balance_sats__: _number_
- __total_balance__: _number_ - __remote_balance_sats__: _number_
### AuthApp ### AddAppRequest
- __app__: _[Application](#Application)_ - __name__: _string_
- __auth_token__: _string_ - __allow_user_creation__: _boolean_
### EncryptionExchangeRequest ### Application
- __publicKey__: _string_ - __name__: _string_
- __deviceId__: _string_ - __id__: _string_
- __balance__: _number_
- __npub__: _string_
### PayAddressRequest
- __address__: _string_
- __amoutSats__: _number_
- __satsPerVByte__: _number_
### AppsMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
- __include_operations__: _boolean_ *this field is optional
### LndMetricsRequest
- __from_unix__: _number_ *this field is optional
- __to_unix__: _number_ *this field is optional
### SendAppUserToAppUserPaymentRequest
- __from_user_identifier__: _string_
- __to_user_identifier__: _string_
- __amount__: _number_
### LnurlLinkResponse
- __lnurl__: _string_
- __k1__: _string_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### UsageMetrics
- __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_
### AuthAppRequest ### AuthAppRequest
- __name__: _string_ - __name__: _string_
- __allow_user_creation__: _boolean_ *this field is optional - __allow_user_creation__: _boolean_ *this field is optional
### SendAppUserToAppPaymentRequest ### GetUserOperationsResponse
- __from_user_identifier__: _string_ - __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __amount__: _number_ - __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### LndNodeMetrics
- __channels_balance_events__: ARRAY of: _[ChannelBalanceEvent](#ChannelBalanceEvent)_
- __chain_balance_events__: ARRAY of: _[ChainBalanceEvent](#ChainBalanceEvent)_
- __offline_channels__: _number_
- __online_channels__: _number_
- __pending_channels__: _number_
- __closing_channels__: _number_
- __open_channels__: ARRAY of: _[OpenChannel](#OpenChannel)_
- __closed_channels__: ARRAY of: _[ClosedChannel](#ClosedChannel)_
- __channel_routing__: ARRAY of: _[ChannelRouting](#ChannelRouting)_
### UserOperations
- __fromIndex__: _number_
- __toIndex__: _number_
- __operations__: ARRAY of: _[UserOperation](#UserOperation)_
### RelaysMigration
- __relays__: ARRAY of: _string_
### Empty
### UsersInfo
- __total__: _number_
- __no_balance__: _number_
- __negative_balance__: _number_
- __always_been_inactive__: _number_
- __balance_avg__: _number_
- __balance_median__: _number_
### NewAddressResponse
- __address__: _string_
### Product
- __id__: _string_
- __name__: _string_
- __price_sats__: _number_
### AppsMetrics
- __apps__: ARRAY of: _[AppMetrics](#AppMetrics)_
### ClosedChannel
- __channel_id__: _string_
- __capacity__: _number_
- __closed_height__: _number_
### PayAddressResponse
- __txId__: _string_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### LnurlWithdrawInfoResponse ### LnurlWithdrawInfoResponse
- __tag__: _string_ - __tag__: _string_
@ -679,94 +760,25 @@ The nostr server will send back a message response, and inside the body there wi
- __balanceCheck__: _string_ - __balanceCheck__: _string_
- __payLink__: _string_ - __payLink__: _string_
### Product ### HandleLnurlPayResponse
- __id__: _string_ - __pr__: _string_
- __name__: _string_ - __routes__: ARRAY of: _[Empty](#Empty)_
- __price_sats__: _number_
### LndGetInfoResponse
- __alias__: _string_
### AddAppUserInvoiceRequest
- __receiver_identifier__: _string_
- __payer_identifier__: _string_
- __http_callback_url__: _string_
- __invoice_req__: _[NewInvoiceRequest](#NewInvoiceRequest)_
### SetMockAppBalanceRequest
- __amount__: _number_
### GetUserOperationsResponse
- __latestOutgoingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingInvoiceOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingTxOperations__: _[UserOperations](#UserOperations)_
- __latestIncomingTxOperations__: _[UserOperations](#UserOperations)_
- __latestOutgoingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
- __latestIncomingUserToUserPayemnts__: _[UserOperations](#UserOperations)_
### UsageMetrics
- __metrics__: ARRAY of: _[UsageMetric](#UsageMetric)_
### PayAddressResponse
- __txId__: _string_
- __operation_id__: _string_
- __service_fee__: _number_
- __network_fee__: _number_
### OpenChannelResponse
- __channelId__: _string_
### NewAddressResponse
- __address__: _string_
### AddAppRequest
- __name__: _string_
- __allow_user_creation__: _boolean_
### SetMockAppUserBalanceRequest
- __user_identifier__: _string_
- __amount__: _number_
### DecodeInvoiceResponse
- __amount__: _number_
### LnurlPayInfoResponse
- __tag__: _string_
- __callback__: _string_
- __maxSendable__: _number_
- __minSendable__: _number_
- __metadata__: _string_
- __allowsNostr__: _boolean_
- __nostrPubkey__: _string_
### UserInfo
- __userId__: _string_
- __balance__: _number_
- __max_withdrawable__: _number_
- __user_identifier__: _string_
### GetProductBuyLinkResponse ### GetProductBuyLinkResponse
- __link__: _string_ - __link__: _string_
### LiveUserOperation ### NewInvoiceRequest
- __operation__: _[UserOperation](#UserOperation)_ - __amountSats__: _number_
- __memo__: _string_
### AppsMetricsRequest ### ClosureMigration
- __from_unix__: _number_ *this field is optional - __closes_at_unix__: _number_
- __to_unix__: _number_ *this field is optional
- __include_operations__: _boolean_ *this field is optional
### NewInvoiceResponse ### LndMetrics
- __invoice__: _string_ - __nodes__: ARRAY of: _[LndNodeMetrics](#LndNodeMetrics)_
### MigrationUpdate ### OpenChannelResponse
- __closure__: _[ClosureMigration](#ClosureMigration)_ *this field is optional - __channelId__: _string_
- __relays__: _[RelaysMigration](#RelaysMigration)_ *this field is optional
### ClosedChannel
- __channel_id__: _string_
- __capacity__: _number_
- __closed_height__: _number_
## Enums ## Enums
### The enumerators used in the messages ### The enumerators used in the messages

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -112,16 +112,29 @@ message ClosedChannel {
int64 closed_height =4; int64 closed_height =4;
} }
message ChannelRouting {
string channel_id = 1;
int64 send_errors = 2;
int64 receive_errors = 3;
int64 forward_errors_as_input = 4;
int64 forward_errors_as_output = 5;
int64 missed_forward_fee_as_input = 6;
int64 missed_forward_fee_as_output = 7;
int64 forward_fee_as_input = 8;
int64 forward_fee_as_output = 9;
int64 events_number = 10;
}
message LndNodeMetrics { message LndNodeMetrics {
repeated ChannelBalanceEvent channels_balance_events = 1; repeated ChannelBalanceEvent channels_balance_events = 1;
repeated ChainBalanceEvent chain_balance_events = 2; repeated ChainBalanceEvent chain_balance_events = 2;
repeated RoutingEvent routing_events = 3;
int64 offline_channels = 4; int64 offline_channels = 4;
int64 online_channels = 5; int64 online_channels = 5;
int64 pending_channels = 6; int64 pending_channels = 6;
int64 closing_channels = 7; int64 closing_channels = 7;
repeated OpenChannel open_channels = 8; repeated OpenChannel open_channels = 8;
repeated ClosedChannel closed_channels = 9; repeated ClosedChannel closed_channels = 9;
repeated ChannelRouting channel_routing = 10;
} }
message LndMetrics { message LndMetrics {

View file

@ -35,6 +35,7 @@ export interface LightningHandler {
ListClosedChannels(): Promise<ClosedChannelsResponse> ListClosedChannels(): Promise<ClosedChannelsResponse>
ListChannels(): Promise<ListChannelsResponse> ListChannels(): Promise<ListChannelsResponse>
ListPendingChannels(): Promise<PendingChannelsResponse> ListPendingChannels(): Promise<PendingChannelsResponse>
GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]>
} }
export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb): LightningHandler => { export default (settings: LndSettings, addressPaidCb: AddressPaidCb, invoicePaidCb: InvoicePaidCb, newBlockCb: NewBlockCb, htlcCb: HtlcCb): LightningHandler => {

View file

@ -311,6 +311,10 @@ export default class {
return { confirmedBalance: Number(confirmedBalance), unconfirmedBalance: Number(unconfirmedBalance), totalBalance: Number(totalBalance), channelsBalance } return { confirmedBalance: Number(confirmedBalance), unconfirmedBalance: Number(unconfirmedBalance), totalBalance: Number(totalBalance), channelsBalance }
} }
async GetForwardingHistory(indexOffset: number): Promise<{ fee: number, chanIdIn: string, chanIdOut: string, timestampNs: number, offset: number }[]> {
const { response } = await this.lightning.forwardingHistory({ indexOffset, numMaxEvents: 0, startTime: 0n, endTime: 0n, peerAliasLookup: false }, DeadLineMetadata())
return response.forwardingEvents.map(e => ({ fee: Number(e.fee), chanIdIn: e.chanIdIn, chanIdOut: e.chanIdOut, timestampNs: Number(e.timestampNs), offset: response.lastOffsetIndex }))
}
async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string> { async OpenChannel(destination: string, closeAddress: string, fundingAmount: number, pushSats: number): Promise<string> {
await this.Health() await this.Health()

View file

@ -42,6 +42,7 @@ export default class {
async ListClosedChannels(): Promise<ClosedChannelsResponse> { throw new Error("ListClosedChannels disabled in mock mode") } async ListClosedChannels(): Promise<ClosedChannelsResponse> { throw new Error("ListClosedChannels disabled in mock mode") }
async ListChannels(): Promise<ListChannelsResponse> { throw new Error("ListChannels disabled in mock mode") } async ListChannels(): Promise<ListChannelsResponse> { throw new Error("ListChannels disabled in mock mode") }
async ListPendingChannels(): Promise<PendingChannelsResponse> { throw new Error("ListPendingChannels disabled in mock mode") } async ListPendingChannels(): Promise<PendingChannelsResponse> { 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<NodeInfo> { async GetInfo(): Promise<NodeInfo> {
return { alias: "mock", syncedToChain: true, syncedToGraph: true, blockHeight: 1, blockHash: "" } return { alias: "mock", syncedToChain: true, syncedToGraph: true, blockHeight: 1, blockHash: "" }

View file

@ -0,0 +1,131 @@
import Storage from '../storage/index.js'
import { ForwardEvent, HtlcEvent, HtlcEvent_EventType } from "../../../proto/lnd/router.js";
import { getLogger } from "../helpers/logger.js";
type EventInfo = {
eventType: HtlcEvent_EventType
outgoingHtlcId: number
incomingHtlcId: number
outgoingChannelId: number
incomingChannelId: number
}
export default class HtlcTracker {
storage: Storage
pendingSendHtlcs: Map<number, number> = new Map()
pendingReceiveHtlcs: Map<number, number> = new Map()
pendingForwardHtlcs: Map<number, number> = new Map()
constructor(storage: Storage) {
this.storage = storage
}
log = getLogger({ appName: 'htlcTracker' })
onHtlcEvent = async (htlc: HtlcEvent) => {
const htlcEvent = htlc.event
if (htlcEvent.oneofKind === 'subscribedEvent') {
this.log("htlc subscribed")
return
}
const outgoingHtlcId = Number(htlc.outgoingHtlcId)
const incomingHtlcId = Number(htlc.incomingHtlcId)
const outgoingChannelId = Number(htlc.outgoingChannelId)
const incomingChannelId = Number(htlc.incomingChannelId)
const info: EventInfo = { eventType: htlc.eventType, outgoingChannelId, incomingChannelId, outgoingHtlcId, incomingHtlcId }
switch (htlcEvent.oneofKind) {
case 'forwardEvent':
return this.handleForward(htlcEvent.forwardEvent, info)
case 'forwardFailEvent':
return this.handleFailure(info)
case 'linkFailEvent':
return this.handleFailure(info)
case 'finalHtlcEvent':
if (!htlcEvent.finalHtlcEvent.settled) {
return this.handleFailure(info)
} else {
return this.handleSuccess(info)
}
case 'settleEvent':
return this.handleSuccess(info)
default:
this.log("unknown htlc event type")
}
}
handleForward = (fwe: ForwardEvent, { eventType, outgoingHtlcId, incomingHtlcId }: EventInfo) => {
this.log("new forward event, currently tracked htlcs: (s,r,f)", this.pendingSendHtlcs.size, this.pendingReceiveHtlcs.size, this.pendingForwardHtlcs.size)
const { info } = fwe
const incomingAmtMsat = info ? Number(info.incomingAmtMsat) : 0
const outgoingAmtMsat = info ? Number(info.outgoingAmtMsat) : 0
if (eventType === HtlcEvent_EventType.SEND) {
this.pendingSendHtlcs.set(outgoingHtlcId, outgoingAmtMsat - incomingAmtMsat)
} else if (eventType === HtlcEvent_EventType.RECEIVE) {
this.pendingReceiveHtlcs.set(incomingHtlcId, incomingAmtMsat - outgoingAmtMsat)
} else if (eventType === HtlcEvent_EventType.FORWARD) {
this.pendingForwardHtlcs.set(outgoingHtlcId, outgoingAmtMsat - incomingAmtMsat)
} else {
this.log("unknown htlc event type for forward event")
}
}
handleFailure = ({ eventType, outgoingHtlcId, incomingHtlcId, incomingChannelId, outgoingChannelId }: EventInfo) => {
if (eventType === HtlcEvent_EventType.SEND && this.deleteMapEntry(outgoingHtlcId, this.pendingSendHtlcs) !== null) {
return this.incrementSendFailures(outgoingChannelId)
}
if (eventType === HtlcEvent_EventType.RECEIVE && this.deleteMapEntry(incomingHtlcId, this.pendingReceiveHtlcs) !== null) {
return this.incrementReceiveFailures(incomingChannelId)
}
if (eventType === HtlcEvent_EventType.FORWARD) {
const amt = this.deleteMapEntry(outgoingHtlcId, this.pendingForwardHtlcs)
if (amt !== null) {
return this.incrementForwardFailures(incomingChannelId, outgoingChannelId, amt)
}
}
if (eventType === HtlcEvent_EventType.UNKNOWN) {
const fwdAmt = this.deleteMapEntry(outgoingHtlcId, this.pendingForwardHtlcs)
if (fwdAmt !== null) {
return this.incrementForwardFailures(incomingChannelId, outgoingChannelId, fwdAmt)
}
if (this.deleteMapEntry(outgoingHtlcId, this.pendingSendHtlcs) !== null) {
return this.incrementSendFailures(outgoingChannelId)
}
if (this.deleteMapEntry(incomingHtlcId, this.pendingReceiveHtlcs) !== null) {
return this.incrementReceiveFailures(incomingChannelId)
}
}
this.log("unknown htlc event type for failure event")
}
handleSuccess = ({ eventType, outgoingHtlcId, incomingHtlcId }: EventInfo) => {
if (eventType === HtlcEvent_EventType.SEND) {
this.deleteMapEntry(outgoingHtlcId, this.pendingSendHtlcs)
} else if (eventType === HtlcEvent_EventType.RECEIVE) {
this.deleteMapEntry(incomingHtlcId, this.pendingReceiveHtlcs)
} else if (eventType === HtlcEvent_EventType.FORWARD) {
this.deleteMapEntry(outgoingHtlcId, this.pendingForwardHtlcs)
} else if (eventType === HtlcEvent_EventType.UNKNOWN) {
if (this.deleteMapEntry(outgoingHtlcId, this.pendingSendHtlcs) !== null) return
if (this.deleteMapEntry(incomingHtlcId, this.pendingReceiveHtlcs) !== null) return
if (this.deleteMapEntry(outgoingHtlcId, this.pendingForwardHtlcs) !== null) return
} else {
this.log("unknown htlc event type for success event")
}
}
deleteMapEntry = (key: number, map: Map<number, number>) => {
if (!map.has(key)) {
return null
}
const v = map.get(key)
map.delete(key)
return v || null
}
incrementSendFailures = async (outgoingChannelId: number) => {
await this.storage.metricsStorage.IncrementChannelRouting(outgoingChannelId.toString(), { send_errors: 1 })
}
incrementReceiveFailures = async (incomingChannelId: number) => {
await this.storage.metricsStorage.IncrementChannelRouting(incomingChannelId.toString(), { receive_errors: 1 })
}
incrementForwardFailures = async (incomingChannelId: number, outgoingChannelId: number, amt: number) => {
await this.storage.metricsStorage.IncrementChannelRouting(incomingChannelId.toString(), { forward_errors_as_input: 1, missed_forward_fee_as_input: amt })
await this.storage.metricsStorage.IncrementChannelRouting(outgoingChannelId.toString(), { forward_errors_as_output: 1, missed_forward_fee_as_output: amt })
}
}

View file

@ -2,47 +2,25 @@ import Storage from '../storage/index.js'
import * as Types from '../../../proto/autogenerated/ts/types.js' import * as Types from '../../../proto/autogenerated/ts/types.js'
import { Application } from '../storage/entity/Application.js' import { Application } from '../storage/entity/Application.js'
import { HtlcEvent, HtlcEvent_EventType } from '../../../proto/lnd/router.js' import { HtlcEvent, HtlcEvent_EventType } from '../../../proto/lnd/router.js'
import { RoutingEvent } from '../storage/entity/RoutingEvent.js'
import { BalanceInfo } from '../lnd/settings.js' import { BalanceInfo } from '../lnd/settings.js'
import { BalanceEvent } from '../storage/entity/BalanceEvent.js' import { BalanceEvent } from '../storage/entity/BalanceEvent.js'
import { ChannelBalanceEvent } from '../storage/entity/ChannelsBalanceEvent.js' import { ChannelBalanceEvent } from '../storage/entity/ChannelsBalanceEvent.js'
import { LightningHandler } from '../lnd/index.js' import { LightningHandler } from '../lnd/index.js'
import HtlcTracker from './htlcTracker.js'
const maxEvents = 100_000 const maxEvents = 100_000
export default class Handler { export default class Handler {
storage: Storage storage: Storage
lnd: LightningHandler lnd: LightningHandler
htlcTracker: HtlcTracker
metrics: Types.UsageMetric[] = [] metrics: Types.UsageMetric[] = []
constructor(storage: Storage, lnd: LightningHandler) { constructor(storage: Storage, lnd: LightningHandler) {
this.storage = storage this.storage = storage
this.lnd = lnd this.lnd = lnd
this.htlcTracker = new HtlcTracker(this.storage)
} }
async HtlcCb(htlc: HtlcEvent) { async HtlcCb(htlc: HtlcEvent) {
const routingEvent: Partial<RoutingEvent> = {} await this.htlcTracker.onHtlcEvent(htlc)
routingEvent.event_type = HtlcEvent_EventType[htlc.eventType]
routingEvent.incoming_channel_id = Number(htlc.incomingChannelId)
routingEvent.incoming_htlc_id = Number(htlc.incomingHtlcId)
routingEvent.outgoing_channel_id = Number(htlc.outgoingChannelId)
routingEvent.outgoing_htlc_id = Number(htlc.outgoingHtlcId)
routingEvent.timestamp_ns = Number(htlc.timestampNs)
if (htlc.event.oneofKind === 'finalHtlcEvent') {
routingEvent.offchain = htlc.event.finalHtlcEvent.offchain
routingEvent.settled = htlc.event.finalHtlcEvent.settled
} else if (htlc.event.oneofKind === 'forwardEvent') {
const { info } = htlc.event.forwardEvent
routingEvent.incoming_amt_msat = info ? Number(info.incomingAmtMsat) : undefined
routingEvent.outgoing_amt_msat = info ? Number(info.outgoingAmtMsat) : undefined
} else if (htlc.event.oneofKind === 'settleEvent') {
} else if (htlc.event.oneofKind === 'subscribedEvent') {
} else if (htlc.event.oneofKind === 'forwardFailEvent') {
routingEvent.forward_fail_event = true
} else if (htlc.event.oneofKind === 'linkFailEvent') {
routingEvent.failure_string = htlc.event.linkFailEvent.failureString
const { info } = htlc.event.linkFailEvent
routingEvent.incoming_amt_msat = info ? Number(info.incomingAmtMsat) : undefined
routingEvent.outgoing_amt_msat = info ? Number(info.outgoingAmtMsat) : undefined
}
await this.storage.metricsStorage.SaveRoutingEvent(routingEvent)
} }
async NewBlockCb(height: number, balanceInfo: BalanceInfo) { async NewBlockCb(height: number, balanceInfo: BalanceInfo) {
@ -60,6 +38,15 @@ export default class Handler {
await this.storage.metricsStorage.SaveBalanceEvents(balanceEvent, channelsEvents) await this.storage.metricsStorage.SaveBalanceEvents(balanceEvent, channelsEvents)
} }
async FetchLatestForwardingEvents() {
const latestIndex = await this.storage.metricsStorage.GetLatestForwardingIndexOffset()
const forwards = await this.lnd.GetForwardingHistory(latestIndex)
await Promise.all(forwards.map(async f => {
await this.storage.metricsStorage.IncrementChannelRouting(f.chanIdIn, { forward_fee_as_input: f.fee, latest_index_offset: f.offset })
await this.storage.metricsStorage.IncrementChannelRouting(f.chanIdOut, { forward_fee_as_output: f.fee, latest_index_offset: f.offset })
}))
}
AddMetrics(newMetrics: (Types.RequestMetric & { app_id?: string })[]) { AddMetrics(newMetrics: (Types.RequestMetric & { app_id?: string })[]) {
const parsed: Types.UsageMetric[] = newMetrics.map(m => ({ const parsed: Types.UsageMetric[] = newMetrics.map(m => ({
rpc_name: m.rpcName, rpc_name: m.rpcName,
@ -218,8 +205,33 @@ export default class Handler {
const { openChannels, totalActive, totalInactive } = await this.GetChannelsInfo() const { openChannels, totalActive, totalInactive } = await this.GetChannelsInfo()
const { totalPendingOpen, totalPendingClose } = await this.GetPendingChannelsInfo() const { totalPendingOpen, totalPendingClose } = await this.GetPendingChannelsInfo()
const { channels: closedChannels } = await this.lnd.ListClosedChannels() const { channels: closedChannels } = await this.lnd.ListClosedChannels()
const rawRouting = await this.storage.metricsStorage.GetChannelRouting({ from: req.from_unix, to: req.to_unix })
const routingEvents = await this.storage.metricsStorage.GetRoutingEvents({ from: req.from_unix, to: req.to_unix }) const routingMap: Record<string, Types.ChannelRouting> = {}
rawRouting.forEach(r => {
if (!routingMap[r.channel_id]) {
routingMap[r.channel_id] = {
channel_id: r.channel_id,
send_errors: 0,
receive_errors: 0,
forward_errors_as_input: 0,
forward_errors_as_output: 0,
missed_forward_fee_as_input: 0,
missed_forward_fee_as_output: 0,
forward_fee_as_input: 0,
forward_fee_as_output: 0,
events_number: 0
}
}
routingMap[r.channel_id].send_errors += r.send_errors
routingMap[r.channel_id].receive_errors += r.receive_errors
routingMap[r.channel_id].forward_errors_as_input += r.forward_errors_as_input
routingMap[r.channel_id].forward_errors_as_output += r.forward_errors_as_output
routingMap[r.channel_id].missed_forward_fee_as_input += r.missed_forward_fee_as_input
routingMap[r.channel_id].missed_forward_fee_as_output += r.missed_forward_fee_as_output
routingMap[r.channel_id].forward_fee_as_input += r.forward_fee_as_input
routingMap[r.channel_id].forward_fee_as_output += r.forward_fee_as_output
routingMap[r.channel_id].events_number++
})
const { channelsBalanceEvents, chainBalanceEvents } = await this.storage.metricsStorage.GetBalanceEvents({ from: req.from_unix, to: req.to_unix }) const { channelsBalanceEvents, chainBalanceEvents } = await this.storage.metricsStorage.GetBalanceEvents({ from: req.from_unix, to: req.to_unix })
return { return {
nodes: [{ nodes: [{
@ -235,26 +247,13 @@ export default class Handler {
local_balance_sats: e.local_balance_sats, local_balance_sats: e.local_balance_sats,
remote_balance_sats: e.remote_balance_sats remote_balance_sats: e.remote_balance_sats
})), })),
routing_events: routingEvents.map(e => ({
event_type: e.event_type,
failure_string: e.failure_string || "",
forward_fail_event: e.forward_fail_event || false,
incoming_amt_msat: e.incoming_amt_msat || 0,
incoming_channel_id: e.incoming_channel_id || 0,
incoming_htlc_id: e.incoming_htlc_id || 0,
offchain: e.offchain || false,
outgoing_amt_msat: e.outgoing_amt_msat || 0,
outgoing_channel_id: e.outgoing_channel_id,
outgoing_htlc_id: e.outgoing_htlc_id,
settled: e.settled || false,
timestamp_ns: e.timestamp_ns
})),
closing_channels: totalPendingClose, closing_channels: totalPendingClose,
pending_channels: totalPendingOpen, pending_channels: totalPendingOpen,
offline_channels: totalInactive, offline_channels: totalInactive,
online_channels: totalActive, online_channels: totalActive,
closed_channels: closedChannels.map(c => ({ capacity: Number(c.capacity), channel_id: c.chanId, closed_height: c.closeHeight })), closed_channels: closedChannels.map(c => ({ capacity: Number(c.capacity), channel_id: c.chanId, closed_height: c.closeHeight })),
open_channels: openChannels.map(c => ({ active: c.active, capacity: Number(c.capacity), channel_id: c.chanId, lifetime: Number(c.lifetime), local_balance: Number(c.localBalance), remote_balance: Number(c.remoteBalance) })) open_channels: openChannels.map(c => ({ active: c.active, capacity: Number(c.capacity), channel_id: c.chanId, lifetime: Number(c.lifetime), local_balance: Number(c.localBalance), remote_balance: Number(c.remoteBalance) })),
channel_routing: Object.values(routingMap)
}], }],
} }

View file

@ -13,12 +13,10 @@ import { Product } from "./entity/Product.js"
import { UserToUserPayment } from "./entity/UserToUserPayment.js" import { UserToUserPayment } from "./entity/UserToUserPayment.js"
import { Application } from "./entity/Application.js" import { Application } from "./entity/Application.js"
import { ApplicationUser } from "./entity/ApplicationUser.js" import { ApplicationUser } from "./entity/ApplicationUser.js"
import { RoutingEvent } from "./entity/RoutingEvent.js"
import { BalanceEvent } from "./entity/BalanceEvent.js" import { BalanceEvent } from "./entity/BalanceEvent.js"
import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js" import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js"
import { getLogger } from "../helpers/logger.js" import { getLogger } from "../helpers/logger.js"
import { Initial1703170309875 } from "./migrations/1703170309875-initial.js" import { ChannelRouting } from "./entity/ChannelRouting.js"
import { LndMetrics1703170330183 } from "./migrations/1703170330183-lnd_metrics.js"
export type DbSettings = { export type DbSettings = {
@ -38,7 +36,7 @@ export const newMetricsDb = async (settings: DbSettings, metricsMigrations: Func
const source = await new DataSource({ const source = await new DataSource({
type: "sqlite", type: "sqlite",
database: settings.metricsDatabaseFile, database: settings.metricsDatabaseFile,
entities: [ RoutingEvent, BalanceEvent, ChannelBalanceEvent], entities: [BalanceEvent, ChannelBalanceEvent, ChannelRouting],
migrations: metricsMigrations migrations: metricsMigrations
}).initialize(); }).initialize();
const log = getLogger({}); const log = getLogger({});

View file

@ -0,0 +1,46 @@
import { Entity, PrimaryGeneratedColumn, Column, Index, Check, CreateDateColumn, UpdateDateColumn } from "typeorm"
@Entity()
export class ChannelRouting {
@PrimaryGeneratedColumn()
serial_id: number
@Column()
day_unix: number
@Column()
channel_id: string
@Column({ default: 0 })
send_errors: number
@Column({ default: 0 })
receive_errors: number
@Column({ default: 0 })
forward_errors_as_input: number
@Column({ default: 0 })
forward_errors_as_output: number
@Column({ default: 0 })
missed_forward_fee_as_input: number
@Column({ default: 0 })
missed_forward_fee_as_output: number
@Column({ default: 0 })
forward_fee_as_input: number
@Column({ default: 0 })
forward_fee_as_output: number
@Column({ default: 0 })
latest_index_offset: number
@CreateDateColumn()
created_at: Date
@UpdateDateColumn()
updated_at: Date
}

View file

@ -1,49 +0,0 @@
import { Entity, PrimaryGeneratedColumn, Column, Index, Check, CreateDateColumn, UpdateDateColumn } from "typeorm"
@Entity()
export class RoutingEvent {
@PrimaryGeneratedColumn()
serial_id: number
@Column()
incoming_channel_id: number
@Column()
incoming_htlc_id: number
@Column()
outgoing_channel_id: number
@Column()
outgoing_htlc_id: number
@Column()
timestamp_ns: number
@Column()
event_type: string
@Column({ nullable: true })
incoming_amt_msat?: number
@Column({ nullable: true })
outgoing_amt_msat?: number
@Column({ nullable: true })
failure_string?: string
@Column({ nullable: true })
settled?: boolean
@Column({ nullable: true })
offchain?: boolean
@Column({ nullable: true })
forward_fail_event?: boolean
@CreateDateColumn()
created_at: Date
@UpdateDateColumn()
updated_at: Date
}

View file

@ -1,11 +1,12 @@
import { Between, DataSource, EntityManager, FindOperator, LessThanOrEqual, MoreThanOrEqual } from "typeorm" import { Between, DataSource, EntityManager, FindOperator, LessThanOrEqual, MoreThanOrEqual } from "typeorm"
import { RoutingEvent } from "./entity/RoutingEvent.js"
import { BalanceEvent } from "./entity/BalanceEvent.js" import { BalanceEvent } from "./entity/BalanceEvent.js"
import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js" import { ChannelBalanceEvent } from "./entity/ChannelsBalanceEvent.js"
import TransactionsQueue, { TX } from "./transactionsQueue.js"; import TransactionsQueue, { TX } from "./transactionsQueue.js";
import { StorageSettings } from "./index.js"; import { StorageSettings } from "./index.js";
import { newMetricsDb } from "./db.js"; import { newMetricsDb } from "./db.js";
import { ChannelRouting } from "./entity/ChannelRouting.js";
export default class { export default class {
DB: DataSource | EntityManager DB: DataSource | EntityManager
settings: StorageSettings settings: StorageSettings
txQueue: TransactionsQueue txQueue: TransactionsQueue
@ -18,10 +19,6 @@ export default class {
this.txQueue = new TransactionsQueue("metrics", this.DB) this.txQueue = new TransactionsQueue("metrics", this.DB)
return executedMigrations; return executedMigrations;
} }
async SaveRoutingEvent(event: Partial<RoutingEvent>) {
const entry = this.DB.getRepository(RoutingEvent).create(event)
return this.txQueue.PushToQueue<RoutingEvent>({ exec: async db => db.getRepository(RoutingEvent).save(entry), dbTx: false })
}
async SaveBalanceEvents(balanceEvent: Partial<BalanceEvent>, channelBalanceEvents: Partial<ChannelBalanceEvent>[]) { async SaveBalanceEvents(balanceEvent: Partial<BalanceEvent>, channelBalanceEvents: Partial<ChannelBalanceEvent>[]) {
const blanceEventEntry = this.DB.getRepository(BalanceEvent).create(balanceEvent) const blanceEventEntry = this.DB.getRepository(BalanceEvent).create(balanceEvent)
@ -32,27 +29,8 @@ export default class {
return { balanceEntry, channelsEntries } return { balanceEntry, channelsEntries }
} }
async GetRoutingEvents({ from, to }: { from?: number, to?: number }, entityManager = this.DB) {
let q: { where: { created_at: FindOperator<Date> } } | {} = {}
if (!!from && !!to) {
q = { where: { created_at: Between<Date>(new Date(from * 1000), new Date(to * 1000)) } }
} else if (!!from) {
q = { where: { created_at: MoreThanOrEqual<Date>(new Date(from * 1000)) } }
} else if (!!to) {
q = { where: { created_at: LessThanOrEqual<Date>(new Date(to * 1000)) } }
}
return entityManager.getRepository(RoutingEvent).find(q)
}
async GetBalanceEvents({ from, to }: { from?: number, to?: number }, entityManager = this.DB) { async GetBalanceEvents({ from, to }: { from?: number, to?: number }, entityManager = this.DB) {
let q: { where: { created_at: FindOperator<Date> } } | {} = {} const q = getTimeQuery({ from, to })
if (!!from && !!to) {
q = { where: { created_at: Between<Date>(new Date(from * 1000), new Date(to * 1000)) } }
} else if (!!from) {
q = { where: { created_at: MoreThanOrEqual<Date>(new Date(from * 1000)) } }
} else if (!!to) {
q = { where: { created_at: LessThanOrEqual<Date>(new Date(to * 1000)) } }
}
const [chainBalanceEvents, channelsBalanceEvents] = await Promise.all([ const [chainBalanceEvents, channelsBalanceEvents] = await Promise.all([
entityManager.getRepository(BalanceEvent).find(q), entityManager.getRepository(BalanceEvent).find(q),
@ -60,4 +38,77 @@ export default class {
]) ])
return { chainBalanceEvents, channelsBalanceEvents } return { chainBalanceEvents, channelsBalanceEvents }
} }
async initChannelRoutingEvent(dayUnix: number, channelId: string) {
const existing = await this.DB.getRepository(ChannelRouting).findOne({ where: { day_unix: dayUnix, channel_id: channelId } })
if (!existing) {
const entry = this.DB.getRepository(ChannelRouting).create({ day_unix: dayUnix, channel_id: channelId })
return this.txQueue.PushToQueue<ChannelRouting>({ exec: async db => db.getRepository(ChannelRouting).save(entry), dbTx: false })
}
return existing
}
GetChannelRouting({ from, to }: { from?: number, to?: number }, entityManager = this.DB) {
const q = getTimeQuery({ from, to })
return entityManager.getRepository(ChannelRouting).find(q)
}
async GetLatestForwardingIndexOffset() {
const latestIndex = await this.DB.getRepository(ChannelRouting).findOne({ order: { latest_index_offset: "DESC" } })
if (latestIndex) {
return latestIndex.latest_index_offset
}
return 0
}
async IncrementChannelRouting(channelId: string, event: Partial<ChannelRouting>) {
const dayUnix = getTodayUnix()
const existing = await this.initChannelRoutingEvent(dayUnix, channelId)
const repo = this.DB.getRepository(ChannelRouting)
if (event.send_errors) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "send_errors", event.send_errors)
}
if (event.receive_errors) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "receive_errors", event.receive_errors)
}
if (event.forward_errors_as_input) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "forwar_errors_as_input", event.forward_errors_as_input)
}
if (event.forward_errors_as_output) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "forwar_errors_as_output", event.forward_errors_as_output)
}
if (event.missed_forward_fee_as_input) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "missed_forward_fee_as_input", event.missed_forward_fee_as_input)
}
if (event.missed_forward_fee_as_output) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "missed_forward_fee_as_output", event.missed_forward_fee_as_output)
}
if (event.forward_fee_as_input) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "forward_fee_as_input", event.forward_fee_as_input)
}
if (event.forward_fee_as_output) {
await repo.increment({ day_unix: dayUnix, channel_id: channelId }, "forward_fee_as_output", event.forward_fee_as_output)
}
if (event.latest_index_offset) {
await repo.update(existing.serial_id, { latest_index_offset: event.latest_index_offset })
}
}
}
const getTimeQuery = ({ from, to }: { from?: number, to?: number }) => {
if (!!from && !!to) {
return { where: { created_at: Between<Date>(new Date(from * 1000), new Date(to * 1000)) } }
} else if (!!from) {
return { where: { created_at: MoreThanOrEqual<Date>(new Date(from * 1000)) } }
} else if (!!to) {
return { where: { created_at: LessThanOrEqual<Date>(new Date(to * 1000)) } }
}
return {}
}
const getTodayUnix = () => {
const now = new Date()
return new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime() / 1000
} }

View file

@ -0,0 +1,14 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class ChannelRouting1709316653538 implements MigrationInterface {
name = 'ChannelRouting1709316653538'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "channel_routing" ("serial_id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "day_unix" integer NOT NULL, "channel_id" varchar NOT NULL, "send_errors" integer NOT NULL DEFAULT (0), "receive_errors" integer NOT NULL DEFAULT (0), "forward_errors_as_input" integer NOT NULL DEFAULT (0), "forward_errors_as_output" integer NOT NULL DEFAULT (0), "missed_forward_fee_as_input" integer NOT NULL DEFAULT (0), "missed_forward_fee_as_output" integer NOT NULL DEFAULT (0), "forward_fee_as_input" integer NOT NULL DEFAULT (0), "forward_fee_as_output" integer NOT NULL DEFAULT (0), "latest_index_offset" integer NOT NULL DEFAULT (0), "created_at" datetime NOT NULL DEFAULT (datetime('now')), "updated_at" datetime NOT NULL DEFAULT (datetime('now')))`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "channel_routing"`);
}
}

View file

@ -3,7 +3,8 @@ import { DbSettings } from '../db.js'
import Storage, { StorageSettings } from '../index.js' import Storage, { StorageSettings } from '../index.js'
import { Initial1703170309875 } from './1703170309875-initial.js' import { Initial1703170309875 } from './1703170309875-initial.js'
import { LndMetrics1703170330183 } from './1703170330183-lnd_metrics.js' import { LndMetrics1703170330183 } from './1703170330183-lnd_metrics.js'
const allMigrations = [LndMetrics1703170330183] import { ChannelRouting1709316653538 } from './1709316653538-channel_routing.js'
const allMigrations = [LndMetrics1703170330183, ChannelRouting1709316653538]
export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Storage, settings: DbSettings, arg: string | undefined): Promise<boolean> => { export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Storage, settings: DbSettings, arg: string | undefined): Promise<boolean> => {
if (arg === 'initial_migration') { if (arg === 'initial_migration') {
await connectAndMigrate(log, storageManager, true, settings, [Initial1703170309875], []) await connectAndMigrate(log, storageManager, true, settings, [Initial1703170309875], [])
@ -11,6 +12,9 @@ export const TypeOrmMigrationRunner = async (log: PubLogger, storageManager: Sto
} else if (arg === 'lnd_metrics_migration') { } else if (arg === 'lnd_metrics_migration') {
await connectAndMigrate(log, storageManager, true, settings, [], [LndMetrics1703170330183]) await connectAndMigrate(log, storageManager, true, settings, [], [LndMetrics1703170330183])
return true return true
} else if (arg === 'channel_routing_migration') {
await connectAndMigrate(log, storageManager, true, settings, [], [ChannelRouting1709316653538])
return true
} else if (arg === 'all_migrations') { } else if (arg === 'all_migrations') {
await connectAndMigrate(log, storageManager, true, settings, [], allMigrations) await connectAndMigrate(log, storageManager, true, settings, [], allMigrations)
return true return true
@ -34,9 +38,9 @@ const connectAndMigrate = async (log: PubLogger, storageManager: Storage, manual
log(executedMigrations.length, "of", migrations.length, "migrations were executed correctly") log(executedMigrations.length, "of", migrations.length, "migrations were executed correctly")
log(executedMigrations) log(executedMigrations)
log("-------------------") log("-------------------")
} if (metricsMigrations.length > 0) { } if (metricsMigrations.length > 0) {
log(executedMetricsMigrations.length, "of", migrations.length, "metrics migrations were executed correctly") log(executedMetricsMigrations.length, "of", metricsMigrations.length, "metrics migrations were executed correctly")
log(executedMetricsMigrations) log(executedMetricsMigrations)
} }