optimize getProof. misc.

This commit is contained in:
Christopher Jeffrey 2016-05-17 12:47:16 -07:00
parent a6727fd84e
commit ec21986062
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 38 additions and 16 deletions

View File

@ -481,7 +481,8 @@ Block.reward = function reward(height, network) {
network = bcoin.network.get(network);
halvings = height / network.halvingInterval | 0;
// BIP 42
// BIP 42 (not technically necessary since we
// don't suffer from the undefined behavior of C++).
// https://github.com/bitcoin/bips/blob/master/bip-0042.mediawiki
if (halvings >= 64)
return 0;

View File

@ -59,6 +59,13 @@ function ChainEntry(chain, data, prev) {
this.chainwork = data.chainwork || this.getChainwork(prev);
}
/**
* The max chainwork (1 << 256).
* @const {BN}
*/
ChainEntry.MAX_CHAINWORK = new bn(1).ushln(256);
/**
* Calculate the proof: (1 << 256) / (target + 1)
* @returns {BN} proof
@ -68,7 +75,7 @@ ChainEntry.prototype.getProof = function getProof() {
var target = utils.fromCompact(this.bits);
if (target.isNeg() || target.cmpn(0) === 0)
return new bn(0);
return new bn(1).ushln(256).div(target.addn(1));
return ChainEntry.MAX_CHAINWORK.div(target.iaddn(1));
};
/**
@ -78,7 +85,12 @@ ChainEntry.prototype.getProof = function getProof() {
*/
ChainEntry.prototype.getChainwork = function getChainwork(prev) {
return (prev ? prev.chainwork : new bn(0)).add(this.getProof());
var proof = this.getProof();
if (!prev)
return proof;
return proof.iadd(prev.chainwork);
};
/**

View File

@ -1817,13 +1817,13 @@ MempoolEntry.fromTX = function fromTX(tx, height) {
* can still be parsed as a regular tx since
* the mempool entry data comes after the
* serialized transaction.
* @param {TX} tx
* @param {Number} height - Entry height.
* @returns {MempoolEntry}
* @param {BufferWriter?} writer
* @returns {Buffer}
*/
MempoolEntry.prototype.toRaw = function toRaw() {
var p = new BufferWriter();
MempoolEntry.prototype.toRaw = function toRaw(writer) {
var p = new BufferWriter(writer);
bcoin.protocol.framer.renderTX(this.tx, true, p);
p.writeU32(this.height);
p.writeDouble(this.priority);
@ -1832,7 +1832,11 @@ MempoolEntry.prototype.toRaw = function toRaw() {
p.writeU32(this.count);
p.writeU32(this.size);
p.writeVarint(this.fees);
return p.render();
if (!writer)
p = p.render();
return p;
};
/**

View File

@ -1966,15 +1966,15 @@ utils.write64 = function write64(dst, num, off) {
off = off >>> 0;
if (num.isNeg())
num = num.neg().notn(64).addn(1);
num = num.neg().inotn(64).iaddn(1);
if (num.bitLength() > 64)
num = num.uand(utils.U64);
num = num.toBuffer('le', 8);
num = num.toArray('le', 8);
for (i = 0; i < num.length; i++)
dst[off++] = num[i] & 0xff;
dst[off++] = num[i];
return 8;
};
@ -1996,15 +1996,15 @@ utils.write64BE = function write64BE(dst, num, off) {
off = off >>> 0;
if (num.isNeg())
num = num.neg().notn(64).addn(1);
num = num.neg().inotn(64).iaddn(1);
if (num.bitLength() > 64)
num = num.uand(utils.U64);
num = num.toBuffer('be', 8);
num = num.toArray('be', 8);
for (i = 0; i < num.length; i++)
dst[off++] = num[i] & 0xff;
dst[off++] = num[i];
return 8;
};

View File

@ -701,6 +701,9 @@ Framer.item = function _item(item, p) {
} else if (item instanceof bcoin.chainentry) {
p.writeU8(43);
item.toRaw(p);
} else if (item instanceof bcoin.mempoolentry) {
p.writeU8(44);
item.toRaw(p);
} else if (bn.isBN(item)) {
p.writeU8(50);
p.writeVarBytes(item.toBuffer());
@ -859,6 +862,8 @@ Parser.parseItem = function parseItem(p) {
return bcoin.coin.fromExtended(p);
case 43:
return bcoin.chainentry.fromRaw(null, p);
case 43:
return bcoin.mempoolentry.fromRaw(p);
case 50:
return new bn(p.readVarBytes());
default:

View File

@ -26,7 +26,7 @@ function createGenesisBlock(options) {
}
if (!options.reward)
options.reward = new bn(50).mul(constants.COIN);
options.reward = 50 * constants.COIN;
tx = {
version: 1,