Skip to content

Commit

Permalink
fix types related bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Dec 3, 2024
1 parent 56d3604 commit fe09648
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/target/
**/node_modules/**
**/pkg/**
/test
18 changes: 15 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<script type="module">
import LightClient from "./light-client-js/dist/index.js";

const devConfig = await (await fetch("./dev-config.toml")).text();
const devSpec = await (await fetch("./dev-spec.toml")).text();
const devConfig = await (await fetch("./test/dev-config.toml")).text();
const devSpec = await (await fetch("./test/dev-spec.toml")).text();

console.log(LightClient);
window.initWorkers = async () => {
Expand All @@ -20,11 +20,23 @@
type: "DevNet",
config: devConfig,
spec: devSpec
}
},
"info"
);

$("#init-button").removeClass("loading");
};
window.doTest = async () => {
await window.client.setScripts([{ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23", }, scriptType: "lock", blockNumber: "0xb8d328" }], 0)


console.log(await window.client.getCellsCapacity({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" }))


console.log(await window.client.getCells({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" }))

console.log(await window.client.getTransactions({ script: { codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", hashType: "type", args: "0xef0273afa67242b1b407eced450f78b2c02a2a23" }, scriptType: "lock" }))
};
</script>

</head>
Expand Down
16 changes: 8 additions & 8 deletions light-client-js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ClientFindCellsResponse, ClientFindTransactionsGroupedResponse, ClientFindTransactionsResponse, ClientIndexerSearchKeyLike, ClientIndexerSearchKeyTransactionLike, ClientTransactionResponse } from "@ckb-ccc/core/client";
import { FetchResponse, JsonRpcLocalNode, JsonRpcRemoteNode, JsonRpcScriptStatus, LocalNode, localNodeTo, NetworkFlag, RemoteNode, remoteNodeTo, ScriptStatus, scriptStatusFrom, scriptStatusTo, transformFetchResponse } from "./types";
import { FetchResponse, JsonRpcLocalNode, JsonRpcRemoteNode, JsonRpcScriptStatus, LocalNode, localNodeTo, NetworkFlag, RemoteNode, remoteNodeTo, ScriptStatus, scriptStatusFrom, scriptStatusTo, LightClientWasmSetScriptsCommand, transformFetchResponse, LightClientWasmOrder } from "./types";
import { ClientBlock, ClientBlockHeader, Hex, hexFrom, HexLike, Num, numFrom, NumLike, numToHex, TransactionLike } from "@ckb-ccc/core/barrel";
import { JsonRpcBlockHeader, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
const DEFAULT_BUFFER_SIZE = 50 * (1 << 20);
Expand Down Expand Up @@ -125,7 +125,7 @@ class LightClient {
* @param scripts Array of script status
* @param command An optional enum parameter to control the behavior of set_scripts
*/
async setScripts(scripts: ScriptStatus[], command?: "all" | "partial" | "delete"): Promise<void> {
async setScripts(scripts: ScriptStatus[], command?: LightClientWasmSetScriptsCommand): Promise<void> {
await this.invokeLightClientCommand("set_scripts", [scripts.map(x => scriptStatusFrom(x)), command]);
}
/**
Expand All @@ -143,14 +143,14 @@ class LightClient {
*/
async getCells(
searchKey: ClientIndexerSearchKeyLike,
order?: "asc" | "desc",
order?: LightClientWasmOrder,
limit?: NumLike,
afterCursor?: string
): Promise<ClientFindCellsResponse> {
return JsonRpcTransformers.findCellsResponseTo(await this.invokeLightClientCommand("get_cells", [
JsonRpcTransformers.indexerSearchKeyFrom(searchKey),
order ?? "asc",
numToHex(limit ?? 10),
order ?? LightClientWasmOrder.Asc,
Number(numFrom(numToHex(limit ?? 10))),
afterCursor
]));
}
Expand All @@ -164,7 +164,7 @@ class LightClient {
*/
async getTransactions(
searchKey: ClientIndexerSearchKeyTransactionLike,
order?: "asc" | "desc",
order?: LightClientWasmOrder,
limit?: NumLike,
afterCursor?: string
): Promise<ClientFindTransactionsResponse | ClientFindTransactionsGroupedResponse> {
Expand All @@ -173,8 +173,8 @@ class LightClient {
"get_transactions",
[
JsonRpcTransformers.indexerSearchKeyTransactionFrom(searchKey),
order ?? "asc",
numToHex(limit ?? 10),
order ?? LightClientWasmOrder.Asc,
Number(numFrom(numToHex(limit ?? 10))),
afterCursor
]
)
Expand Down
13 changes: 11 additions & 2 deletions light-client-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ScriptLike } from "@ckb-ccc/core";
import { JsonRpcScript, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
import { Num, Script } from "@ckb-ccc/core/barrel";

Expand Down Expand Up @@ -40,7 +41,7 @@ interface JsonRpcScriptStatus {
}

interface ScriptStatus {
script: Script,
script: ScriptLike,
scriptType: JsonRpcScriptType,
blockNumber: Num
}
Expand Down Expand Up @@ -151,7 +152,15 @@ export function localNodeTo(input: JsonRpcLocalNode): LocalNode {
})
}
type NetworkFlag = { type: "MainNet" } | { type: "TestNet" } | { type: "DevNet"; spec: string; config: string; };

export enum LightClientWasmSetScriptsCommand {
All = 0,
Partial = 1,
Delete = 2,
}
export enum LightClientWasmOrder {
Desc = 0,
Asc = 1,
}
export type {
LightClientFunctionCall,
WorkerInitializeOptions,
Expand Down
6 changes: 3 additions & 3 deletions light-client-lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub struct PeerSyncState {
pub proved_best_known_header: Option<HeaderView>,
}

#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Debug)]
pub struct SearchKey {
pub script: Script,
pub script_type: ScriptType,
Expand All @@ -175,7 +175,7 @@ impl Default for SearchKey {
}
}

#[derive(Deserialize, Default, Serialize)]
#[derive(Deserialize, Default, Serialize, Debug)]
pub struct SearchKeyFilter {
pub script: Option<Script>,
pub script_len_range: Option<[Uint64; 2]>,
Expand All @@ -184,7 +184,7 @@ pub struct SearchKeyFilter {
pub block_range: Option<[BlockNumber; 2]>,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum ScriptType {
Lock,
Expand Down
4 changes: 2 additions & 2 deletions light-client-lib/src/storage/db/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use light_client_db_common::{
DbCommandRequest, DbCommandResponse, InputCommand, OutputCommand, KV,
};

use log::{debug, info};
use log::debug;

use crate::{
error::{Error, Result},
Expand Down Expand Up @@ -417,7 +417,7 @@ impl Storage {
skip: 0,
})
.unwrap();
info!("Received {:?}", remove_keys);
debug!("Received {:?}", remove_keys);
if let DbCommandResponse::IteratorKey { keys } = remove_keys {
batch.delete_many(keys).unwrap();
} else {
Expand Down
2 changes: 2 additions & 0 deletions light-client-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ckb_light_client_lib::{
types::RunEnv,
verify::verify_tx,
};
use log::debug;
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::Serializer;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -947,6 +948,7 @@ pub fn get_cells_capacity(search_key: JsValue) -> Result<JsValue, JsValue> {
}

let search_key: SearchKey = serde_wasm_bindgen::from_value(search_key)?;
debug!("Call get_cells_capacity: {:?}", search_key);
let (prefix, from_key, direction, skip) = build_query_options(
&search_key,
KeyPrefix::CellLockScript,
Expand Down

0 comments on commit fe09648

Please sign in to comment.