more parsing.

This commit is contained in:
Christopher Jeffrey 2016-03-27 18:52:26 -07:00
parent 30148368a8
commit e9c0e24b21
3 changed files with 24 additions and 17 deletions

View File

@ -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);

View File

@ -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;
};

View File

@ -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();