-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Block.js #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Block.js #300
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| var assert = require('assert') | ||
| var bufferutils = require('./bufferutils') | ||
| var crypto = require('./crypto') | ||
|
|
||
| var Transaction = require('./transaction') | ||
| var Script = require('./script') | ||
|
|
||
| function Block() { | ||
| this.version = 1 | ||
| this.prevHash = null | ||
| this.merkleRoot = null | ||
| this.timestamp = 0 | ||
| this.bits = 0 | ||
| this.nonce = 0 | ||
| } | ||
|
|
||
| Block.fromBuffer = function(buffer) { | ||
| assert(buffer.length >= 80, 'Buffer too small (< 80 bytes)') | ||
|
|
||
| var offset = 0 | ||
| function readSlice(n) { | ||
| offset += n | ||
| return buffer.slice(offset - n, offset) | ||
| } | ||
|
|
||
| function readUInt32() { | ||
| var i = buffer.readUInt32LE(offset) | ||
| offset += 4 | ||
| return i | ||
| } | ||
|
|
||
| var block = new Block() | ||
| block.version = readUInt32() | ||
| block.prevHash = readSlice(32) | ||
| block.merkleRoot = readSlice(32) | ||
| block.timestamp = readUInt32() | ||
| block.bits = readUInt32() | ||
| block.nonce = readUInt32() | ||
|
|
||
| if (buffer.length === 80) return block | ||
|
|
||
| function readUInt64() { | ||
| var i = bufferutils.readUInt64LE(buffer, offset) | ||
| offset += 8 | ||
| return i | ||
| } | ||
|
|
||
| function readVarInt() { | ||
| var vi = bufferutils.readVarInt(buffer, offset) | ||
| offset += vi.size | ||
| return vi.number | ||
| } | ||
|
|
||
| function readScript() { | ||
| return Script.fromBuffer(readSlice(readVarInt())) | ||
| } | ||
|
|
||
| function readTransaction() { | ||
| var tx = new Transaction() | ||
| tx.version = readUInt32() | ||
|
|
||
| var vinLen = readVarInt() | ||
| for (var i = 0; i < vinLen; ++i) { | ||
| tx.ins.push({ | ||
| hash: readSlice(32), | ||
| index: readUInt32(), | ||
| script: readScript(), | ||
| sequence: readUInt32() | ||
| }) | ||
| } | ||
|
|
||
| var voutLen = readVarInt() | ||
| for (i = 0; i < voutLen; ++i) { | ||
| tx.outs.push({ | ||
| value: readUInt64(), | ||
| script: readScript(), | ||
| }) | ||
| } | ||
|
|
||
| tx.locktime = readUInt32() | ||
|
|
||
| return tx | ||
| } | ||
|
|
||
| var nTransactions = readVarInt() | ||
| block.transactions = [] | ||
|
|
||
| for (var i = 0; i < nTransactions; ++i) { | ||
| var tx = readTransaction() | ||
| block.transactions.push(tx) | ||
| } | ||
|
|
||
| return block | ||
| } | ||
|
|
||
| Block.fromHex = function(hex) { | ||
| return Block.fromBuffer(new Buffer(hex, 'hex')) | ||
| } | ||
|
|
||
| Block.prototype.getHash = function() { | ||
| return crypto.hash256(this.toBuffer(true)) | ||
| } | ||
|
|
||
| Block.prototype.getId = function() { | ||
| return bufferutils.reverse(this.getHash()).toString('hex') | ||
| } | ||
|
|
||
| Block.prototype.getUTCDate = function() { | ||
| var date = new Date(0) // epoch | ||
| date.setUTCSeconds(this.timestamp) | ||
|
|
||
| return date | ||
| } | ||
|
|
||
| Block.prototype.toBuffer = function(headersOnly) { | ||
| var buffer = new Buffer(80) | ||
|
|
||
| var offset = 0 | ||
| function writeSlice(slice) { | ||
| slice.copy(buffer, offset) | ||
| offset += slice.length | ||
| } | ||
|
|
||
| function writeUInt32(i) { | ||
| buffer.writeUInt32LE(i, offset) | ||
| offset += 4 | ||
| } | ||
|
|
||
| writeUInt32(this.version) | ||
| writeSlice(this.prevHash) | ||
| writeSlice(this.merkleRoot) | ||
| writeUInt32(this.timestamp) | ||
| writeUInt32(this.bits) | ||
| writeUInt32(this.nonce) | ||
|
|
||
| if (headersOnly || !this.transactions) return buffer | ||
|
|
||
| var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length) | ||
| var txBuffers = this.transactions.map(function(tx) { | ||
| return tx.toBuffer() | ||
| }) | ||
|
|
||
| return Buffer.concat([buffer, txLenBuffer].concat(txBuffers)) | ||
| } | ||
|
|
||
| Block.prototype.toHex = function(headersOnly) { | ||
| return this.toBuffer(headersOnly).toString('hex') | ||
| } | ||
|
|
||
| module.exports = Block | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| var assert = require('assert') | ||
|
|
||
| var Block = require('../src/block') | ||
|
|
||
| var fixtures = require('./fixtures/block') | ||
|
|
||
| describe('Block', function() { | ||
| describe('fromBuffer/fromHex', function() { | ||
| fixtures.valid.forEach(function(f) { | ||
| it('imports the block: ' + f.description + ' correctly', function() { | ||
| var block = Block.fromHex(f.hex) | ||
|
|
||
| assert.equal(block.version, f.version) | ||
| assert.equal(block.prevHash.toString('hex'), f.prevHash) | ||
| assert.equal(block.merkleRoot.toString('hex'), f.merkleRoot) | ||
| assert.equal(block.timestamp, f.timestamp) | ||
| assert.equal(block.bits, f.bits) | ||
| assert.equal(block.nonce, f.nonce) | ||
| }) | ||
| }) | ||
|
|
||
| fixtures.invalid.forEach(function(f) { | ||
| it('throws on ' + f.exception, function() { | ||
| assert.throws(function() { | ||
| Block.fromHex(f.hex) | ||
| }, new RegExp(f.exception)) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('toBuffer/toHex', function() { | ||
| fixtures.valid.forEach(function(f) { | ||
| var block | ||
|
|
||
| beforeEach(function() { | ||
| block = Block.fromHex(f.hex) | ||
| }) | ||
|
|
||
| it('exports the block: ' + f.description + ' correctly', function() { | ||
| assert.equal(block.toHex(), f.hex) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getHash', function() { | ||
| fixtures.valid.forEach(function(f) { | ||
| var block | ||
|
|
||
| beforeEach(function() { | ||
| block = Block.fromHex(f.hex) | ||
| }) | ||
|
|
||
| it('calculates ' + f.hash + ' for the block: ' + f.description, function() { | ||
| assert.equal(block.getHash().toString('hex'), f.hash) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getId', function() { | ||
| fixtures.valid.forEach(function(f) { | ||
| var block | ||
|
|
||
| beforeEach(function() { | ||
| block = Block.fromHex(f.hex) | ||
| }) | ||
|
|
||
| it('calculates ' + f.id + ' for the block: ' + f.description, function() { | ||
| assert.equal(block.getId(), f.id) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('getUTCDate', function() { | ||
| fixtures.valid.forEach(function(f) { | ||
| var block | ||
|
|
||
| beforeEach(function() { | ||
| block = Block.fromHex(f.hex) | ||
| }) | ||
|
|
||
| it('returns UTC date of ' + f.id, function() { | ||
| var utcDate = block.getUTCDate().getTime() | ||
|
|
||
| assert.equal(utcDate, f.timestamp * 1e3) | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have
toBuffer()andheadersToBuffer()instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
fromBufferimports both types.I'm unsure on this, but absolutely agree it would be consistent with the pattern we have applied to all other instances of this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of
headersToBuffer, but I definitely agree this should change.