Skip to content

Commit

Permalink
feat: upgrade ckb-light-client rpc for support v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PainterPuppets committed Nov 10, 2023
1 parent cbab8d9 commit 0ea5dd3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 17 deletions.
46 changes: 41 additions & 5 deletions packages/e2e-test/tests/light-client-rpc.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,33 @@ test.before(async (t) => {
};
});

test("light client get_tip_header rpc", async (t) => {
test("light-client get_tip_header rpc", async (t) => {
const tipHeader = await lightClientRPC.getTipHeader();
t.true(typeof tipHeader.dao === "string");
t.true(typeof tipHeader.number == "string");
t.true(typeof tipHeader.hash == "string");
});

test("light-client get_peers rpc", async (t) => {
const peers = await lightClientRPC.getPeers();
t.true(Array.isArray(peers));
t.true(Array.isArray(peers[0].addresses));
t.true(Array.isArray(peers[0].protocols));
t.true(typeof peers[0].connectedDuration == "string");
t.true(typeof peers[0].nodeId == "string");
t.true(typeof peers[0].version == "string");
});

test("light-client local_node_info rpc", async (t) => {
const nodeInfo = await lightClientRPC.localNodeInfo();
t.true(typeof nodeInfo.active == "boolean");
t.true(typeof nodeInfo.connections == "string");
t.true(typeof nodeInfo.nodeId == "string");
t.true(typeof nodeInfo.version == "string");
t.true(Array.isArray(nodeInfo.addresses));
t.true(Array.isArray(nodeInfo.protocols));
});

test("light-client get_genesis_block rpc", async (t) => {
const res = await lightClientRPC.getGenesisBlock();
t.deepEqual(res.header.epoch, "0x0");
Expand Down Expand Up @@ -235,17 +255,31 @@ test.serial("test setScripts", async (t) => {
);
t.deepEqual(listeningBob, false);

await lightClientRPC.setScripts([
...beforeScripts,
{ script: bob.lockScript, scriptType: "lock", blockNumber: "0x0" },
]);
// partial case
await lightClientRPC.setScripts(
[{ script: bob.lockScript, scriptType: "lock", blockNumber: "0x0" }],
"partial"
);

const afterScripts = await lightClientRPC.getScripts();

const afterListeningBob = afterScripts.some((script) =>
isEqual(script.script, bob.lockScript)
);
t.deepEqual(afterListeningBob, true);

// delete case
await lightClientRPC.setScripts(
[{ script: bob.lockScript, scriptType: "lock", blockNumber: "0x0" }],
"delete"
);

const deletedAfterScripts = await lightClientRPC.getScripts();

const deletedAfterListeningBob = deletedAfterScripts.some((script) =>
isEqual(script.script, bob.lockScript)
);
t.deepEqual(deletedAfterListeningBob, false);
});

test.serial("test lightClient getCells by Cellcollector", async (t) => {
Expand Down Expand Up @@ -319,9 +353,11 @@ test.serial(

const tx = await waitLightClientFetchTransaction(lightClientRPC, txHash);
t.deepEqual(tx.transaction.hash, txHash);
t.true(typeof tx.txStatus.status == "string");

const gotTx = await lightClientRPC.getTransaction(txHash);
t.deepEqual(gotTx.transaction.hash, txHash);
t.true(typeof tx.txStatus.status == "string");
}
);

Expand Down
28 changes: 24 additions & 4 deletions packages/light-client/src/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { HexString, utils, Header, Block } from "@ckb-lumos/base";
import {
HexString,
utils,
Header,
Block,
RemoteNode,
LocalNode,
TransactionWithStatus,
} from "@ckb-lumos/base";
import { CKBComponents } from "@ckb-lumos/rpc/lib/types/api";
import { ParamsFormatter } from "@ckb-lumos/rpc";

Expand All @@ -19,8 +27,8 @@ import {
FetchHeaderResult,
FetchTransactionResult,
LightClientScript,
TransactionWithHeader,
LightClientTransactionList,
SetScriptCommand,
} from "./type";
import fetch from "cross-fetch";

Expand All @@ -36,6 +44,14 @@ export class LightClientRPC {
return utils.deepCamel(await request(this.uri, "get_tip_header"));
}

async getPeers(): Promise<Array<RemoteNode>> {
return utils.deepCamel(await request(this.uri, "get_peers"));
}

async localNodeInfo(): Promise<LocalNode> {
return utils.deepCamel(await request(this.uri, "local_node_info"));
}

async fetchHeader(blockHash: string): Promise<FetchHeaderResult> {
const params = [blockHash];
return utils.deepCamel(await request(this.uri, "fetch_header", params));
Expand All @@ -53,7 +69,7 @@ export class LightClientRPC {
);
}

async getTransaction(txHash: string): Promise<TransactionWithHeader> {
async getTransaction(txHash: string): Promise<TransactionWithStatus> {
const params = [txHash];
return utils.deepCamel(await request(this.uri, "get_transaction", params));
}
Expand All @@ -69,13 +85,17 @@ export class LightClientRPC {
return utils.deepCamel(await request(this.uri, "get_scripts"));
}

async setScripts(scripts: Array<LightClientScript>): Promise<void> {
async setScripts(
scripts: Array<LightClientScript>,
command?: SetScriptCommand
): Promise<void> {
const params = [
scripts.map(({ script, scriptType, blockNumber }) => ({
script: toScript(script),
script_type: scriptType,
block_number: blockNumber,
})),
command,
];
return utils.deepCamel(await request(this.uri, "set_scripts", params));
}
Expand Down
17 changes: 10 additions & 7 deletions packages/light-client/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
export type ScriptType = "type" | "lock";

import { HexNumber, Script, Transaction, Header } from "@ckb-lumos/base";
import {
HexNumber,
Script,
Transaction,
Header,
TransactionWithStatus,
} from "@ckb-lumos/base";

export enum FetchFlag {
Fetched = "fetched",
Expand All @@ -15,13 +21,8 @@ export type FetchHeaderResult =
| { status: FetchFlag.Added; timestamp: string }
| { status: FetchFlag.NotFound };

export type TransactionWithHeader = {
transaction: Transaction;
header: Header;
};

export type FetchTransactionResult =
| { status: FetchFlag.Fetched; data: TransactionWithHeader }
| { status: FetchFlag.Fetched; data: TransactionWithStatus }
| { status: FetchFlag.Fetching; firstSent: string }
| { status: FetchFlag.Added; timestamp: string }
| { status: FetchFlag.NotFound };
Expand Down Expand Up @@ -54,6 +55,8 @@ export type GroupedLightClientTransaction = {
cells: Array<[IOType, HexNum]>;
};

export type SetScriptCommand = "all" | "partial" | "delete";

export type LightClientScript = {
script: Script;
blockNumber: HexNumber;
Expand Down
2 changes: 1 addition & 1 deletion packages/runner/src/ckb/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface GetReleaseUrlOptions {
*/
export function getReleaseUrl(options: GetReleaseUrlOptions = {}): string {
const {
version = "v0.109.0",
version = "v0.111.0",
arch = os.arch(),
platform = os.platform(),
} = options;
Expand Down

0 comments on commit 0ea5dd3

Please sign in to comment.