bitcore-node
Version:
A blockchain indexing node with extended capabilities using bitcore
133 lines • 5.54 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWallet = createWallet;
const bitcore_client_1 = require("bitcore-client");
const _ = __importStar(require("lodash"));
const coin_1 = require("../../src/models/coin");
const storage_1 = require("../../src/services/storage");
async function getAllAddressesFromBlocks(start, end) {
if (!storage_1.Storage.connected)
await storage_1.Storage.start({});
const coins = await coin_1.CoinStorage.collection
.find({ chain: 'BTC', network: 'mainnet', mintHeight: { $gte: start, $lte: end } })
.project({ address: 1 })
.toArray();
const uniqueAddresses = _.uniq(coins.map(c => c.address));
return uniqueAddresses;
}
async function createWallet(addresses, iteration, networkName) {
const walletName = 'Benchmark Wallet' + iteration;
const password = 'iamsatoshi';
const chain = 'BTC';
const network = networkName || 'mainnet';
const baseUrl = 'http://localhost:3000/api';
let lockedWallet;
try {
lockedWallet = await bitcore_client_1.Wallet.loadWallet({ name: walletName });
}
catch (err) {
lockedWallet = await bitcore_client_1.Wallet.create({
name: walletName,
chain,
network,
baseUrl,
password
});
}
await lockedWallet.register({ baseUrl });
if (addresses.length > 0) {
const unlockedWallet = await lockedWallet.unlock(password);
const keysToImport = addresses.map(a => ({ address: a }));
await unlockedWallet.importKeys({ keys: keysToImport });
}
return lockedWallet;
}
async function benchMarkUtxoList(unlockedWallet, addresses, includeSpent = false) {
const utxoListStart = new Date();
const utxoStream = unlockedWallet.getUtxos({ includeSpent });
const utxoBenchmark = new Promise(resolve => {
const utxos = new Array();
utxoStream
.on('data', data => {
const stringData = data.toString().replace(',\n', '');
if (stringData.includes('{') && stringData.includes('}')) {
utxos.push(JSON.parse(stringData));
}
})
.on('complete', () => {
const includeUnspentMsg = includeSpent ? '(+spent)' : '';
console.log(`Listed ${includeUnspentMsg} `, (utxos || []).length, ' utxos for a wallet with', addresses.length, 'addresses. Took ', new Date().getTime() - utxoListStart.getTime(), ' ms');
resolve(utxos);
});
});
await utxoBenchmark;
}
async function bench(iteration = 0, startBlock = 0, endBlock = 100) {
console.log('Benchmark', iteration, 'START');
const addresses = await getAllAddressesFromBlocks(startBlock, endBlock);
console.log('Collected', addresses.length, 'addresses');
const walletCreationStart = new Date();
const unlockedWallet = await createWallet(addresses, iteration);
console.log('Create a wallet with', addresses.length, 'addresses. Took ', new Date().getTime() - walletCreationStart.getTime(), ' ms');
await benchMarkUtxoList(unlockedWallet, addresses);
await benchMarkUtxoList(unlockedWallet, addresses, true);
const walletTxListStart = new Date();
const txStream = unlockedWallet.listTransactions({ startBlock, endBlock });
let benchmarkComplete = new Promise(resolve => {
const txs = new Array();
txStream.on('data', data => {
const stringData = data.toString().replace(',\n', '');
if (stringData.includes('{') && stringData.includes('}')) {
txs.push(JSON.parse(stringData));
}
});
txStream.on('complete', () => {
console.log('Listed ', txs.length, ' txs for a wallet with', addresses.length, 'addresses. Took ', new Date().getTime() - walletTxListStart.getTime(), ' ms');
resolve(txs);
});
});
await benchmarkComplete;
}
async function main() {
for (let i = 1; i < 6; i++) {
await bench(i, 0, Math.pow(10, i));
}
process.exit(0);
}
if (require.main === module) {
main();
}
//# sourceMappingURL=wallet-benchmark.js.map