From 022785a5552099f3a4b9c66684be3874d291683f Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 20 Oct 2022 02:09:02 +0000 Subject: [PATCH 1/2] Fix ln close classification logic --- .../tasks/lightning/network-sync.service.ts | 71 +++++++++++-------- 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/backend/src/tasks/lightning/network-sync.service.ts b/backend/src/tasks/lightning/network-sync.service.ts index 8d8f1759f..838170a3e 100644 --- a/backend/src/tasks/lightning/network-sync.service.ts +++ b/backend/src/tasks/lightning/network-sync.service.ts @@ -289,6 +289,24 @@ class NetworkSyncService { 1. Mutually closed 2. Forced closed 3. Forced closed with penalty + + ┌────────────────────────────────────┐ ┌────────────────────────────┐ + │ outputs contain revocation script? ├──yes──► force close w/ penalty = 3 │ + └──────────────┬─────────────────────┘ └────────────────────────────┘ + no + ┌──────────────▼──────────────────────────┐ + │ outputs contain other lightning script? ├──┐ + └──────────────┬──────────────────────────┘ │ + no yes + ┌──────────────▼─────────────┐ │ + │ sequence starts with 0x80 │ ┌────────▼────────┐ + │ and ├──────► force close = 2 │ + │ locktime starts with 0x20? │ └─────────────────┘ + └──────────────┬─────────────┘ + no + ┌─────────▼────────┐ + │ mutual close = 1 │ + └──────────────────┘ */ private async $runClosedChannelsForensics(): Promise { @@ -326,36 +344,31 @@ class NetworkSyncService { lightningScriptReasons.push(lightningScript); } } - if (lightningScriptReasons.length === outspends.length - && lightningScriptReasons.filter((r) => r === 1).length === outspends.length) { - reason = 1; - } else { - const filteredReasons = lightningScriptReasons.filter((r) => r !== 1); - if (filteredReasons.length) { - if (filteredReasons.some((r) => r === 2 || r === 4)) { - reason = 3; - } else { - reason = 2; - } + const filteredReasons = lightningScriptReasons.filter((r) => r !== 1); + if (filteredReasons.length) { + if (filteredReasons.some((r) => r === 2 || r === 4)) { + reason = 3; } else { - /* - We can detect a commitment transaction (force close) by reading Sequence and Locktime - https://github.com/lightning/bolts/blob/master/03-transactions.md#commitment-transaction - */ - let closingTx: IEsploraApi.Transaction | undefined; - try { - closingTx = await bitcoinApi.$getRawTransaction(channel.closing_transaction_id); - } catch (e) { - logger.err(`Failed to call ${config.ESPLORA.REST_API_URL + '/tx/' + channel.closing_transaction_id}. Reason ${e instanceof Error ? e.message : e}`); - continue; - } - const sequenceHex: string = closingTx.vin[0].sequence.toString(16); - const locktimeHex: string = closingTx.locktime.toString(16); - if (sequenceHex.substring(0, 2) === '80' && locktimeHex.substring(0, 2) === '20') { - reason = 2; // Here we can't be sure if it's a penalty or not - } else { - reason = 1; - } + reason = 2; + } + } else { + /* + We can detect a commitment transaction (force close) by reading Sequence and Locktime + https://github.com/lightning/bolts/blob/master/03-transactions.md#commitment-transaction + */ + let closingTx: IEsploraApi.Transaction | undefined; + try { + closingTx = await bitcoinApi.$getRawTransaction(channel.closing_transaction_id); + } catch (e) { + logger.err(`Failed to call ${config.ESPLORA.REST_API_URL + '/tx/' + channel.closing_transaction_id}. Reason ${e instanceof Error ? e.message : e}`); + continue; + } + const sequenceHex: string = closingTx.vin[0].sequence.toString(16); + const locktimeHex: string = closingTx.locktime.toString(16); + if (sequenceHex.substring(0, 2) === '80' && locktimeHex.substring(0, 2) === '20') { + reason = 2; // Here we can't be sure if it's a penalty or not + } else { + reason = 1; } } if (reason) { From a5c4f8e2f31fbb8f734b7b2408615d421973c731 Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 21 Oct 2022 22:47:14 +0400 Subject: [PATCH 2/2] Adding migration to force rescan of closed channels --- backend/src/api/database-migration.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backend/src/api/database-migration.ts b/backend/src/api/database-migration.ts index 1dc0b9704..ec201de0c 100644 --- a/backend/src/api/database-migration.ts +++ b/backend/src/api/database-migration.ts @@ -4,7 +4,7 @@ import logger from '../logger'; import { Common } from './common'; class DatabaseMigration { - private static currentVersion = 40; + private static currentVersion = 41; private queryTimeout = 120000; private statisticsAddedIndexed = false; private uniqueLogs: string[] = []; @@ -348,6 +348,10 @@ class DatabaseMigration { await this.$executeQuery('ALTER TABLE `nodes` ADD channels int(11) unsigned DEFAULT NULL'); await this.$executeQuery('ALTER TABLE `nodes` ADD INDEX `capacity` (`capacity`);'); } + + if (databaseSchemaVersion < 41 && isBitcoin === true) { + await this.$executeQuery('UPDATE channels SET closing_reason = NULL WHERE closing_reason = 1'); + } } /**