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

Commit

Permalink
Fix terminate chain method (#8740)
Browse files Browse the repository at this point in the history
* Check if terminatedState exists instead of getting terminatedStateAccount

* πŸ’…πŸ» Add comment and remove unnecessary mock

---------

Co-authored-by: Ishan <[email protected]>
  • Loading branch information
Phanco and ishantiw authored Jul 21, 2023
1 parent e816bf8 commit 4faa22c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export abstract class BaseInteroperabilityMethod<

// https://github.com/LiskHQ/lips/blob/main/proposals/lip-0045.md#terminatechain
public async terminateChain(context: MethodContext, chainID: Buffer): Promise<void> {
if (await this.getTerminatedStateAccount(context, chainID)) {
// Chain was already terminated, do nothing.
if (await this.stores.get(TerminatedStateStore).has(context, chainID)) {
return;
}

Expand Down
24 changes: 2 additions & 22 deletions framework/test/unit/modules/interoperability/method.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import { MainchainInteroperabilityModule, TokenMethod, ChannelData } from '../..
import { BaseInteroperabilityMethod } from '../../../../src/modules/interoperability/base_interoperability_method';
import {
CCMStatusCode,
CHAIN_ID_LENGTH,
CROSS_CHAIN_COMMAND_CHANNEL_TERMINATED,
EMPTY_BYTES,
EMPTY_FEE_ADDRESS,
HASH_LENGTH,
MAX_CCM_SIZE,
MODULE_NAME_INTEROPERABILITY,
MAX_RESERVED_ERROR_STATUS,
Expand Down Expand Up @@ -534,38 +532,20 @@ describe('Sample Method', () => {
});

describe('terminateChain', () => {
const sidechainChainAccount = {
name: 'sidechain1',
chainID: Buffer.alloc(CHAIN_ID_LENGTH),
lastCertificate: {
height: 10,
stateRoot: utils.getRandomBytes(32),
timestamp: 100,
validatorsHash: utils.getRandomBytes(32),
},
status: ChainStatus.TERMINATED,
};

beforeEach(() => {
interopMod['internalMethod'].sendInternal = jest.fn();
interopMod['internalMethod'].createTerminatedStateAccount = jest.fn();
});

it('should do nothing if chain was already terminated', async () => {
jest.spyOn(sampleInteroperabilityMethod, 'getTerminatedStateAccount').mockResolvedValue({
stateRoot: sidechainChainAccount.lastCertificate.stateRoot,
mainchainStateRoot: Buffer.alloc(HASH_LENGTH),
initialized: true,
});
jest.spyOn(terminatedStateAccountStoreMock, 'has').mockResolvedValue(true);
await sampleInteroperabilityMethod.terminateChain(methodContext, chainID);

expect(interopMod['internalMethod'].sendInternal).not.toHaveBeenCalled();
});

it('should process with input chainID', async () => {
jest
.spyOn(sampleInteroperabilityMethod as any, 'getTerminatedStateAccount')
.mockResolvedValue(undefined);
jest.spyOn(terminatedStateAccountStoreMock, 'has').mockResolvedValue(false);

await sampleInteroperabilityMethod.terminateChain(methodContext, chainID);

Expand Down

0 comments on commit 4faa22c

Please sign in to comment.