Skip to content

Commit

Permalink
feat: add config files update cive version
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh committed Nov 1, 2024
1 parent d3b341b commit e18c3e2
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 38 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-dragons-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@xcfx/node": patch
---

Added config files update cive version"
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ Temporary Items
# iCloud generated files
*.icloud
*.js
index.d.ts
index.d.ts.map
!conflux.js
!conflux.d.ts
## pnpm ##
Expand Down
110 changes: 92 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ npm install @xcfx/node

```ts
import { createServer } from "@xcfx/node";
import { http, createPublicClient } from "cive";

async function main() {
const server = await createServer();
const server = await createServer({
jsonrpcHttpPort: 12537,
});

await server.start();

const options = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: '{"jsonrpc":"2.0","method":"cfx_getStatus","id":1}',
};
const client = createPublicClient({
transport: http(`http://127.0.0.1:12537`),
});

const response = await fetch("http://localhost:12537", options);
const data = await response.json();
const status = await client.getStatus();

// safe to stop the server
await server.stop();
Expand All @@ -55,7 +55,7 @@ await main();
import { createServer } from "@xcfx/node";

async function main() {
const server = await createServer({ ...serverConfig, ...ConfluxConfig });
const server = await createServer({ ...ConfluxConfig });
}
```

Expand Down Expand Up @@ -99,21 +99,32 @@ export interface ConfluxConfig {
*/
logLevel?: string;
/**
* The port of the websocket JSON-RPC server.
* @default 12535
* The port of the websocket JSON-RPC server(public_rpc_apis is user defined).
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcWsPort?: number;
/**
* The port of the HTTP JSON-RPC server.
* @default 12537
* `tcp_port` is the TCP port that the process listens for P2P messages. The default is 32323.
* @default 32323
*/
jsonrpcHttpPort?: number;
tcpPort?: number;
/**
* `udp_port` is the UDP port used for node discovery.
* @default 32323
*/
udpPort?: number;
/**
* Possible Core space names are: all, safe, cfx, pos, debug, pubsub, test, trace, txpool.
* `safe` only includes `cfx` and `pubsub`, `txpool`.
* @default "all"
*/
publicRpcApis?: string;
/**
* Possible eSpace names are: eth, ethpubsub, ethdebug.
* @default 'evm'
*/
publicEvmRpcApis?: string;
/**
* The chain ID of the network.(core space)
* @default 1234
Expand All @@ -129,14 +140,20 @@ export interface ConfluxConfig {
* @default "123456"
*/
devPosPrivateKeyEncryptionPassword?: string;
/** The private key of the genesis, every account will be receive 1000 CFX */
/** The private key of the genesis, every account will be receive 10000 CFX */
genesisSecrets?: Array<string>;
/** @default: false */
/**
* If it's `true`, `DEFERRED_STATE_EPOCH_COUNT` blocks are generated after
* receiving a new tx through RPC calling to pack and execute this
* transaction
*/
devPackTxImmediately?: boolean;
/** @default:0 */
/** @default:1 */
posReferenceEnableHeight?: number;
/** @default: 1 */
/** @default:2 */
defaultTransitionTime?: number;
/** @default:3 */
cip1559TransitionHeight?: number;
/** @default: temp dir */
confluxDataDir?: string;
/** pos config path */
Expand All @@ -151,5 +168,62 @@ export interface ConfluxConfig {
* @default sqlite
*/
blockDbType?: string;
/** bootnodes is a list of nodes that a conflux node trusts, and will be used to sync the blockchain when a node starts. */
bootnodes?: string;
/** Window size for PoW manager */
powProblemWindowSize?: number;
/** # Secret key for stratum. The value is 64-digit hex string. If not set, the RPC subscription will not check the authorization. */
stratumSecret?: string;
/** `public_address` is the address of this node used */
publicAddress?: string;
/**
* `jsonrpc_http_keep_alive` is used to control whether to set KeepAlive for rpc HTTP connections.
* @default false
*/
jsonrpcHttpKeepAlive?: boolean;
/** `print_memory_usage_period_s` is the period for printing memory usage. */
printMemoryUsagePeriodS?: number;
/**
* The port of the http JSON-RPC server.public_rpc_apis is user defined).
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcHttpPort?: number;
/**
* The port of the tcp JSON-RPC server. public_rpc_apis is user defined).
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcTcpPort?: number;
/**
* The port of the http JSON-RPC server public_rpc_apis is user defined).
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcHttpEthPort?: number;
/**
* The port of the websocket JSON-RPC serverpublic_rpc_apis is user defined).
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcWsEthPort?: number;
/**
* The port of the tcp JSON-RPC server(public_rpc_apis is "all").
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcLocalTcpPort?: number;
/**
* The port of the http JSON-RPC server(public_rpc_apis is "all").
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcLocalHttpPort?: number;
/**
* The port of the websocket JSON-RPC server(public_rpc_apis is "all").
* if not set, the JSON-RPC server will not be started.
* @default null
*/
jsonrpcLocalWsPort?: number;
}
```
18 changes: 13 additions & 5 deletions __test__/manualBlockGeneration.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { http, createPublicClient, createWalletClient, webSocket } from "cive";
import {
http,
createPublicClient,
createTestClient,
createWalletClient,
webSocket,
} from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { parseCFX } from "cive/utils";
import { beforeAll, describe, expect, test } from "vitest";
Expand Down Expand Up @@ -88,12 +94,14 @@ describe("manual block generation", async () => {
transport: http(`http://127.0.0.1:${HTTP_PORT}`),
});

