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;
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);

View File

@ -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);
}
};

View File

@ -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;
};