miner, chainblock, workers.

This commit is contained in:
Christopher Jeffrey 2016-05-10 12:10:34 -07:00
parent 45e84ff168
commit 62bc8b077d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 21 additions and 11 deletions

View File

@ -385,8 +385,8 @@ ChainBlock.prototype.__defineGetter__('rhash', function() {
* @returns {Buffer}
*/
ChainBlock.prototype.toRaw = function toRaw() {
var p = new BufferWriter();
ChainBlock.prototype.toRaw = function toRaw(writer) {
var p = new BufferWriter(writer);
p.write32(this.version);
p.writeHash(this.prevBlock);
@ -397,7 +397,10 @@ ChainBlock.prototype.toRaw = function toRaw() {
p.writeU32(this.height);
p.writeBytes(this.chainwork.toBuffer('le', 32));
return p.render();
if (!writer)
p = p.render();
return p;
};
/**
@ -408,8 +411,10 @@ ChainBlock.prototype.toRaw = function toRaw() {
*/
ChainBlock.fromRaw = function fromRaw(chain, buf) {
var p = new BufferReader(buf);
var hash = utils.dsha256(buf.slice(0, 80));
var p = new BufferReader(buf, true);
var hash = utils.dsha256(p.readBytes(80));
p.seek(-80);
return new ChainBlock(chain, {
hash: hash.toString('hex'),
@ -440,7 +445,7 @@ ChainBlock.prototype.toJSON = function toJSON() {
bits: this.bits,
nonce: this.nonce,
height: this.height,
chainwork: this.chainwork.toString('hex')
chainwork: this.chainwork.toString(10)
};
};
@ -455,7 +460,7 @@ ChainBlock.fromJSON = function fromJSON(chain, json) {
json.hash = utils.revHex(json.hash);
json.prevBlock = utils.revHex(json.prevBlock);
json.merkleRoot = utils.revHex(json.merkleRoot);
json.chainwork = new bn(json.chainwork, 'hex');
json.chainwork = new bn(json.chainwork, 10);
return new ChainBlock(chain, json);
};

View File

@ -555,7 +555,7 @@ MinerBlock.prototype.findNonce = function findNonce() {
// Overflow the nonce and increment the extraNonce.
block.nonce = 0;
block.extraNonce.iaddn(1);
this.extraNonce.iaddn(1);
// We incremented the extraNonce, need to update coinbase.
this.updateCoinbase();

View File

@ -195,7 +195,7 @@ Workers.prototype.verify = function verify(tx, index, force, flags, callback) {
Workers.prototype.mine = function mine(attempt, callback) {
var data = {
tip: attempt.tip.toRaw(),
tip: attempt.tip,
version: attempt.block.version,
target: attempt.block.bits,
address: attempt.options.address,
@ -608,7 +608,7 @@ jobs.verify = function verify(tx, index, force, flags) {
jobs.mine = function mine(data) {
var attempt = new bcoin.miner.minerblock({
tip: bcoin.chainblock.fromRaw(null, data.tip),
tip: data.tip,
version: data.version,
target: data.target,
address: data.address,
@ -697,8 +697,11 @@ Framer.item = function _item(item, p) {
} else if (item instanceof bcoin.coin) {
p.writeU8(42);
bcoin.protocol.framer.coin(item, true, p);
} else if (bn.isBN(item)) {
} else if (item instanceof bcoin.chainblock) {
p.writeU8(43);
item.toRaw(p);
} else if (bn.isBN(item)) {
p.writeU8(50);
p.writeVarBytes(item.toBuffer());
} else if (Buffer.isBuffer(item)) {
p.writeU8(4);
@ -854,6 +857,8 @@ Parser.parseItem = function parseItem(p) {
case 42:
return bcoin.coin.fromExtended(p);
case 43:
return bcoin.chainblock.fromRaw(null, p);
case 50:
return new bn(p.readVarBytes());
default:
assert(false, 'Bad type.');