rpc: refactor getTXOutProof and mining calls.

This commit is contained in:
Christopher Jeffrey 2017-08-14 17:52:31 -07:00
parent 09a2dc0f55
commit 2ce2f1f9f7
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -862,23 +862,26 @@ RPC.prototype.getTXOutProof = async function getTXOutProof(args, help) {
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid TXIDs.'); throw new RPCError(errs.INVALID_PARAMETER, 'Invalid TXIDs.');
const items = new Validator([txids]); const items = new Validator([txids]);
const uniq = new Set(); const hashes = new Set();
let last, block;
let last = null;
for (let i = 0; i < txids.length; i++) { for (let i = 0; i < txids.length; i++) {
const txid = items.hash(i); const hash = items.hash(i);
if (!txid) if (!hash)
throw new RPCError(errs.TYPE_ERROR, 'Invalid TXID.'); throw new RPCError(errs.TYPE_ERROR, 'Invalid TXID.');
if (uniq.has(txid)) if (hashes.has(hash))
throw new RPCError(errs.INVALID_PARAMETER, 'Duplicate txid.'); throw new RPCError(errs.INVALID_PARAMETER, 'Duplicate txid.');
uniq.add(txid); hashes.add(hash);
txids[i] = txid;
last = txid; last = hash;
} }
let block = null;
if (hash) { if (hash) {
block = await this.chain.db.getBlock(hash); block = await this.chain.db.getBlock(hash);
} else if (this.chain.options.indexTX) { } else if (this.chain.options.indexTX) {
@ -886,15 +889,15 @@ RPC.prototype.getTXOutProof = async function getTXOutProof(args, help) {
if (tx) if (tx)
block = await this.chain.db.getBlock(tx.block); block = await this.chain.db.getBlock(tx.block);
} else { } else {
const coins = await this.chain.db.getCoins(last); const coin = await this.chain.db.getCoin(last, 0);
if (coins) if (coin)
block = await this.chain.db.getBlock(coins.height); block = await this.chain.db.getBlock(coin.height);
} }
if (!block) if (!block)
throw new RPCError(errs.MISC_ERROR, 'Block not found.'); throw new RPCError(errs.MISC_ERROR, 'Block not found.');
for (const txid of txids) { for (const txid of hashes) {
if (!block.hasTX(txid)) { if (!block.hasTX(txid)) {
throw new RPCError(errs.VERIFY_ERROR, throw new RPCError(errs.VERIFY_ERROR,
'Block does not contain all txids.'); 'Block does not contain all txids.');
@ -2425,14 +2428,17 @@ RPC.prototype.getHashRate = async function getHashRate(lookup, height) {
if (!tip) if (!tip)
return 0; return 0;
if (lookup <= 0) assert(typeof lookup === 'number');
assert(lookup >= 0);
if (lookup === 0)
lookup = tip.height % this.network.pow.retargetInterval + 1; lookup = tip.height % this.network.pow.retargetInterval + 1;
if (lookup > tip.height) if (lookup > tip.height)
lookup = tip.height; lookup = tip.height;
let minTime = tip.time; let min = tip.time;
let maxTime = minTime; let max = min;
let entry = tip; let entry = tip;
for (let i = 0; i < lookup; i++) { for (let i = 0; i < lookup; i++) {
@ -2441,18 +2447,18 @@ RPC.prototype.getHashRate = async function getHashRate(lookup, height) {
if (!entry) if (!entry)
throw new RPCError(errs.DATABASE_ERROR, 'Not found.'); throw new RPCError(errs.DATABASE_ERROR, 'Not found.');
minTime = Math.min(entry.time, minTime); min = Math.min(entry.time, min);
maxTime = Math.max(entry.time, maxTime); max = Math.max(entry.time, max);
} }
if (minTime === maxTime) const diff = max - min;
if (diff === 0)
return 0; return 0;
const workDiff = tip.chainwork.sub(entry.chainwork); const work = tip.chainwork.sub(entry.chainwork);
const timeDiff = maxTime - minTime;
const ps = parseInt(workDiff.toString(10), 10) / timeDiff;
return ps; return Number(work.toString()) / diff;
}; };
RPC.prototype.mineBlocks = async function mineBlocks(blocks, addr, tries) { RPC.prototype.mineBlocks = async function mineBlocks(blocks, addr, tries) {
@ -2469,8 +2475,9 @@ RPC.prototype._mineBlocks = async function _mineBlocks(blocks, addr, tries) {
for (let i = 0; i < blocks; i++) { for (let i = 0; i < blocks; i++) {
const block = await this.miner.mineBlock(null, addr); const block = await this.miner.mineBlock(null, addr);
hashes.push(block.rhash()); const entry = await this.chain.add(block);
assert(await this.chain.add(block)); assert(entry);
hashes.push(entry.rhash());
} }
return hashes; return hashes;