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

Commit

Permalink
Update Channel Terminated Message cross chain command (#7696)
Browse files Browse the repository at this point in the history
### What was the problem?

This PR resolves #7656

### How was it solved?

Created a new `BaseCCChannelTerminatedCommand` inside `base/cc_commands`

### How was it tested?

Unit Test Updated accordingly
  • Loading branch information
ishantiw authored Nov 7, 2022
2 parents 0ca0709 + 8da2be3 commit 1cf249d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { BaseInteroperabilityCCCommand } from '../base_interoperability_cc_commands';
import { channelTerminatedCCMParamsSchema } from '../schemas';
import { CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED } from '../constants';
import { CCCommandExecuteContext } from '../types';

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

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

public async execute(context: CCCommandExecuteContext): Promise<void> {
if (!context.ccm) {
throw new Error(
`CCM to execute cross chain command '${CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED}' is missing.`,
);
}
const interoperabilityInternalMethod = this.getInteroperabilityInternalMethod(context);
if (await interoperabilityInternalMethod.isLive(context.ccm.sendingChainID, Date.now())) {
await interoperabilityInternalMethod.createTerminatedStateAccount(
context,
context.ccm.sendingChainID,
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,11 @@
*/

import { StoreGetter } from '../../../base_store';
import { BaseInteroperabilityCCCommand } from '../../base_interoperability_cc_commands';
import { CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED } from '../../constants';
import { channelTerminatedCCMParamsSchema } from '../../schemas';
import { CCCommandExecuteContext } from '../../types';
import { MainchainInteroperabilityInternalMethod } from '../store';
import { BaseCCChannelTerminatedCommand } from '../../base_cc_commands/channel_terminated';

export class MainchainCCChannelTerminatedCommand extends BaseInteroperabilityCCCommand {
public schema = channelTerminatedCCMParamsSchema;

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

public async execute(context: CCCommandExecuteContext): Promise<void> {
const interoperabilityInternalMethod = this.getInteroperabilityInternalMethod(context);
if (!context.ccm) {
throw new Error('CCM to execute channel terminated cross chain command is missing.');
}
await interoperabilityInternalMethod.createTerminatedStateAccount(
context,
context.ccm.sendingChainID,
);
}

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0049.md#channel-terminated-message-1
export class MainchainCCChannelTerminatedCommand extends BaseCCChannelTerminatedCommand {
protected getInteroperabilityInternalMethod(
context: StoreGetter,
): MainchainInteroperabilityInternalMethod {
Expand Down
2 changes: 2 additions & 0 deletions framework/src/modules/interoperability/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,11 @@ 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: {},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,14 @@
* Removal or modification of this copyright notice is prohibited.
*/

import { ImmutableStoreGetter, StoreGetter } from '../../../base_store';
import { BaseInteroperabilityCCCommand } from '../../base_interoperability_cc_commands';
import { CROSS_CHAIN_COMMAND_NAME_CHANNEL_TERMINATED } from '../../constants';
import { channelTerminatedCCMParamsSchema } from '../../schemas';
import { CCCommandExecuteContext } from '../../types';
import { StoreGetter } from '../../../base_store';
import { SidechainInteroperabilityInternalMethod } from '../store';
import { BaseCCChannelTerminatedCommand } from '../../base_cc_commands/channel_terminated';

export class SidechainCCChannelTerminatedCommand extends BaseInteroperabilityCCCommand {
public schema = channelTerminatedCCMParamsSchema;

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

public async execute(context: CCCommandExecuteContext): Promise<void> {
const interoperabilityInternalMethod = this.getInteroperabilityInternalMethod(context);
if (!context.ccm) {
throw new Error('CCM to execute channel terminated cross chain command is missing.');
}
await interoperabilityInternalMethod.createTerminatedStateAccount(
context,
context.ccm.sendingChainID,
);
}

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0049.md#channel-terminated-message-1
export class SidechainCCChannelTerminatedCommand extends BaseCCChannelTerminatedCommand {
protected getInteroperabilityInternalMethod(
context: StoreGetter | ImmutableStoreGetter,
context: StoreGetter,
): SidechainInteroperabilityInternalMethod {
return new SidechainInteroperabilityInternalMethod(
this.stores,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
*/

import { utils } from '@liskhq/lisk-cryptography';
import { MainchainInteroperabilityModule } from '../../../../../../src';
import { MainchainInteroperabilityModule } from '../../../../../src';
import {
CROSS_CHAIN_COMMAND_NAME_REGISTRATION,
EMPTY_BYTES,
MODULE_NAME_INTEROPERABILITY,
} from '../../../../../../src/modules/interoperability/constants';
import { MainchainCCChannelTerminatedCommand } from '../../../../../../src/modules/interoperability/mainchain/cc_commands/channel_terminated';
import { MainchainInteroperabilityInternalMethod } from '../../../../../../src/modules/interoperability/mainchain/store';
import { CCCommandExecuteContext } from '../../../../../../src/modules/interoperability/types';
import { NamedRegistry } from '../../../../../../src/modules/named_registry';
import { createExecuteCCMsgMethodContext } from '../../../../../../src/testing';
} from '../../../../../src/modules/interoperability/constants';
import { MainchainCCChannelTerminatedCommand } from '../../../../../src/modules/interoperability/mainchain/cc_commands';
import { MainchainInteroperabilityInternalMethod } from '../../../../../src/modules/interoperability/mainchain/store';
import { CCCommandExecuteContext } from '../../../../../src/modules/interoperability/types';
import { NamedRegistry } from '../../../../../src/modules/named_registry';
import { createExecuteCCMsgMethodContext } from '../../../../../src/testing';

describe('MainchainCCChannelTerminatedCommand', () => {
describe('BaseCCChannelTerminatedCommand', () => {
const interopMod = new MainchainInteroperabilityModule();
const createTerminatedStateAccountMock = jest.fn();

Expand All @@ -48,9 +49,10 @@ describe('MainchainCCChannelTerminatedCommand', () => {
receivingChainID: utils.intToBuffer(3, 4),
fee: BigInt(20000),
status: 0,
params: Buffer.alloc(0),
params: EMPTY_BYTES,
};
const sampleExecuteContext: CCCommandExecuteContext = createExecuteCCMsgMethodContext({
ccm,
chainID,
});

Expand All @@ -71,9 +73,17 @@ describe('MainchainCCChannelTerminatedCommand', () => {
] = jest.fn().mockReturnValue(mainchainInteroperabilityInternalMethod);

describe('execute', () => {
it('should call validators Method registerValidatorKeys', async () => {
it('should skip if isLive is false ', async () => {
mainchainInteroperabilityInternalMethod.isLive = jest.fn().mockResolvedValue(false);

await ccChannelTerminatedCommand.execute(sampleExecuteContext);
expect(createTerminatedStateAccountMock).toHaveBeenCalledTimes(0);
});

it('should call createTerminatedStateAccount if isLive', async () => {
mainchainInteroperabilityInternalMethod.isLive = jest.fn().mockResolvedValue(true);

await ccChannelTerminatedCommand.execute(sampleExecuteContext);
expect(createTerminatedStateAccountMock).toHaveBeenCalledWith(
sampleExecuteContext,
ccm.sendingChainID,
Expand Down

This file was deleted.

0 comments on commit 1cf249d

Please sign in to comment.