Skip to content

Commit 2a6202f

Browse files
committed
Added a default option to read big blocks, additional test
1 parent d86d5af commit 2a6202f

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ We use:
1919

2020
`bs58` here for encoding and decoding addresses and ids.
2121

22-
2. default usage with TypeScript looks like:
22+
2. Create a channel:
23+
```typescript
24+
import * as w from '@waves/node-api-grpc'
25+
const grpcChannel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6870', {
26+
// Additional options if required. For example:
27+
// "grpc.max_receive_message_length": 10 * 1024 * 1024
28+
})
29+
```
30+
See a full list of options in [@grpc/grpc-js](https://www.npmjs.com/package/@grpc/grpc-js#user-content-supported-channel-options).
31+
32+
3. A typical usage with TypeScript looks like:
2333
```typescript
2434
import * as w from '@waves/node-api-grpc'
2535
import b58 from 'bs58'

api.ts

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const grpc = {
6262
// https://github.com/grpc/grpc-node/tree/master/packages/grpc-js#supported-channel-options
6363
{
6464
'grpc.enable_channelz': 0, // https://github.com/grpc/grpc-node/issues/1941
65+
'grpc.max_receive_message_length': 8 * 1024 * 1024, // Some blocks on MainNet and TestNet are huge
6566
...options
6667
}
6768
);

tests/test.ts

+26-22
Original file line numberDiff line numberDiff line change
@@ -28,46 +28,50 @@ async function getTransactionHeight(channel: Channel, txnId: string) {
2828
})
2929
}
3030

31-
async function getBigestBlock(channel: Channel, height: number) {
31+
async function loadBlocks(channel: Channel, fromHeight: number, toHeight: number) {
32+
let n = 0
3233
return new Promise<number>((resolve, reject) => {
33-
const blocksApi = w.api.waves.node.grpc.mkBlocksApi(channel)
34+
const blocksApi = w.api.waves.events.grpc.mkBlockchainUpdatesApi(channel)
3435
blocksApi
35-
.getBlock({
36-
height: height,
37-
includeTransactions: true
38-
},
39-
(error, response) => {
40-
if (error) return reject(error);
41-
if (!response) return reject("Block not found");
42-
43-
resolve(response.height);
44-
}
45-
)
36+
.subscribe({
37+
fromHeight: fromHeight,
38+
toHeight: toHeight,
39+
})
40+
.on("data", (item: w.api.waves.events.grpc.SubscribeEvent) => {
41+
n += 1
42+
})
43+
.on("error", (e: Error) => reject(e))
44+
.on("end", () => resolve(n))
4645
})
4746
}
4847

49-
describe('Waves gRPC API', () => {
50-
let channel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6870')
48+
describe('Waves gRPC API', function () {
49+
this.timeout(5000)
50+
51+
let grpcChannel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6870')
52+
let updatesChannel = w.grpc.mkDefaultChannel('grpc.wavesnodes.com:6881')
5153

5254
// One request
5355
it('#AccountsApi.resolveAlias - resolves an alias', async () => {
54-
let address = await resolveAlias(channel, 'likli')
56+
let address = await resolveAlias(grpcChannel, 'likli')
5557
assert.equal(address, '3PNaua1fMrQm4TArqeTuakmY1u985CgMRk6')
5658
})
5759

5860
// Stream
5961
it('#TransactionApi.getTransaction - finds a transaction', async () => {
60-
let txnHeight = await getTransactionHeight(channel, '287XcMXPDY7pnw2tECbV86TZetPi2x9JBg9BVUsGaSJx')
62+
let txnHeight = await getTransactionHeight(grpcChannel, '287XcMXPDY7pnw2tECbV86TZetPi2x9JBg9BVUsGaSJx')
6163
assert.equal(txnHeight, 3131305)
6264
})
6365

64-
it('#BlocksApi.getBlock - reads the biggest block', async () => {
65-
const height = 1
66-
const r = await getBigestBlock(channel, height)
67-
assert.equal(r, height)
66+
it('#BlockchainUpdatesApi.subscribe - load blocks', async () => {
67+
const fromHeight = 3371000
68+
const toHeight = 3371001
69+
const n = await loadBlocks(updatesChannel, fromHeight, toHeight)
70+
assert.equal(n, 2)
6871
})
6972

7073
after(() => {
71-
channel.close()
74+
updatesChannel.close()
75+
grpcChannel.close()
7276
})
7377
})

0 commit comments

Comments
 (0)