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 bufferutils = require('./bufferutils')
|
||||
var fastMerkleRoot = require('merkle-lib/fastRoot')
|
||||
var typeforce = require('typeforce')
|
||||
var types = require('./types')
|
||||
var varuint = require('varuint-bitcoin')
|
||||
|
||||
var Transaction = require('./transaction')
|
||||
|
||||
|
@ -54,7 +55,6 @@ Block.fromBuffer = function (buffer) {
|
|||
|
||||
function readTransaction () {
|
||||
var tx = Transaction.fromBuffer(buffer.slice(offset), true)
|
||||
|
||||
offset += tx.byteLength()
|
||||
return tx
|
||||
}
|
||||
|
@ -70,6 +70,14 @@ Block.fromBuffer = function (buffer) {
|
|||
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) {
|
||||
return Block.fromBuffer(new Buffer(hex, 'hex'))
|
||||
}
|
||||
|
@ -89,8 +97,9 @@ Block.prototype.getUTCDate = function () {
|
|||
return date
|
||||
}
|
||||
|
||||
// TODO: buffer, offset compatibility
|
||||
Block.prototype.toBuffer = function (headersOnly) {
|
||||
var buffer = new Buffer(80)
|
||||
var buffer = new Buffer(this.byteLength(headersOnly))
|
||||
|
||||
var offset = 0
|
||||
function writeSlice (slice) {
|
||||
|
@ -116,12 +125,16 @@ Block.prototype.toBuffer = function (headersOnly) {
|
|||
|
||||
if (headersOnly || !this.transactions) return buffer
|
||||
|
||||
var txLenBuffer = bufferutils.varIntBuffer(this.transactions.length)
|
||||
var txBuffers = this.transactions.map(function (tx) {
|
||||
return tx.toBuffer()
|
||||
varuint.encode(this.transactions.length, buffer, offset)
|
||||
offset += varuint.encode.bytes
|
||||
|
||||
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) {
|
||||
|
|
|
@ -58,6 +58,7 @@ describe('Block', function () {
|
|||
})
|
||||
|
||||
it('exports ' + f.description, function () {
|
||||
assert.strictEqual(block.toHex(true), f.hex.slice(0, 160))
|
||||
assert.strictEqual(block.toHex(), f.hex)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue