block: add byteLength
This commit is contained in:
parent
e205b99180
commit
f3d0dddc5a
2 changed files with 21 additions and 7 deletions
27
src/block.js
27
src/block.js
|
@ -1,8 +1,9 @@
|
||||||
var bufferutils = require('./bufferutils')
|
|
||||||
var bcrypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
|
var bufferutils = require('./bufferutils')
|
||||||
var fastMerkleRoot = require('merkle-lib/fastRoot')
|
var fastMerkleRoot = require('merkle-lib/fastRoot')
|
||||||
var typeforce = require('typeforce')
|
var typeforce = require('typeforce')
|
||||||
var types = require('./types')
|
var types = require('./types')
|
||||||
|
var varuint = require('varuint-bitcoin')
|
||||||
|
|
||||||
var Transaction = require('./transaction')
|
var Transaction = require('./transaction')
|
||||||
|
|
||||||
|
@ -54,7 +55,6 @@ Block.fromBuffer = function (buffer) {
|
||||||
|
|
||||||
function readTransaction () {
|
function readTransaction () {
|
||||||
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
||||||
|
|
||||||
offset += tx.byteLength()
|
offset += tx.byteLength()
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,14 @@ Block.fromBuffer = function (buffer) {
|
||||||
return block
|
return block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Block.prototype.byteLength = function (headersOnly) {
|
||||||
|
if (headersOnly || !this.transactions) return 80
|
||||||
|
|
||||||
|
return 80 + varuint.encodingLength(this.transactions.length) + this.transactions.reduce(function (a, x) {
|
||||||
|
return a + x.byteLength()
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
|
|
||||||
Block.fromHex = function (hex) {
|
Block.fromHex = function (hex) {
|
||||||
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
||||||
}
|
}
|
||||||
|
@ -89,8 +97,9 @@ Block.prototype.getUTCDate = function () {
|
||||||
return date
|
return date
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: buffer, offset compatibility
|
||||||
Block.prototype.toBuffer = function (headersOnly) {
|
Block.prototype.toBuffer = function (headersOnly) {
|
||||||
var buffer = new Buffer(80)
|
var buffer = new Buffer(this.byteLength(headersOnly))
|
||||||
|
|
||||||
var offset = 0
|
var offset = 0
|
||||||
function writeSlice (slice) {
|
function writeSlice (slice) {
|
||||||
|
@ -116,12 +125,16 @@ Block.prototype.toBuffer = function (headersOnly) {
|
||||||
|
|
||||||
if (headersOnly || !this.transactions) return buffer
|
if (headersOnly || !this.transactions) return buffer
|
||||||
|
|
||||||
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
varuint.encode(this.transactions.length, buffer, offset)
|
||||||
var txBuffers = this.transactions.map(function (tx) {
|
offset += varuint.encode.bytes
|
||||||
return tx.toBuffer()
|
|
||||||
|
this.transactions.forEach(function (tx) {
|
||||||
|
var txSize = tx.byteLength() // TODO: extract from toBuffer?
|
||||||
|
tx.toBuffer(buffer, offset)
|
||||||
|
offset += txSize
|
||||||
})
|
})
|
||||||
|
|
||||||
return Buffer.concat([buffer, txLenBuffer].concat(txBuffers))
|
return buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.prototype.toHex = function (headersOnly) {
|
Block.prototype.toHex = function (headersOnly) {
|
||||||
|
|
|
@ -58,6 +58,7 @@ describe('Block', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
it('exports ' + f.description, function () {
|
it('exports ' + f.description, function () {
|
||||||
|
assert.strictEqual(block.toHex(true), f.hex.slice(0, 160))
|
||||||
assert.strictEqual(block.toHex(), f.hex)
|
assert.strictEqual(block.toHex(), f.hex)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue