Skip to content

Commit

Permalink
add rpc for light client proof
Browse files Browse the repository at this point in the history
  • Loading branch information
bowenwang1996 committed Jun 9, 2020
1 parent 25e7e60 commit 880b3d9
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 5 deletions.
9 changes: 7 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: 7 additions & 0 deletions lib/providers/json-rpc-provider.js

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

45 changes: 45 additions & 0 deletions lib/providers/provider.d.ts

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

7 changes: 6 additions & 1 deletion lib/providers/provider.js

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

12 changes: 10 additions & 2 deletions src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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<LightClientProof>}
*/
async experimental_lightClientProof(request: LightClientProofRequest): Promise<LightClientProof> {
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<any> {
async sendJsonRpc(method: string, params: any): Promise<any> {
const request = {
method,
params,
Expand Down
53 changes: 53 additions & 0 deletions src/providers/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Network>;
abstract async status(): Promise<NodeStatusResult>;
Expand All @@ -192,6 +244,7 @@ export abstract class Provider {
abstract async chunk(chunkId: ChunkId): Promise<ChunkResult>;
abstract async validators(blockId: BlockId): Promise<EpochValidatorInfo>;
abstract async experimental_genesisConfig(): Promise<GenesisConfig>;
abstract async experimental_lightClientProof(request: LightClientProofRequest): Promise<LightClientProof>;
}

export function getTransactionLastResult(txResult: FinalExecutionOutcome): any {
Expand Down

0 comments on commit 880b3d9

Please sign in to comment.