Skip to content

Commit

Permalink
feat: support indexer side data filter
Browse files Browse the repository at this point in the history
  • Loading branch information
homura committed Jun 20, 2024
1 parent b5d0c0e commit b908020
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions .eslintrc.next.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
-1, // index -1 is not found
0, // first element of an array
1, // common for i + 1 in a loop
2, // slice(2) for string that starts with "0x" is common to work with 3rd-party libs
16, // toString(16)
1000, // second to millisecond
],
Expand Down
8 changes: 7 additions & 1 deletion packages/ckb-indexer/src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ export class CKBCellCollector implements BaseCellCollector {
}
}

// set data range to narrow the search result if the data is set
// the default data search filter mode is exact search that is the same as ckb-indexer
if (!query.outputDataLenRange) {
if (query.data && query.data !== "any") {
if (
query.data &&
query.data !== "any" &&
typeof query.data === "string"
) {
const dataLenRange = getHexStringBytes(unwrapDataWrapper(query.data));
query.outputDataLenRange = [
"0x" + dataLenRange.toString(16),
Expand Down
2 changes: 2 additions & 0 deletions packages/ckb-indexer/src/resultFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const toSearchFilter = (data: RPCType.SearchFilter): SearchFilter => {
outputCapacityRange: data.output_capacity_range,
scriptLenRange: data.script_len_range,
blockRange: data.block_range,
outputData: data.output_data,
outputDataFilterMode: data.output_data_filter_mode,
};
};

Expand Down
3 changes: 3 additions & 0 deletions packages/ckb-indexer/src/rpcType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ export type CellOutput = {
export type HexadecimalRange = [Hexadecimal, Hexadecimal];
export type ScriptType = "type" | "lock";
export type ScriptSearchMode = "prefix" | "exact" | "partial";
export type OutputDataFilterMode = "prefix" | "exact" | "partial";

export interface SearchFilter {
script?: Script;
output_data?: HexString;
output_data_filter_mode?: OutputDataFilterMode;
output_data_len_range?: HexadecimalRange; //empty
output_capacity_range?: HexadecimalRange; //empty
block_range?: HexadecimalRange; //fromBlock-toBlock
Expand Down
11 changes: 10 additions & 1 deletion packages/ckb-indexer/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ const generateSearchKey = (queries: CKBIndexerQueryOptions): SearchKey => {
if (queries.scriptSearchMode) {
script_search_mode = queries.scriptSearchMode;
}
if (queries.data) {
if (typeof queries.data === "object") {
filter.output_data_filter_mode = queries.data.searchMode;
filter.output_data = queries.data.data;
} else if (typeof queries.data === "string") {
filter.output_data = queries.data;
}
}
if (!script) {
throw new Error("Either lock or type script must be provided!");
}
Expand Down Expand Up @@ -88,7 +96,8 @@ async function requestBatch<T = any>(
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data.map((item) => ({ id: id++, ...item }))),
});
if (res.status !== 200) {
const HTTP_SUCCESS_STATUS = 200;
if (res.status !== HTTP_SUCCESS_STATUS) {
throw new Error(`Indexer request failed with HTTP code ${res.status}`);
}
const result = await res.json();
Expand Down
3 changes: 3 additions & 0 deletions packages/ckb-indexer/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BIish } from "@ckb-lumos/bi";
export type ScriptType = "type" | "lock";
export type Order = "asc" | "desc";
export type ScriptSearchMode = "prefix" | "exact" | "partial";
export type OutputDataFilterMode = "prefix" | "exact" | "partial";

export interface CKBIndexerQueryOptions extends QueryOptions {
outputDataLenRange?: HexadecimalRange;
Expand All @@ -31,6 +32,8 @@ export type HexadecimalRange = [Hexadecimal, Hexadecimal];
export interface SearchFilter {
script?: Script;
scriptLenRange?: HexadecimalRange;
outputData?: HexString;
outputDataFilterMode?: OutputDataFilterMode;
outputDataLenRange?: HexadecimalRange; //empty
outputCapacityRange?: HexadecimalRange; //empty
blockRange?: HexadecimalRange; //fromBlock-toBlock
Expand Down
4 changes: 4 additions & 0 deletions packages/rpc/__tests__/formatters/params.fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,16 @@
"outputDataLenRange": ["0x1", "0x2"],
"outputCapacityRange": ["0x1", "0x2"],
"blockRange": ["0x1", "0x2"],
"outputData": "0x01",
"outputDataFilterMode": "partial",
"scriptLenRange": [0, 1]
},
"expected": {
"output_data_len_range": ["0x1", "0x2"],
"output_capacity_range": ["0x1", "0x2"],
"block_range": ["0x1", "0x2"],
"output_data": "0x01",
"output_data_filter_mode": "partial",
"script_len_range": [0, 1]
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/rpc/src/paramsFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export const formatter = {
script: data.script ? formatter.toScript(data.script) : data.script,
output_data_len_range: data.outputDataLenRange,
output_capacity_range: data.outputCapacityRange,
output_data: data.outputData,
output_data_filter_mode: data.outputDataFilterMode,
block_range: data.blockRange,
script_len_range: data.scriptLenRange,
};
Expand Down
5 changes: 5 additions & 0 deletions packages/rpc/src/types/api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type * as api from "@ckb-lumos/base";
import { HexString } from "@ckb-lumos/base";
import { RPC } from "./rpc";

/**
* @see https://github.com/nervosnetwork/ckb/blob/develop/protocol/src/protocol.fbs for more infGomation
*/
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */
export namespace CKBComponents {
import OutputDataFilterMode = RPC.OutputDataFilterMode;
export type DAO = string;
export type Hash = string;
export type Number = string;
Expand Down Expand Up @@ -252,6 +255,8 @@ export namespace CKBComponents {
export interface SearchFilter {
script?: Script;
scriptLenRange?: HexadecimalRange;
outputData?: HexString;
outputDataFilterMode?: OutputDataFilterMode;
outputDataLenRange?: HexadecimalRange; //empty
outputCapacityRange?: HexadecimalRange; //empty
blockRange?: HexadecimalRange; //fromBlock-toBlock
Expand Down
3 changes: 3 additions & 0 deletions packages/rpc/src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,14 @@ export namespace RPC {
export type HexadecimalRange = [string, string];
export type ScriptType = "type" | "lock";
export type ScriptSearchMode = "prefix" | "exact" | "partial";
export type OutputDataFilterMode = "prefix" | "exact" | "partial";

export interface SearchFilter {
script?: Script;
output_data_len_range?: HexadecimalRange; //empty
output_capacity_range?: HexadecimalRange; //empty
output_data?: HexString;
output_data_filter_mode?: OutputDataFilterMode;
block_range?: HexadecimalRange; //fromBlock-toBlock
script_len_range?: HexadecimalRange;
}
Expand Down

0 comments on commit b908020

Please sign in to comment.