handle merkleblock vs block better.

This commit is contained in:
Christopher Jeffrey 2015-12-22 15:03:17 -08:00
parent 1c8604477f
commit b522aa2b06
2 changed files with 14 additions and 13 deletions

View File

@ -45,23 +45,22 @@ function Block(data, subtype) {
this.tx = []; this.tx = [];
this.invalid = false; this.invalid = false;
if (this.subtype === 'block') { if (this.subtype === 'merkleblock') {
// Verify partial merkle tree and fill `ts` array
this.invalid = !this._verifyMerkle();
} else if (this.subtype === 'block') {
this.txs = this.txs.map(function(tx) { this.txs = this.txs.map(function(tx) {
tx.network = self.network; tx.network = self.network;
tx.relayedBy = self.relayedBy; tx.relayedBy = self.relayedBy;
tx = bcoin.tx(tx); tx = bcoin.tx(tx);
tx.block = self.hash('hex'); tx.block = self.hash('hex');
tx.ts = tx.ts || self.ts; tx.ts = tx.ts || self.ts;
// self.tx.push(tx.hash('hex'));
return tx; return tx;
}); });
this.invalid = !this._checkBlock(); this.invalid = !this._checkBlock();
} }
this._hash = null; this._hash = null;
// Verify partial merkle tree and fill `ts` array
this._verifyMerkle();
} }
Block.prototype.hash = function hash(enc) { Block.prototype.hash = function hash(enc) {
@ -153,12 +152,12 @@ Block.prototype._verifyMerkle = function verifyMerkle() {
root = visit(1); root = visit(1);
if (!root || root !== this.merkleRoot) { if (!root || root !== this.merkleRoot)
this.invalid = true; return false;
return;
}
this.tx = tx; this.tx = tx;
return true;
}; };
Block.prototype.getMerkleRoot = function getMerkleRoot() { Block.prototype.getMerkleRoot = function getMerkleRoot() {

View File

@ -46,16 +46,18 @@ function TX(data, block) {
this.input(input, null); this.input(input, null);
}, this); }, this);
} }
if (data.outputs) { if (data.outputs) {
data.outputs.forEach(function(out) { data.outputs.forEach(function(out) {
this.out(out, null); this.out(out, null);
}, this); }, this);
} }
if (!data.ts && block && block.hasTX(this.hash('hex'))) { if (block && block.subtype === 'merkleblock') {
// if (block) { if (!data.ts && block && block.hasTX(this.hash('hex'))) {
this.ts = block.ts; this.ts = block.ts;
this.block = block.hash('hex'); this.block = block.hash('hex');
}
} }
// ps = Pending Since // ps = Pending Since