diff --git a/lib/providers/json-rpc-provider.d.ts b/lib/providers/json-rpc-provider.d.ts index 6ddcd7e402..9ff8a81937 100644 --- a/lib/providers/json-rpc-provider.d.ts +++ b/lib/providers/json-rpc-provider.d.ts @@ -1,4 +1,4 @@ -import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockResult } from './provider'; +import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, BlockResult, ChunkId, ChunkResult } from './provider'; import { Network } from '../utils/network'; import { ConnectionInfo } from '../utils/web'; import { SignedTransaction } from '../transaction'; @@ -14,6 +14,7 @@ export declare class JsonRpcProvider extends Provider { sendTransaction(signedTransaction: SignedTransaction): Promise; txStatus(txHash: Uint8Array): Promise; query(path: string, data: string): Promise; - block(height: number): Promise; + block(blockId: BlockId): Promise; + chunk(chunkId: ChunkId): Promise; private sendJsonRpc; } diff --git a/lib/providers/json-rpc-provider.js b/lib/providers/json-rpc-provider.js index 80ae635b75..0fc16cac3e 100644 --- a/lib/providers/json-rpc-provider.js +++ b/lib/providers/json-rpc-provider.js @@ -41,8 +41,11 @@ class JsonRpcProvider extends provider_1.Provider { } return result; } - async block(height) { - return this.sendJsonRpc('block', [height]); + async block(blockId) { + return this.sendJsonRpc('block', [blockId]); + } + async chunk(chunkId) { + return this.sendJsonRpc('chunk', [chunkId]); } async sendJsonRpc(method, params) { const request = { diff --git a/lib/providers/provider.d.ts b/lib/providers/provider.d.ts index 4c3ed52530..c5204f62dd 100644 --- a/lib/providers/provider.d.ts +++ b/lib/providers/provider.d.ts @@ -13,6 +13,9 @@ export interface NodeStatusResult { sync_info: SyncInfo; validators: string[]; } +export declare type BlockHash = string; +export declare type BlockHeight = number; +export declare type BlockId = BlockHash | BlockHeight; export declare enum ExecutionStatusBasic { Unknown = "Unknown", Pending = "Pending", @@ -65,6 +68,35 @@ export interface BlockHeader { total_weight: TotalWeight; tx_root: string; } +export declare type ChunkHash = string; +export declare type ShardId = number; +export declare type BlockShardId = [BlockId, ShardId]; +export declare 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; @@ -107,7 +139,8 @@ export declare abstract class Provider { abstract sendTransaction(signedTransaction: SignedTransaction): Promise; abstract txStatus(txHash: Uint8Array): Promise; abstract query(path: string, data: string): Promise; - abstract block(height: number): Promise; + abstract block(blockId: BlockId): Promise; + abstract chunk(chunkId: ChunkId): Promise; } export declare function getTransactionLastResult(txResult: FinalExecutionOutcome): any; export {}; diff --git a/src.ts/providers/json-rpc-provider.ts b/src.ts/providers/json-rpc-provider.ts index e30d510d3a..50249969bc 100644 --- a/src.ts/providers/json-rpc-provider.ts +++ b/src.ts/providers/json-rpc-provider.ts @@ -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'; @@ -55,8 +58,12 @@ export class JsonRpcProvider extends Provider { return result; } - async block(height: number): Promise { - return this.sendJsonRpc('block', [height]); + async block(blockId: BlockId): Promise { + return this.sendJsonRpc('block', [blockId]); + } + + async chunk(chunkId: ChunkId): Promise { + return this.sendJsonRpc('chunk', [chunkId]); } private async sendJsonRpc(method: string, params: any[]): Promise { diff --git a/src.ts/providers/provider.ts b/src.ts/providers/provider.ts index 81e8719020..2140d7a936 100644 --- a/src.ts/providers/provider.ts +++ b/src.ts/providers/provider.ts @@ -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', @@ -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; @@ -220,7 +256,8 @@ export abstract class Provider { abstract async sendTransaction(signedTransaction: SignedTransaction): Promise; abstract async txStatus(txHash: Uint8Array): Promise; abstract async query(path: string, data: string): Promise; - abstract async block(height: number): Promise; + abstract async block(blockId: BlockId): Promise; + abstract async chunk(chunkId: ChunkId): Promise; } export function getTransactionLastResult(txResult: FinalExecutionOutcome): any { diff --git a/test/providers.test.js b/test/providers.test.js index 094ac43e0c..a3a3448c3b 100644 --- a/test/providers.test.js +++ b/test/providers.test.js @@ -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 = {