minor: style - move block json functions to bottom of the file.
This commit is contained in:
parent
5e3e72ba4c
commit
73ea21a903
@ -127,37 +127,6 @@ Block.prototype._verifyMerkle = function verifyMerkle() {
|
||||
}
|
||||
};
|
||||
|
||||
Block.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
v: '1',
|
||||
type: 'block',
|
||||
subtype: this.subtype,
|
||||
hash: this._hash || this.hash('hex'),
|
||||
prevBlock: this.prevBlock,
|
||||
ts: this.ts,
|
||||
block: utils.toHex(bcoin.protocol.framer.block(this, this.subtype))
|
||||
};
|
||||
};
|
||||
|
||||
Block.fromJSON = function fromJSON(json) {
|
||||
utils.assert.equal(json.v, 1);
|
||||
utils.assert.equal(json.type, 'block');
|
||||
|
||||
var raw = utils.toArray(json.block, 'hex');
|
||||
|
||||
var parser = new bcoin.protocol.parser();
|
||||
|
||||
var data = json.subtype === 'merkleblock' ?
|
||||
parser.parseMerkleBlock(raw) :
|
||||
parser.parseBlock(raw);
|
||||
|
||||
var block = new bcoin.block(data, json.subtype);
|
||||
|
||||
block._hash = json.hash;
|
||||
|
||||
return block;
|
||||
};
|
||||
|
||||
Block.prototype._buildMerkle = function buildMerkle() {
|
||||
var merkleTree = [];
|
||||
for (var i = 0; i < this.txs.length; i++) {
|
||||
@ -218,3 +187,34 @@ Block.prototype._checkBlock = function checkBlock() {
|
||||
// Check merkle root
|
||||
return this.merkleTree[this.merkleTree.length - 1] === this.merkleRoot;
|
||||
};
|
||||
|
||||
Block.prototype.toJSON = function toJSON() {
|
||||
return {
|
||||
v: '1',
|
||||
type: 'block',
|
||||
subtype: this.subtype,
|
||||
hash: this._hash || this.hash('hex'),
|
||||
prevBlock: this.prevBlock,
|
||||
ts: this.ts,
|
||||
block: utils.toHex(bcoin.protocol.framer.block(this, this.subtype))
|
||||
};
|
||||
};
|
||||
|
||||
Block.fromJSON = function fromJSON(json) {
|
||||
utils.assert.equal(json.v, 1);
|
||||
utils.assert.equal(json.type, 'block');
|
||||
|
||||
var raw = utils.toArray(json.block, 'hex');
|
||||
|
||||
var parser = new bcoin.protocol.parser();
|
||||
|
||||
var data = json.subtype === 'merkleblock' ?
|
||||
parser.parseMerkleBlock(raw) :
|
||||
parser.parseBlock(raw);
|
||||
|
||||
var block = new bcoin.block(data, json.subtype);
|
||||
|
||||
block._hash = json.hash;
|
||||
|
||||
return block;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user