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

Missing test(s) for message recovery #9137

Merged
merged 8 commits into from
Nov 23, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { codec } from '@liskhq/lisk-codec';
import { Transaction } from '@liskhq/lisk-chain';
import { utils } from '@liskhq/lisk-cryptography';
import { MerkleTree } from '@liskhq/lisk-tree';
import { MerkleTree, regularMerkleTree } from '@liskhq/lisk-tree';
import { Proof } from '@liskhq/lisk-tree/dist-node/merkle_tree/types';
import {
CROSS_CHAIN_COMMAND_NAME_TRANSFER,
Expand Down Expand Up @@ -574,6 +574,13 @@ describe('MessageRecoveryCommand', () => {

await expect(command.execute(commandExecuteContext)).resolves.toBeUndefined();

expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
1,
CONTEXT_STORE_KEY_CCM_PROCESSING,
true,
);

const recoveredCCMs: Buffer[] = [];
for (const crossChainMessage of commandExecuteContext.params.crossChainMessages) {
const ccm = codec.decode<CCMsg>(ccmSchema, crossChainMessage);
const ctx: CrossChainMessageContext = {
Expand All @@ -584,25 +591,53 @@ describe('MessageRecoveryCommand', () => {
),
};

const recoveredCCM = await command['_applyRecovery'](ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

due to this, the expectation in line 597 will be met, even though it was not triggered from the function you are testing, right? Alternatively, you could create a copy of ctx, change the status of ccm to CCM_STATUS_CODE_RECOVERED and swap ccm.sendingChainID and ccm.receivingChainID.

recoveredCCMs.push(codec.encode(ccmSchema, recoveredCCM));

expect(command['_applyRecovery']).toHaveBeenCalledWith(ctx);
expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
1,
CONTEXT_STORE_KEY_CCM_PROCESSING,
true,
);
expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
2,
CONTEXT_STORE_KEY_CCM_PROCESSING,
false,
);
}

expect(interopModule.stores.get(TerminatedOutboxStore).set).toHaveBeenCalledTimes(1);
expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
2,
CONTEXT_STORE_KEY_CCM_PROCESSING,
false,
);

const terminatedOutboxSubstore = interopModule.stores.get(TerminatedOutboxStore);
jest.spyOn(terminatedOutboxSubstore, 'set');

expect(terminatedOutboxSubstore.set).toHaveBeenCalledTimes(1);

const terminatedOutboxAccount = await terminatedOutboxSubstore.get(
commandExecuteContext,
commandExecuteContext.params.chainID,
);
const proofLocal = {
size: terminatedOutboxAccount.outboxSize,
idxs: commandExecuteContext.params.idxs,
siblingHashes: commandExecuteContext.params.siblingHashes,
};
terminatedOutboxAccount.outboxRoot = regularMerkleTree.calculateRootFromUpdateData(
recoveredCCMs.map(ccm => utils.hash(ccm)),
{ ...proofLocal, indexes: proofLocal.idxs },
);
expect(terminatedOutboxSubstore.set).toHaveBeenCalledWith(
commandExecuteContext,
commandExecuteContext.params.chainID,
terminatedOutboxAccount,
);
});

it('should call forwardRecovery when sending chain is not mainchain', async () => {
await expect(command.execute(commandExecuteContext)).resolves.toBeUndefined();

expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
1,
CONTEXT_STORE_KEY_CCM_PROCESSING,
true,
);

const recoveredCCMs: Buffer[] = [];
for (const crossChainMessage of commandExecuteContext.params.crossChainMessages) {
const ccm = codec.decode<CCMsg>(ccmSchema, crossChainMessage);
const ctx: CrossChainMessageContext = {
Expand All @@ -613,23 +648,41 @@ describe('MessageRecoveryCommand', () => {
),
};

const recoveredCCM = await command['_forwardRecovery'](ctx);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

recoveredCCMs.push(codec.encode(ccmSchema, recoveredCCM));

expect(command['_forwardRecovery']).toHaveBeenCalledWith(ctx);
}

const terminatedOutboxStore = command['stores'].get(TerminatedOutboxStore);
jest.spyOn(terminatedOutboxStore, 'set');

expect(terminatedOutboxStore.set).toHaveBeenCalledTimes(1);
expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
1,
CONTEXT_STORE_KEY_CCM_PROCESSING,
true,
);
expect(commandExecuteContext.contextStore.set).toHaveBeenNthCalledWith(
2,
CONTEXT_STORE_KEY_CCM_PROCESSING,
false,
);

const terminatedOutboxSubstore = command['stores'].get(TerminatedOutboxStore);
const terminatedOutboxAccount = await terminatedOutboxSubstore.get(
commandExecuteContext,
commandExecuteContext.params.chainID,
);
jest.spyOn(terminatedOutboxSubstore, 'set');

expect(terminatedOutboxSubstore.set).toHaveBeenCalledTimes(1);

const proofLocal = {
size: terminatedOutboxAccount.outboxSize,
idxs: commandExecuteContext.params.idxs,
siblingHashes: commandExecuteContext.params.siblingHashes,
};
terminatedOutboxAccount.outboxRoot = regularMerkleTree.calculateRootFromUpdateData(
recoveredCCMs.map(ccm => utils.hash(ccm)),
{ ...proofLocal, indexes: proofLocal.idxs },
);
expect(terminatedOutboxSubstore.set).toHaveBeenCalledWith(
commandExecuteContext,
commandExecuteContext.params.chainID,
terminatedOutboxAccount,
);
});
});

Expand Down