Skip to content

Commit c7f6505

Browse files
committed
Create IBC light clients in ICS scenario
1 parent dc80468 commit c7f6505

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-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: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,87 @@
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+
console.log("Create a light client in chain A");
38+
await createLightClient({ chain: chainA, counterpartyChain: chainB });
39+
console.log("Create a light client in chain B");
40+
await createLightClient({ chain: chainB, counterpartyChain: chainA });
341
}
442

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

0 commit comments

Comments
 (0)