Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export default defineConfig([
},
rules: {
'@typescript-eslint/no-unused-vars': 'off',
'@typescript-eslint/no-this-alias': 'warn',
'@typescript-eslint/no-unused-expressions': 'off',
'sort-imports': 'off',
'import/order': 'off'
}
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-lib/lib/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ Address.getValidationError = function(data, network, type) {
*
* @param {string} data - The encoded data
* @param {String|Network} network - either a Network instance, 'livenet', or 'testnet'
* @param {string} type - The type of address: 'script' or 'pubkey'
* @param {string} [type] - The type of address: 'script' or 'pubkey'
* @returns {boolean} The corresponding error message
*/
Address.isValid = function(data, network, type) {
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcore-lib/lib/privatekey.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ const TaggedHash = require('./crypto/taggedhash');
* var imported = PrivateKey.fromWIF(exported);
* ```
*
* @param {string} data - The encoded data in various formats
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
* @param {string} [data] - The encoded data in various formats
* @param {Network|string=} [network] - a {@link Network} object, or a string with the network name
* @returns {PrivateKey} A new valid instance of an PrivateKey
* @constructor
*/
Expand Down
8 changes: 4 additions & 4 deletions packages/bitcore-lib/lib/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const TaggedHash = require('./crypto/taggedhash');
* var imported = PublicKey.fromString(exported);
* ```
*
* @param {string} data - The encoded data in various formats
* @param {Object} extra - additional options
* @param {string|PrivateKey} [data] - The encoded data in various formats
* @param {Object} [extra] - additional options
* @param {Network=} extra.network - Which network should the address for this public key be for
* @param {String=} extra.compressed - If the public key is compressed
* @returns {PublicKey} A new valid instance of an PublicKey
Expand Down Expand Up @@ -467,8 +467,8 @@ PublicKey.prototype._getID = function _getID() {
/**
* Will return an address for the public key
*
* @param {String|Network=} network - Which network should the address be for
* @param {string} type - Either 'pubkeyhash', 'witnesspubkeyhash', or 'scripthash'
* @param {String|Network=} [network] - Which network should the address be for
* @param {string} [type] - Either 'pubkeyhash', 'witnesspubkeyhash', or 'scripthash'
* @returns {Address} An address generated from the public key
*/
PublicKey.prototype.toAddress = function(network, type) {
Expand Down
5 changes: 3 additions & 2 deletions packages/bitcore-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@
"api:cluster": "npm run tsc && node build/src/workers/api.js --CLUSTER true",
"pruning": "npm run tsc && node build/src/workers/pruning.js --EXIT true",
"p2p": "npm run tsc && node build/src/workers/p2p.js",
"precommit": "echo 'TODO: npm run fix && npm run lint'",
"lint": "eslint .",
"fix": "eslint --fix --quiet .",
"fix:errors": "eslint --fix --quiet .",
"fix:all": "eslint --fix .",
"precommit": "npm run lint; npm run fix:errors",
"test:scripts": "npx mocha scripts/test"
},
"nyc": {
Expand Down
32 changes: 16 additions & 16 deletions packages/bitcore-node/scripts/addBlockFees.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/usr/bin/env node

const { Storage } = require('../build/src/services/storage');
const { BitcoinBlockStorage } = require('../build/src/models/block')
import { BitcoinBlockStorage } from '../build/src/models/block';
import { Storage } from '../build/src/services/storage';

function usage(errMsg) {
console.log('USAGE: ./addBlockFees [options]');
console.log('[OPTIONS]:');
console.log(' --chain <value> BTC, BCH, DOGE, or LTC');
console.log(' --network <value> mainnet, testnet, or regtest');
console.log(' --remove remove all fee data from chain on network')
console.log(' --reset remove and re add all fee data')
console.log(' --verbose enable verbose logging')
console.log(' --print-number number of times to print fee data insertions (default 20)')
console.log(' --pause-every number of blocks sync before pausing 50ms (default 10)')
console.log(' --remove remove all fee data from chain on network');
console.log(' --reset remove and re add all fee data');
console.log(' --verbose enable verbose logging');
console.log(' --print-number number of times to print fee data insertions (default 20)');
console.log(' --pause-every number of blocks sync before pausing 50ms (default 10)');
if (errMsg) {
console.error(errMsg);
}
Expand Down Expand Up @@ -55,13 +55,13 @@ Storage.start()
console.log(`Removing fee data from ${prevBlocksWithFeesCount} of ${totalBlocks} blocks on ${chain} ${network}`);
await BitcoinBlockStorage.collection.updateMany({ chain, network }, { $unset: { feeData: {} } });
const seconds = (Date.now() - startTime) / 1000;
console.log(`Finished in ${seconds} seconds | ${(prevBlocksWithFeesCount / seconds).toFixed(2)} blocks/sec`)
console.log(`Finished in ${seconds} seconds | ${(prevBlocksWithFeesCount / seconds).toFixed(2)} blocks/sec`);

const blocksWithFeesCount = await BitcoinBlockStorage.collection.countDocuments({ chain, network, feeData: { $exists: true } });
if (blocksWithFeesCount == 0) {
console.log('Successfully removed fee data from blocks')
console.log('Successfully removed fee data from blocks');
} else {
console.log(`${blocksWithFeesCount} blocks still have fee data`)
console.log(`${blocksWithFeesCount} blocks still have fee data`);
if (verbose) {
const blocksWithFees = await BitcoinBlockStorage.collection.find({ chain, network, feeData: { $exists: true } }).toArray();
process.stdout.write('Blocks with fees:');
Expand All @@ -75,7 +75,7 @@ Storage.start()
return;
}

const prevBlocksWithoutFeesCount = await BitcoinBlockStorage.collection.countDocuments({ chain, network, feeData: { $exists: false }});
const prevBlocksWithoutFeesCount = await BitcoinBlockStorage.collection.countDocuments({ chain, network, feeData: { $exists: false } });
if (prevBlocksWithoutFeesCount === 0) {
console.log(`${chain} ${network} already has fee data`);
return;
Expand All @@ -87,7 +87,7 @@ Storage.start()
let feeDataAddedCount = 0;

const stream = BitcoinBlockStorage.collection
.find({ chain, network, feeData: { $exists: false } }, { projection: { height: 1, _id: 0 }})
.find({ chain, network, feeData: { $exists: false } }, { projection: { height: 1, _id: 0 } })
.addCursorFlag('noCursorTimeout', true)
.stream();

Expand All @@ -96,7 +96,7 @@ Storage.start()
const fee = await BitcoinBlockStorage.getBlockFee({ chain, network, blockId: height });
feeDataAddedCount++;
if (feeDataAddedCount < prevBlocksWithoutFeesCount) {
BitcoinBlockStorage.collection.updateOne({ chain, network, height }, { $set: { feeData: fee } })
BitcoinBlockStorage.collection.updateOne({ chain, network, height }, { $set: { feeData: fee } });
// Resolve promise on last block
} else {
await BitcoinBlockStorage.collection.updateOne({ chain, network, height }, { $set: { feeData: fee } });
Expand All @@ -107,11 +107,11 @@ Storage.start()
await sleep(50);
}

console.log('100%')
console.log('100%');
const seconds = (Date.now() - startTime) / 1000;
console.log(`Finished in ${seconds} seconds | ${(prevBlocksWithoutFeesCount / seconds).toFixed(2)} blocks/sec`)
console.log(`Finished in ${seconds} seconds | ${(prevBlocksWithoutFeesCount / seconds).toFixed(2)} blocks/sec`);

const blocksWithoutFeesCount = await BitcoinBlockStorage.collection.countDocuments({ chain, network, feeData: { $exists: false }});
const blocksWithoutFeesCount = await BitcoinBlockStorage.collection.countDocuments({ chain, network, feeData: { $exists: false } });
if (blocksWithoutFeesCount == 0) {
console.log(`Successfully added fee data to blocks on ${chain} ${network}`);
} else {
Expand Down
20 changes: 10 additions & 10 deletions packages/bitcore-node/scripts/addGenesisBlock.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#!/usr/bin/env node

import * as readline from 'readline';
import Config from '../build/src/config';
import { BitcoinBlockStorage } from '../build/src/models/block';
import { BitcoinP2PWorker } from '../build/src/modules/bitcoin/p2p';
import { Storage } from '../build/src/services/storage';

const { Storage } = require('../build/src/services/storage');
const { BitcoinBlockStorage } = require('../build/src/models/block');
const Config = require('../build/src/config');
const { BitcoinP2PWorker } = require('../build/src/modules/bitcoin/p2p');

const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });

const rl = readline.createInterface({ input: process.stdin, output: process.stdout });

function usage(errMsg) {
console.log('USAGE: ./addGenesisBlock.js [options]');
Expand Down Expand Up @@ -35,7 +34,7 @@ if (!['BTC', 'BCH', 'DOGE', 'LTC'].includes(chain) || !['mainnet', 'testnet', 'r

const real = !!args.find(a => a === '--real');
console.log('Real:', real);
real && console.log('~~~~ REAL RUN ~~~~');
if (real) console.log('~~~~ REAL RUN ~~~~');

let p2pWorker;

Expand Down Expand Up @@ -79,7 +78,8 @@ Storage.start()
network
});
const cnt = await BitcoinBlockStorage.collection.countDocuments({ chain, network, height: 0 });
cnt == 1 ? console.log('Genesis block added') : console.log(`Somethings wrong. There are ${cnt} genesis blocks in the db`);
if (cnt == 1) console.log('Genesis block added');
else console.log(`Somethings wrong. There are ${cnt} genesis blocks in the db`);
} else {
console.log('Dry run. Genesis block not added.');
}
Expand All @@ -89,4 +89,4 @@ Storage.start()
rl.close();
p2pWorker?.disconnect();
Storage.stop();
})
});
8 changes: 4 additions & 4 deletions packages/bitcore-node/scripts/findReplacedTx.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node

const { Storage } = require('../build/src/services/storage');
const { TransactionStorage } = require('../build/src/models/transaction');
import { TransactionStorage } from '../build/src/models/transaction';
import { Storage } from '../build/src/services/storage';

function usage(errMsg) {
console.log('USAGE: ./findReplacedTx.js <options>');
console.log('OPTIONS:');
console.log(' --chain <value> REQUIRED - e.g. BTC, BCH, DOGE, LTC...');
console.log(' --network <value> REQUIRED - e.g. mainnet, testnet3, regtest...');
console.log(' --txid <value> REQUIRED Transaction Id that replaced');
console.log(' --window [value] Minutes to look back (default: 10)')
console.log(' --window [value] Minutes to look back (default: 10)');
if (errMsg) {
console.log('\nERROR: ' + errMsg);
}
Expand Down Expand Up @@ -49,7 +49,7 @@ Storage.start()
}
const $lt = new Date(confirmedTx.blockTimeNormalized);
const $gt = new Date($lt.getTime() - (1000 * 60 * windowMins));
const related = TransactionStorage.collection.find({ chain, network, blockTimeNormalized: { $lt, $gt }, blockHeight: -3 });
const related = TransactionStorage.collection.find({ chain, network, blockTimeNormalized: { $lt, $gt }, blockHeight: -3 });
for await (const tx of related) {
if (tx.replacedByTxid === txid) {
console.log(tx);
Expand Down
54 changes: 28 additions & 26 deletions packages/bitcore-node/scripts/fixOldSpentTxid.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
// eslint-disable-next-line spaced-comment
/****************************************
*** This migration script will fix historical instances
*** of an issue where coins that were used in a now invalid
Expand All @@ -12,22 +13,23 @@
*** select the chain and network to run against.
*** You must have valid RPC connection specified in bitcore.config.json.
********************************************/
const { CryptoRpc } = require('crypto-rpc');
const { TransactionStorage } = require('../build/src/models/transaction');
const { CoinStorage } = require('../build/src/models/coin');
const fs = require('fs');
import fs from 'fs';
import { CryptoRpc } from 'crypto-rpc';
import Config from '../build/src/config';
import { CoinStorage } from '../build/src/models/coin';
import { TransactionStorage } from '../build/src/models/transaction';
import { Storage } from '../build/src/services/storage';
import { wait } from '../build/src/utils/wait';

const fsPromises = fs.promises;
const { Storage } = require('../build/src/services/storage');
const { wait } = require('../build/src/utils/wait');
const Config = require('../build/src/config');

class Migration {
constructor({ transactionModel = TransactionStorage, coinModel = CoinStorage } = {}) {
this.transactionModel = transactionModel;
this.coinModel = coinModel;
}
async connect() {
console.log("Attempting connection to the database...")
console.log('Attempting connection to the database...');
try {
if (!Storage.connected) {
await Storage.start();
Expand All @@ -39,23 +41,23 @@ class Migration {
}

async endProcess() {
if (Storage.connected){
if (Storage.connected) {
await Storage.stop();
}
process.exit();
}

processArgs(argv) {
let retArgs = {
const retArgs = {
dryrun: true,
chain: '',
network: ''
};
let args = argv.slice(2);
const args = argv.slice(2);

const helpIdx = args.findIndex(i => i == '--help');
if (helpIdx >= 0) {
console.log("Usage: node fixOldSpentTxid.js --chain [CHAIN] --network [NETWORK] --dryrun [BOOL - default: true]");
console.log('Usage: node fixOldSpentTxid.js --chain [CHAIN] --network [NETWORK] --dryrun [BOOL - default: true]');
this.endProcess();
}

Expand All @@ -65,8 +67,8 @@ class Migration {
args[dryRunIdx + 1] == undefined || args[dryRunIdx + 1] == 'true'
? true
: args[dryRunIdx + 1] == 'false'
? false
: true;
? false
: true;
}

const chainIdx = args.findIndex(i => i == '--chain');
Expand All @@ -80,7 +82,7 @@ class Migration {
}

if (!retArgs.chain || !retArgs.network) {
console.log("You must specify a chain and network for the script to run on. Use --help for more info.");
console.log('You must specify a chain and network for the script to run on. Use --help for more info.');
this.endProcess();
}

Expand All @@ -89,14 +91,14 @@ class Migration {

async runScript(args) {
console.log('Running script with these args: ', args);
let output = {};
let actuallySpent = {};
const output = {};
const actuallySpent = {};
const { chain, network, dryrun } = args;
console.log(`Checking records for ${chain}:${network}`);
// Get all unspent coins that have a spentTxid specified
const stream = this.coinModel.collection
.find(
{ chain, network, mintHeight: { $gt: -1 }, spentHeight: -2, spentTxid: {$exists: true, $ne: null, $ne: ""} } // -2 is unspent status
{ chain, network, mintHeight: { $gt: -1 }, spentHeight: -2, spentTxid: { $exists: true, $and: [{ $ne: null }, { $ne: '' }] } } // -2 is unspent status
)
.addCursorFlag('noCursorTimeout', true);

Expand Down Expand Up @@ -134,7 +136,7 @@ class Migration {
});
isUnspent = !!coinData;
} catch (e) {
if (e.message && e.message.match(`No info found for ${data.mintTxid}`)){
if (e.message && e.message.match(`No info found for ${data.mintTxid}`)) {
// Coin must be spent or actually pending in mempool - log so that we can diagnose, this is unexpected
if (actuallySpent[`${chain}-${network}`]) {
actuallySpent[`${chain}-${network}`].push(data);
Expand Down Expand Up @@ -170,20 +172,20 @@ class Migration {
console.log(`Writing output to ${filename}`);
try {
await fsPromises.writeFile(filename, JSON.stringify(output));
} catch (e) {
} catch {
// write to stdout
console.log('Failed to write output to file. Writing to stdout instead.');
console.log(output);
}
// if we found some spent coins mislabelled as unspent, log it
if (actuallySpent[`${chain}-${network}`]) {
console.log("WARNING: Found some coins that were marked unspent that were actually spent.");
console.log('WARNING: Found some coins that were marked unspent that were actually spent.');
const date = new Date().getTime();
const filename = `fixOldSpentTxid-actually-spent-${chain}-${network}-${date}.log`;
console.log(`Writing them to ${filename}`);
try {
await fsPromises.writeFile(filename, JSON.stringify(actuallySpent));
} catch (e) {
} catch {
// write to stdout
console.log('Failed to write output to file. Writing to stdout instead.');
console.log(output);
Expand All @@ -208,8 +210,8 @@ migration
.catch(err => {
console.error(err);
migration.endProcess()
.catch(err => {
console.error(err);
process.exit(1);
});
.catch(err => {
console.error(err);
process.exit(1);
});
});
Loading