detect links between channel close and open txs

This commit is contained in:
Mononaut
2022-11-10 12:21:19 -06:00
committed by softsimon
parent 50993d3b95
commit cf89ded14d
4 changed files with 402 additions and 3 deletions

View File

@@ -4,7 +4,7 @@ import logger from '../logger';
import { Common } from './common';
class DatabaseMigration {
private static currentVersion = 47;
private static currentVersion = 48;
private queryTimeout = 900_000;
private statisticsAddedIndexed = false;
private uniqueLogs: string[] = [];
@@ -379,7 +379,12 @@ class DatabaseMigration {
await this.$executeQuery(this.getCreateCPFPTableQuery(), await this.$checkIfTableExists('cpfp_clusters'));
await this.$executeQuery(this.getCreateTransactionsTableQuery(), await this.$checkIfTableExists('transactions'));
}
}
if (databaseSchemaVersion < 48 && isBitcoin === true) {
await this.$executeQuery('ALTER TABLE `channels` ADD source_checked tinyint(1) DEFAULT 0');
await this.$executeQuery(this.getCreateChannelsForensicsTableQuery(), await this.$checkIfTableExists('channels_forensics'));
}
}
/**
* Special case here for the `statistics` table - It appeared that somehow some dbs already had the `added` field indexed
@@ -759,6 +764,25 @@ class DatabaseMigration {
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`;
}
private getCreateChannelsForensicsTableQuery(): string {
return `CREATE TABLE IF NOT EXISTS channels_forensics (
channel_id bigint(11) unsigned NOT NULL,
closing_fee bigint(20) unsigned DEFAULT 0,
node1_funding_balance bigint(20) unsigned DEFAULT 0,
node2_funding_balance bigint(20) unsigned DEFAULT 0,
node1_closing_balance bigint(20) unsigned DEFAULT 0,
node2_closing_balance bigint(20) unsigned DEFAULT 0,
funding_ratio float unsigned DEFAULT NULL,
closed_by varchar(66) DEFAULT NULL,
single_funded tinyint(1) default 0,
outputs JSON NOT NULL,
PRIMARY KEY (channel_id),
FOREIGN KEY (channel_id)
REFERENCES channels (id)
ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;`;
}
private getCreateNodesStatsQuery(): string {
return `CREATE TABLE IF NOT EXISTS node_stats (
id int(11) unsigned NOT NULL AUTO_INCREMENT,

View File

@@ -128,6 +128,22 @@ class ChannelsApi {
}
}
public async $getChannelsWithoutSourceChecked(): Promise<any[]> {
try {
const query = `
SELECT channels.*, forensics.*
FROM channels
LEFT JOIN channels_forensics AS forensics ON forensics.channel_id = channels.id
WHERE channels.source_checked != 1
`;
const [rows]: any = await DB.query(query);
return rows;
} catch (e) {
logger.err('$getUnresolvedClosedChannels error: ' + (e instanceof Error ? e.message : e));
throw e;
}
}
public async $getChannelsWithoutCreatedDate(): Promise<any[]> {
try {
const query = `SELECT * FROM channels WHERE created IS NULL`;
@@ -145,12 +161,16 @@ class ChannelsApi {
SELECT n1.alias AS alias_left, n1.longitude as node1_longitude, n1.latitude as node1_latitude,
n2.alias AS alias_right, n2.longitude as node2_longitude, n2.latitude as node2_latitude,
channels.*,
ns1.channels AS channels_left, ns1.capacity AS capacity_left, ns2.channels AS channels_right, ns2.capacity AS capacity_right
ns1.channels AS channels_left, ns1.capacity AS capacity_left, ns2.channels AS channels_right, ns2.capacity AS capacity_right,
forensics.closing_fee as closing_fee, forensics.node1_funding_balance as node1_funding_balance, forensics.node2_funding_balance as node2_funding_balance,
forensics.funding_ratio as funding_ratio, forensics.node1_closing_balance as node1_closing_balance, forensics.node2_closing_balance as node2_closing_balance,
forensics.closed_by as closed_by, forensics.single_funded as single_funded
FROM channels
LEFT JOIN nodes AS n1 ON n1.public_key = channels.node1_public_key
LEFT JOIN nodes AS n2 ON n2.public_key = channels.node2_public_key
LEFT JOIN node_stats AS ns1 ON ns1.public_key = channels.node1_public_key
LEFT JOIN node_stats AS ns2 ON ns2.public_key = channels.node2_public_key
LEFT JOIN channels_forensics AS forensics ON forensics.channel_id = channels.id
WHERE (
ns1.id = (
SELECT MAX(id)
@@ -257,6 +277,118 @@ class ChannelsApi {
}
}
public async $getChannelForensicsByTransactionId(transactionId: string): Promise<any> {
try {
const query = `
SELECT
channels.id, channels.node1_public_key, channels.node2_public_key,
channels.closing_reason, channels.closing_transaction_id,
forensics.*
FROM channels
LEFT JOIN channels_forensics as forensics ON forensics.channel_id = channels.id
WHERE channels.closing_transaction_id = ?
`;
const [rows]: any = await DB.query(query, [transactionId]);
if (rows.length > 0) {
rows[0].outputs = JSON.parse(rows[0].outputs);
return rows[0];
}
} catch (e) {
logger.err('$getChannelForensicsByTransactionId error: ' + (e instanceof Error ? e.message : e));
// don't throw - this data isn't essential
}
}
public async $updateClosingInfo(channelInfo: { id: string, node1_closing_balance: number, node2_closing_balance: number, closed_by: string | null, closing_fee: number, outputs: ILightningApi.ForensicOutput[]}): Promise<void> {
try {
const query = `
INSERT INTO channels_forensics
(
channel_id,
node1_closing_balance,
node2_closing_balance,
closed_by,
closing_fee,
outputs
)
VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
node1_closing_balance = ?,
node2_closing_balance = ?,
closed_by = ?,
closing_fee = ?,
outputs = ?
`;
const jsonOutputs = JSON.stringify(channelInfo.outputs);
await DB.query<ResultSetHeader>(query, [
channelInfo.id,
channelInfo.node1_closing_balance,
channelInfo.node2_closing_balance,
channelInfo.closed_by,
channelInfo.closing_fee,
jsonOutputs,
channelInfo.node1_closing_balance,
channelInfo.node2_closing_balance,
channelInfo.closed_by,
channelInfo.closing_fee,
jsonOutputs
]);
} catch (e) {
logger.err('$updateClosingInfo error: ' + (e instanceof Error ? e.message : e));
// don't throw - this data isn't essential
}
}
public async $updateOpeningInfo(channelInfo: { id: string, node1_funding_balance: number, node2_funding_balance: number, funding_ratio: number, single_funded: boolean | void }): Promise<void> {
try {
const query = `
INSERT INTO channels_forensics
(
channel_id,
node1_funding_balance,
node2_funding_balance,
funding_ratio,
single_funded,
outputs
)
VALUES (?, ?, ?, ?, ?, 'null')
ON DUPLICATE KEY UPDATE
node1_funding_balance = ?,
node2_funding_balance = ?,
funding_ratio = ?,
single_funded = ?
`;
await DB.query<ResultSetHeader>(query, [
channelInfo.id,
channelInfo.node1_funding_balance,
channelInfo.node2_funding_balance,
channelInfo.funding_ratio,
channelInfo.single_funded ? 1 : 0,
channelInfo.node1_funding_balance,
channelInfo.node2_funding_balance,
channelInfo.funding_ratio,
channelInfo.single_funded ? 1 : 0,
]);
} catch (e) {
logger.err('$updateOpeningInfo error: ' + (e instanceof Error ? e.message : e));
// don't throw - this data isn't essential
}
}
public async $markChannelSourceChecked(id: string): Promise<void> {
try {
const query = `
UPDATE channels
SET source_checked = 1
WHERE id = ?
`;
await DB.query<ResultSetHeader>(query, [id]);
} catch (e) {
logger.err('$markChannelSourceChecked error: ' + (e instanceof Error ? e.message : e));
// don't throw - this data isn't essential
}
}
public async $getChannelsForNode(public_key: string, index: number, length: number, status: string): Promise<any[]> {
try {
let channelStatusFilter;
@@ -385,11 +517,15 @@ class ChannelsApi {
'transaction_id': channel.transaction_id,
'transaction_vout': channel.transaction_vout,
'closing_transaction_id': channel.closing_transaction_id,
'closing_fee': channel.closing_fee,
'closing_reason': channel.closing_reason,
'closing_date': channel.closing_date,
'updated_at': channel.updated_at,
'created': channel.created,
'status': channel.status,
'funding_ratio': channel.funding_ratio,
'closed_by': channel.closed_by,
'single_funded': !!channel.single_funded,
'node_left': {
'alias': channel.alias_left,
'public_key': channel.node1_public_key,
@@ -404,6 +540,9 @@ class ChannelsApi {
'updated_at': channel.node1_updated_at,
'longitude': channel.node1_longitude,
'latitude': channel.node1_latitude,
'funding_balance': channel.node1_funding_balance,
'closing_balance': channel.node1_closing_balance,
'initiated_close': channel.closed_by === channel.node1_public_key ? true : undefined,
},
'node_right': {
'alias': channel.alias_right,
@@ -419,6 +558,9 @@ class ChannelsApi {
'updated_at': channel.node2_updated_at,
'longitude': channel.node2_longitude,
'latitude': channel.node2_latitude,
'funding_balance': channel.node2_funding_balance,
'closing_balance': channel.node2_closing_balance,
'initiated_close': channel.closed_by === channel.node2_public_key ? true : undefined,
},
};
}

View File

@@ -83,4 +83,10 @@ export namespace ILightningApi {
is_required: boolean;
is_known: boolean;
}
export interface ForensicOutput {
node?: 1 | 2;
type: number;
value: number;
}
}