Skip to content

Commit

Permalink
chain: add getEntries, similar to getHashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
nodech committed Mar 13, 2024
1 parent d16bc5a commit 95b2152
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ process and allows parallel rescans.
- `lastCompaction` - when was the last compaction run.
- Introduce `scan interactive` hook (start, filter)
- Add `get median time` hook to get median time past for a blockhash.
- Add `get entries` hook to get entries. Similar to `get hashes`, but returns
encoded entries.

### hs-client Node
- Introduce `scanInteractive` method that starts interactive rescan.
- expects ws hook for `block rescan interactive` params `rawEntry, rawTXs`
that returns scanAction object.
- expects ws hook for `block rescan interactive abort` param `message`.
- Adds `getMedianTime(blockhash)` that returns median time past of the block.
- Adds `getEntries(start, end)` that returns encoded chain entries.

### Wallet Changes
#### Configuration
Expand Down
13 changes: 12 additions & 1 deletion lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2954,13 +2954,24 @@ class Chain extends AsyncEmitter {
* Get range of hashes.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise}
* @returns {Promise<Hash[]>}
*/

getHashes(start = -1, end = -1) {
return this.db.getHashes(start, end);
}

/**
* Get range of entries.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise<ChainEntry[]>}
*/

getEntries(start = -1, end = -1) {
return this.db.getEntries(start, end);
}

/**
* Get a coin (unspents only).
* @private
Expand Down
29 changes: 20 additions & 9 deletions lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ class ChainDB {
* Get hash range.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise}
* @returns {Promise<Hash[]>}
*/

async getHashes(start = -1, end = -1) {
Expand All @@ -1131,16 +1131,27 @@ class ChainDB {
}

/**
* Get all entries.
* @returns {Promise} - Returns {@link ChainEntry}[].
* Get entries range.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise<ChainEntry[]>}
*/

async getEntries() {
return this.db.values({
gte: layout.e.min(),
lte: layout.e.max(),
parse: data => ChainEntry.decode(data)
});
async getEntries(start = -1, end = -1) {
if (start === -1)
start = 0;

if (end === -1)
end >>>= 0;

assert((start >>> 0) === start);
assert((end >>> 0) === end);

const hashes = await this.getHashes(start, end);

return Promise.all(hashes.map((hash) => {
return this.getEntryByHash(hash);
}));
}

/**
Expand Down
13 changes: 12 additions & 1 deletion lib/client/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,24 @@ class NodeClient extends Client {
* Get hashes.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise}
* @returns {Promise<Hash[]>}
*/

getHashes(start, end) {
return this.call('get hashes', start, end);
}

/**
* Get entries.
* @param {Number} [start=-1]
* @param {Number} [end=-1]
* @returns {Promise<Buffer[]>} - {@link ChainEntry}
*/

getEntries(start, end) {
return this.call('get entries', start, end);
}

/**
* Send a transaction. Do not wait for promise.
* @param {TX} tx
Expand Down
9 changes: 9 additions & 0 deletions lib/node/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,15 @@ class HTTP extends Server {
return this.chain.getHashes(start, end);
});

socket.hook('get entries', async (...args) => {
const valid = new Validator(args);
const start = valid.i32(0, -1);
const end = valid.i32(1, -1);

const entries = await this.chain.getEntries(start, end);
return entries.map(entry => entry.encode());
});

socket.hook('add filter', (...args) => {
const valid = new Validator(args);
const chunks = valid.array(0);
Expand Down
55 changes: 52 additions & 3 deletions test/node-http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const Witness = require('../lib/script/witness');
const Script = require('../lib/script/script');
const HDPrivateKey = require('../lib/hd/private');
const Output = require('../lib/primitives/output');
const Block = require('../lib/primitives/block');
const Coin = require('../lib/primitives/coin');
const MTX = require('../lib/primitives/mtx');
const rules = require('../lib/covenants/rules');
Expand Down Expand Up @@ -430,10 +431,10 @@ describe('Node HTTP', function() {
});
});

describe('Websockets', function () {
describe('Websockets', function() {
this.timeout(15000);

describe('Get entry and mtp', () => {
describe('Get entry and mtp', function() {
const nodeCtx = new NodeContext({
wallet: true
});
Expand Down Expand Up @@ -526,6 +527,52 @@ describe('Node HTTP', function() {
});
});

describe('get hashes and entries', function() {
const nodeCtx = new NodeContext({
wallet: true
});

const {nclient, network} = nodeCtx;
const genesisBlock = Block.decode(network.genesisBlock);
let minedHashes;

before(async () => {
await nodeCtx.open();

const {address} = await nodeCtx.wclient.createAddress('primary', 'default');
minedHashes = await mineBlocks(nodeCtx, 15, address);
});

after(async () => {
await nodeCtx.close();
});

it('should get hashes by range', async () => {
const hashes = await nclient.getHashes(0, 15);
assert(hashes && hashes.length === 16);

for (const [index, hash] of hashes.entries()) {
if (index === 0) {
assert.bufferEqual(hash, genesisBlock.hash());
continue;
}

assert.bufferEqual(hash, minedHashes[index - 1]);
}
});

it('should get entries by range', async () => {
const entries = await nclient.getEntries(0, 15);
assert(entries && entries.length === 16);

for (const rawEntry of entries) {
const entry = ChainEntry.decode(rawEntry);
const gotEntry = await nclient.getEntry(entry.hash);
assert.bufferEqual(rawEntry, gotEntry);
}
});
});

describe('tree commit', () => {
const {types} = rules;

Expand Down Expand Up @@ -655,6 +702,8 @@ async function mineBlocks(nodeCtx, count, address) {
'block connect',
count
);
await nodeCtx.mineBlocks(count, address);

const hashes = await nodeCtx.mineBlocks(count, address);
await blockEvents;
return hashes;
}

0 comments on commit 95b2152

Please sign in to comment.