Skip to content

Commit cdfd183

Browse files
committed
Create IBC light clients in ICS scenario
1 parent b40f9ec commit cdfd183

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

ibc.ts/src/common/chain.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class Chain {
7272
}
7373

7474
public async queryClient(
75-
blockNumber: number
75+
blockNumber?: number
7676
): Promise<IBCQueryResult<ClientState> | null> {
7777
return this.sdk.rpc.sendRpcRequest("ibc_query_client_state", [
7878
this.counterpartyIdentifiers.client,
@@ -85,6 +85,12 @@ export class Chain {
8585
): Promise<IBCHeader | null> {
8686
return this.sdk.rpc.sendRpcRequest("ibc_compose_header", [blockNumber]);
8787
}
88+
89+
public async queryChainHeader(blockNumber: number): Promise<string | null> {
90+
return this.sdk.rpc.sendRpcRequest("chain_getRawHeaderByNumber", [
91+
blockNumber
92+
]);
93+
}
8894
}
8995

9096
async function waitForTx(sdk: SDK, txHash: H256) {

ibc.ts/src/scenario/index.ts

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
1+
import Debug from "debug";
2+
import { getConfig } from "../common/config";
3+
import { Chain } from "../common/chain";
4+
import { PlatformAddress } from "codechain-primitives/lib";
5+
import { CreateClientDatagram } from "../common/datagram/createClient";
6+
import { strict as assert } from "assert";
7+
8+
require("dotenv").config();
9+
10+
const debug = Debug("scenario:main");
11+
112
async function main() {
2-
console.log("Not implemented");
13+
const config = getConfig();
14+
const chainA = new Chain({
15+
server: config.chainA.rpcURL,
16+
networkId: config.chainA.networkId,
17+
faucetAddress: PlatformAddress.fromString(config.chainA.faucetAddress),
18+
counterpartyIdentifiers: {
19+
client: config.chainA.counterpartyClientId,
20+
connection: config.chainA.counterpartyConnectionId,
21+
channel: config.chainA.counterpartyChannelId
22+
},
23+
keystorePath: config.chainA.keystorePath
24+
});
25+
const chainB = new Chain({
26+
server: config.chainB.rpcURL,
27+
networkId: config.chainB.networkId,
28+
faucetAddress: PlatformAddress.fromString(config.chainB.faucetAddress),
29+
counterpartyIdentifiers: {
30+
client: config.chainB.counterpartyClientId,
31+
connection: config.chainB.counterpartyConnectionId,
32+
channel: config.chainB.counterpartyChannelId
33+
},
34+
keystorePath: config.chainB.keystorePath
35+
});
36+
37+
await createLightClient({ chain: chainA, counterpartyChain: chainB });
38+
await createLightClient({ chain: chainB, counterpartyChain: chainA });
339
}
440

541
main().catch(console.error);
42+
43+
async function createLightClient({
44+
chain,
45+
counterpartyChain
46+
}: {
47+
chain: Chain;
48+
counterpartyChain: Chain;
49+
}) {
50+
debug("Create light client");
51+
const counterpartyBlockNumber = await counterpartyChain.latestHeight();
52+
const blockNumber = await chain.latestHeight();
53+
debug(`height is ${counterpartyBlockNumber}`);
54+
const counterpartyRawHeader = await counterpartyChain.queryChainHeader(
55+
counterpartyBlockNumber
56+
);
57+
debug(`rawHeader is ${counterpartyRawHeader}`);
58+
59+
assert(counterpartyRawHeader, "headet should not empty");
60+
assert.notStrictEqual(
61+
counterpartyRawHeader!.substr(0, 2),
62+
"0x",
63+
"should not start with 0x"
64+
);
65+
66+
debug(`Get queryClient`);
67+
const clientStateBefore = await chain.queryClient(blockNumber);
68+
assert.notEqual(clientStateBefore, null, "querying on the best block");
69+
assert.equal(clientStateBefore!.data, null, "client is not initialized");
70+
71+
const createClient = new CreateClientDatagram({
72+
id: chain.counterpartyIdentifiers.client,
73+
kind: 0,
74+
consensusState: Buffer.alloc(0),
75+
data: Buffer.from(counterpartyRawHeader!, "hex")
76+
});
77+
78+
debug(`Submit datagram`);
79+
await chain.submitDatagram(createClient);
80+
81+
const clientStateAfter = await chain.queryClient();
82+
assert.notEqual(clientStateAfter, null, "querying on the best block");
83+
assert.notEqual(clientStateAfter!.data, null, "client is initialized");
84+
debug(`Create client is ${JSON.stringify(clientStateAfter)}`);
85+
}

0 commit comments

Comments
 (0)