From e9c0e24b21df7492db548ac40bcc5e6fcdb1ab44 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 27 Mar 2016 18:52:26 -0700 Subject: [PATCH] more parsing. --- lib/bcoin/mempool.js | 4 +++- lib/bcoin/merkleblock.js | 19 ++++++++++++------- lib/bcoin/protocol/parser.js | 18 +++++++++--------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/mempool.js b/lib/bcoin/mempool.js index aa171e8b..3dff81af 100644 --- a/lib/bcoin/mempool.js +++ b/lib/bcoin/mempool.js @@ -76,6 +76,8 @@ Mempool.prototype._init = function _init() { var self = this; var unlock = this._lock(utils.nop, []); + assert(unlock); + bcoin.ldb.destroy('mempool', 'memdown', function(err) { if (err) { unlock(); @@ -380,7 +382,7 @@ Mempool.prototype.addTX = function addTX(tx, peer, callback, force) { Mempool.prototype.addUnchecked = function addUnchecked(tx, peer, callback) { var self = this; - self.tx.addUnchecked(tx, function(err) { + this.tx.addUnchecked(tx, function(err) { if (err) return callback(err); diff --git a/lib/bcoin/merkleblock.js b/lib/bcoin/merkleblock.js index 57e6c08e..40d9c852 100644 --- a/lib/bcoin/merkleblock.js +++ b/lib/bcoin/merkleblock.js @@ -20,12 +20,13 @@ function MerkleBlock(data) { this.type = 'merkleblock'; this.hashes = (data.hashes || []).map(function(hash) { - return utils.toHex(hash); + return utils.toBuffer(hash, 'hex'); }); this.flags = data.flags || []; // List of matched TXs + this.txMap = {}; this.tx = []; // TXs that will be pushed on @@ -53,12 +54,13 @@ MerkleBlock.prototype.getRaw = function getRaw() { }; MerkleBlock.prototype.hasTX = function hasTX(hash) { - return this.tx.indexOf(hash) !== -1; + return this.txMap[hash] === true; }; MerkleBlock.prototype._verifyPartial = function _verifyPartial() { var height = 0; var tx = []; + var txMap = {}; var j = 0; var hashes = this.hashes; var flags = this.flags; @@ -81,8 +83,10 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() { i++; if (flag === 0 || depth === height) { - if (depth === height) - tx.push(hashes[j]); + if (depth === height) { + tx.push(utils.toHex(hashes[j])); + txMap[tx[tx.length - 1]] = true; + } return hashes[j++]; } @@ -92,21 +96,22 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() { return null; right = visit(depth + 1); - if (right === left) + if (right && utils.isEqual(right, left)) return null; if (!right) right = left; - return utils.toHex(utils.dsha256(left + right, 'hex')); + return utils.dsha256(Buffer.concat([left, right])); } - root = visit(1); + root = utils.toHex(visit(1)); if (!root || root !== this.merkleRoot) return false; this.tx = tx; + this.txMap = txMap; return true; }; diff --git a/lib/bcoin/protocol/parser.js b/lib/bcoin/protocol/parser.js index e7b2dceb..692d244e 100644 --- a/lib/bcoin/protocol/parser.js +++ b/lib/bcoin/protocol/parser.js @@ -232,7 +232,7 @@ Parser.parseInvList = function parseInvList(p) { for (i = 0; i < count; i++) { items.push({ type: constants.invByVal[p.readU32()], - hash: p.readHash() + hash: p.readHash('hex') }); } @@ -249,8 +249,8 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { p.start(); version = p.read32(); - prevBlock = p.readHash(); - merkleRoot = p.readHash(); + prevBlock = p.readHash('hex'); + merkleRoot = p.readHash('hex'); ts = p.readU32(); bits = p.readU32(); nonce = p.readU32(); @@ -261,7 +261,7 @@ Parser.parseMerkleBlock = function parseMerkleBlock(p) { hashes = new Array(hashCount); for (i = 0; i < hashCount; i++) - hashes[i] = p.readHash(); + hashes[i] = p.readHash('hex'); flags = p.readVarBytes(); @@ -291,8 +291,8 @@ Parser.parseHeaders = function parseHeaders(p) { for (i = 0; i < count; i++) { headers.push({ version: p.read32(), - prevBlock: p.readHash(), - merkleRoot: p.readHash(), + prevBlock: p.readHash('hex'), + merkleRoot: p.readHash('hex'), ts: p.readU32(), bits: p.readU32(), nonce: p.readU32(), @@ -470,7 +470,7 @@ Parser.parseCoin = function parseCoin(p, extended) { coinbase = p.readU8() === 1; if (extended) { - hash = p.readHash(); + hash = p.readHash('hex'); index = p.readU32(); } @@ -639,7 +639,7 @@ Parser.parseReject = function parseReject(p) { reason = p.readVarString('ascii'); try { - data = p.readHash(); + data = p.readHash('hex'); } catch (e) { data = null; } @@ -709,7 +709,7 @@ Parser.parseAlert = function parseAlert(p) { var version, relayUntil, expiration, id, cancel; var cancels, count, i, minVer, maxVer, subVers; var priority, comment, statusBar, msg; - var payload, p2, size; + var payload, size; p = new BufferReader(p); p.start();