Rescan unresolved LN channel force closes
This commit is contained in:
		
							parent
							
								
									02820b0e68
								
							
						
					
					
						commit
						c1e741a025
					
				| @ -4,7 +4,7 @@ import logger from '../logger'; | |||||||
| import { Common } from './common'; | import { Common } from './common'; | ||||||
| 
 | 
 | ||||||
| class DatabaseMigration { | class DatabaseMigration { | ||||||
|   private static currentVersion = 41; |   private static currentVersion = 42; | ||||||
|   private queryTimeout = 120000; |   private queryTimeout = 120000; | ||||||
|   private statisticsAddedIndexed = false; |   private statisticsAddedIndexed = false; | ||||||
|   private uniqueLogs: string[] = []; |   private uniqueLogs: string[] = []; | ||||||
| @ -352,6 +352,10 @@ class DatabaseMigration { | |||||||
|     if (databaseSchemaVersion < 41 && isBitcoin === true) { |     if (databaseSchemaVersion < 41 && isBitcoin === true) { | ||||||
|       await this.$executeQuery('UPDATE channels SET closing_reason = NULL WHERE closing_reason = 1'); |       await this.$executeQuery('UPDATE channels SET closing_reason = NULL WHERE closing_reason = 1'); | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     if (databaseSchemaVersion < 42 && isBitcoin === true) { | ||||||
|  |       await this.$executeQuery('ALTER TABLE `channels` ADD closing_resolved tinyint(1) DEFAULT 0'); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /** |   /** | ||||||
|  | |||||||
| @ -117,6 +117,17 @@ class ChannelsApi { | |||||||
|     } |     } | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   public async $getUnresolvedClosedChannels(): Promise<any[]> { | ||||||
|  |     try { | ||||||
|  |       const query = `SELECT * FROM channels WHERE status = 2 AND closing_reason = 2 AND closing_resolved = 0 AND closing_transaction_id != ''`; | ||||||
|  |       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[]> { |   public async $getChannelsWithoutCreatedDate(): Promise<any[]> { | ||||||
|     try { |     try { | ||||||
|       const query = `SELECT * FROM channels WHERE created IS NULL`; |       const query = `SELECT * FROM channels WHERE created IS NULL`; | ||||||
|  | |||||||
| @ -309,7 +309,7 @@ class NetworkSyncService { | |||||||
|          └──────────────────┘ |          └──────────────────┘ | ||||||
|   */ |   */ | ||||||
| 
 | 
 | ||||||
|   private async $runClosedChannelsForensics(): Promise<void> { |   private async $runClosedChannelsForensics(skipUnresolved: boolean = false): Promise<void> { | ||||||
|     if (!config.ESPLORA.REST_API_URL) { |     if (!config.ESPLORA.REST_API_URL) { | ||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
| @ -318,9 +318,18 @@ class NetworkSyncService { | |||||||
| 
 | 
 | ||||||
|     try { |     try { | ||||||
|       logger.info(`Started running closed channel forensics...`); |       logger.info(`Started running closed channel forensics...`); | ||||||
|       const channels = await channelsApi.$getClosedChannelsWithoutReason(); |       let channels; | ||||||
|  |       const closedChannels = await channelsApi.$getClosedChannelsWithoutReason(); | ||||||
|  |       if (skipUnresolved) { | ||||||
|  |         channels = closedChannels; | ||||||
|  |       } else { | ||||||
|  |         const unresolvedChannels = await channelsApi.$getUnresolvedClosedChannels(); | ||||||
|  |         channels = [...closedChannels, ...unresolvedChannels]; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|       for (const channel of channels) { |       for (const channel of channels) { | ||||||
|         let reason = 0; |         let reason = 0; | ||||||
|  |         let resolvedForceClose = false; | ||||||
|         // Only Esplora backend can retrieve spent transaction outputs
 |         // Only Esplora backend can retrieve spent transaction outputs
 | ||||||
|         try { |         try { | ||||||
|           let outspends: IEsploraApi.Outspend[] | undefined; |           let outspends: IEsploraApi.Outspend[] | undefined; | ||||||
| @ -350,6 +359,7 @@ class NetworkSyncService { | |||||||
|               reason = 3; |               reason = 3; | ||||||
|             } else { |             } else { | ||||||
|               reason = 2; |               reason = 2; | ||||||
|  |               resolvedForceClose = true; | ||||||
|             } |             } | ||||||
|           } else { |           } else { | ||||||
|             /* |             /* | ||||||
| @ -374,6 +384,9 @@ class NetworkSyncService { | |||||||
|           if (reason) { |           if (reason) { | ||||||
|             logger.debug('Setting closing reason ' + reason + ' for channel: ' + channel.id + '.'); |             logger.debug('Setting closing reason ' + reason + ' for channel: ' + channel.id + '.'); | ||||||
|             await DB.query(`UPDATE channels SET closing_reason = ? WHERE id = ?`, [reason, channel.id]); |             await DB.query(`UPDATE channels SET closing_reason = ? WHERE id = ?`, [reason, channel.id]); | ||||||
|  |             if (reason === 2 && resolvedForceClose) { | ||||||
|  |               await DB.query(`UPDATE channels SET closing_resolved = ? WHERE id = ?`, [true, channel.id]); | ||||||
|  |             } | ||||||
|           } |           } | ||||||
|         } catch (e) { |         } catch (e) { | ||||||
|           logger.err(`$runClosedChannelsForensics() failed for channel ${channel.short_id}. Reason: ${e instanceof Error ? e.message : e}`); |           logger.err(`$runClosedChannelsForensics() failed for channel ${channel.short_id}. Reason: ${e instanceof Error ? e.message : e}`); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user