Skip to content

Commit 8ddaa95

Browse files
Park Juhyungmajecty
authored andcommitted
Send connection datagrams in the scenario script and the relayer
* connOpenInit * connOpenTry * connOpenAck * connOpenConfirm
1 parent 815982e commit 8ddaa95

File tree

6 files changed

+326
-0
lines changed

6 files changed

+326
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const RLP = require("rlp");
2+
3+
export class ConnOpenAckDatagram {
4+
private identifier: string;
5+
private proofTry: Buffer;
6+
private proofConsensus: Buffer;
7+
private proofHeight: number;
8+
private consensusHeight: number;
9+
10+
public constructor({
11+
identifier,
12+
proofTry,
13+
proofConsensus,
14+
proofHeight,
15+
consensusHeight
16+
}: {
17+
identifier: string;
18+
proofTry: Buffer;
19+
proofConsensus: Buffer;
20+
proofHeight: number;
21+
consensusHeight: number;
22+
}) {
23+
this.identifier = identifier;
24+
this.proofTry = proofTry;
25+
this.proofConsensus = proofConsensus;
26+
this.proofHeight = proofHeight;
27+
this.consensusHeight = consensusHeight;
28+
}
29+
30+
public rlpBytes(): Buffer {
31+
return RLP.encode(this.toEncodeObject());
32+
}
33+
34+
public toEncodeObject(): any[] {
35+
return [
36+
5,
37+
this.identifier,
38+
this.proofTry,
39+
this.proofConsensus,
40+
this.proofHeight,
41+
this.consensusHeight
42+
];
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const RLP = require("rlp");
2+
3+
export class ConnOpenConfirmDatagram {
4+
private identifier: string;
5+
private proofAck: Buffer;
6+
private proofHeight: number;
7+
8+
public constructor({
9+
identifier,
10+
proofAck,
11+
proofHeight
12+
}: {
13+
identifier: string;
14+
proofAck: Buffer;
15+
proofHeight: number;
16+
}) {
17+
this.identifier = identifier;
18+
this.proofAck = proofAck;
19+
this.proofHeight = proofHeight;
20+
}
21+
22+
public rlpBytes(): Buffer {
23+
return RLP.encode(this.toEncodeObject());
24+
}
25+
26+
public toEncodeObject(): any[] {
27+
return [6, this.identifier, this.proofAck, this.proofHeight];
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const RLP = require("rlp");
2+
3+
export class ConnOpenInitDatagram {
4+
private id: string;
5+
private desiredCounterpartyConnectionIdentifier: string;
6+
private counterpartyPrefix: string;
7+
private clientIdentifier: string;
8+
private counterpartyClientIdentifier: string;
9+
10+
public constructor({
11+
id,
12+
desiredCounterpartyConnectionIdentifier,
13+
counterpartyPrefix,
14+
clientIdentifier,
15+
counterpartyClientIdentifier
16+
}: {
17+
id: string;
18+
desiredCounterpartyConnectionIdentifier: string;
19+
counterpartyPrefix: string;
20+
clientIdentifier: string;
21+
counterpartyClientIdentifier: string;
22+
}) {
23+
this.id = id;
24+
this.desiredCounterpartyConnectionIdentifier = desiredCounterpartyConnectionIdentifier;
25+
this.counterpartyPrefix = counterpartyPrefix;
26+
this.clientIdentifier = clientIdentifier;
27+
this.counterpartyClientIdentifier = counterpartyClientIdentifier;
28+
}
29+
30+
public rlpBytes(): Buffer {
31+
return RLP.encode(this.toEncodeObject());
32+
}
33+
34+
public toEncodeObject(): any[] {
35+
return [
36+
3,
37+
this.id,
38+
this.desiredCounterpartyConnectionIdentifier,
39+
this.counterpartyPrefix,
40+
this.clientIdentifier,
41+
this.counterpartyClientIdentifier
42+
];
43+
}
44+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const RLP = require("rlp");
2+
3+
export class ConnOpenTryDatagram {
4+
private desiredIdentifier: string;
5+
private counterpartyConnectionIdentifier: string;
6+
private counterpartyPrefix: string;
7+
private counterpartyClientIdentifier: string;
8+
private clientIdentifier: string;
9+
private proofInit: Buffer;
10+
private proofConsensus: Buffer;
11+
private proofHeight: number;
12+
private consensusHeight: number;
13+
14+
public constructor({
15+
desiredIdentifier,
16+
counterpartyConnectionIdentifier,
17+
counterpartyPrefix,
18+
counterpartyClientIdentifier,
19+
clientIdentifier,
20+
proofInit,
21+
proofConsensus,
22+
proofHeight,
23+
consensusHeight
24+
}: {
25+
desiredIdentifier: string;
26+
counterpartyConnectionIdentifier: string;
27+
counterpartyPrefix: string;
28+
counterpartyClientIdentifier: string;
29+
clientIdentifier: string;
30+
proofInit: Buffer;
31+
proofConsensus: Buffer;
32+
proofHeight: number;
33+
consensusHeight: number;
34+
}) {
35+
this.desiredIdentifier = desiredIdentifier;
36+
this.counterpartyConnectionIdentifier = counterpartyConnectionIdentifier;
37+
this.counterpartyPrefix = counterpartyPrefix;
38+
this.counterpartyClientIdentifier = counterpartyClientIdentifier;
39+
this.clientIdentifier = clientIdentifier;
40+
this.proofInit = proofInit;
41+
this.proofConsensus = proofConsensus;
42+
this.proofHeight = proofHeight;
43+
this.consensusHeight = consensusHeight;
44+
}
45+
46+
public rlpBytes(): Buffer {
47+
return RLP.encode(this.toEncodeObject());
48+
}
49+
50+
public toEncodeObject(): any[] {
51+
return [
52+
4,
53+
this.desiredIdentifier,
54+
this.counterpartyConnectionIdentifier,
55+
this.counterpartyPrefix,
56+
this.counterpartyClientIdentifier,
57+
this.clientIdentifier,
58+
this.proofInit,
59+
this.proofConsensus,
60+
this.proofHeight,
61+
this.consensusHeight
62+
];
63+
}
64+
}

ibc.ts/src/relayer/index.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { getConfig } from "../common/config";
66
import { PlatformAddress } from "codechain-primitives/lib";
77
import { UpdateClientDatagram } from "../common/datagram/updateClient";
88
import { strict as assert } from "assert";
9+
import { ConnOpenTryDatagram } from "../common/datagram/connOpenTry";
10+
import { ConnOpenAckDatagram } from "../common/datagram/connOpenAck";
11+
import { ConnOpenConfirmDatagram } from "../common/datagram/connOpenConfirm";
912

1013
require("dotenv").config();
1114

@@ -97,6 +100,21 @@ async function pendingDatagrams({
97100
})
98101
);
99102

103+
const {
104+
localDatagrams: localDatagramsForConnection,
105+
counterpartyDatagrams: counterpartyDatagramsForConnection
106+
} = await buildConnection({
107+
chain,
108+
counterpartyChain,
109+
height,
110+
counterpartyChainHeight
111+
});
112+
113+
localDatagrams = localDatagrams.concat(localDatagramsForConnection);
114+
counterpartyDatagrams = counterpartyDatagrams.concat(
115+
counterpartyDatagramsForConnection
116+
);
117+
100118
return { localDatagrams, counterpartyDatagrams };
101119
}
102120

@@ -136,3 +154,94 @@ async function updateLightClient({
136154

137155
return datagrams;
138156
}
157+
158+
async function buildConnection({
159+
chain,
160+
counterpartyChain,
161+
height,
162+
counterpartyChainHeight
163+
}: {
164+
chain: Chain;
165+
counterpartyChain: Chain;
166+
height: number;
167+
counterpartyChainHeight: number;
168+
}) {
169+
const localDatagrams: Datagram[] = [];
170+
const counterpartyDatagrams = [];
171+
const connectionIdentifiers = await chain.queryClientConnections(height);
172+
173+
assert.notEqual(connectionIdentifiers, null, "Client should be exist");
174+
for (const connectionIdentifier of connectionIdentifiers!.data || []) {
175+
const client = await chain.queryClient(height);
176+
const counterpartyClient = await counterpartyChain.queryClient(
177+
counterpartyChainHeight
178+
);
179+
180+
assert.strictEqual(
181+
connectionIdentifier,
182+
chain.counterpartyIdentifiers.connection,
183+
"PoC supports only one connection"
184+
);
185+
const connectionEnd = await chain.queryConnection(height);
186+
const counterpartyConnectionEnd = await counterpartyChain.queryConnection(
187+
counterpartyChainHeight
188+
);
189+
assert.notEqual(
190+
connectionEnd!.data,
191+
null,
192+
"Connection exists because we acquired the identifier from RPC"
193+
);
194+
if (
195+
connectionEnd!.data!.state === "INIT" &&
196+
counterpartyConnectionEnd!.data == null
197+
) {
198+
counterpartyDatagrams.push(
199+
new ConnOpenTryDatagram({
200+
desiredIdentifier:
201+
counterpartyChain.counterpartyIdentifiers.connection,
202+
counterpartyConnectionIdentifier:
203+
chain.counterpartyIdentifiers.connection,
204+
counterpartyClientIdentifier:
205+
chain.counterpartyIdentifiers.client,
206+
clientIdentifier:
207+
counterpartyChain.counterpartyIdentifiers.client,
208+
proofInit: Buffer.from(connectionEnd!.proof, "hex"),
209+
proofConsensus: Buffer.alloc(0),
210+
proofHeight: height,
211+
consensusHeight: client!.data!.number,
212+
counterpartyPrefix: ""
213+
})
214+
);
215+
} else if (
216+
connectionEnd!.data!.state === "INIT" &&
217+
counterpartyConnectionEnd!.data!.state === "TRYOPEN"
218+
) {
219+
localDatagrams.push(
220+
new ConnOpenAckDatagram({
221+
identifier: chain.counterpartyIdentifiers.connection,
222+
proofTry: Buffer.from(
223+
counterpartyConnectionEnd!.proof,
224+
"hex"
225+
),
226+
proofConsensus: Buffer.alloc(0),
227+
proofHeight: counterpartyChainHeight,
228+
consensusHeight: counterpartyClient!.data!.number
229+
})
230+
);
231+
} else if (
232+
connectionEnd!.data!.state === "OPEN" &&
233+
counterpartyConnectionEnd!.data!.state === "TRYOPEN"
234+
) {
235+
counterpartyDatagrams.push(
236+
new ConnOpenConfirmDatagram({
237+
identifier:
238+
counterpartyChain.counterpartyIdentifiers.connection,
239+
proofAck: Buffer.from(connectionEnd!.proof, "hex"),
240+
proofHeight: height
241+
})
242+
);
243+
}
244+
}
245+
246+
return { localDatagrams, counterpartyDatagrams };
247+
}

ibc.ts/src/scenario/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Chain } from "../common/chain";
44
import { PlatformAddress } from "codechain-primitives/lib";
55
import { CreateClientDatagram } from "../common/datagram/createClient";
66
import { strict as assert } from "assert";
7+
import { ConnOpenInitDatagram } from "../common/datagram/connOpenInit";
78
const { Select } = require("enquirer");
89

910
require("dotenv").config();
@@ -56,6 +57,22 @@ async function main() {
5657
console.log("Create a light client in chain B");
5758
await createLightClient({ chain: chainB, counterpartyChain: chainA });
5859
}
60+
61+
const connectionPrompt = new Select({
62+
name: "connection",
63+
message: "Will you create connection?",
64+
choices: ["yes", "skip", "exit"]
65+
});
66+
const connectionAnswer = await connectionPrompt.run();
67+
68+
if (connectionAnswer === "exit") {
69+
return;
70+
}
71+
72+
if (connectionAnswer === "yes") {
73+
console.log("Create a connection");
74+
await createConnection({ chainA, chainB });
75+
}
5976
}
6077

6178
main().catch(console.error);
@@ -103,3 +120,22 @@ async function createLightClient({
103120
assert.notEqual(clientStateAfter!.data, null, "client is initialized");
104121
debug(`Create client is ${JSON.stringify(clientStateAfter)}`);
105122
}
123+
124+
async function createConnection({
125+
chainA,
126+
chainB
127+
}: {
128+
chainA: Chain;
129+
chainB: Chain;
130+
}) {
131+
await chainA.submitDatagram(
132+
new ConnOpenInitDatagram({
133+
id: chainA.counterpartyIdentifiers.connection,
134+
desiredCounterpartyConnectionIdentifier:
135+
chainB.counterpartyIdentifiers.connection,
136+
counterpartyPrefix: "",
137+
clientIdentifier: chainA.counterpartyIdentifiers.client,
138+
counterpartyClientIdentifier: chainB.counterpartyIdentifiers.client
139+
})
140+
);
141+
}

0 commit comments

Comments
 (0)