From 169be71d792a61e1ecc43dde216ae22600870a35 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 10 May 2014 20:22:35 +0400 Subject: [PATCH] tx: store as JSON --- lib/bcoin/chain.js | 4 ++-- lib/bcoin/pool.js | 11 ++++------- lib/bcoin/tx.js | 32 +++++++++++++++++++------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/bcoin/chain.js b/lib/bcoin/chain.js index 091ec9bc..ec1c5e62 100644 --- a/lib/bcoin/chain.js +++ b/lib/bcoin/chain.js @@ -54,8 +54,8 @@ Chain.prototype._init = function _init() { this.loading = true; var self = this; var s = this.storage.createReadStream({ - start: this._prefix, - end: this._prefix + 'z' + start: this.prefix, + end: this.prefix + 'z' }); s.on('data', function(data) { var hash = data.key.slice(self.prefix.length); diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index b0ba8628..1f8e6a52 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -412,9 +412,6 @@ Pool.prototype.search = function search(id, range) { }; Pool.prototype._request = function _request(type, hash, options, cb) { - if (typeof hash === 'string') - hash = utils.toArray(hash, 'hex'); - // Optional `force` if (typeof options === 'function') { cb = options; @@ -423,9 +420,9 @@ Pool.prototype._request = function _request(type, hash, options, cb) { if (!options) options = {}; - var hex = utils.toHex(hash); - if (this.request.map[hex]) - return this.request.map[hex].addCallback(cb); + hash = utils.toHex(hash); + if (this.request.map[hash]) + return this.request.map[hash].addCallback(cb); var self = this; @@ -442,7 +439,7 @@ Pool.prototype._request = function _request(type, hash, options, cb) { if (self.destroyed) return; - var req = new LoadRequest(self, type, hex, cb); + var req = new LoadRequest(self, type, hash, cb); req.add(options.noQueue); } }; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index c3db4e1b..302c0c5f 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -17,6 +17,7 @@ function TX(data, block) { this.outputs = []; this.lock = data.lock || 0; this.ts = data.ts || 0; + this.block = null; this.funds = new bn(0); this._hash = null; @@ -33,8 +34,10 @@ function TX(data, block) { }, this); } - if (!data.ts && block && block.hasTX(this.hash('hex'))) + if (!data.ts && block && block.hasTX(this.hash('hex'))) { this.ts = block.ts; + this.block = block.hash('hex'); + } // ps = Pending Since this.ps = this.ts === 0 ? +new Date / 1000 : 0; @@ -249,22 +252,25 @@ TX.prototype.inputAddrs = function inputAddrs() { TX.prototype.toJSON = function toJSON() { // Compact representation - var ts = new Array(8); - bcoin.utils.writeU32(ts, this.ts, 0); - bcoin.utils.writeU32(ts, this.ps, 4); - return utils.toHex(this.render().concat(ts)); + return { + v: '1', + type: 'tx', + ts: this.ts, + ps: this.ps, + block: this.block, + tx: utils.toHex(this.render()) + }; }; TX.fromJSON = function fromJSON(json) { - // Compact representation - var data = utils.toArray(json, 'hex'); - var tx = data.slice(0, -8); - var ts = bcoin.utils.readU32(data, data.length - 8); - var ps = bcoin.utils.readU32(data, data.length - 4); + assert.equal(json.v, 1); + assert.equal(json.type, 'tx'); - tx = new TX(new bcoin.protocol.parser().parseTX(tx)); - tx.ts = ts; - tx.ps = ps; + var raw = utils.toArray(json.tx, 'hex'); + tx = new TX(new bcoin.protocol.parser().parseTX(raw)); + tx.ts = json.ts; + tx.block = json.block || null; + tx.ps = json.ps; return tx; };