Skip to content

Commit 6447f4e

Browse files
committed
Implementing light client algorithm
1 parent 2c4609a commit 6447f4e

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

ibc.ts/src/common/chain.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IBC } from "./foundry/transaction";
55
import { delay } from "./util";
66
import Debug from "debug";
77
import { ClientState } from "./foundry/types";
8+
import { IBCHeader, IBCQueryResult } from "./types";
89

910
const debug = Debug("common:tx");
1011

@@ -36,7 +37,7 @@ export interface ChainConfig {
3637
export class Chain {
3738
private readonly sdk: SDK;
3839
private readonly faucetAddress: PlatformAddress;
39-
private readonly counterpartyIdentifiers: CounterpartyIdentifiers;
40+
public readonly counterpartyIdentifiers: CounterpartyIdentifiers;
4041

4142
public constructor(config: ChainConfig) {
4243
this.sdk = new SDK({
@@ -64,6 +65,19 @@ export class Chain {
6465
public async latestHeight(): Promise<number> {
6566
return await this.sdk.rpc.chain.getBestBlockNumber();
6667
}
68+
69+
public async queryClient(
70+
blockNumber: number
71+
): Promise<IBCQueryResult<ClientState> | null> {
72+
return this.sdk.rpc.sendRpcRequest("ibc_query_client_state", [
73+
this.counterpartyIdentifiers.client,
74+
blockNumber
75+
]);
76+
}
77+
78+
public async queryHeader(blockNumber: number): Promise<IBCHeader | null> {
79+
return this.sdk.rpc.sendRpcRequest("ibc_compose_header", [blockNumber]);
80+
}
6781
}
6882

6983
async function waitForTx(sdk: SDK, txHash: H256) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const RLP = require("rlp");
2+
3+
export class UpdateClientDatagram {
4+
private id: string;
5+
private header: Buffer;
6+
7+
public constructor({ id, header }: { id: string; header: Buffer }) {
8+
this.id = id;
9+
this.header = header;
10+
}
11+
12+
public rlpBytes(): Buffer {
13+
return RLP.encode(this.toEncodeObject());
14+
}
15+
16+
public toEncodeObject(): any[] {
17+
return [2, this.id, this.header];
18+
}
19+
}

ibc.ts/src/common/foundry/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ClientState {
2+
number: number;
3+
next_validator_set_hash: string;
4+
}

ibc.ts/src/common/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface IBCQueryResult<T> {
2+
data: T | null;
3+
proof: string;
4+
}
5+
6+
export type IBCHeader = string;

ibc.ts/src/relayer/index.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Datagram } from "../common/datagram/index";
44
import { delay } from "../common/util";
55
import { getConfig } from "./config";
66
import { PlatformAddress } from "codechain-primitives/lib";
7+
import { UpdateClientDatagram } from "../common/datagram/updateClient";
78

89
require("dotenv").config();
910

@@ -111,6 +112,27 @@ async function updateLightClient({
111112
height: number;
112113
counterpartyChainHeight: number;
113114
}): Promise<Datagram[]> {
114-
console.error("Not implemented");
115-
return [];
115+
const datagrams = [];
116+
const clientState = await chain.queryClient(height);
117+
118+
if (clientState!.data == null) {
119+
throw new Error(
120+
`No client state found. Please create a light client with identifier: ${chain.counterpartyIdentifiers.client}`
121+
);
122+
}
123+
let currentBlockNumber = clientState!.data!.number;
124+
while (currentBlockNumber < counterpartyChainHeight) {
125+
const header = (await counterpartyChain.queryHeader(
126+
currentBlockNumber + 1
127+
))!;
128+
datagrams.push(
129+
new UpdateClientDatagram({
130+
id: chain.counterpartyIdentifiers.client,
131+
header: Buffer.from(header, "hex")
132+
})
133+
);
134+
currentBlockNumber += 1;
135+
}
136+
137+
return datagrams;
116138
}

0 commit comments

Comments
 (0)