rpc: refactor getTXOutProof and mining calls.
This commit is contained in:
parent
09a2dc0f55
commit
2ce2f1f9f7
@ -862,23 +862,26 @@ RPC.prototype.getTXOutProof = async function getTXOutProof(args, help) {
|
||||
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid TXIDs.');
|
||||
|
||||
const items = new Validator([txids]);
|
||||
const uniq = new Set();
|
||||
let last, block;
|
||||
const hashes = new Set();
|
||||
|
||||
let last = null;
|
||||
|
||||
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.');
|
||||
|
||||
if (uniq.has(txid))
|
||||
if (hashes.has(hash))
|
||||
throw new RPCError(errs.INVALID_PARAMETER, 'Duplicate txid.');
|
||||
|
||||
uniq.add(txid);
|
||||
txids[i] = txid;
|
||||
last = txid;
|
||||
hashes.add(hash);
|
||||
|
||||
last = hash;
|
||||
}
|
||||
|
||||
let block = null;
|
||||
|
||||
if (hash) {
|
||||
block = await this.chain.db.getBlock(hash);
|
||||
} else if (this.chain.options.indexTX) {
|
||||
@ -886,15 +889,15 @@ RPC.prototype.getTXOutProof = async function getTXOutProof(args, help) {
|
||||
if (tx)
|
||||
block = await this.chain.db.getBlock(tx.block);
|
||||
} else {
|
||||
const coins = await this.chain.db.getCoins(last);
|
||||
if (coins)
|
||||
block = await this.chain.db.getBlock(coins.height);
|
||||
const coin = await this.chain.db.getCoin(last, 0);
|
||||
if (coin)
|
||||
block = await this.chain.db.getBlock(coin.height);
|
||||
}
|
||||
|
||||
if (!block)
|
||||
throw new RPCError(errs.MISC_ERROR, 'Block not found.');
|
||||
|
||||
for (const txid of txids) {
|
||||
for (const txid of hashes) {
|
||||
if (!block.hasTX(txid)) {
|
||||
throw new RPCError(errs.VERIFY_ERROR,
|
||||
'Block does not contain all txids.');
|
||||
@ -2425,14 +2428,17 @@ RPC.prototype.getHashRate = async function getHashRate(lookup, height) {
|
||||
if (!tip)
|
||||
return 0;
|
||||
|
||||
if (lookup <= 0)
|
||||
assert(typeof lookup === 'number');
|
||||
assert(lookup >= 0);
|
||||
|
||||
if (lookup === 0)
|
||||
lookup = tip.height % this.network.pow.retargetInterval + 1;
|
||||
|
||||
if (lookup > tip.height)
|
||||
lookup = tip.height;
|
||||
|
||||
let minTime = tip.time;
|
||||
let maxTime = minTime;
|
||||
let min = tip.time;
|
||||
let max = min;
|
||||
let entry = tip;
|
||||
|
||||
for (let i = 0; i < lookup; i++) {
|
||||
@ -2441,18 +2447,18 @@ RPC.prototype.getHashRate = async function getHashRate(lookup, height) {
|
||||
if (!entry)
|
||||
throw new RPCError(errs.DATABASE_ERROR, 'Not found.');
|
||||
|
||||
minTime = Math.min(entry.time, minTime);
|
||||
maxTime = Math.max(entry.time, maxTime);
|
||||
min = Math.min(entry.time, min);
|
||||
max = Math.max(entry.time, max);
|
||||
}
|
||||
|
||||
if (minTime === maxTime)
|
||||
const diff = max - min;
|
||||
|
||||
if (diff === 0)
|
||||
return 0;
|
||||
|
||||
const workDiff = tip.chainwork.sub(entry.chainwork);
|
||||
const timeDiff = maxTime - minTime;
|
||||
const ps = parseInt(workDiff.toString(10), 10) / timeDiff;
|
||||
const work = tip.chainwork.sub(entry.chainwork);
|
||||
|
||||
return ps;
|
||||
return Number(work.toString()) / diff;
|
||||
};
|
||||
|
||||
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++) {
|
||||
const block = await this.miner.mineBlock(null, addr);
|
||||
hashes.push(block.rhash());
|
||||
assert(await this.chain.add(block));
|
||||
const entry = await this.chain.add(block);
|
||||
assert(entry);
|
||||
hashes.push(entry.rhash());
|
||||
}
|
||||
|
||||
return hashes;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user