Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Update sidechain terminated cross chain command (#7779)
Browse files Browse the repository at this point in the history
### What was the problem?

This PR resolves #7658 

### How was it solved?

- Update CCCommand execute to use `CCCommandExecuteContext`
- Update CCCommand to be able to have no schema
- Remove Sidechain terminated CCCommand from mainchain
- Update Sidechain terminated CCCommand on sidechain

### How was it tested?

- Add unit test to cover the cases
  • Loading branch information
shuse2 authored Nov 17, 2022
1 parent 44900d3 commit 8cf4f6e
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 321 deletions.
10 changes: 5 additions & 5 deletions framework/src/modules/interoperability/base_cc_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@

import { Schema } from '@liskhq/lisk-codec';
import { NamedRegistry } from '../named_registry';
import { CrossChainMessageContext, ImmutableCrossChainMessageContext } from './types';
import { CCCommandExecuteContext, ImmutableCrossChainMessageContext } from './types';

export abstract class BaseCCCommand {
public abstract schema: Schema;
export abstract class BaseCCCommand<T = unknown> {
public schema?: Schema;

public get name(): string {
const name = this.constructor.name.replace('CCCommand', '');
return name.charAt(0).toLowerCase() + name.substr(1);
return name.charAt(0).toLowerCase() + name.substring(1);
}

// eslint-disable-next-line no-useless-constructor
public constructor(protected stores: NamedRegistry, protected events: NamedRegistry) {}
public verify?(ctx: ImmutableCrossChainMessageContext): Promise<void>;
public abstract execute(ctx: CrossChainMessageContext): Promise<void>;
public abstract execute(ctx: CCCommandExecuteContext<T>): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
*/

import { BaseInteroperabilityCCCommand } from '../base_interoperability_cc_commands';
import { channelTerminatedCCMParamsSchema } from '../schemas';
import { CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED } from '../constants';
import { CrossChainMessageContext } from '../types';
import { CCCommandExecuteContext } from '../types';
import { BaseInteroperabilityInternalMethod } from '../base_interoperability_internal_methods';

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0049.md#channel-terminated-message-1
export abstract class BaseCCChannelTerminatedCommand<
T extends BaseInteroperabilityInternalMethod
> extends BaseInteroperabilityCCCommand<T> {
public schema = channelTerminatedCCMParamsSchema;

public get name(): string {
return CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED;
}

public async execute(context: CrossChainMessageContext): Promise<void> {
public async execute(context: CCCommandExecuteContext<void>): Promise<void> {
if (!context.ccm) {
throw new Error(
`CCM to execute cross chain command '${CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED}' is missing.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ export abstract class BaseCrossChainUpdateCommand<
const execStateSnapshotID = context.stateStore.createSnapshot();

try {
await command.execute(context);
const params = command.schema ? codec.decode(command.schema, context.ccm.params) : {};
await command.execute({ ...context, params });
this.events.get(CcmProcessedEvent).log(context, ccm.sendingChainID, ccm.receivingChainID, {
ccmID,
code: CCMProcessedCode.SUCCESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,3 @@

export { MainchainCCChannelTerminatedCommand } from './channel_terminated';
export { MainchainCCRegistrationCommand } from './registration';
export { MainchainCCSidechainTerminatedCommand } from './sidechain_terminated';

This file was deleted.

12 changes: 4 additions & 8 deletions framework/src/modules/interoperability/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,6 @@ export const registrationCCMParamsSchema = {
},
};

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0049.md#parameters-1
export const channelTerminatedCCMParamsSchema = {
$id: '/modules/interoperability/ccCommand/channelTerminated',
type: 'object',
required: [],
properties: {},
};

export const sidechainTerminatedCCMParamsSchema = {
$id: '/modules/interoperability/ccCommand/sidechainTerminated',
type: 'object',
Expand All @@ -352,10 +344,14 @@ export const sidechainTerminatedCCMParamsSchema = {
chainID: {
dataType: 'bytes',
fieldNumber: 1,
minLength: CHAIN_ID_LENGTH,
maxLength: CHAIN_ID_LENGTH,
},
stateRoot: {
dataType: 'bytes',
fieldNumber: 2,
minLength: HASH_LENGTH,
maxLength: HASH_LENGTH,
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { codec } from '@liskhq/lisk-codec';
import { BaseInteroperabilityCCCommand } from '../../base_interoperability_cc_commands';
import { CROSS_CHAIN_COMMAND_NAME_SIDECHAIN_TERMINATED, MAINCHAIN_ID } from '../../constants';
import { CCMStatusCode, CROSS_CHAIN_COMMAND_NAME_SIDECHAIN_TERMINATED } from '../../constants';
import { sidechainTerminatedCCMParamsSchema } from '../../schemas';
import { TerminatedStateStore } from '../../stores/terminated_state';
import { CrossChainMessageContext } from '../../types';
import { getIDAsKeyForStore } from '../../utils';
import { CCCommandExecuteContext, ImmutableCrossChainMessageContext } from '../../types';
import { getMainchainID } from '../../utils';
import { SidechainInteroperabilityInternalMethod } from '../internal_method';

interface CCMSidechainTerminatedParams {
Expand All @@ -33,31 +31,27 @@ export class SidechainCCSidechainTerminatedCommand extends BaseInteroperabilityC
return CROSS_CHAIN_COMMAND_NAME_SIDECHAIN_TERMINATED;
}

public async execute(context: CrossChainMessageContext): Promise<void> {
const { ccm } = context;
if (!ccm) {
throw new Error('CCM to execute sidechain terminated cross chain command is missing.');
// eslint-disable-next-line @typescript-eslint/require-await
public async verify(ctx: ImmutableCrossChainMessageContext): Promise<void> {
if (ctx.ccm.status !== CCMStatusCode.OK) {
throw new Error('Sidechain terminated message must have status OK.');
}
const ccmSidechainTerminatedParams = codec.decode<CCMSidechainTerminatedParams>(
sidechainTerminatedCCMParamsSchema,
ccm.params,
);

if (ccm.sendingChainID.equals(getIDAsKeyForStore(MAINCHAIN_ID))) {
const isTerminated = await this.stores
.get(TerminatedStateStore)
.get(context, ccmSidechainTerminatedParams.chainID);
if (!ctx.ccm.sendingChainID.equals(getMainchainID(ctx.chainID))) {
throw new Error('Sidechain terminated message must be sent from the mainchain.');
}
}

if (isTerminated) {
return;
}
await this.internalMethods.createTerminatedStateAccount(
context,
ccmSidechainTerminatedParams.chainID,
ccmSidechainTerminatedParams.stateRoot,
);
} else {
await this.internalMethods.terminateChainInternal(context, ccm.sendingChainID);
public async execute(
context: CCCommandExecuteContext<CCMSidechainTerminatedParams>,
): Promise<void> {
const isLive = await this.internalMethods.isLive(context, context.params.chainID);
if (!isLive) {
return;
}
await this.internalMethods.createTerminatedStateAccount(
context,
context.params.chainID,
context.params.stateRoot,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
MODULE_NAME_INTEROPERABILITY,
} from '../../../../../src/modules/interoperability/constants';
import { MainchainCCChannelTerminatedCommand } from '../../../../../src/modules/interoperability/mainchain/cc_commands';
import { CrossChainMessageContext } from '../../../../../src/modules/interoperability/types';
import { createCrossChainMessageContext } from '../../../../../src/testing';

describe('BaseCCChannelTerminatedCommand', () => {
Expand Down Expand Up @@ -49,10 +48,13 @@ describe('BaseCCChannelTerminatedCommand', () => {
status: 0,
params: EMPTY_BYTES,
};
const sampleExecuteContext: CrossChainMessageContext = createCrossChainMessageContext({
ccm,
chainID,
});
const sampleExecuteContext = {
...createCrossChainMessageContext({
ccm,
chainID,
}),
params: undefined,
};

const ccChannelTerminatedCommand = new MainchainCCChannelTerminatedCommand(
interopMod.stores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('BaseCrossChainUpdateCommand', () => {
ccCommands = new Map();
ccCommands.set('token', [
new (class CrossChainTransfer extends BaseCCCommand {
public schema = { $id: 'test/ccu', properties: {}, type: 'object' };
public verify = jest.fn();
public execute = jest.fn();
})(interopModule.stores, interopModule.events),
Expand Down

This file was deleted.

Loading

0 comments on commit 8cf4f6e

Please sign in to comment.