Skip to content

Commit

Permalink
feat(rpc): support withCycle for getBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Sep 25, 2023
1 parent 1e8389b commit dea17ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
52 changes: 42 additions & 10 deletions packages/rpc/src/Base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ export const rpcProperties: RpcPropertes = {
// skip subscription
};

/**
* - `0x0`: without verbosity, returns the raw serialized block bytes
* - `0x2`: with verbosity, returns the serialized block JSON object
* - `null`: default value, the same with `0x2`
*/
export type GetBlockVerbosityOptions = "0x0" | "0x2" | null;
export type GetBlockWithCycleOptions = boolean | null;
export type VerbosityBlock<Verbosity extends GetBlockVerbosityOptions> =
Verbosity extends "0x0" ? string : CKBComponents.BlockView;
export type BlockResponse<
Verbosity extends GetBlockVerbosityOptions,
WithCycle extends GetBlockWithCycleOptions
> = WithCycle extends true
? { block: VerbosityBlock<Verbosity>; cycles: CKBComponents.UInt64 }
: VerbosityBlock<Verbosity>;

// prettier-ignore
export interface GetTransaction {
(hash: CKBComponents.Hash): Promise<CKBComponents.TransactionWithStatus>;
Expand Down Expand Up @@ -86,10 +102,19 @@ export interface Base {
* @method getBlock
* @memberof DefaultRPC
* @description rpc to get block by its hash
* @param {string} hash - the block hash of the target block
* @returns {Promise<object>} block object
* @param {string} hash
* @param {string} verbosity
* @param {boolean} withCycle
* @return {Promise<BlockResponse | null>}
*/
getBlock: (hash: CKBComponents.Hash) => Promise<CKBComponents.Block>;
getBlock<
V extends GetBlockVerbosityOptions = null,
C extends GetBlockWithCycleOptions = null
>(
hash: CKBComponents.Hash,
verbosity?: V,
withCycle?: C
): Promise<BlockResponse<V, C> | null>;

/**
* @method getHeader
Expand Down Expand Up @@ -191,13 +216,20 @@ export interface Base {
/**
* @method getBlockByNumber
* @memberof DefaultRPC
* @description rpc to get block by its number
* @param {string} number - the block number of the target block
* @returns {Promise<object>} block object
*/
getBlockByNumber: (
number: CKBComponents.BlockNumber | bigint
) => Promise<CKBComponents.Block>;
* @description rpc to get block by its hash
* @param {CKBComponents.BlockNumber | bigint} number
* @param {string} verbosity
* @param {boolean} withCycle
* @return {Promise<BlockResponse | null>}
*/
getBlockByNumber<
V extends GetBlockVerbosityOptions = null,
C extends GetBlockWithCycleOptions = null
>(
number: CKBComponents.BlockNumber | bigint,
verbosity?: V,
withCycle?: C
): Promise<BlockResponse<V, C> | null>;

/* Experimental */

Expand Down
8 changes: 6 additions & 2 deletions packages/rpc/src/resultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ const toTip = (tip: RPC.Tip): CKBComponents.Tip => ({
blockNumber: tip.block_number,
});

const toBlock = (block: RPC.Block): CKBComponents.Block => {
function toBlock(block: string): string;
function toBlock(block: RPC.Block): CKBComponents.Block;
function toBlock(block: string | RPC.Block): CKBComponents.Block | string {
if (typeof block === "string") return block;

Check warning on line 144 in packages/rpc/src/resultFormatter.ts

View check run for this annotation

Codecov / codecov/patch

packages/rpc/src/resultFormatter.ts#L143-L144

Added lines #L143 - L144 were not covered by tests
if (!block) return block;
const { header, uncles = [], transactions = [], ...rest } = block;
return {
Expand All @@ -147,7 +150,8 @@ const toBlock = (block: RPC.Block): CKBComponents.Block => {
transactions: transactions.map(toTransaction),
...rest,
};
};
}

const toAlertMessage = (
alertMessage: RPC.AlertMessage
): CKBComponents.AlertMessage => {
Expand Down

0 comments on commit dea17ab

Please sign in to comment.