Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: add uptime #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/Examples/get-tx-from-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 5 additions & 18 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 11 additions & 2 deletions lib/indexer/chainclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions lib/indexer/nullclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions lib/indexer/txindexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 6 additions & 1 deletion lib/node/fullnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
13 changes: 13 additions & 0 deletions test/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down