11#!/usr/bin/env node
2+ // eslint-disable-next-line spaced-comment
23/****************************************
34 *** This migration script will fix historical instances
45 *** of an issue where coins that were used in a now invalid
1213 *** select the chain and network to run against.
1314 *** You must have valid RPC connection specified in bitcore.config.json.
1415 ********************************************/
15- const { CryptoRpc } = require ( 'crypto-rpc' ) ;
16- const { TransactionStorage } = require ( '../build/src/models/transaction' ) ;
17- const { CoinStorage } = require ( '../build/src/models/coin' ) ;
18- const fs = require ( 'fs' ) ;
16+ import fs from 'fs' ;
17+ import { CryptoRpc } from 'crypto-rpc' ;
18+ import Config from '../build/src/config' ;
19+ import { CoinStorage } from '../build/src/models/coin' ;
20+ import { TransactionStorage } from '../build/src/models/transaction' ;
21+ import { Storage } from '../build/src/services/storage' ;
22+ import { wait } from '../build/src/utils/wait' ;
23+
1924const fsPromises = fs . promises ;
20- const { Storage } = require ( '../build/src/services/storage' ) ;
21- const { wait } = require ( '../build/src/utils/wait' ) ;
22- const Config = require ( '../build/src/config' ) ;
2325
2426class Migration {
2527 constructor ( { transactionModel = TransactionStorage , coinModel = CoinStorage } = { } ) {
2628 this . transactionModel = transactionModel ;
2729 this . coinModel = coinModel ;
2830 }
2931 async connect ( ) {
30- console . log ( " Attempting connection to the database..." )
32+ console . log ( ' Attempting connection to the database...' ) ;
3133 try {
3234 if ( ! Storage . connected ) {
3335 await Storage . start ( ) ;
@@ -39,23 +41,23 @@ class Migration {
3941 }
4042
4143 async endProcess ( ) {
42- if ( Storage . connected ) {
44+ if ( Storage . connected ) {
4345 await Storage . stop ( ) ;
4446 }
4547 process . exit ( ) ;
4648 }
4749
4850 processArgs ( argv ) {
49- let retArgs = {
51+ const retArgs = {
5052 dryrun : true ,
5153 chain : '' ,
5254 network : ''
5355 } ;
54- let args = argv . slice ( 2 ) ;
56+ const args = argv . slice ( 2 ) ;
5557
5658 const helpIdx = args . findIndex ( i => i == '--help' ) ;
5759 if ( helpIdx >= 0 ) {
58- console . log ( " Usage: node fixOldSpentTxid.js --chain [CHAIN] --network [NETWORK] --dryrun [BOOL - default: true]" ) ;
60+ console . log ( ' Usage: node fixOldSpentTxid.js --chain [CHAIN] --network [NETWORK] --dryrun [BOOL - default: true]' ) ;
5961 this . endProcess ( ) ;
6062 }
6163
@@ -65,8 +67,8 @@ class Migration {
6567 args [ dryRunIdx + 1 ] == undefined || args [ dryRunIdx + 1 ] == 'true'
6668 ? true
6769 : args [ dryRunIdx + 1 ] == 'false'
68- ? false
69- : true ;
70+ ? false
71+ : true ;
7072 }
7173
7274 const chainIdx = args . findIndex ( i => i == '--chain' ) ;
@@ -80,7 +82,7 @@ class Migration {
8082 }
8183
8284 if ( ! retArgs . chain || ! retArgs . network ) {
83- console . log ( " You must specify a chain and network for the script to run on. Use --help for more info." ) ;
85+ console . log ( ' You must specify a chain and network for the script to run on. Use --help for more info.' ) ;
8486 this . endProcess ( ) ;
8587 }
8688
@@ -89,14 +91,14 @@ class Migration {
8991
9092 async runScript ( args ) {
9193 console . log ( 'Running script with these args: ' , args ) ;
92- let output = { } ;
93- let actuallySpent = { } ;
94+ const output = { } ;
95+ const actuallySpent = { } ;
9496 const { chain, network, dryrun } = args ;
9597 console . log ( `Checking records for ${ chain } :${ network } ` ) ;
9698 // Get all unspent coins that have a spentTxid specified
9799 const stream = this . coinModel . collection
98100 . find (
99- { chain, network, mintHeight : { $gt : - 1 } , spentHeight : - 2 , spentTxid : { $exists : true , $ne : null , $ne : "" } } // -2 is unspent status
101+ { chain, network, mintHeight : { $gt : - 1 } , spentHeight : - 2 , spentTxid : { $exists : true , $and : [ { $ ne : null } , { $ne : '' } ] } } // -2 is unspent status
100102 )
101103 . addCursorFlag ( 'noCursorTimeout' , true ) ;
102104
@@ -134,7 +136,7 @@ class Migration {
134136 } ) ;
135137 isUnspent = ! ! coinData ;
136138 } catch ( e ) {
137- if ( e . message && e . message . match ( `No info found for ${ data . mintTxid } ` ) ) {
139+ if ( e . message && e . message . match ( `No info found for ${ data . mintTxid } ` ) ) {
138140 // Coin must be spent or actually pending in mempool - log so that we can diagnose, this is unexpected
139141 if ( actuallySpent [ `${ chain } -${ network } ` ] ) {
140142 actuallySpent [ `${ chain } -${ network } ` ] . push ( data ) ;
@@ -170,20 +172,20 @@ class Migration {
170172 console . log ( `Writing output to ${ filename } ` ) ;
171173 try {
172174 await fsPromises . writeFile ( filename , JSON . stringify ( output ) ) ;
173- } catch ( e ) {
175+ } catch {
174176 // write to stdout
175177 console . log ( 'Failed to write output to file. Writing to stdout instead.' ) ;
176178 console . log ( output ) ;
177179 }
178180 // if we found some spent coins mislabelled as unspent, log it
179181 if ( actuallySpent [ `${ chain } -${ network } ` ] ) {
180- console . log ( " WARNING: Found some coins that were marked unspent that were actually spent." ) ;
182+ console . log ( ' WARNING: Found some coins that were marked unspent that were actually spent.' ) ;
181183 const date = new Date ( ) . getTime ( ) ;
182184 const filename = `fixOldSpentTxid-actually-spent-${ chain } -${ network } -${ date } .log` ;
183185 console . log ( `Writing them to ${ filename } ` ) ;
184186 try {
185187 await fsPromises . writeFile ( filename , JSON . stringify ( actuallySpent ) ) ;
186- } catch ( e ) {
188+ } catch {
187189 // write to stdout
188190 console . log ( 'Failed to write output to file. Writing to stdout instead.' ) ;
189191 console . log ( output ) ;
@@ -208,8 +210,8 @@ migration
208210 . catch ( err => {
209211 console . error ( err ) ;
210212 migration . endProcess ( )
211- . catch ( err => {
212- console . error ( err ) ;
213- process . exit ( 1 ) ;
214- } ) ;
213+ . catch ( err => {
214+ console . error ( err ) ;
215+ process . exit ( 1 ) ;
216+ } ) ;
215217 } ) ;
0 commit comments