refactor framer
This commit is contained in:
parent
69038ac55e
commit
e64a366400
@ -65,7 +65,7 @@ function Block(data, subtype) {
|
||||
else if (this.txs.length)
|
||||
this.subtype = 'block';
|
||||
else
|
||||
this.subtype = 'header';
|
||||
this.subtype = 'headers';
|
||||
}
|
||||
|
||||
if (this.subtype === 'block') {
|
||||
@ -120,7 +120,14 @@ Block.verify = function verify(data, subtype) {
|
||||
Block.prototype.render = function render() {
|
||||
if (this._raw)
|
||||
return this._raw;
|
||||
return bcoin.protocol.framer.block(this, this.subtype);
|
||||
|
||||
if (this.subtype === 'merkleblock')
|
||||
return bcoin.protocol.framer.merkleBlock(this);
|
||||
|
||||
if (this.subtype === 'headers')
|
||||
return bcoin.protocol.framer.headers(this);
|
||||
|
||||
return bcoin.protocol.framer.block(this);
|
||||
};
|
||||
|
||||
Block.prototype.getSize = function getSize() {
|
||||
|
||||
@ -386,32 +386,22 @@ Framer.prototype.tx = function tx(tx) {
|
||||
return this.packet('tx', Framer.tx(tx));
|
||||
};
|
||||
|
||||
Framer.block = function _block(block, type) {
|
||||
Framer.block = function _block(block) {
|
||||
var off = 0;
|
||||
var txSize = 0;
|
||||
var txs = [];
|
||||
var i, tx, p;
|
||||
|
||||
if (!type)
|
||||
type = block.subtype;
|
||||
|
||||
if (type === 'merkleblock') {
|
||||
p = new Buffer(80 + 4
|
||||
+ utils.sizeIntv(block.hashes.length) + (block.hashes.length * 32)
|
||||
+ utils.sizeIntv(block.flags.length) + block.flags.length);
|
||||
} else if (type === 'header') {
|
||||
p = new Buffer(80 + utils.sizeIntv(block.txs.length));
|
||||
} else {
|
||||
for (i = 0; i < block.txs.length; i++) {
|
||||
tx = block.txs[i].render
|
||||
? block.txs[i].render()
|
||||
: Framer.tx(block.txs[i]);
|
||||
txs.push(tx);
|
||||
txSize += tx.length;
|
||||
}
|
||||
p = new Buffer(80 + utils.sizeIntv(block.txs.length) + txSize);
|
||||
for (i = 0; i < block.txs.length; i++) {
|
||||
tx = block.txs[i].render
|
||||
? block.txs[i].render()
|
||||
: Framer.tx(block.txs[i]);
|
||||
txs.push(tx);
|
||||
txSize += tx.length;
|
||||
}
|
||||
|
||||
p = new Buffer(80 + utils.sizeIntv(block.txs.length) + txSize);
|
||||
|
||||
// version
|
||||
off += utils.write32(p, block.version, off);
|
||||
|
||||
@ -432,44 +422,106 @@ Framer.block = function _block(block, type) {
|
||||
|
||||
assert.equal(off, 80);
|
||||
|
||||
if (type === 'merkleblock') {
|
||||
// txn_count
|
||||
off += utils.writeU32(p, block.totalTX, off);
|
||||
// txn_count
|
||||
off += utils.writeIntv(p, block.txs.length, off);
|
||||
|
||||
// hash count
|
||||
off += utils.writeIntv(p, block.hashes.length, off);
|
||||
// txs
|
||||
for (i = 0; i < txs.length; i++)
|
||||
off += utils.copy(txs[i], p, off);
|
||||
|
||||
// hashes
|
||||
for (i = 0; i < block.hashes.length; i++)
|
||||
off += utils.copy(new Buffer(block.hashes[i], 'hex'), p, off);
|
||||
return p;
|
||||
};
|
||||
|
||||
// flag count
|
||||
off += utils.writeIntv(p, block.flags.length, off);
|
||||
Framer.merkleBlock = function _merkleBlock(block) {
|
||||
var off = 0;
|
||||
var p, i;
|
||||
|
||||
// flags
|
||||
for (i = 0; i < block.flags.length; i++)
|
||||
p[off++] = block.flags[i];
|
||||
} else if (type === 'header') {
|
||||
// txn_count
|
||||
off += utils.writeIntv(p, block.txs.length, off);
|
||||
} else {
|
||||
// txn_count
|
||||
off += utils.writeIntv(p, block.txs.length, off);
|
||||
p = new Buffer(80 + 4
|
||||
+ utils.sizeIntv(block.hashes.length) + (block.hashes.length * 32)
|
||||
+ utils.sizeIntv(block.flags.length) + block.flags.length);
|
||||
|
||||
// txs
|
||||
for (i = 0; i < txs.length; i++)
|
||||
off += utils.copy(txs[i], p, off);
|
||||
}
|
||||
// version
|
||||
off += utils.write32(p, block.version, off);
|
||||
|
||||
// prev_block
|
||||
off += utils.copy(new Buffer(block.prevBlock, 'hex'), p, off);
|
||||
|
||||
// merkle_root
|
||||
off += utils.copy(new Buffer(block.merkleRoot, 'hex'), p, off);
|
||||
|
||||
// timestamp
|
||||
off += utils.writeU32(p, block.ts, off);
|
||||
|
||||
// bits
|
||||
off += utils.writeU32(p, block.bits, off);
|
||||
|
||||
// nonce
|
||||
off += utils.writeU32(p, block.nonce, off);
|
||||
|
||||
assert.equal(off, 80);
|
||||
|
||||
// txn_count
|
||||
off += utils.writeU32(p, block.totalTX, off);
|
||||
|
||||
// hash count
|
||||
off += utils.writeIntv(p, block.hashes.length, off);
|
||||
|
||||
// hashes
|
||||
for (i = 0; i < block.hashes.length; i++)
|
||||
off += utils.copy(new Buffer(block.hashes[i], 'hex'), p, off);
|
||||
|
||||
// flag count
|
||||
off += utils.writeIntv(p, block.flags.length, off);
|
||||
|
||||
// flags
|
||||
for (i = 0; i < block.flags.length; i++)
|
||||
p[off++] = block.flags[i];
|
||||
|
||||
return p;
|
||||
};
|
||||
|
||||
Framer.headers = function _headers(block) {
|
||||
var off = 0;
|
||||
var p, i;
|
||||
|
||||
p = new Buffer(80 + utils.sizeIntv(data.totalTX));
|
||||
|
||||
// version
|
||||
off += utils.write32(p, block.version, off);
|
||||
|
||||
// prev_block
|
||||
off += utils.copy(new Buffer(block.prevBlock, 'hex'), p, off);
|
||||
|
||||
// merkle_root
|
||||
off += utils.copy(new Buffer(block.merkleRoot, 'hex'), p, off);
|
||||
|
||||
// timestamp
|
||||
off += utils.writeU32(p, block.ts, off);
|
||||
|
||||
// bits
|
||||
off += utils.writeU32(p, block.bits, off);
|
||||
|
||||
// nonce
|
||||
off += utils.writeU32(p, block.nonce, off);
|
||||
|
||||
assert.equal(off, 80);
|
||||
|
||||
// txn_count
|
||||
off += utils.writeIntv(p, data.totalTX, off);
|
||||
|
||||
return p;
|
||||
};
|
||||
|
||||
Framer.prototype.block = function _block(block) {
|
||||
return this.packet('block', Framer.block(block, 'block'));
|
||||
return this.packet('block', Framer.block(block));
|
||||
};
|
||||
|
||||
Framer.prototype.merkleBlock = function merkleBlock(block) {
|
||||
return this.packet('merkleblock', Framer.block(block, 'merkleblock'));
|
||||
return this.packet('merkleblock', Framer.merkleBlock(block));
|
||||
};
|
||||
|
||||
Framer.prototype.headers = function headers(block) {
|
||||
return this.packet('headers', Framer.headers(block));
|
||||
};
|
||||
|
||||
Framer.prototype.reject = function reject(details) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user