full json. cache.
This commit is contained in:
parent
21ff3f1d78
commit
6b6a84a774
@ -522,6 +522,31 @@ Block.fromJSON = function fromJSON(json) {
|
|||||||
return block;
|
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) {
|
Block.prototype.toRaw = function toRaw(enc) {
|
||||||
var data;
|
var data;
|
||||||
|
|
||||||
|
|||||||
@ -386,7 +386,7 @@ Chain.prototype._preload = function _preload(callback) {
|
|||||||
// Filthy hack to avoid writing
|
// Filthy hack to avoid writing
|
||||||
// redundant blocks to disk!
|
// redundant blocks to disk!
|
||||||
if (height <= chainHeight) {
|
if (height <= chainHeight) {
|
||||||
self.db._cache(entry, true);
|
self.db._cache(entry);
|
||||||
self.db._populate(entry);
|
self.db._populate(entry);
|
||||||
} else {
|
} else {
|
||||||
self.db.saveAsync(entry);
|
self.db.saveAsync(entry);
|
||||||
|
|||||||
@ -149,9 +149,6 @@ ChainDB.prototype.load = function load(start, callback) {
|
|||||||
if (err)
|
if (err)
|
||||||
return callback(err);
|
return callback(err);
|
||||||
|
|
||||||
// Force caching
|
|
||||||
self._cache(entry, true);
|
|
||||||
|
|
||||||
// Do some paranoid checks.
|
// Do some paranoid checks.
|
||||||
if (lastEntry && entry.prevBlock !== lastEntry.hash)
|
if (lastEntry && entry.prevBlock !== lastEntry.hash)
|
||||||
return done(Math.max(0, i - 2));
|
return done(Math.max(0, i - 2));
|
||||||
@ -249,16 +246,12 @@ ChainDB.prototype.getSize = function getSize() {
|
|||||||
return len;
|
return len;
|
||||||
};
|
};
|
||||||
|
|
||||||
ChainDB.prototype._cache = function _cache(entry, force) {
|
ChainDB.prototype._cache = function _cache(entry) {
|
||||||
// if (!force && this.loading)
|
if (entry.height === this.highest + 1) {
|
||||||
// return;
|
|
||||||
|
|
||||||
if (entry.height > this.highest) {
|
|
||||||
this.highest = entry.height;
|
this.highest = entry.height;
|
||||||
delete this.cache[entry.height - this._cacheWindow];
|
delete this.cache[entry.height - this._cacheWindow];
|
||||||
this.cache[entry.height] = entry;
|
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);
|
return next(err);
|
||||||
if (!tx)
|
if (!tx)
|
||||||
return send(404);
|
return send(404);
|
||||||
send(200, tx.toJSON());
|
send(200, tx.toFullJSON());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -116,7 +116,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return next(err);
|
return next(err);
|
||||||
if (!txs.length)
|
if (!txs.length)
|
||||||
return send(404);
|
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);
|
return next(err);
|
||||||
if (!txs.length)
|
if (!txs.length)
|
||||||
return send(404);
|
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);
|
return next(err);
|
||||||
if (!block)
|
if (!block)
|
||||||
return send(404);
|
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
|
* 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
|
* 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) {
|
TX.fromJSON = function fromJSON(json) {
|
||||||
var raw, data, tx;
|
var raw, data, tx;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user