Skip to content

Commit

Permalink
bcoin: use buffer-map. see #533.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj authored and tuxcanfly committed Sep 3, 2018
1 parent 4500cac commit 7034a1a
Show file tree
Hide file tree
Showing 74 changed files with 810 additions and 809 deletions.
12 changes: 6 additions & 6 deletions bench/tx.js
Expand Up @@ -72,7 +72,7 @@ const tx10 = common.readTX('tx10');
const end = bench('input hashes');

for (let i = 0; i < 10000; i++)
tx.getInputHashes(null, 'hex');
tx.getInputHashes();

end(10000);
}
Expand All @@ -82,7 +82,7 @@ const tx10 = common.readTX('tx10');
const end = bench('output hashes');

for (let i = 0; i < 10000; i++)
tx.getOutputHashes('hex');
tx.getOutputHashes();

end(10000);
}
Expand All @@ -92,7 +92,7 @@ const tx10 = common.readTX('tx10');
const end = bench('all hashes');

for (let i = 0; i < 10000; i++)
tx.getHashes(null, 'hex');
tx.getHashes();

end(10000);
}
Expand Down Expand Up @@ -164,7 +164,7 @@ const tx2 = mtx.toTX();
const end = bench('input hashes');

for (let i = 0; i < 10000; i++)
tx2.getInputHashes(null, 'hex');
tx2.getInputHashes();

end(10000);
}
Expand All @@ -173,7 +173,7 @@ const tx2 = mtx.toTX();
const end = bench('output hashes');

for (let i = 0; i < 10000; i++)
tx2.getOutputHashes('hex');
tx2.getOutputHashes();

end(10000);
}
Expand All @@ -182,7 +182,7 @@ const tx2 = mtx.toTX();
const end = bench('all hashes');

for (let i = 0; i < 10000; i++)
tx2.getHashes(null, 'hex');
tx2.getHashes();

end(10000);
}
2 changes: 1 addition & 1 deletion bench/walletdb.js
Expand Up @@ -7,7 +7,7 @@ const MTX = require('../lib/primitives/mtx');
const Outpoint = require('../lib/primitives/outpoint');

function dummy() {
const hash = random.randomBytes(32).toString('hex');
const hash = random.randomBytes(32);
return new Outpoint(hash, 0);
}

Expand Down
2 changes: 2 additions & 0 deletions bin/node
Expand Up @@ -2,6 +2,8 @@

'use strict';

Buffer.poolSize = 2;

process.title = 'bcash';

