From 3fc8ddb10384729c7dccba85a959c43a5bb47f26 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 30 Jun 2017 10:05:43 -0400 Subject: [PATCH] wip --- lib/services/block/index.js | 57 +++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/lib/services/block/index.js b/lib/services/block/index.js index dc96555e..0b17954a 100644 --- a/lib/services/block/index.js +++ b/lib/services/block/index.js @@ -296,16 +296,73 @@ BlockService.prototype._selectActiveChain = function() { }; +/* + +arith_uint256 GetBlockProof(const CBlockIndex& block) +{ + arith_uint256 bnTarget; + bool fNegative; + bool fOverflow; + bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); + if (fNegative || fOverflow || bnTarget == 0) + return 0; + // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 + // as it's too large for a arith_uint256. However, as 2**256 is at least as large + // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, + // or ~bnTarget / (nTarget+1) + 1. + return (~bnTarget / (bnTarget + 1)) + 1; +} + + +*/ BlockService.prototype._computeChainWork = function(chainTip) { + // pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex); //for super old forks that have cycled out of our cache, just return zero work + + + // given the block: + // we compute the target, which is a 256 bit numnber + // but we need to check to ensure the result isn't negative or overflows or is zero + // var blockHeader = this._blockHeaderQueue.get(chainTip); if (!blockHeader) { return 0; } + + arith_uint256 bnTarget; + bool fNegative; + bool fOverflow; + bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); + if (fNegative || fOverflow || bnTarget == 0) + return 0; + // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 + // as it's too large for a arith_uint256. However, as 2**256 is at least as large + // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1, + // or ~bnTarget / (nTarget+1) + 1. + return (~bnTarget / (bnTarget + 1)) + 1; blockHeader.chainwork; }; +BlockService.prototype._setCompact = function() { + nt nSize = nCompact >> 24; + uint32_t nWord = nCompact & 0x007fffff; + if (nSize <= 3) { + nWord >>= 8 * (3 - nSize); + *this = nWord; + } else { + *this = nWord; + *this <<= 8 * (nSize - 3); + } + if (pfNegative) + *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0; + if (pfOverflow) + *pfOverflow = nWord != 0 && ((nSize > 34) || + (nWord > 0xff && nSize > 33) || + (nWord > 0xffff && nSize > 32)); + return *this; +}; + BlockService.prototype._getAllUnsentBlocksFromActiveChain = function(block) { var blocksToSend = [block];