/
module.ts
379 lines (357 loc) · 12.9 KB
/
module.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
/*
* Copyright © 2022 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*/
import { codec } from '@liskhq/lisk-codec';
import { validator } from '@liskhq/lisk-validator';
import { ModuleInitArgs, ModuleMetadata } from '../../base_module';
import { BaseInteroperabilityModule } from '../base_interoperability_module';
import { SidechainInteroperabilityMethod } from './method';
import { SidechainCCMethod } from './cc_method';
import { RegisterMainchainCommand } from './commands/register_mainchain';
import { SidechainInteroperabilityEndpoint } from './endpoint';
import {
genesisInteroperabilitySchema,
getChainAccountRequestSchema,
getChainValidatorsRequestSchema,
getChainValidatorsResponseSchema,
getChannelRequestSchema,
getTerminatedOutboxAccountRequestSchema,
getTerminatedStateAccountRequestSchema,
getMainchainIDRequestSchema,
getMainchainIDResponseSchema,
getCCMSchemaResponseSchema,
} from '../schemas';
import { allChainAccountsSchema, chainDataSchema, ChainStatus } from '../stores/chain_account';
import { channelSchema } from '../stores/channel_data';
import { ownChainAccountSchema } from '../stores/own_chain_account';
import { terminatedStateSchema } from '../stores/terminated_state';
import { terminatedOutboxSchema } from '../stores/terminated_outbox';
import { BaseCCMethod } from '../base_cc_method';
import {
GenesisInteroperability,
TerminatedStateAccountWithChainID,
ValidatorsMethod,
} from '../types';
import { SidechainInteroperabilityInternalMethod } from './internal_method';
import { SubmitSidechainCrossChainUpdateCommand } from './commands';
import { InitializeStateRecoveryCommand } from './commands/initialize_state_recovery';
import { RecoverStateCommand } from './commands/recover_state';
import { SidechainCCChannelTerminatedCommand, SidechainCCRegistrationCommand } from './cc_commands';
import { GenesisBlockExecuteContext } from '../../../state_machine';
import {
CHAIN_NAME_MAINCHAIN,
EMPTY_HASH,
MAX_CHAIN_NAME_LENGTH,
MIN_CHAIN_NAME_LENGTH,
MODULE_NAME_INTEROPERABILITY,
} from '../constants';
import { getMainchainID, isValidName } from '../utils';
import { TokenMethod } from '../../token';
import { InvalidNameError } from '../errors';
export class SidechainInteroperabilityModule extends BaseInteroperabilityModule {
public crossChainMethod: BaseCCMethod = new SidechainCCMethod(this.stores, this.events);
protected internalMethod = new SidechainInteroperabilityInternalMethod(
this.stores,
this.events,
this.interoperableCCMethods,
);
// eslint-disable-next-line @typescript-eslint/member-ordering
public method = new SidechainInteroperabilityMethod(
this.stores,
this.events,
this.interoperableCCMethods,
this.internalMethod,
);
// eslint-disable-next-line @typescript-eslint/member-ordering
public endpoint = new SidechainInteroperabilityEndpoint(this.stores, this.offchainStores);
private readonly _mainchainRegistrationCommand = new RegisterMainchainCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.interoperableCCCommands,
this.internalMethod,
);
private readonly _crossChainUpdateCommand = new SubmitSidechainCrossChainUpdateCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.interoperableCCCommands,
this.internalMethod,
);
private readonly _stateRecoveryInitCommand = new InitializeStateRecoveryCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.interoperableCCCommands,
this.internalMethod,
);
private readonly _stateRecoveryCommand = new RecoverStateCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.interoperableCCCommands,
this.internalMethod,
);
// eslint-disable-next-line @typescript-eslint/member-ordering
public commands = [
this._mainchainRegistrationCommand,
this._crossChainUpdateCommand,
this._stateRecoveryInitCommand,
this._stateRecoveryCommand,
];
// eslint-disable-next-line @typescript-eslint/member-ordering
public crossChainCommand = [
new SidechainCCRegistrationCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.internalMethod,
),
new SidechainCCChannelTerminatedCommand(
this.stores,
this.events,
this.interoperableCCMethods,
this.internalMethod,
),
];
private _validatorsMethod!: ValidatorsMethod;
public addDependencies(validatorsMethod: ValidatorsMethod, tokenMethod: TokenMethod) {
this._validatorsMethod = validatorsMethod;
this._crossChainUpdateCommand.init(this.method, tokenMethod);
this.internalMethod.addDependencies(tokenMethod);
this.tokenMethod = tokenMethod;
}
public metadata(): ModuleMetadata {
return {
...this.baseMetadata(),
endpoints: [
{
name: this.endpoint.getChainAccount.name,
request: getChainAccountRequestSchema,
response: chainDataSchema,
},
{
name: this.endpoint.getAllChainAccounts.name,
request: getChainAccountRequestSchema,
response: allChainAccountsSchema,
},
{
name: this.endpoint.getChannel.name,
request: getChannelRequestSchema,
response: channelSchema,
},
{
name: this.endpoint.getOwnChainAccount.name,
response: ownChainAccountSchema,
},
{
name: this.endpoint.getTerminatedStateAccount.name,
request: getTerminatedStateAccountRequestSchema,
response: terminatedStateSchema,
},
{
name: this.endpoint.getTerminatedOutboxAccount.name,
request: getTerminatedOutboxAccountRequestSchema,
response: terminatedOutboxSchema,
},
{
name: this.endpoint.getChainValidators.name,
request: getChainValidatorsRequestSchema,
response: getChainValidatorsResponseSchema,
},
{
name: this.endpoint.getMainchainID.name,
request: getMainchainIDRequestSchema,
response: getMainchainIDResponseSchema,
},
{
name: this.endpoint.getCCMSchema.name,
response: getCCMSchemaResponseSchema,
},
],
assets: [
{
version: 0,
data: genesisInteroperabilitySchema,
},
],
stores: this.stores.values().map(v => ({
key: v.key.toString('hex'),
data: v.schema,
})),
};
}
// eslint-disable-next-line @typescript-eslint/require-await
public async init(_args: ModuleInitArgs) {
this._mainchainRegistrationCommand.addDependencies(this._validatorsMethod);
}
// @see https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#mainchain
// eslint-disable-next-line @typescript-eslint/require-await
public async initGenesisState(ctx: GenesisBlockExecuteContext): Promise<void> {
const genesisBlockAssetBytes = ctx.assets.getAsset(MODULE_NAME_INTEROPERABILITY);
if (!genesisBlockAssetBytes) {
return;
}
const genesisInteroperability = codec.decode<GenesisInteroperability>(
genesisInteroperabilitySchema,
genesisBlockAssetBytes,
);
validator.validate<GenesisInteroperability>(
genesisInteroperabilitySchema,
genesisInteroperability,
);
this._verifyChainInfos(ctx, genesisInteroperability);
const { terminatedStateAccounts, terminatedOutboxAccounts } = genesisInteroperability;
this._verifyTerminatedStateAccounts(ctx, terminatedStateAccounts, getMainchainID(ctx.chainID));
// terminatedOutboxAccounts
if (terminatedOutboxAccounts.length !== 0) {
throw new Error('terminatedOutboxAccounts must be empty.');
}
await super.processGenesisState(ctx, genesisInteroperability);
}
private _verifyChainInfos(
ctx: GenesisBlockExecuteContext,
genesisInteroperability: GenesisInteroperability,
) {
const {
ownChainName,
ownChainNonce,
chainInfos,
terminatedStateAccounts,
terminatedOutboxAccounts,
} = genesisInteroperability;
// If chainInfos is empty, then check that:
//
// ownChainName is the empty string;
// ownChainNonce == 0;
// terminatedStateAccounts is empty;
// terminatedOutboxAccounts is empty.
if (chainInfos.length === 0) {
const ifChainInfosIsEmpty = 'if chainInfos is empty.';
if (ownChainName !== '') {
throw new Error(`ownChainName must be empty string, ${ifChainInfosIsEmpty}.`);
}
if (ownChainNonce !== BigInt(0)) {
throw new Error(`ownChainNonce must be 0, ${ifChainInfosIsEmpty}.`);
}
if (terminatedStateAccounts.length !== 0) {
throw new Error(`terminatedStateAccounts must be empty, ${ifChainInfosIsEmpty}.`);
}
if (terminatedOutboxAccounts.length !== 0) {
throw new Error(`terminatedOutboxAccounts must be empty, ${ifChainInfosIsEmpty}.`);
}
} else {
// ownChainName
// has length between MIN_CHAIN_NAME_LENGTH and MAX_CHAIN_NAME_LENGTH,
// is from the character set a-z0-9!@$&_.,
// and ownChainName != CHAIN_NAME_MAINCHAIN;
if (
ownChainName.length < MIN_CHAIN_NAME_LENGTH ||
ownChainName.length > MAX_CHAIN_NAME_LENGTH // will only run if not already applied in schema
) {
throw new Error(
`ownChainName.length must be inclusively between ${MIN_CHAIN_NAME_LENGTH} and ${MAX_CHAIN_NAME_LENGTH}.`,
);
}
// CAUTION!
// this check is intentionally applied after MIN_CHAIN_NAME_LENGTH, as it will fail for empty string
if (!isValidName(ownChainName)) {
throw new InvalidNameError('ownChainName');
}
if (ownChainName === CHAIN_NAME_MAINCHAIN) {
throw new Error(`ownChainName must be not equal to ${CHAIN_NAME_MAINCHAIN}.`);
}
// ownChainNonce > 0
if (ownChainNonce <= 0) {
throw new Error('ownChainNonce must be > 0.');
}
// chainInfos contains exactly one entry mainchainInfo with:
if (chainInfos.length !== 1) {
throw new Error('chainInfos must contain exactly one entry.');
}
// mainchainInfo.chainID == getMainchainID();
const mainchainInfo = chainInfos[0];
const mainchainID = getMainchainID(ctx.chainID);
if (!mainchainInfo.chainID.equals(mainchainID)) {
throw new Error(`mainchainInfo.chainID must be equal to ${mainchainID.toString('hex')}.`);
}
// mainchainInfo.chainData.name == CHAIN_NAME_MAINCHAIN,
// mainchainInfo.chainData.status is either equal to CHAIN_STATUS_REGISTERED or to CHAIN_STATUS_ACTIVE,
// mainchainInfo.chainData.lastCertificate.timestamp < g.header.timestamp;
if (mainchainInfo.chainData.name !== CHAIN_NAME_MAINCHAIN) {
throw new Error(`chainData.name must be equal to ${CHAIN_NAME_MAINCHAIN}.`);
}
const validStatuses = [ChainStatus.REGISTERED, ChainStatus.ACTIVE];
if (!validStatuses.includes(mainchainInfo.chainData.status)) {
throw new Error(`chainData.status must be one of ${validStatuses.join(', ')}.`);
}
if (mainchainInfo.chainData.lastCertificate.timestamp >= ctx.header.timestamp) {
throw new Error('chainData.lastCertificate.timestamp must be < header.timestamp.');
}
// channelData
this._verifyChannelData(ctx, mainchainInfo);
// activeValidators
this._verifyChainValidators(mainchainInfo);
}
}
private _verifyTerminatedStateAccounts(
ctx: GenesisBlockExecuteContext,
terminatedStateAccounts: TerminatedStateAccountWithChainID[],
mainchainID: Buffer,
) {
this._verifyTerminatedStateAccountsIDs(terminatedStateAccounts.map(a => a.chainID));
for (const stateAccount of terminatedStateAccounts) {
this._verifyChainID(stateAccount.chainID, mainchainID, 'stateAccount.');
// and stateAccount.chainID != OWN_CHAIN_ID.
if (stateAccount.chainID.equals(ctx.chainID)) {
throw new Error(`stateAccount.chainID must not be equal to OWN_CHAIN_ID.`);
}
// For each entry stateAccount in terminatedStateAccounts either:
// stateAccount.stateRoot != EMPTY_HASH, stateAccount.mainchainStateRoot == EMPTY_HASH, and stateAccount.initialized == True;
// or stateAccount.stateRoot == EMPTY_HASH, stateAccount.mainchainStateRoot != EMPTY_HASH, and stateAccount.initialized == False.
const { terminatedStateAccount } = stateAccount;
if (terminatedStateAccount.initialized) {
if (terminatedStateAccount.stateRoot.equals(EMPTY_HASH)) {
throw new Error(
`stateAccount.stateRoot must not be equal to "${EMPTY_HASH.toString(
'hex',
)}", if initialized is true.`,
);
}
if (!terminatedStateAccount.mainchainStateRoot.equals(EMPTY_HASH)) {
throw new Error(
`terminatedStateAccount.mainchainStateRoot must be equal to "${EMPTY_HASH.toString(
'hex',
)}", if initialized is true`,
);
}
} else {
// initialized is false
if (!terminatedStateAccount.stateRoot.equals(EMPTY_HASH)) {
throw new Error(
`stateAccount.stateRoot mst be equal to "${EMPTY_HASH.toString(
'hex',
)}", if initialized is false.`,
);
}
if (terminatedStateAccount.mainchainStateRoot.equals(EMPTY_HASH)) {
throw new Error(
`terminatedStateAccount.mainchainStateRoot must not be equal to "${EMPTY_HASH.toString(
'hex',
)}", if initialized is false.`,
);
}
}
}
}
}