Skip to content

Commit

Permalink
feat(rpc): new rpc get_raw_tx_pool (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
homura authored Jul 25, 2024
1 parent 2d0b448 commit 3763977
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-buses-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/rpc": minor
---

feat: add rpc `test_tx_pool_accept` https://github.com/nervosnetwork/ckb/pull/4433
6 changes: 6 additions & 0 deletions .changeset/lemon-jobs-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ckb-lumos/base": minor
"@ckb-lumos/rpc": minor
---

feat: added the `conflicted` field to `get_raw_tx_pool` https://github.com/nervosnetwork/ckb/pull/4339
5 changes: 5 additions & 0 deletions .changeset/small-cups-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ckb-lumos/rpc": minor
---

fix: added `well_known_scripts_only` to `OutputValidator`
1 change: 1 addition & 0 deletions packages/base/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ export interface TxPoolVerbosity {
proposed: {
[key: string]: TxVerbosity;
};
conflicted: Hash[];
}

export type RawTxPool = TxPoolIds | TxPoolVerbosity;
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/__tests__/ckb-rpc-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('ckb-rpc settings and helpers', () => {
})

it('has 47 basic rpc', () => {
expect(Object.values(rpc)).toHaveLength(47)
expect(Object.values(rpc)).toHaveLength(48)
})

it('set node url to http://test.localhost:8114', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/__tests__/exceptions/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"params": [],
"expected": {
"code": 101,
"message": "Expect outputs validator to be 'default' or 'passthrough'"
"message": "Expect outputs validator to be 'well_known_scripts_only' or 'passthrough'"
}
},
"BigintOrHexStringTypeException": {
Expand Down
6 changes: 3 additions & 3 deletions packages/rpc/__tests__/formatters/params.fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@
"expected": "undefined"
},
{
"param": "default",
"expected": "default"
"param": "well_known_scripts_only",
"expected": "well_known_scripts_only"
},
{
"param": "passthrough",
Expand All @@ -335,7 +335,7 @@
},
{
"param": "unknown",
"exception": "Expect outputs validator to be 'default' or 'passthrough'"
"exception": "Expect outputs validator to be 'well_known_scripts_only' or 'passthrough'"
}
],
"toBoolean": [
Expand Down
11 changes: 9 additions & 2 deletions packages/rpc/src/Base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,13 @@ export interface Base {
*/
getForkBlock(
blockHash: CKBComponents.Hash256,
verbosity?: 2n | "0x2"
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
verbosity?: 2 | 2n | "0x2"
): Promise<CKBComponents.BlockView | null>;
getForkBlock(
blockHash: CKBComponents.Hash256,
verbosity: 0n | "0x0"
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
verbosity: 0 | 0n | "0x0"
): Promise<CKBComponents.SerializedBlock | null>;

/**
Expand Down Expand Up @@ -534,6 +536,11 @@ export interface Base {
) => Promise<CKBComponents.FeeRateStatistics>;

getDeploymentsInfo: () => Promise<CKBComponents.DeploymentsInfo>;

testTxPoolAccept: (
tx: CKBComponents.RawTransaction,
outputsValidator?: CKBComponents.OutputsValidator
) => Promise<CKBComponents.EntryCompleted>;
}

export class Base {
Expand Down
8 changes: 8 additions & 0 deletions packages/rpc/src/Base/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,12 @@ export default {
paramsFormatters: [],
resultFormatters: resultFmts.toRawTxPool,
},

testTxPoolAccept: {
method: "test_tx_pool_accept",
paramsFormatters: [
paramsFmts.toRawTransaction,
paramsFmts.toOutputsValidator,
],
},
};
4 changes: 3 additions & 1 deletion packages/rpc/src/exceptions/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export class OutputsValidatorTypeException extends TypeError {
code = ErrorCode.ParameterInvalid;

constructor() {
super(`Expect outputs validator to be 'default' or 'passthrough'`);
super(
`Expect outputs validator to be 'well_known_scripts_only' or 'passthrough'`
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rpc/src/paramsFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const formatter = {
toReverseOrder: (reverse = false) => !!reverse,
toOutputsValidator: (outputsValidator: CKBComponents.OutputsValidator) => {
if (!outputsValidator) return undefined;
const VALIDATORS = ["default", "passthrough"];
const VALIDATORS = ["well_known_scripts_only", "passthrough"];
if (VALIDATORS.indexOf(outputsValidator) > -1) {
return outputsValidator;
}
Expand Down
19 changes: 13 additions & 6 deletions packages/rpc/src/resultFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable camelcase, @typescript-eslint/no-explicit-any */
import { CKBComponents } from "./types/api";
import { RPC } from "./types/rpc";

Expand Down Expand Up @@ -138,18 +137,27 @@ const toTip = (tip: RPC.Tip): CKBComponents.Tip => ({
blockNumber: tip.block_number,
});

function isBlockWithCycles(value: unknown): value is BlockWithCycles {
return (
!!value &&
typeof value === "object" &&
"block" in value &&
"cycles" in value
);
}

type BlockWithCycles = { block: RPC.Block | string; cycles: string[] };
function toBlock(block: string): string;
function toBlock(block: RPC.Block): CKBComponents.Block;
function toBlock<T extends BlockWithCycles>(block: T): T;
function toBlock(res: string | RPC.Block | BlockWithCycles): any {
function toBlock(res: string | RPC.Block | BlockWithCycles): unknown {
if (!res) return res;
if (typeof res === "string") return res;

if ("block" in res && "cycles" in res) {
if (isBlockWithCycles(res)) {
return {
cycles: res.cycles,
block: toBlock(res.block as any),
block: toBlock(res.block as RPC.Block),
};
}

Expand Down Expand Up @@ -643,7 +651,7 @@ const toRawTxPool = (rawTxPool: RPC.RawTxPool): CKBComponents.RawTxPool => {
pending[hash] = toTxVerbosity(rawTxPool.pending[hash]);
});

return { proposed, pending };
return { proposed, pending, conflicted: rawTxPool.conflicted };
};

const toIndexerCell = (
Expand Down Expand Up @@ -881,4 +889,3 @@ export {
toDeploymentInfo,
toDeploymentsInfo,
};
/* eslint-enable camelcase */
13 changes: 11 additions & 2 deletions packages/rpc/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ export namespace CKBComponents {
export type Nonce = string;
export type Cycles = string;
export type Size = string;
export type OutputsValidator = "default" | "passthrough" | undefined;
export type OutputsValidator =
| "well_known_scripts_only"
| "passthrough"
| undefined;
export type RationalU256 = Record<"denom" | "numer", string>;
export type ProposalWindow = Record<"closest" | "farthest", BlockNumber>;
export type EpochNumberWithFraction = string;
Expand All @@ -35,7 +38,7 @@ export namespace CKBComponents {
Proposed = "proposed",
Committed = "committed",
}

export type Cycle = string;
export type ScriptHashType = api.HashType;

export type DepType = "code" | "depGroup";
Expand Down Expand Up @@ -316,6 +319,12 @@ export namespace CKBComponents {
}

export type DeploymentPos = api.DeploymentPos;

export interface EntryCompleted {
cycles: Cycle;
/// Cached tx fee
fee: Capacity;
}
export type DeploymentState = api.DeploymentState;
export type DeploymentInfo = api.DeploymentInfo;
export type DeploymentsInfo = api.DeploymentsInfo;
Expand Down
18 changes: 14 additions & 4 deletions packages/rpc/src/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,15 @@ export namespace RPC {
ancestors_count: Count;
}

export type TxPoolVerbosity = Record<
"pending" | "proposed",
Record<Hash256, TxVerbosity>
>;
export interface TxPoolVerbosity {
pending: {
[key: string]: TxVerbosity;
};
proposed: {
[key: string]: TxVerbosity;
};
conflicted: Hash[];
}

export type RawTxPool = TxPoolIds | TxPoolVerbosity;

Expand Down Expand Up @@ -544,4 +549,9 @@ export namespace RPC {
is_initial_block_download: boolean;
alerts: Vec<AlertMessage>;
}

export interface EntryCompleted {
cycles: Cycles;
fee: Capacity;
}
}

1 comment on commit 3763977

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 New canary release: 0.0.0-canary-3763977-20240725033024

npm install @ckb-lumos/[email protected]

Please sign in to comment.