if (process.argv.indexOf('--help') !== -1
Expand Down
4 changes: 3 additions & 1 deletion browser/src/app.js
Expand Up @@ -4,6 +4,7 @@ const Logger = require('blgr');
const FullNode = require('../../lib/node/fullnode');
const Amount = require('../../lib/btc/amount');
const plugin = require('../../lib/wallet/plugin');
const util = require('../../lib/utils/util');
const ProxySocket = require('./proxysocket');

const body = document.getElementsByTagName('body')[0];
Expand Down Expand Up @@ -267,8 +268,9 @@ async function _formatWallet(wallet) {
wdiv.innerHTML = html;

for (const tx of det) {
const hash = util.revHex(tx.hash);
const el = create(
`<a style="display:block;" href="#${tx.hash}">${tx.hash}</a>`);
`<a style="display:block;" href="#${hash}">${hash}</a>`);
wdiv.appendChild(el);
setMouseup(el, tx.toJSON());
}
Expand Down
4 changes: 2 additions & 2 deletions docs/Examples/wallet.js
Expand Up @@ -4,7 +4,7 @@ const bcoin = require('../..');
const random = require('bcrypto/lib/random');

function dummy() {
const hash = random.randomBytes(32).toString('hex');
const hash = random.randomBytes(32);
return new bcoin.Outpoint(hash, 0);
}

Expand Down Expand Up @@ -36,7 +36,7 @@ const walletdb = new bcoin.wallet.WalletDB({

await walletdb.addTX(tx);

const wtx = await wallet.getTX(tx.hash('hex'));
const wtx = await wallet.getTX(tx.hash());

console.log('Added transaction');
console.log(wtx);
Expand Down
53 changes: 27 additions & 26 deletions lib/blockchain/chain.js
Expand Up @@ -14,6 +14,7 @@ const Logger = require('blgr');
const {Lock} = require('bmutex');
const BN = require('bcrypto/lib/bn.js');
const LRU = require('blru');
const {BufferMap} = require('buffer-map');
const Network = require('../protocol/network');
const ChainDB = require('./chaindb');
const common = require('./common');
Expand Down Expand Up @@ -53,16 +54,16 @@ class Chain extends AsyncEmitter {

this.db = new ChainDB(this.options);

this.locker = new Lock(true);
this.invalid = new LRU(100);
this.locker = new Lock(true, BufferMap);
this.invalid = new LRU(100, null, BufferMap);
this.state = new DeploymentState();

this.tip = new ChainEntry();
this.height = -1;
this.synced = false;

this.orphanMap = new Map();
this.orphanPrev = new Map();
this.orphanMap = new BufferMap();
this.orphanPrev = new BufferMap();
}

/**
Expand Down Expand Up @@ -322,11 +323,11 @@ class Chain extends AsyncEmitter {
assert(typeof flags === 'number');

// Extra sanity check.
if (block.prevBlock !== prev.hash)
if (!block.prevBlock.equals(prev.hash))
throw new VerifyError(block, 'invalid', 'bad-prevblk', 0);

// Verify a checkpoint if there is one.
const hash = block.hash('hex');
const hash = block.hash();
if (!this.verifyCheckpoint(prev, hash)) {
throw new VerifyError(block,
'checkpoint',
Expand All @@ -343,9 +344,9 @@ class Chain extends AsyncEmitter {
if (flags & common.flags.VERIFY_BODY) {
assert(typeof block.createMerkleRoot === 'function');

const root = block.createMerkleRoot('hex');
const root = block.createMerkleRoot();

if (!root || block.merkleRoot !== root) {
if (!root || !block.merkleRoot.equals(root)) {
throw new VerifyError(block,
'invalid',
'bad-txnmrklroot',
Expand Down Expand Up @@ -594,7 +595,7 @@ class Chain extends AsyncEmitter {
// Blocks 91842 and 91880 created duplicate
// txids by using the same exact output script
// and extraNonce.
if (!hash || block.hash('hex') !== hash)
if (!hash || !block.hash().equals(hash))
throw new VerifyError(block, 'invalid', 'bad-txns-BIP30', 100);
}
}
Expand Down Expand Up @@ -754,14 +755,14 @@ class Chain extends AsyncEmitter {
*/

async findFork(fork, longer) {
while (fork.hash !== longer.hash) {
while (!fork.hash.equals(longer.hash)) {
while (longer.height > fork.height) {
longer = await this.getPrevious(longer);
if (!longer)
throw new Error('No previous entry for new tip.');
}

if (fork.hash === longer.hash)
if (fork.hash.equals(longer.hash))
return fork;

fork = await this.getPrevious(fork);
Expand Down Expand Up @@ -791,7 +792,7 @@ class Chain extends AsyncEmitter {
// Blocks to disconnect.
const disconnect = [];
let entry = tip;
while (entry.hash !== fork.hash) {
while (!entry.hash.equals(fork.hash)) {
disconnect.push(entry);
entry = await this.getPrevious(entry);
assert(entry);
Expand All @@ -800,7 +801,7 @@ class Chain extends AsyncEmitter {
// Blocks to connect.
const connect = [];
entry = competitor;
while (entry.hash !== fork.hash) {
while (!entry.hash.equals(fork.hash)) {
connect.push(entry);
entry = await this.getPrevious(entry);
assert(entry);
Expand Down Expand Up @@ -848,7 +849,7 @@ class Chain extends AsyncEmitter {
// Buffer disconnected blocks.
const disconnect = [];
let entry = tip;
while (entry.hash !== fork.hash) {
while (!entry.hash.equals(fork.hash)) {
disconnect.push(entry);
entry = await this.getPrevious(entry);
assert(entry);
Expand Down Expand Up @@ -977,7 +978,7 @@ class Chain extends AsyncEmitter {
async setBestChain(entry, block, prev, flags) {
// A higher fork has arrived.
// Time to reorganize the chain.
if (entry.prevBlock !== this.tip.hash) {
if (!entry.prevBlock.equals(this.tip.hash)) {
this.logger.warning('WARNING: Reorganizing chain.');

// In spv-mode, we reset the
Expand Down Expand Up @@ -1236,7 +1237,7 @@ class Chain extends AsyncEmitter {
*/

async add(block, flags, id) {
const hash = block.hash('hex');
const hash = block.hash();
const unlock = await this.locker.lock(hash);
try {
return await this._add(block, flags, id);
Expand All @@ -1255,7 +1256,7 @@ class Chain extends AsyncEmitter {
*/

async _add(block, flags, id) {
const hash = block.hash('hex');
const hash = block.hash();

if (flags == null)
flags = common.flags.DEFAULT_FLAGS;
Expand All @@ -1264,7 +1265,7 @@ class Chain extends AsyncEmitter {
id = -1;

// Special case for genesis block.
if (hash === this.network.genesis.hash) {
if (hash.equals(this.network.genesis.hash)) {
this.logger.debug('Saw genesis block: %s.', block.rhash());
throw new VerifyError(block, 'duplicate', 'duplicate', 0);
}
Expand Down Expand Up @@ -1333,7 +1334,7 @@ class Chain extends AsyncEmitter {
const start = util.bench();

// Sanity check.
assert(block.prevBlock === prev.hash);
assert(block.prevBlock.equals(prev.hash));

// Explanation: we try to keep as much data
// off the javascript heap as possible. Blocks
Expand Down Expand Up @@ -1492,7 +1493,7 @@ class Chain extends AsyncEmitter {
if (!checkpoint)
return true;

if (hash === checkpoint) {
if (hash.equals(checkpoint)) {
this.logger.debug('Hit checkpoint block %s (%d).',
util.revHex(hash), height);
this.emit('checkpoint', hash, height);
Expand Down Expand Up @@ -1529,8 +1530,8 @@ class Chain extends AsyncEmitter {

// The orphan chain forked.
if (orphan) {
assert(orphan.block.hash('hex') !== block.hash('hex'));
assert(orphan.block.prevBlock === block.prevBlock);
assert(!orphan.block.hash().equals(block.hash()));
assert(orphan.block.prevBlock.equals(block.prevBlock));

this.logger.warning(
'Removing forked orphan block: %s (%d).',
Expand Down Expand Up @@ -1558,7 +1559,7 @@ class Chain extends AsyncEmitter {

addOrphan(orphan) {
const block = orphan.block;
const hash = block.hash('hex');
const hash = block.hash();

assert(!this.orphanMap.has(hash));
assert(!this.orphanPrev.has(block.prevBlock));
Expand All @@ -1579,7 +1580,7 @@ class Chain extends AsyncEmitter {

removeOrphan(orphan) {
const block = orphan.block;
const hash = block.hash('hex');
const hash = block.hash();

assert(this.orphanMap.has(hash));
assert(this.orphanPrev.has(block.prevBlock));
Expand Down Expand Up @@ -1670,7 +1671,7 @@ class Chain extends AsyncEmitter {
*/

hasInvalid(block) {
const hash = block.hash('hex');
const hash = block.hash();

if (this.invalid.has(hash))
return true;
Expand Down Expand Up @@ -2085,7 +2086,7 @@ class Chain extends AsyncEmitter {
if (start == null)
start = this.tip.hash;

assert(typeof start === 'string');
assert(Buffer.isBuffer(start));

let entry = await this.getEntry(start);

Expand Down

0 comments on commit 7034a1a

Please sign in to comment.