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

Exposed chunk RPC API (nearcore 0.4.2+) #88

Merged
merged 2 commits into from
Oct 30, 2019
Merged
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
5 changes: 3 additions & 2 deletions lib/providers/json-rpc-provider.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions lib/providers/json-rpc-provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 34 additions & 1 deletion lib/providers/provider.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src.ts/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
'use strict';

import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockResult, adaptTransactionResult } from './provider';
import {
Provider, FinalExecutionOutcome, NodeStatusResult, BlockId,
BlockResult, ChunkId, ChunkResult, adaptTransactionResult
} from './provider';
import { Network } from '../utils/network';
import { ConnectionInfo, fetchJson } from '../utils/web';
import { base_encode } from '../utils/serialize';
Expand Down Expand Up @@ -55,8 +58,12 @@ export class JsonRpcProvider extends Provider {
return result;
}

async block(height: number): Promise<BlockResult> {
return this.sendJsonRpc('block', [height]);
async block(blockId: BlockId): Promise<BlockResult> {
return this.sendJsonRpc('block', [blockId]);
}

async chunk(chunkId: ChunkId): Promise<ChunkResult> {
return this.sendJsonRpc('chunk', [chunkId]);
}

private async sendJsonRpc(method: string, params: any[]): Promise<any> {
Expand Down
39 changes: 38 additions & 1 deletion src.ts/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export interface NodeStatusResult {
validators: string[];
}

export type BlockHash = string;
export type BlockHeight = number;
export type BlockId = BlockHash | BlockHeight;

export enum ExecutionStatusBasic {
Unknown = 'Unknown',
Pending = 'Pending',
Expand Down Expand Up @@ -80,6 +84,38 @@ export interface BlockHeader {
tx_root: string;
}

export type ChunkHash = string;
export type ShardId = number;
export type BlockShardId = [BlockId, ShardId];
export type ChunkId = ChunkHash | BlockShardId;

export interface ChunkHeader {
balance_burnt: string;
chunk_hash: ChunkHash;
encoded_length: number;
encoded_merkle_root: string;
gas_limit: number;
gas_used: number;
height_created: number;
height_included: number;
outgoing_receipts_root: string;
prev_block_hash: string;
prev_state_num_parts: number;
prev_state_root_hash: string;
rent_paid: string;
shard_id: number;
signature: string;
tx_root: string;
validator_proposals: any[];
validator_reward: string;
}

export interface ChunkResult {
header: ChunkHeader;
receipts: any[];
transactions: Transaction[];
}

export interface Transaction {
hash: string;
public_key: string;
Expand Down Expand Up @@ -220,7 +256,8 @@ export abstract class Provider {
abstract async sendTransaction(signedTransaction: SignedTransaction): Promise<FinalExecutionOutcome>;
abstract async txStatus(txHash: Uint8Array): Promise<FinalExecutionOutcome>;
abstract async query(path: string, data: string): Promise<any>;
abstract async block(height: number): Promise<BlockResult>;
abstract async block(blockId: BlockId): Promise<BlockResult>;
abstract async chunk(chunkId: ChunkId): Promise<ChunkResult>;
}

export function getTransactionLastResult(txResult: FinalExecutionOutcome): any {
Expand Down
30 changes: 20 additions & 10 deletions test/providers.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@

const nearlib = require('../lib/index');

test('json rpc fetch node status', async () => {
const withProvider = (fn) => {
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'));
const provider = new nearlib.providers.JsonRpcProvider(config.nodeUrl);
return () => fn(provider);
};

test('json rpc fetch node status', withProvider(async (provider) => {
let response = await provider.status();
expect(response.chain_id).toContain('test-chain');
});
}));

test('json rpc fetch block info', async () => {
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'));
const provider = new nearlib.providers.JsonRpcProvider(config.nodeUrl);
test('json rpc fetch block info', withProvider(async (provider) => {
let response = await provider.block(1);
expect(response.header.height).toEqual(1);
});
let sameBlock = await provider.block(response.header.hash);
expect(sameBlock.header.height).toEqual(1);
}));

test('json rpc query account', async () => {
const config = Object.assign(require('./config')(process.env.NODE_ENV || 'test'));
const provider = new nearlib.providers.JsonRpcProvider(config.nodeUrl);
test('json rpc fetch chunk info', withProvider(async (provider) => {
let response = await provider.chunk([1, 0]);
expect(response.header.shard_id).toEqual(0);
let sameChunk = await provider.chunk(response.header.chunk_hash);
expect(sameChunk.header.chunk_hash).toEqual(response.header.chunk_hash);
expect(sameChunk.header.shard_id).toEqual(0);
}));

test('json rpc query account', withProvider(async (provider) => {
let response = await provider.query('account/test.near', '');
expect(response.code_hash).toEqual('11111111111111111111111111111111');
});
}));

test('final tx result', async() => {
const result = {
Expand Down