block: verify
This commit is contained in:
parent
1ac3208360
commit
cd3f04268a
@ -37,8 +37,7 @@ Block.prototype.abbr = function abbr() {
|
||||
};
|
||||
|
||||
Block.prototype.verify = function verify() {
|
||||
// TODO(indutny): verify nonce
|
||||
return true;
|
||||
return utils.testTarget(this.bits, this.hash());
|
||||
};
|
||||
|
||||
Block.prototype.render = function render(framer) {
|
||||
|
||||
@ -168,11 +168,11 @@ Chain.prototype.get = function get(hash, cb) {
|
||||
|
||||
if (this.request.map[hash]) {
|
||||
this.request.map[hash].push(cb);
|
||||
this.request.count++;
|
||||
} else {
|
||||
this.request.map[hash] = [ cb ];
|
||||
this.request.count++;
|
||||
this.emit('missing', hash);
|
||||
}
|
||||
this.emit('missing', hash);
|
||||
};
|
||||
|
||||
Chain.prototype.isFull = function isFull() {
|
||||
|
||||
@ -223,3 +223,46 @@ function binaryInsert(list, item, compare, search) {
|
||||
return start;
|
||||
}
|
||||
utils.binaryInsert = binaryInsert;
|
||||
|
||||
function bitsToTarget(bits) {
|
||||
var len = (bits >>> 24) & 0xff;
|
||||
var hi = (bits >>> 16) & 0xff;
|
||||
var mid = (bits >>> 8) & 0xff;
|
||||
var lo = bits & 0xff;
|
||||
|
||||
var res = new Array(len);
|
||||
for (var i = 0; i < len - 3; i++)
|
||||
res[i] = 0;
|
||||
res[i++] = lo;
|
||||
res[i++] = mid;
|
||||
res[i++] = hi;
|
||||
|
||||
if (hi === 0)
|
||||
res.pop();
|
||||
if (hi === 0 && mid === 0)
|
||||
res.pop();
|
||||
|
||||
return res;
|
||||
}
|
||||
utils.bitsToTarget = bitsToTarget;
|
||||
|
||||
function testTarget(target, hash) {
|
||||
if (typeof target === 'number')
|
||||
target = bitsToTarget(target);
|
||||
hash = utils.toArray(hash, 'hex');
|
||||
|
||||
for (var i = hash.length - 1; i >= target.length; i--)
|
||||
if (hash[i] !== 0)
|
||||
return false;
|
||||
|
||||
for (; i >= 0; i--) {
|
||||
if (hash[i] === target[i])
|
||||
continue;
|
||||
if (hash[i] > target[i])
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
utils.testTarget = testTarget;
|
||||
|
||||
@ -8,4 +8,14 @@ describe('Utils', function() {
|
||||
assert.equal(b, '1116h8cQN');
|
||||
assert.deepEqual(bcoin.utils.fromBase58(b), arr);
|
||||
});
|
||||
|
||||
it('should translate bits to target', function() {
|
||||
var bits = 0x1900896c;
|
||||
var hash = bcoin.utils.toArray(
|
||||
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
|
||||
'hex'
|
||||
);
|
||||
var target = bcoin.utils.bitsToTarget(bits);
|
||||
assert(bcoin.utils.testTarget(target, hash));
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user