tx: store as JSON

This commit is contained in:
Fedor Indutny 2014-05-10 20:22:35 +04:00
parent c7fda1391c
commit 169be71d79
3 changed files with 25 additions and 22 deletions

View File

@ -54,8 +54,8 @@ Chain.prototype._init = function _init() {
this.loading = true; this.loading = true;
var self = this; var self = this;
var s = this.storage.createReadStream({ var s = this.storage.createReadStream({
start: this._prefix, start: this.prefix,
end: this._prefix + 'z' end: this.prefix + 'z'
}); });
s.on('data', function(data) { s.on('data', function(data) {
var hash = data.key.slice(self.prefix.length); var hash = data.key.slice(self.prefix.length);

View File

@ -412,9 +412,6 @@ Pool.prototype.search = function search(id, range) {
}; };
Pool.prototype._request = function _request(type, hash, options, cb) { Pool.prototype._request = function _request(type, hash, options, cb) {
if (typeof hash === 'string')
hash = utils.toArray(hash, 'hex');
// Optional `force` // Optional `force`
if (typeof options === 'function') { if (typeof options === 'function') {
cb = options; cb = options;
@ -423,9 +420,9 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
if (!options) if (!options)
options = {}; options = {};
var hex = utils.toHex(hash); hash = utils.toHex(hash);
if (this.request.map[hex]) if (this.request.map[hash])
return this.request.map[hex].addCallback(cb); return this.request.map[hash].addCallback(cb);
var self = this; var self = this;
@ -442,7 +439,7 @@ Pool.prototype._request = function _request(type, hash, options, cb) {
if (self.destroyed) if (self.destroyed)
return; return;
var req = new LoadRequest(self, type, hex, cb); var req = new LoadRequest(self, type, hash, cb);
req.add(options.noQueue); req.add(options.noQueue);
} }
}; };

View File

@ -17,6 +17,7 @@ function TX(data, block) {
this.outputs = []; this.outputs = [];
this.lock = data.lock || 0; this.lock = data.lock || 0;
this.ts = data.ts || 0; this.ts = data.ts || 0;
this.block = null;
this.funds = new bn(0); this.funds = new bn(0);
this._hash = null; this._hash = null;
@ -33,8 +34,10 @@ function TX(data, block) {
}, this); }, this);
} }
if (!data.ts && block && block.hasTX(this.hash('hex'))) if (!data.ts && block && block.hasTX(this.hash('hex'))) {
this.ts = block.ts; this.ts = block.ts;
this.block = block.hash('hex');
}
// ps = Pending Since // ps = Pending Since
this.ps = this.ts === 0 ? +new Date / 1000 : 0; this.ps = this.ts === 0 ? +new Date / 1000 : 0;
@ -249,22 +252,25 @@ TX.prototype.inputAddrs = function inputAddrs() {
TX.prototype.toJSON = function toJSON() { TX.prototype.toJSON = function toJSON() {
// Compact representation // Compact representation
var ts = new Array(8); return {
bcoin.utils.writeU32(ts, this.ts, 0); v: '1',
bcoin.utils.writeU32(ts, this.ps, 4); type: 'tx',
return utils.toHex(this.render().concat(ts)); ts: this.ts,
ps: this.ps,
block: this.block,
tx: utils.toHex(this.render())
};
}; };
TX.fromJSON = function fromJSON(json) { TX.fromJSON = function fromJSON(json) {
// Compact representation assert.equal(json.v, 1);
var data = utils.toArray(json, 'hex'); assert.equal(json.type, 'tx');
var tx = data.slice(0, -8);
var ts = bcoin.utils.readU32(data, data.length - 8);
var ps = bcoin.utils.readU32(data, data.length - 4);
tx = new TX(new bcoin.protocol.parser().parseTX(tx)); var raw = utils.toArray(json.tx, 'hex');
tx.ts = ts; tx = new TX(new bcoin.protocol.parser().parseTX(raw));
tx.ps = ps; tx.ts = json.ts;
tx.block = json.block || null;
tx.ps = json.ps;
return tx; return tx;
}; };