const testClient = createTestClient({
chain: localChain,
transport: http(`http://127.0.0.1:${HTTP_PORT}`),
});

const status = await client.getStatus();

await client.request<any>({
method: "generate_empty_blocks",
params: [10],
});
await testClient.mine({ blocks: 10 });

const status1 = await client.getStatus();
expect(status1.blockNumber).toBe(status.blockNumber + 10n);
Expand Down
1 change: 0 additions & 1 deletion __test__/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe("server", () => {
});
await server.start();
const client = createPublicClient({
chain: localChain,
transport: http(`http://127.0.0.1:${jsonrpcHttpPort}`),
});

Expand Down
59 changes: 58 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from "node:path";
import { http, createTestClient, webSocket } from "cive";
import { type ConfluxConfig, ConfluxNode } from "./conflux";

export type Config = {
Expand All @@ -7,6 +8,8 @@ export type Config = {
* @default false
*/
log?: boolean;
timeout?: number;
retryInterval?: number;
} & ConfluxConfig;

export type CreateServerReturnType = {
Expand All @@ -21,6 +24,8 @@ export async function createServer(
if (isServiceCreated) {
throw new Error("The server has already been created");
}
const { timeout = 20000, retryInterval = 300 } = config;

isServiceCreated = true;

const { log = false, ...userConfig } = config;
Expand All @@ -41,7 +46,7 @@ export async function createServer(
const node = new ConfluxNode();
return {
async start() {
return new Promise((resolve, reject) => {
await new Promise<void>((resolve, reject) => {
node.startNode(filledConfig, (err) => {
if (err) {
reject(err);
Expand All @@ -50,6 +55,15 @@ export async function createServer(
}
});
});

if (filledConfig.jsonrpcHttpPort || filledConfig.jsonrpcWsPort) {
await retryGetCurrentSyncPhase({
httpPort: filledConfig.jsonrpcHttpPort,
wsPort: filledConfig.jsonrpcWsPort,
timeout: timeout,
retryInterval: retryInterval,
});
}
},
async stop() {
return new Promise((resolve, reject) => {
Expand All @@ -64,3 +78,46 @@ export async function createServer(
},
};
}

type retryGetCurrentSyncPhaseParameters = {
httpPort?: number;
wsPort?: number;
timeout: number;
retryInterval: number;
};

async function retryGetCurrentSyncPhase({
httpPort,
wsPort,
timeout,
retryInterval,
}: retryGetCurrentSyncPhaseParameters) {
if (!httpPort && !wsPort) return;

const testClient = createTestClient({
transport: httpPort
? http(`http://127.0.0.1:${httpPort}`)
: webSocket(`ws://127.0.0.1:${wsPort}`),
});

const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);

try {
while (!controller.signal.aborted) {
const phase = await testClient.getCurrentSyncPhase();
if (phase === "NormalSyncPhase") {
clearTimeout(timeoutId);
return;
}
await new Promise((resolve) => setTimeout(resolve, retryInterval));
}
} catch (error) {
if (!controller.signal.aborted) {
throw new Error("Get node sync phase timeout");
}
throw error;
} finally {
clearTimeout(timeoutId);
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"license": "MIT",
"keywords": ["conflux", "conflux-client"],
"files": ["index.d.ts", "index.js", "conflux.js", "conflux.d.ts"],
"files": ["index.d.ts", "index.js", "conflux.js", "conflux.d.ts", "configs"],
"napi": {
"binaryName": "node",
"targets": [
Expand Down Expand Up @@ -43,7 +43,7 @@
"changeset:publish": "pnpm changeset:prepublish && changeset publish"
},
"dependencies": {
"cive": "^0.6.0"
"cive": "0.7.0"
},
"devDependencies": {
"@biomejs/biome": "1.8.3",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e18c3e2

Please sign in to comment.