Skip to content

Commit

Permalink
Fix cursor bug of get_transaction and get_cells
Browse files Browse the repository at this point in the history
  • Loading branch information
Officeyutong committed Dec 12, 2024
1 parent 7f7b7da commit 579185d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
24 changes: 20 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,27 @@
$("#init-button").removeClass("loading");
};
window.doTest = async () => {
const tx = { "version": 0n, "cellDeps": [{ "outPoint": { "txHash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37", "index": 0n }, "depType": "depGroup" }], "headerDeps": [], "inputs": [{ "previousOutput": { "txHash": "0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f", "index": 7n }, "since": 0n }], "outputs": [{ "capacity": 20000000000000000n, "lock": { "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type", "args": "0xff5094c2c5f476fc38510018609a3fd921dd28ad" } }, { "capacity": 819957834400000000n, "lock": { "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type", "args": "0x64257f00b6b63e987609fa9be2d0c86d351020fb" } }], "outputsData": ["0x", "0x"], "witnesses": ["0x5500000010000000550000005500000041000000af34b54bebf8c5971da6a880f2df5a186c3f8d0b5c9a1fe1a90c95b8a4fb89ef3bab1ccec13797dcb3fee80400f953227dd7741227e08032e3598e16ccdaa49c00"] };
console.log(await window.client.sendTransaction(tx));
console.log(await window.client.estimateCycles(tx));

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" }))
};
window.doTest2 = async () => {
const cells = await window.client.getCells({
script: {
codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
args: "0xef0273afa67242b1b407eced450f78b2c02a2a23"
}, scriptType: "lock"
}, "asc", 1);
return await window.client.getCells({
script: {
codeHash: "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
hashType: "type",
args: "0xef0273afa67242b1b407eced450f78b2c02a2a23"
}, scriptType: "lock"
}, "asc", 1, cells.lastCursor);
}
</script>

</head>
Expand Down
9 changes: 5 additions & 4 deletions light-client-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { FetchResponse, LocalNode, localNodeTo, RemoteNode, remoteNodeTo, Script
import { ClientBlock, ClientBlockHeader, Hex, hexFrom, HexLike, Num, numFrom, NumLike, numToHex, TransactionLike } from "@ckb-ccc/core/barrel";
import { JsonRpcBlockHeader, JsonRpcTransformers } from "@ckb-ccc/core/advancedBarrel";
import { Mutex } from "async-mutex";
import { bytesFrom } from "@ckb-ccc/core";

const DEFAULT_BUFFER_SIZE = 50 * (1 << 20);
/**
Expand Down Expand Up @@ -174,13 +175,13 @@ class LightClient {
searchKey: ClientIndexerSearchKeyLike,
order?: "asc" | "desc",
limit?: NumLike,
afterCursor?: string
afterCursor?: Hex
): Promise<ClientFindCellsResponse> {
return JsonRpcTransformers.findCellsResponseTo(await this.invokeLightClientCommand("get_cells", [
JsonRpcTransformers.indexerSearchKeyFrom(searchKey),
cccOrderToLightClientWasmOrder(order ?? "asc"),
Number(numFrom(numToHex(limit ?? 10))),
afterCursor
afterCursor ? bytesFrom(afterCursor) : afterCursor
]));
}
/**
Expand All @@ -195,15 +196,15 @@ class LightClient {
searchKey: ClientIndexerSearchKeyTransactionLike,
order?: "asc" | "desc",
limit?: NumLike,
afterCursor?: string
afterCursor?: Hex
): Promise<GetTransactionsResponse<TxWithCell> | GetTransactionsResponse<TxWithCells>> {
return lightClientGetTransactionsResultTo(await this.invokeLightClientCommand(
"get_transactions",
[
JsonRpcTransformers.indexerSearchKeyTransactionFrom(searchKey),
cccOrderToLightClientWasmOrder(order ?? "asc"),
Number(numFrom(numToHex(limit ?? 10))),
afterCursor
afterCursor ? bytesFrom(afterCursor) : afterCursor
]
));
}
Expand Down
4 changes: 2 additions & 2 deletions light-client-lib/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ pub enum ScriptType {
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen::prelude::wasm_bindgen)]
#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "snake_case")]
pub enum Order {
Desc,
Asc,
}

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Debug)]
pub struct Cell {
pub output: CellOutput,
pub output_data: Option<JsonBytes>,
Expand Down
21 changes: 17 additions & 4 deletions light-client-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,10 @@ pub fn get_cells(
if !status(0b1) {
return Err(JsValue::from_str("light client not on start state"));
}
debug!(
"Calling get_cells with {:?}, {:?}, {:?}, {:?}",
search_key, order, limit, after_cursor
);
let search_key: SearchKey = serde_wasm_bindgen::from_value(search_key)?;

let (prefix, from_key, direction, skip) = build_query_options(
Expand Down Expand Up @@ -560,6 +564,7 @@ pub fn get_cells(
let mut cells = Vec::new();
let mut last_key = Vec::new();
for (key, value) in kvs.into_iter().map(|kv| (kv.key, kv.value)) {
debug!("get cells iterator at {:?} {:?}", key, value);
let tx_hash = packed::Byte32::from_slice(&value).expect("stored tx hash");
let output_index = u32::from_be_bytes(
key[key.len() - 4..]
Expand Down Expand Up @@ -602,6 +607,7 @@ pub fn get_cells(
.as_slice()
.starts_with(prefix)
{
debug!("skipped at {}", line!());
continue;
}
}
Expand All @@ -611,6 +617,7 @@ pub fn get_cells(
.as_slice()
.starts_with(prefix)
{
debug!("skipped at {}", line!());
continue;
}
}
Expand All @@ -622,6 +629,7 @@ pub fn get_cells(
ScriptType::Lock => {
let script_len = extract_raw_data(&output.lock()).len();
if script_len < r0 || script_len > r1 {
debug!("skipped at {}", line!());
continue;
}
}
Expand All @@ -632,6 +640,7 @@ pub fn get_cells(
.map(|script| extract_raw_data(&script).len())
.unwrap_or_default();
if script_len < r0 || script_len > r1 {
debug!("skipped at {}", line!());
continue;
}
}
Expand All @@ -640,26 +649,28 @@ pub fn get_cells(

if let Some([r0, r1]) = filter_output_data_len_range {
if output_data.len() < r0 || output_data.len() >= r1 {
debug!("skipped at {}", line!());
continue;
}
}

if let Some([r0, r1]) = filter_output_capacity_range {
let capacity: core::Capacity = output.capacity().unpack();
if capacity < r0 || capacity >= r1 {
debug!("skipped at {}", line!());
continue;
}
}

if let Some([r0, r1]) = filter_block_range {
if block_number < r0 || block_number >= r1 {
debug!("skipped at {}", line!());
continue;
}
}

last_key = key.to_vec();

cells.push(Cell {
let cell_to_push = Cell {
output: output.into(),
output_data: if with_data {
Some(output_data.into())
Expand All @@ -669,9 +680,11 @@ pub fn get_cells(
out_point: packed::OutPoint::new(tx_hash, output_index).into(),
block_number: block_number.into(),
tx_index: tx_index.into(),
});
};
debug!("pushed cell {:#?}", cell_to_push);
cells.push(cell_to_push);
}

debug!("get_cells last_key={:?}", last_key);
Ok((Pagination {
objects: cells,
last_cursor: JsonBytes::from_vec(last_key),
Expand Down

0 comments on commit 579185d

Please sign in to comment.