diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index f7e55475..a8a5ab0f 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -522,6 +522,31 @@ Block.fromJSON = function fromJSON(json) { return block; }; +Block.prototype.toFullJSON = function toFullJSON() { + return { + v: 1, + type: 'block', + subtype: this.subtype, + height: this.height, + network: this.network, + relayedBy: this.relayedBy, + hash: this.hash('hex'), + version: this.version, + prevBlock: this.prevBlock, + merkleRoot: this.merkleRoot, + ts: this.ts, + bits: this.bits, + nonce: this.nonce, + txs: this.txs.map(function(tx) { + return tx.toFullJSON(); + }) + }; +}; + +Block.fromFullJSON = function fromFullJSON(json) { + return new Block(json, json.subtype); +}; + Block.prototype.toRaw = function toRaw(enc) { var data; diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 06fec87e..2bf2d814 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -386,7 +386,7 @@ Chain.prototype._preload = function _preload(callback) { // Filthy hack to avoid writing // redundant blocks to disk! if (height <= chainHeight) { - self.db._cache(entry, true); + self.db._cache(entry); self.db._populate(entry); } else { self.db.saveAsync(entry); diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index b92021c3..7f57400d 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -149,9 +149,6 @@ ChainDB.prototype.load = function load(start, callback) { if (err) return callback(err); - // Force caching - self._cache(entry, true); - // Do some paranoid checks. if (lastEntry && entry.prevBlock !== lastEntry.hash) return done(Math.max(0, i - 2)); @@ -249,16 +246,12 @@ ChainDB.prototype.getSize = function getSize() { return len; }; -ChainDB.prototype._cache = function _cache(entry, force) { - // if (!force && this.loading) - // return; - - if (entry.height > this.highest) { +ChainDB.prototype._cache = function _cache(entry) { + if (entry.height === this.highest + 1) { this.highest = entry.height; delete this.cache[entry.height - this._cacheWindow]; this.cache[entry.height] = entry; - if (!this.loading) - assert(Object.keys(this.cache).length <= this._cacheWindow); + assert(Object.keys(this.cache).length <= this._cacheWindow); } }; diff --git a/lib/bcoin/http.js b/lib/bcoin/http.js index 76c73c84..bd829e3c 100644 --- a/lib/bcoin/http.js +++ b/lib/bcoin/http.js @@ -104,7 +104,7 @@ HTTPServer.prototype._init = function _init() { return next(err); if (!tx) return send(404); - send(200, tx.toJSON()); + send(200, tx.toFullJSON()); }); }); @@ -116,7 +116,7 @@ HTTPServer.prototype._init = function _init() { return next(err); if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { return tx.toFullJSON(); })); }); }); @@ -127,7 +127,7 @@ HTTPServer.prototype._init = function _init() { return next(err); if (!txs.length) return send(404); - send(200, txs.map(function(tx) { return tx.toJSON(); })); + send(200, txs.map(function(tx) { return tx.toFullJSON(); })); }); }); @@ -138,7 +138,7 @@ HTTPServer.prototype._init = function _init() { return next(err); if (!block) return send(404); - send(200, block.toJSON()); + send(200, block.toFullJSON()); }); }); diff --git a/lib/bcoin/input.js b/lib/bcoin/input.js index 6e3c4e76..4dd78e82 100644 --- a/lib/bcoin/input.js +++ b/lib/bcoin/input.js @@ -365,6 +365,27 @@ Input.prototype.inspect = function inspect() { }; }; +Input.prototype.toJSON = function toJSON() { + return { + prevout: { + hash: this.prevout.hash, + index: this.prevout.index + }, + output: this.output ? this.output.toJSON() : null, + script: utils.toHex(bcoin.script.encode(this.script)), + sequence: this.sequence + }; +}; + +Input.fromJSON = function fromJSON(json) { + return new Input({ + prevout: json.prevout, + output: json.output, + script: bcoin.script.decode(utils.toArray(json.script, 'hex')), + sequence: json.sequence + }); +}; + /** * Expose */ diff --git a/lib/bcoin/output.js b/lib/bcoin/output.js index 218ba644..706566be 100644 --- a/lib/bcoin/output.js +++ b/lib/bcoin/output.js @@ -259,6 +259,20 @@ Output.prototype.inspect = function inspect() { }; }; +Output.prototype.toJSON = function toJSON() { + return { + value: utils.btc(this.value), + script: utils.toHex(bcoin.script.encode(this.script)) + }; +}; + +Output.fromJSON = function fromJSON(json) { + return new Output({ + value: utils.satoshi(json.value), + script: bcoin.script.decode(utils.toArray(json.script, 'hex')) + }); +}; + /** * Expose */ diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 6df733d6..05f108dd 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -1822,6 +1822,49 @@ TX.prototype.toJSON = function toJSON(coins) { }; }; +TX.prototype.toFullJSON = function toFullJSON() { + return { + v: 1, + type: 'tx', + ts: this.ts, + ps: this.ps, + block: this.block, + height: this.height, + network: this.network, + relayedBy: this.relayedBy, + changeIndex: this.changeIndex, + version: this.version, + inputs: this.inputs.map(function(input) { + return input.toJSON(); + }), + outputs: this.outputs.map(function(output) { + return output.toJSON(); + }), + locktime: this.locktime + }; +}; + +TX.fromFullJSON = function fromFullJSON(json) { + return new TX({ + ts: json.ts, + ps: json.ps, + block: json.block, + height: json.height, + network: json.network, + relayedBy: json.relayedBy, + changeIndex: json.changeIndex, + version: json.version, + inputs: json.inputs.map(function(input) { + return bcoin.input.fromJSON(input); + }), + outputs: json.outputs.map(function(output) { + return bcoin.output.fromJSON(output); + }), + locktime: json.locktime + }); +}; + + TX.fromJSON = function fromJSON(json) { var raw, data, tx;