full json. cache.
This commit is contained in:
parent
21ff3f1d78
commit
6b6a84a774
@ -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;
|
||||
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -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());
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user