Skip to content

Commit f391351

Browse files
committed
Merge pull request bitcoinjs#509 from bitcoinjs/pow
Add Block proof-of-work validation function
2 parents bda2e28 + e4ba88e commit f391351

File tree

4 files changed

+112
-10
lines changed

4 files changed

+112
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"bigi": "^1.4.0",
6363
"bip66": "^1.1.0",
6464
"bs58check": "^1.0.5",
65+
"buffer-compare": "^1.1.0",
6566
"buffer-equals": "^1.0.3",
6667
"buffer-reverse": "^1.0.0",
6768
"create-hash": "^1.1.0",

src/block.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var bufferutils = require('./bufferutils')
22
var bcrypto = require('./crypto')
3+
var compare = require('buffer-compare')
34

45
var Transaction = require('./transaction')
56

@@ -115,4 +116,27 @@ Block.prototype.toHex = function (headersOnly) {
115116
return this.toBuffer(headersOnly).toString('hex')
116117
}
117118

119+
Block.calculateTarget = function (bits) {
120+
var exponent = ((bits & 0xff000000) >> 24) - 3
121+
var mantissa = bits & 0x007fffff
122+
var i = 31 - exponent
123+
124+
var target = new Buffer(32)
125+
target.fill(0)
126+
127+
target[i] = mantissa & 0xff
128+
target[i - 1] = mantissa >> 8
129+
target[i - 2] = mantissa >> 16
130+
target[i - 3] = mantissa >> 24
131+
132+
return target
133+
}
134+
135+
Block.prototype.checkProofOfWork = function () {
136+
var hash = [].reverse.call(this.getHash())
137+
var target = Block.calculateTarget(this.bits)
138+
139+
return compare(hash, target) <= 0
140+
}
141+
118142
module.exports = Block

test/block.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
/* global describe, it, beforeEach */
22

33
var assert = require('assert')
4-
54
var Block = require('../src/block')
65

76
var fixtures = require('./fixtures/block')
87

98
describe('Block', function () {
9+
describe('calculateTarget', function () {
10+
fixtures.targets.forEach(function (f) {
11+
it('returns ' + f.expected + ' for 0x' + f.bits, function () {
12+
var bits = parseInt(f.bits, 16)
13+
14+
assert.equal(Block.calculateTarget(bits).toString('hex'), f.expected)
15+
})
16+
})
17+
})
18+
1019
describe('fromBuffer/fromHex', function () {
1120
fixtures.valid.forEach(function (f) {
1221
it('imports the block: ' + f.description + ' correctly', function () {
@@ -52,7 +61,7 @@ describe('Block', function () {
5261
block = Block.fromHex(f.hex)
5362
})
5463

55-
it('calculates ' + f.hash + ' for the block: ' + f.description, function () {
64+
it('returns ' + f.hash + ' for the block: ' + f.description, function () {
5665
assert.strictEqual(block.getHash().toString('hex'), f.hash)
5766
})
5867
})
@@ -66,7 +75,7 @@ describe('Block', function () {
6675
block = Block.fromHex(f.hex)
6776
})
6877

69-
it('calculates ' + f.id + ' for the block: ' + f.description, function () {
78+
it('returns ' + f.id + ' for the block: ' + f.description, function () {
7079
assert.strictEqual(block.getId(), f.id)
7180
})
7281
})
@@ -87,4 +96,18 @@ describe('Block', function () {
8796
})
8897
})
8998
})
99+
100+
describe('checkProofOfWork', function () {
101+
fixtures.valid.forEach(function (f) {
102+
var block
103+
104+
beforeEach(function () {
105+
block = Block.fromHex(f.hex)
106+
})
107+
108+
it('returns ' + f.valid + ' for ' + f.id, function () {
109+
assert.strictEqual(block.checkProofOfWork(), f.valid)
110+
})
111+
})
112+
})
90113
})

test/fixtures/block.json

Lines changed: 61 additions & 7 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)