diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b7914f91..de52382d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ An index can be dropped by just deleting the corresponding database. + `getMeta(hash)` + `getTX(hash)` + `hasTX(hash)` + + `getSpentView(tx)` `node.addrindex` implements: @@ -48,9 +49,6 @@ An index can be dropped by just deleting the corresponding database. Using these methods on the chain is deprecated. - -- `__/lib/blockchain/chain__` - `getSpentView` accepts a `TXMeta` insted of `TX` - ## v1.0.0 ### Migration diff --git a/docs/Examples/get-tx-from-chain.js b/docs/Examples/get-tx-from-chain.js index 8e12f57b0..9f364b174 100644 --- a/docs/Examples/get-tx-from-chain.js +++ b/docs/Examples/get-tx-from-chain.js @@ -73,11 +73,11 @@ const indexer = new bcoin.TXIndexer({ const block = await chain.getBlock(tip.hash); const meta = await indexer.getMeta(block.txs[0].hash()); const tx = meta.tx; - const coinview = await chain.db.getSpentView(meta); + const view = await indexer.getSpentView(tx); - console.log(`Tx with hash ${tx.hash()}:`, meta); - console.log(`Tx input: ${tx.getInputValue(coinview)},` + - ` output: ${tx.getOutputValue()}, fee: ${tx.getFee(coinview)}`); + console.log(`Tx with hash ${tx.rhash()}:`, meta); + console.log(`Tx input: ${tx.getInputValue(view)},` + + ` output: ${tx.getOutputValue()}, fee: ${tx.getFee(view)}`); await indexer.close(); await chain.close(); diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index bdf4b2ab1..93d65d19f 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -976,24 +976,11 @@ class ChainDB { */ async getSpentView(meta) { - const tx = meta.tx; - const view = await this.getCoinView(tx); - - for (const {prevout} of tx.inputs) { - if (view.hasEntry(prevout)) - continue; - - // TODO: partial destructured assignment? - // eslint-disable-next-line no-unused-vars - const {hash, index} = prevout; - - const {tx, height} = meta; - - if (index < tx.outputs.length) - view.addIndex(tx, index, height); - } - - return view; + process.emitWarning( + 'deprecated, use node.txindex.getSpentView', + 'DeprecationWarning' + ); + return null; } /** diff --git a/lib/indexer/chainclient.js b/lib/indexer/chainclient.js index 27d238cfd..9aac15152 100644 --- a/lib/indexer/chainclient.js +++ b/lib/indexer/chainclient.js @@ -155,8 +155,17 @@ class ChainClient extends AsyncEmitter { */ async getBlockView(block) { - const view = this.chain.getBlockView(block); - return view; + return this.chain.getBlockView(block); + } + + /** + * Get coin viewpoint. + * @param {TX} tx + * @returns {Promise} - Returns {@link CoinView}. + */ + + async getCoinView(tx) { + return this.chain.getCoinView(tx); } /** diff --git a/lib/indexer/nullclient.js b/lib/indexer/nullclient.js index 1cad9d646..44838994d 100644 --- a/lib/indexer/nullclient.js +++ b/lib/indexer/nullclient.js @@ -114,6 +114,16 @@ class NullClient extends EventEmitter { return null; } + /** + * Get coin viewpoint. + * @param {TX} tx + * @returns {Promise} - Returns {@link CoinView}. + */ + + async getCoinView(tx) { + return null; + } + /** * Rescan for any missed blocks. * @param {Number} start - Start block. diff --git a/lib/indexer/txindexer.js b/lib/indexer/txindexer.js index 86bfed831..b33db2507 100644 --- a/lib/indexer/txindexer.js +++ b/lib/indexer/txindexer.js @@ -118,6 +118,34 @@ class TXIndexer extends Indexer { async hasTX(hash) { return this.db.has(layout.t.encode(hash)); } + + /** + * Get coin viewpoint (historical). + * @param {TX} tx + * @returns {Promise} - Returns {@link CoinView}. + */ + + async getSpentView(tx) { + const view = await this.client.getCoinView(tx); + + for (const {prevout} of tx.inputs) { + if (view.hasEntry(prevout)) + continue; + + const {hash, index} = prevout; + const meta = await this.getMeta(hash); + + if (!meta) + continue; + + const {tx, height} = meta; + + if (index < tx.outputs.length) + view.addIndex(tx, index, height); + } + + return view; + } } module.exports = TXIndexer; diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index c69f0a2fc..5c2bec0b8 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -520,7 +520,12 @@ class FullNode extends Node { async getMetaView(meta) { if (meta.height === -1) return this.mempool.getSpentView(meta.tx); - return this.chain.getSpentView(meta); + + if (this.txindex) { + return this.txindex.getSpentView(meta.tx); + } + + return null; } /** diff --git a/lib/node/http.js b/lib/node/http.js index eaedfedd6..15a1f16b0 100644 --- a/lib/node/http.js +++ b/lib/node/http.js @@ -148,6 +148,10 @@ class HTTP extends Server { }); }); + this.get('/uptime', async (req, res) => { + res.json(200, { uptime: this.node.uptime().toString()}); + }); + // UTXO by address this.get('/coin/address/:address', async (req, res) => { const valid = Validator.fromRequest(req); diff --git a/package.json b/package.json index 9d89992e5..b152eb33a 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,8 @@ "devDependencies": { "eslint": "^5.1.0", "istanbul": "^1.1.0-alpha.1", - "mocha": "^5.2.0" + "mocha": "^5.2.0", + "mockdate": "^2.0.2" }, "main": "./lib/bcoin.js", "bin": { diff --git a/test/http-test.js b/test/http-test.js index b6772258e..5c5e6430a 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -69,6 +69,19 @@ describe('HTTP', function() { assert.strictEqual(info.chain.height, 0); }); + it('should get uptime', async () => { + const MockDate = require('mockdate'); + const mockedNow = 1225476600; + MockDate.set(mockedNow * 1000); + + node.startTime = (mockedNow - 100) * 1000; + const uptime = await nclient.getUptime(); + assert.strictEqual(uptime.uptime, '100'); + + MockDate.reset(); + node.startTime = -1; + }); + it('should get wallet info', async () => { const info = await wallet.getInfo(); assert.strictEqual(info.id, 'test');