From 880b3d98affaf29eed11bb50ed5041487d74aa5d Mon Sep 17 00:00:00 2001 From: Bowen Wang Date: Mon, 8 Jun 2020 18:33:07 -0700 Subject: [PATCH] add rpc for light client proof --- lib/providers/json-rpc-provider.d.ts | 9 +++-- lib/providers/json-rpc-provider.js | 7 ++++ lib/providers/provider.d.ts | 45 +++++++++++++++++++++++ lib/providers/provider.js | 7 +++- src/providers/json-rpc-provider.ts | 12 +++++-- src/providers/provider.ts | 53 ++++++++++++++++++++++++++++ 6 files changed, 128 insertions(+), 5 deletions(-) diff --git a/lib/providers/json-rpc-provider.d.ts b/lib/providers/json-rpc-provider.d.ts index f363284f14..6ddfb6f4b5 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, BlockId, BlockResult, ChunkId, ChunkResult, EpochValidatorInfo, GenesisConfig } from './provider'; +import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, BlockResult, ChunkId, ChunkResult, EpochValidatorInfo, GenesisConfig, LightClientProof, LightClientProofRequest } from './provider'; import { Network } from '../utils/network'; import { ConnectionInfo } from '../utils/web'; import { TypedError } from '../utils/errors'; @@ -64,10 +64,15 @@ export declare class JsonRpcProvider extends Provider { * @returns {Promise} */ experimental_genesisConfig(): Promise; + /** + * Gets EXPERIMENTAL_light_client_proof from RPC + * @returns {Promise} + */ + experimental_lightClientProof(request: LightClientProofRequest): Promise; /** * Directly call the RPC specifying the method and params * @param method RPC method * @param params Parameters to the method */ - sendJsonRpc(method: string, params: any[]): Promise; + sendJsonRpc(method: string, params: any): Promise; } diff --git a/lib/providers/json-rpc-provider.js b/lib/providers/json-rpc-provider.js index 371c92071e..e609b0bf1a 100644 --- a/lib/providers/json-rpc-provider.js +++ b/lib/providers/json-rpc-provider.js @@ -97,6 +97,13 @@ class JsonRpcProvider extends provider_1.Provider { async experimental_genesisConfig() { return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); } + /** + * Gets EXPERIMENTAL_light_client_proof from RPC + * @returns {Promise} + */ + async experimental_lightClientProof(request) { + return await this.sendJsonRpc('EXPERIMENTAL_light_client_proof', request); + } /** * Directly call the RPC specifying the method and params * @param method RPC method diff --git a/lib/providers/provider.d.ts b/lib/providers/provider.d.ts index f7d9238e51..576bbfe794 100644 --- a/lib/providers/provider.d.ts +++ b/lib/providers/provider.d.ts @@ -54,6 +54,12 @@ export interface ExecutionOutcome { gas_burnt: number; status: ExecutionStatus | ExecutionStatusBasic; } +export interface ExecutionOutcomeWithIdView { + proof: MerklePath; + block_hash: string; + id: string; + outcome: ExecutionOutcome; +} export interface FinalExecutionOutcome { status: FinalExecutionStatus | FinalExecutionStatusBasic; transaction: any; @@ -148,6 +154,44 @@ export interface EpochValidatorInfo { prev_epoch_kickout: ValidatorStakeView[]; epoch_start_height: number; } +export interface MerkleNode { + hash: string; + direction: string; +} +export declare type MerklePath = MerkleNode[]; +export interface BlockHeaderInnerLiteView { + height: number; + epoch_id: string; + next_epoch_id: string; + prev_state_root: string; + outcome_root: string; + timestamp: number; + next_bp_hash: string; + block_merkle_root: string; +} +export interface LightClientBlockLiteView { + prev_block_hash: string; + inner_rest_hash: string; + inner_lite: BlockHeaderInnerLiteView; +} +export interface LightClientProof { + outcome_proof: ExecutionOutcomeWithIdView; + outcome_root_proof: MerklePath; + block_header_lite: LightClientBlockLiteView; + block_proof: MerklePath; +} +export declare enum IdType { + Transaction = "transaction", + Receipt = "receipt" +} +export interface LightClientProofRequest { + type: IdType; + light_client_head: string; + transaction_hash?: string; + sender_id?: string; + receipt_id?: string; + receiver_id?: string; +} export declare abstract class Provider { abstract getNetwork(): Promise; abstract status(): Promise; @@ -158,6 +202,7 @@ export declare abstract class Provider { abstract chunk(chunkId: ChunkId): Promise; abstract validators(blockId: BlockId): Promise; abstract experimental_genesisConfig(): Promise; + abstract experimental_lightClientProof(request: LightClientProofRequest): Promise; } export declare function getTransactionLastResult(txResult: FinalExecutionOutcome): any; export declare function adaptTransactionResult(txResult: any): FinalExecutionOutcome; diff --git a/lib/providers/provider.js b/lib/providers/provider.js index d18dc30e7a..a2a7d232ae 100644 --- a/lib/providers/provider.js +++ b/lib/providers/provider.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.adaptTransactionResult = exports.getTransactionLastResult = exports.Provider = exports.FinalExecutionStatusBasic = exports.ExecutionStatusBasic = void 0; +exports.adaptTransactionResult = exports.getTransactionLastResult = exports.Provider = exports.IdType = exports.FinalExecutionStatusBasic = exports.ExecutionStatusBasic = void 0; var ExecutionStatusBasic; (function (ExecutionStatusBasic) { ExecutionStatusBasic["Unknown"] = "Unknown"; @@ -13,6 +13,11 @@ var FinalExecutionStatusBasic; FinalExecutionStatusBasic["Started"] = "Started"; FinalExecutionStatusBasic["Failure"] = "Failure"; })(FinalExecutionStatusBasic = exports.FinalExecutionStatusBasic || (exports.FinalExecutionStatusBasic = {})); +var IdType; +(function (IdType) { + IdType["Transaction"] = "transaction"; + IdType["Receipt"] = "receipt"; +})(IdType = exports.IdType || (exports.IdType = {})); class Provider { } exports.Provider = Provider; diff --git a/src/providers/json-rpc-provider.ts b/src/providers/json-rpc-provider.ts index 927f5b2cb8..4e825e19d2 100644 --- a/src/providers/json-rpc-provider.ts +++ b/src/providers/json-rpc-provider.ts @@ -1,7 +1,7 @@ import { Provider, FinalExecutionOutcome, NodeStatusResult, BlockId, BlockResult, ChunkId, ChunkResult, adaptTransactionResult, EpochValidatorInfo, - GenesisConfig + GenesisConfig, LightClientProof, LightClientProofRequest } from './provider'; import { Network } from '../utils/network'; import { ConnectionInfo, fetchJson } from '../utils/web'; @@ -115,12 +115,20 @@ export class JsonRpcProvider extends Provider { return await this.sendJsonRpc('EXPERIMENTAL_genesis_config', []); } + /** + * Gets EXPERIMENTAL_light_client_proof from RPC + * @returns {Promise} + */ + async experimental_lightClientProof(request: LightClientProofRequest): Promise { + return await this.sendJsonRpc('EXPERIMENTAL_light_client_proof', request); + } + /** * Directly call the RPC specifying the method and params * @param method RPC method * @param params Parameters to the method */ - async sendJsonRpc(method: string, params: any[]): Promise { + async sendJsonRpc(method: string, params: any): Promise { const request = { method, params, diff --git a/src/providers/provider.ts b/src/providers/provider.ts index c278e26d4e..a3929325a1 100644 --- a/src/providers/provider.ts +++ b/src/providers/provider.ts @@ -66,6 +66,13 @@ export interface ExecutionOutcome { status: ExecutionStatus | ExecutionStatusBasic; } +export interface ExecutionOutcomeWithIdView { + proof: MerklePath; + block_hash: string; + id: string; + outcome: ExecutionOutcome; +} + export interface FinalExecutionOutcome { status: FinalExecutionStatus | FinalExecutionStatusBasic; transaction: any; @@ -181,6 +188,51 @@ export interface EpochValidatorInfo { epoch_start_height: number; } +export interface MerkleNode { + hash: string; + direction: string; +} + +export type MerklePath = MerkleNode[]; + +export interface BlockHeaderInnerLiteView { + height: number; + epoch_id: string; + next_epoch_id: string; + prev_state_root: string; + outcome_root: string; + timestamp: number; + next_bp_hash: string; + block_merkle_root: string; +} + +export interface LightClientBlockLiteView { + prev_block_hash: string; + inner_rest_hash: string; + inner_lite: BlockHeaderInnerLiteView; +} + +export interface LightClientProof { + outcome_proof: ExecutionOutcomeWithIdView; + outcome_root_proof: MerklePath; + block_header_lite: LightClientBlockLiteView; + block_proof: MerklePath; +} + +export enum IdType { + Transaction = 'transaction', + Receipt = 'receipt', +} + +export interface LightClientProofRequest { + type: IdType; + light_client_head: string; + transaction_hash?: string; + sender_id?: string; + receipt_id?: string; + receiver_id?: string; +} + export abstract class Provider { abstract async getNetwork(): Promise; abstract async status(): Promise; @@ -192,6 +244,7 @@ export abstract class Provider { abstract async chunk(chunkId: ChunkId): Promise; abstract async validators(blockId: BlockId): Promise; abstract async experimental_genesisConfig(): Promise; + abstract async experimental_lightClientProof(request: LightClientProofRequest): Promise; } export function getTransactionLastResult(txResult: FinalExecutionOutcome): any {