-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.ts
397 lines (374 loc) · 18.9 KB
/
bridge.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import { BridgeInfo } from "./configure";
import { Chain, ChainManager } from "./chain";
import {
LnBridgeContract,
LnDefaultBridgeContract,
LnOppositeBridgeContract,
Lnv3BridgeContract,
} from "./contract";
import { Log } from "./log";
import { Messager } from "./messager";
export class BridgeEndpoint {
public chain: Chain;
public bridgeContract: LnBridgeContract;
public daoOnChain: string;
public operatorOnChain: string;
constructor(address: string, type: string, chain: Chain) {
this.chain = chain;
this.daoOnChain = "";
this.operatorOnChain = "";
if (type === 'default') {
this.bridgeContract = new LnDefaultBridgeContract(address, chain.wallet);
} else if (type === 'opposite') {
this.bridgeContract = new LnOppositeBridgeContract(address, chain.wallet);
} else { // lnv3
this.bridgeContract = new Lnv3BridgeContract(address, chain.wallet);
}
}
get address(): string {
return this.bridgeContract.address;
}
async checkDao(): Promise<boolean> {
if (!this.daoOnChain) {
this.daoOnChain = await this.bridgeContract.dao();
}
return this.daoOnChain === this.chain.dao;
}
async checkOperator(): Promise<boolean> {
if (!this.operatorOnChain) {
this.operatorOnChain = await this.bridgeContract.operator();
}
return this.operatorOnChain === this.chain.operator;
}
async checkProxyAdmin(): Promise<boolean> {
return await this.chain.checkProxyAdmin(this.address);
}
async checkTokenInfo(symbol: string, remoteEndpoint: BridgeEndpoint): Promise<boolean> {
const srcToken = this.chain.tokens.get(symbol);
const dstToken = remoteEndpoint.chain.tokens.get(symbol);
if (srcToken === undefined || dstToken === undefined) {
return false;
}
return await this.bridgeContract.tokenRegistered(
remoteEndpoint.chain.id,
srcToken!.address,
dstToken!.address,
srcToken!.decimals,
dstToken!.decimals
);
}
}
export class Bridge {
public name: string;
public type: string;
public messager: string;
public sourceBridge: BridgeEndpoint;
public targetBridge: BridgeEndpoint;
public symbols: string[] | undefined;
constructor(bridgeInfo: BridgeInfo, fromEndpoint: BridgeEndpoint, toEndpoint: BridgeEndpoint) {
this.name = bridgeInfo.name;
this.type = bridgeInfo.type;
this.messager = bridgeInfo.messager;
this.sourceBridge = fromEndpoint;
this.targetBridge = toEndpoint;
this.symbols = bridgeInfo.tokens;
}
getSourceMessagerFromConfigure(): Messager | undefined {
return this.sourceBridge.chain.messagers.get(this.messager);
}
getTargetMessagerFromConfigure(): Messager | undefined {
return this.targetBridge.chain.messagers.get(this.messager);
}
async getSourceMessagerFromChain(): Promise<string> {
const remoteChainId = this.targetBridge.chain.id;
const messagerService = await this.sourceBridge.bridgeContract.messager(remoteChainId);
return (this.type === 'default' ? messagerService.sendService : messagerService.receiveService).toLowerCase();
}
async getTargetMessagerFromChain(): Promise<string> {
const remoteChainId = this.sourceBridge.chain.id;
const messagerService = await this.targetBridge.bridgeContract.messager(remoteChainId);
return (this.type === 'default' ? messagerService.receiveService : messagerService.sendService).toLowerCase();
}
async checkSourceConnectTarget(): Promise<boolean> {
const srcMessager = this.getSourceMessagerFromConfigure();
const dstMessager = this.getTargetMessagerFromConfigure();
if (srcMessager === undefined || dstMessager === undefined) {
return false;
}
return await srcMessager!.isConnected(this.targetBridge.chain, dstMessager!.address());
}
async checkTargetConnectSource(): Promise<boolean> {
const srcMessager = this.getSourceMessagerFromConfigure();
const dstMessager = this.getTargetMessagerFromConfigure();
if (srcMessager === undefined || dstMessager === undefined) {
return false;
}
return await dstMessager!.isConnected(this.sourceBridge.chain, srcMessager!.address());
}
async checkSourceAppConnectTarget(): Promise<boolean> {
const srcMessager = this.getSourceMessagerFromConfigure();
const dstMessager = this.getTargetMessagerFromConfigure();
if (srcMessager === undefined || dstMessager === undefined) {
return false;
}
if (this.type === 'default') {
return await srcMessager!.remoteAppIsReceiver(this.targetBridge.chain.id, this.sourceBridge.address, this.targetBridge.address);
} else {
return await srcMessager!.remoteAppIsSender(this.targetBridge.chain.id, this.sourceBridge.address, this.targetBridge.address);
}
}
async checkTargetAppConnectSource(): Promise<boolean> {
const srcMessager = this.getSourceMessagerFromConfigure();
const dstMessager = this.getTargetMessagerFromConfigure();
if (srcMessager === undefined || dstMessager === undefined) {
return false;
}
if (this.type === 'default') {
return await dstMessager!.remoteAppIsSender(this.sourceBridge.chain.id, this.targetBridge.address, this.sourceBridge.address);
} else {
return await dstMessager!.remoteAppIsReceiver(this.sourceBridge.chain.id, this.targetBridge.address, this.sourceBridge.address);
}
}
}
// for lnv3Bridge: all endpoint should be connected for each other
export class BridgeManager {
public log: Log;
public bridges: Map<string, Bridge> = new Map<string, Bridge>();
public v3bridges: Map<string, Bridge> = new Map<string, Bridge>();
// address + chain = key
public bridgeEndpointCache: Map<string, BridgeEndpoint> = new Map<string, BridgeEndpoint>();
constructor(bridgeInfos: BridgeInfo[], v3BridgeInfos: BridgeInfo[], chainManager: ChainManager) {
this.log = new Log();
for (const bridgeInfo of bridgeInfos) {
const fromChain = chainManager.chains.get(bridgeInfo.from);
const toChain = chainManager.chains.get(bridgeInfo.to);
if (fromChain === undefined || toChain === undefined) {
this.log.error(`[${bridgeInfo.name}] bridge's chain not configured`);
continue;
}
const fromEndpoint = this.newBridgeEndpoint(bridgeInfo.fromAddress, bridgeInfo.type, fromChain);
const toEndpoint = this.newBridgeEndpoint(bridgeInfo.toAddress, bridgeInfo.type, toChain);
this.bridges.set(bridgeInfo.name, new Bridge(bridgeInfo, fromEndpoint, toEndpoint));
}
for (const bridgeInfo of v3BridgeInfos) {
const fromChain = chainManager.chains.get(bridgeInfo.from);
const toChain = chainManager.chains.get(bridgeInfo.to);
if (fromChain === undefined || toChain === undefined) {
this.log.error(`[${bridgeInfo.name}] bridge's chain not configured`);
continue;
}
const fromEndpoint = this.newBridgeEndpoint(bridgeInfo.fromAddress, bridgeInfo.type, fromChain);
const toEndpoint = this.newBridgeEndpoint(bridgeInfo.toAddress, bridgeInfo.type, toChain);
this.v3bridges.set(bridgeInfo.name, new Bridge(bridgeInfo, fromEndpoint, toEndpoint));
}
}
newBridgeEndpoint(address: string, type: string, chain: Chain): BridgeEndpoint {
const key = chain.name + ":" + address;
let endpoint = this.bridgeEndpointCache.get(key);
if (!endpoint) {
endpoint = new BridgeEndpoint(address, type, chain);
this.bridgeEndpointCache.set(key, endpoint);
}
return endpoint;
}
// check dao
async checkBridgeDao(version: string): Promise<void> {
const bridges = version === 'v2' ? Array.from(this.bridges.values()) : Array.from(this.v3bridges.values());
const count = this.bridges.size * 2;
var index = 1;
await this.log.progress2("waiting for check bridge dao", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[Math.floor(index / 2)];
draw(index++, count, `[${bridge.name}]check source bridge: ${bridge.sourceBridge.address} dao: ${bridge.sourceBridge.chain.dao}`, '');
let result = await bridge.sourceBridge.checkDao();
if (!result) {
draw(index, count, '', `[${bridge.name}]source bridge's dao invalid`);
return false;
}
draw(index++, count, `[${bridge.name}]check target bridge ${bridge.targetBridge.address} dao: ${bridge.targetBridge.chain.dao}`, '');
result = await bridge.targetBridge.checkDao();
if (!result) {
draw(index, count, '', `[${bridge.name}]target bridge's dao invalid`);
return false;
}
return index < count;
});
}
// check operator
async checkBridgeOperator(): Promise<void> {
const bridges = Array.from(this.bridges.values());
const count = this.bridges.size * 2;
var index = 1;
await this.log.progress2("waiting for check bridge operator", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[Math.floor(index / 2)];
draw(index++, count, `[${bridge.name}]check source bridge: ${bridge.sourceBridge.address} operator: ${bridge.sourceBridge.chain.operator}`, '');
let result = await bridge.sourceBridge.checkOperator();
if (!result) {
draw(index, count, '', `[${bridge.name}]source bridge's operator invalid`);
return false;
}
draw(index++, count, `[${bridge.name}]check target bridge: ${bridge.targetBridge.address} operator: ${bridge.targetBridge.chain.operator}`, '');
result = await bridge.targetBridge.checkOperator();
if (!result) {
draw(index, count, '', `[${bridge.name}]target bridge's operator invalid`);
return false;
}
return index < count;
});
}
// check proxy admin
async checkProxyAdmin(version: string): Promise<void> {
const bridges = version === 'v2' ? Array.from(this.bridges.values()) : Array.from(this.v3bridges.values());
const count = this.bridges.size * 2;
var index = 1;
await this.log.progress2("waiting for check bridge's proxyAdmin", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[Math.floor(index / 2)];
draw(index++, count, `[${bridge.name}]check source bridge:${bridge.sourceBridge.address} proxyAdmin: ${bridge.sourceBridge.chain.proxyAdmin.address}`, '');
let result = await bridge.sourceBridge.checkProxyAdmin();
if (!result) {
draw(index, count, '', `[${bridge.name}]source bridge's proxyAmin invalid`);
return false;
}
draw(index++, count, `[${bridge.name}]check target bridge:${bridge.targetBridge.address} proxyAdmin: ${bridge.targetBridge.chain.proxyAdmin.address}`, '');
result = await bridge.targetBridge.checkProxyAdmin();
if (!result) {
draw(index, count, '', `[${bridge.name}]target bridge's proxyAmin invalid`);
return false;
}
return index < count;
});
}
// check messager service
async checkMessagerService(): Promise<void> {
const bridges = Array.from(this.bridges.values());
const countEachTime = 6;
const count = this.bridges.size * countEachTime;
var index = 1;
await this.log.progress2("waiting for check bridge's message service", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[Math.floor(index / countEachTime)];
// check
draw(index++, count, `[${bridge.name}]check source messager setting`, '');
const sourceMessagerAddress = await bridge.getSourceMessagerFromChain();
const sourceMessager = bridge.getSourceMessagerFromConfigure();
if (sourceMessagerAddress !== sourceMessager?.address()) {
draw(index, count, 'check source messager', `[${bridge.name}]source address not matched, ${sourceMessagerAddress} != ${sourceMessager?.address()}`);
return false;
}
draw(index++, count, `[${bridge.name}]check target messager setting`, '');
const targetMessagerAddress = await bridge.getTargetMessagerFromChain();
const targetMessager = bridge.getTargetMessagerFromConfigure();
if (targetMessagerAddress !== targetMessager?.address()) {
draw(index, count, 'check target messager', `[${bridge.name}]target address not matched, ${targetMessagerAddress} != ${targetMessager?.address()}`);
return false;
}
draw(index++, count, `[${bridge.name}]check messager connection source->target`, '');
const srcConnected = await bridge.checkSourceConnectTarget();
if (!srcConnected) {
draw(index, count, 'check connect source->target', `[${bridge.name}]source not connected`);
return false;
}
draw(index++, count, `[${bridge.name}]check messager connection target->source`, '');
const dstConnected = await bridge.checkTargetConnectSource();
if (!dstConnected) {
draw(index, count, 'check connect target->source', `[${bridge.name}]target not connected`);
return false;
}
// check app connection
draw(index++, count, `[${bridge.name}]check app connection source->target`, '');
const srcAppConnected = await bridge.checkSourceAppConnectTarget();
if (!srcAppConnected) {
draw(index, count, 'check app connect source->target', `[${bridge.name}]source app not connected`);
return false;
}
draw(index++, count, `[${bridge.name}]check app connection target->source`, '');
const dstAppConnected = await bridge.checkTargetAppConnectSource();
if (!dstAppConnected) {
draw(index, count, 'check app connect target->source', `[${bridge.name}]target app not connected`);
return false;
}
return index < count;
});
}
// check messager service
async checkv3MessagerService(): Promise<void> {
const bridges = Array.from(this.v3bridges.values());
const countEachTime = 6;
const count = this.v3bridges.size * countEachTime;
var index = 1;
await this.log.progress2("waiting for check bridge's message service", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[Math.floor(index / countEachTime)];
// check
draw(index++, count, `[${bridge.name}]check source messager setting`, '');
const sourceMessagerAddress = await bridge.getSourceMessagerFromChain();
const sourceMessager = bridge.getSourceMessagerFromConfigure();
if (sourceMessagerAddress !== sourceMessager?.address()) {
draw(index, count, 'check source messager', `[${bridge.name}]source address not matched, ${sourceMessagerAddress} != ${sourceMessager?.address()}`);
return false;
}
draw(index++, count, `[${bridge.name}]check target messager setting`, '');
const targetMessagerAddress = await bridge.getTargetMessagerFromChain();
const targetMessager = bridge.getTargetMessagerFromConfigure();
if (targetMessagerAddress !== targetMessager?.address()) {
draw(index, count, 'check target messager', `[${bridge.name}]target address not matched, ${targetMessagerAddress} != ${targetMessager?.address()}`);
return false;
}
draw(index++, count, `[${bridge.name}]check messager connection source->target`, '');
const srcConnected = await bridge.checkSourceConnectTarget();
if (!srcConnected) {
draw(index, count, 'check connect source->target', `[${bridge.name}]source not connected`);
return false;
}
draw(index++, count, `[${bridge.name}]check messager connection target->source`, '');
const dstConnected = await bridge.checkTargetConnectSource();
if (!dstConnected) {
draw(index, count, 'check connect target->source', `[${bridge.name}]target not connected`);
return false;
}
// check app connection
draw(index++, count, `[${bridge.name}]check app connection source->target`, '');
const srcAppConnected = await bridge.checkSourceAppConnectTarget();
if (!srcAppConnected) {
draw(index, count, 'check app connect source->target', `[${bridge.name}]source app not connected`);
return false;
}
draw(index++, count, `[${bridge.name}]check app connection target->source`, '');
const dstAppConnected = await bridge.checkTargetAppConnectSource();
if (!dstAppConnected) {
draw(index, count, 'check app connect target->source', `[${bridge.name}]target app not connected`);
return false;
}
return index < count;
});
}
// check token register info
// get all token symbols to check
async checkv3TokenRegisterInfos(): Promise<void> {
const bridges = Array.from(this.v3bridges.values());
const count = bridges.reduce((sum, current) => sum + current.symbols!.length, 0);
var index = 1;
var bridgeIndex = 0;
await this.log.progress2("waiting for check bridge's token register info", async (
draw: (current: number, total: number, msg: string, err: string)=>void
)=>{
const bridge = bridges[bridgeIndex++];
for (const symbol of bridge.symbols!) {
draw(index++, count, `[${bridge.name}]check app bridge token ${symbol} register info`, '');
const tokenRegistered = await bridge.sourceBridge.checkTokenInfo(symbol, bridge.targetBridge);
if (!tokenRegistered) {
draw(index, count, 'check app bridge token', `[${bridge.name}-${symbol}] register invalid`);
return false;
}
}
return index < count;
});
}
}