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