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

Commit

Permalink
♻️ Remove duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed Oct 31, 2023
1 parent 0c2b1b0 commit 8688ff3
Showing 1 changed file with 19 additions and 129 deletions.
148 changes: 19 additions & 129 deletions framework/test/unit/modules/interoperability/mainchain/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,25 @@ describe('initGenesisState', () => {
});
});

describe('terminatedStateAccounts', () => {
it('should check that _verifyChainValidators is called from _verifyChainInfos', async () => {
jest.spyOn(interopMod, '_verifyChainValidators' as any);

await expect(
interopMod.initGenesisState(contextWithValidValidatorsHash),
).resolves.toBeUndefined();

// must be true to pass this test
expect(interopMod['_verifyChainValidators']).toHaveBeenCalled();
});

it(`should call _verifyTerminatedStateAccounts from initGenesisState`, async () => {
jest.spyOn(interopMod, '_verifyTerminatedStateAccounts' as any);

await interopMod.initGenesisState(contextWithValidValidatorsHash);
expect(interopMod['_verifyTerminatedStateAccounts']).toHaveBeenCalledTimes(1);
});

describe('_verifyTerminatedStateAccounts', () => {
it('should not throw error if length of terminatedStateAccounts is zero', async () => {
const context = createInitGenesisStateContext(
{
Expand Down Expand Up @@ -611,27 +629,7 @@ describe('initGenesisState', () => {
'stateAccount is not initialized.',
);
});
});

it('should check that _verifyChainValidators is called from _verifyChainInfos', async () => {
jest.spyOn(interopMod, '_verifyChainValidators' as any);

await expect(
interopMod.initGenesisState(contextWithValidValidatorsHash),
).resolves.toBeUndefined();

// must be true to pass this test
expect(interopMod['_verifyChainValidators']).toHaveBeenCalled();
});

it(`should call _verifyTerminatedStateAccounts from initGenesisState`, async () => {
jest.spyOn(interopMod, '_verifyTerminatedStateAccounts' as any);

await interopMod.initGenesisState(contextWithValidValidatorsHash);
expect(interopMod['_verifyTerminatedStateAccounts']).toHaveBeenCalledTimes(1);
});

describe('_verifyTerminatedStateAccounts', () => {
it("should not throw error if length of terminatedStateAccounts is zero while there doesn't exist some chain in chainData with status TERMINATED", async () => {
certificateThreshold = BigInt(10);
const context = createInitGenesisStateContext(
Expand Down Expand Up @@ -735,42 +733,6 @@ describe('initGenesisState', () => {
);
});

it('should throw error if chainID in terminatedStateAccounts does not exist in chainInfo', async () => {
const context = createInitGenesisStateContext(
{
...genesisInteroperability,
// this is needed to verify `validatorsHash` related tests (above)
chainInfos: [
{
...chainInfo,
chainData: {
...chainData,
lastCertificate: {
...lastCertificate,
validatorsHash: computeValidatorsHash(activeValidators, certificateThreshold),
},
},
chainValidators: {
activeValidators,
certificateThreshold,
},
},
],
terminatedStateAccounts: [
{
chainID: Buffer.from([0, 0, 0, 2]),
terminatedStateAccount,
},
],
},
params,
);

await expect(interopMod.initGenesisState(context)).rejects.toThrow(
'For each terminatedStateAccount there should be a corresponding chainInfo at TERMINATED state',
);
});

it('should throw if chainInfo.chainData.status === TERMINATED but no corresponding terminateStateAccount', async () => {
const context = createInitGenesisStateContext(
{
Expand Down Expand Up @@ -839,78 +801,6 @@ describe('initGenesisState', () => {
await expect(interopMod.initGenesisState(context)).resolves.toBeUndefined();
expect(interopMod['_verifyTerminatedStateAccounts']).toHaveBeenCalledTimes(1);
});

it('should throw error if some stateAccount in terminatedStateAccounts have mainchainStateRoot not equal to EMPTY_HASH', async () => {
const context = createInitGenesisStateContext(
{
...genesisInteroperability,
// this is needed to verify `validatorsHash` related tests (above)
chainInfos: validChainInfos,
terminatedStateAccounts: [
{
chainID: Buffer.from([0, 0, 0, 1]),
terminatedStateAccount: {
...terminatedStateAccount,
mainchainStateRoot: Buffer.from(utils.getRandomBytes(HASH_LENGTH)), // ***
},
},
],
},
params,
);

await expect(interopMod.initGenesisState(context)).rejects.toThrow(
`stateAccount.mainchainStateRoot is not equal to ${EMPTY_HASH.toString('hex')}.`,
);
});

it('should throw error if some stateAccount in terminatedStateAccounts is not initialized', async () => {
const context = createInitGenesisStateContext(
{
...genesisInteroperability,
// this is needed to verify `validatorsHash` related tests (above)
chainInfos: validChainInfos,
terminatedStateAccounts: [
{
chainID: Buffer.from([0, 0, 0, 1]),
terminatedStateAccount: {
...terminatedStateAccount,
initialized: false, // ***
},
},
],
},
params,
);

await expect(interopMod.initGenesisState(context)).rejects.toThrow(
'stateAccount is not initialized.',
);
});

it('should throw error if some stateAccount in terminatedStateAccounts have stateRoot not equal to chainData.lastCertificate.stateRoot', async () => {
const context = createInitGenesisStateContext(
{
...genesisInteroperability,
// this is needed to verify `validatorsHash` related tests (above)
chainInfos: validChainInfos,
terminatedStateAccounts: [
{
chainID: Buffer.from([0, 0, 0, 1]),
terminatedStateAccount: {
...terminatedStateAccount,
stateRoot: Buffer.from(utils.getRandomBytes(HASH_LENGTH)), // ***
},
},
],
},
params,
);

await expect(interopMod.initGenesisState(context)).rejects.toThrow(
"stateAccount.stateRoot doesn't match chainInfo.chainData.lastCertificate.stateRoot.",
);
});
});

it(`should call _verifyTerminatedOutboxAccounts from initGenesisState `, async () => {
Expand Down

0 comments on commit 8688ff3

Please sign in to comment.