-
Notifications
You must be signed in to change notification settings - Fork 458
/
submit_mainchain_cross_chain_update.ts
271 lines (240 loc) · 8.37 KB
/
submit_mainchain_cross_chain_update.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
/*
* 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 {
CommandExecuteContext,
CommandVerifyContext,
VerificationResult,
VerifyStatus,
} from '../../../../state_machine';
import { BaseCrossChainUpdateCommand } from '../../base_cross_chain_update_command';
import {
CCMStatusCode,
CONTEXT_STORE_KEY_CCM_PROCESSING,
CROSS_CHAIN_COMMAND_SIDECHAIN_TERMINATED,
EMPTY_FEE_ADDRESS,
EVENT_TOPIC_CCM_EXECUTION,
MODULE_NAME_INTEROPERABILITY,
} from '../../constants';
import {
CCMProcessedCode,
CcmProcessedEvent,
CCMProcessedResult,
} from '../../events/ccm_processed';
import { sidechainTerminatedCCMParamsSchema } from '../../schemas';
import { ChainAccount, ChainAccountStore, ChainStatus } from '../../stores/chain_account';
import {
CrossChainMessageContext,
CrossChainUpdateTransactionParams,
BeforeCCMForwardingContext,
} from '../../types';
import {
getEncodedCCMAndID,
getMainchainID,
isInboxUpdateEmpty,
verifyLivenessConditionForRegisteredChains,
getIDFromCCMBytes,
} from '../../utils';
import { MainchainInteroperabilityInternalMethod } from '../internal_method';
import { panic } from '../../../../utils/panic';
// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0053.md#commands
export class SubmitMainchainCrossChainUpdateCommand extends BaseCrossChainUpdateCommand<MainchainInteroperabilityInternalMethod> {
public async verify(
context: CommandVerifyContext<CrossChainUpdateTransactionParams>,
): Promise<VerificationResult> {
const { params } = context;
await this.verifyCommon(context, true);
// Liveness condition is only checked on the mainchain for the first CCU with a non-empty inbox update.
// after `verifyCommon`,`sendingChainAccount` is not `undefined`
const sendingChainAccount = (await this.stores
.get(ChainAccountStore)
.getOrUndefined(context, params.sendingChainID)) as ChainAccount;
if (
sendingChainAccount.status === ChainStatus.REGISTERED &&
!isInboxUpdateEmpty(params.inboxUpdate)
) {
verifyLivenessConditionForRegisteredChains(
context.header.timestamp,
context.params.certificate,
);
}
return {
status: VerifyStatus.OK,
};
}
public async execute(
context: CommandExecuteContext<CrossChainUpdateTransactionParams>,
): Promise<void> {
const { params } = context;
// This call can throw error and fails a transaction
await this.verifyCertificateSignatureAndPartnerChainOutboxRoot(context);
const [decodedCCMs, ok] = await this.beforeCrossChainMessagesExecution(context, true);
if (!ok) {
return;
}
try {
// Update the context to indicate that now we start the CCM processing.
context.contextStore.set(CONTEXT_STORE_KEY_CCM_PROCESSING, true);
for (let i = 0; i < decodedCCMs.length; i += 1) {
const ccm = decodedCCMs[i];
const ccmBytes = params.inboxUpdate.crossChainMessages[i];
const ccmID = getIDFromCCMBytes(ccmBytes);
const ccmContext = {
...context,
ccm,
eventQueue: context.eventQueue.getChildQueue(
Buffer.concat([EVENT_TOPIC_CCM_EXECUTION, ccmID]),
),
};
// If the receiving chain is the mainchain, apply the CCM
// This function never raises an error.
if (ccm.receivingChainID.equals(getMainchainID(context.chainID))) {
await this.apply(ccmContext);
} else {
await this._forward(ccmContext);
}
// We append at the very end. This implies that if the message leads to a chain termination,
// it is still possible to recover it (because the channel terminated message
// would refer to an inbox where the message has not been appended yet).
await this.internalMethod.appendToInboxTree(context, params.sendingChainID, ccmBytes);
}
} catch (error) {
panic(context.logger, error as Error);
} finally {
// Update the context to indicate that now we stop the CCM processing.
context.contextStore.delete(CONTEXT_STORE_KEY_CCM_PROCESSING);
}
await this.afterCrossChainMessagesExecute(context);
}
private async _beforeCrossChainMessageForwarding(
context: BeforeCCMForwardingContext,
eventSnapshotID: number,
stateSnapshotID: number,
): Promise<boolean> {
const { ccm, logger } = context;
const { ccmID } = getEncodedCCMAndID(ccm);
try {
for (const [module, method] of this.interoperableCCMethods.entries()) {
if (method.beforeCrossChainMessageForwarding) {
logger.debug(
{
moduleName: module,
commandName: ccm.crossChainCommand,
ccmID: ccmID.toString('hex'),
},
'Execute beforeCrossChainMessageForwarding',
);
await method.beforeCrossChainMessageForwarding(context);
}
}
} catch (error) {
context.eventQueue.restoreSnapshot(eventSnapshotID);
context.stateStore.restoreSnapshot(stateSnapshotID);
logger.info(
{ err: error as Error, moduleName: ccm.module, commandName: ccm.crossChainCommand },
'Fail to execute beforeCrossChainMessageForwarding.',
);
await this.internalMethod.terminateChainInternal(context, ccm.sendingChainID);
this.events.get(CcmProcessedEvent).log(context, ccm.sendingChainID, ccm.receivingChainID, {
code: CCMProcessedCode.INVALID_CCM_BEFORE_CCC_FORWARDING_EXCEPTION,
result: CCMProcessedResult.DISCARDED,
ccm,
});
return false;
}
return true;
}
// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0049.md#forward
private async _forward(context: CrossChainMessageContext): Promise<void> {
const { ccm } = context;
const { ccmID, encodedCCM } = getEncodedCCMAndID(ccm);
if (!(await this.verifyCCM(context, ccmID))) {
return;
}
let ccmFailed = false;
const chainAccountStore = this.stores.get(ChainAccountStore);
const receivingChainAccount = await chainAccountStore.getOrUndefined(
context,
ccm.receivingChainID,
);
// do not continue, if
// the chain account does not exist
// the chain has REGISTERED status
if (!receivingChainAccount || receivingChainAccount.status === ChainStatus.REGISTERED) {
ccmFailed = true;
} else {
// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0043.md#sidechain-registration-process
// Liveness condition is only applicable to ACTIVE status chains
const live = await this.internalMethod.isLive(
context,
ccm.receivingChainID,
context.header.timestamp,
);
if (!live) {
ccmFailed = true;
/**
* If the receiving chain is active, it means it violated the liveness condition, and we terminate it.
* If the receiving chain is already terminated, terminateChain does nothing.
*/
await this.internalMethod.terminateChainInternal(context, ccm.receivingChainID);
/**
* A sidechain terminated message is returned to the sending chain
* to inform them that the receiving chain is terminated.
*/
await this.internalMethod.sendInternal(
context,
EMPTY_FEE_ADDRESS,
MODULE_NAME_INTEROPERABILITY,
CROSS_CHAIN_COMMAND_SIDECHAIN_TERMINATED,
ccm.sendingChainID,
BigInt(0),
CCMStatusCode.OK,
codec.encode(sidechainTerminatedCCMParamsSchema, {
chainID: ccm.receivingChainID,
stateRoot: receivingChainAccount?.lastCertificate.stateRoot,
}),
);
}
}
// create a state snapshot
const stateSnapshotID = context.stateStore.createSnapshot();
const eventSnapshotID = context.eventQueue.createSnapshot();
if (
!(await this._beforeCrossChainMessageForwarding(
{ ...context, ccmFailed } as BeforeCCMForwardingContext,
eventSnapshotID,
stateSnapshotID,
))
) {
return;
}
// since the codes are same in all cases, let's use them directly here
if (ccmFailed) {
await this.bounce(
context,
encodedCCM.length,
CCMStatusCode.CHANNEL_UNAVAILABLE,
CCMProcessedCode.CHANNEL_UNAVAILABLE,
);
} else {
await this.internalMethod.addToOutbox(context, ccm.receivingChainID, ccm);
// Emit CCM forwarded event.
this.events.get(CcmProcessedEvent).log(context, ccm.sendingChainID, ccm.receivingChainID, {
code: CCMProcessedCode.SUCCESS,
result: CCMProcessedResult.FORWARDED,
ccm,
});
}
}
}