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

Commit

Permalink
Add verifyLivenessConditionForRegisteredChains (#7771)
Browse files Browse the repository at this point in the history
### What was the problem?

This PR resolves #7686 

### How was it solved?

- Add `verifyLivenessConditionForRegisteredChains` in utils for
interoperability according to LIP

### How was it tested?

- Add unit tests
  • Loading branch information
shuse2 authored Nov 15, 2022
1 parent f01c673 commit 28a78ac
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
16 changes: 16 additions & 0 deletions framework/src/modules/interoperability/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,22 @@ export const chainAccountToJSON = (chainAccount: ChainAccount) => {
};
};

export const verifyLivenessConditionForRegisteredChains = (
ccu: CrossChainUpdateTransactionParams,
blockTimestamp: number,
) => {
if (ccu.certificate.length === 0 || isInboxUpdateEmpty(ccu.inboxUpdate)) {
return;
}
const certificate = codec.decode<Certificate>(certificateSchema, ccu.certificate);
const limitSecond = LIVENESS_LIMIT / 2;
if (blockTimestamp - certificate.timestamp > limitSecond) {
throw new Error(
`The first CCU with a non-empty inbox update cannot contain a certificate older than ${limitSecond} seconds.`,
);
}
};

export const getMainchainID = (chainID: Buffer): Buffer => {
const networkID = chainID.slice(0, 1);
// 3 bytes for remaining chainID bytes
Expand Down
80 changes: 80 additions & 0 deletions framework/test/unit/modules/interoperability/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
getIDAsKeyForStore,
initGenesisStateUtil,
validateFormat,
verifyLivenessConditionForRegisteredChains,
} from '../../../../src/modules/interoperability/utils';
import { certificateSchema } from '../../../../src/engine/consensus/certificate_generation/schema';
import { Certificate } from '../../../../src/engine/consensus/certificate_generation/types';
Expand Down Expand Up @@ -1927,4 +1928,83 @@ describe('Utils', () => {
expect(() => validateFormat(buildCCM({}))).not.toThrow();
});
});

describe('verifyLivenessConditionForRegisteredChains', () => {
const certificate = {
blockID: utils.getRandomBytes(20),
height: 23,
stateRoot: Buffer.alloc(2),
timestamp: 100000,
validatorsHash: utils.getRandomBytes(20),
aggregationBits: utils.getRandomBytes(1),
signature: utils.getRandomBytes(32),
};
const ccuParams = {
activeValidatorsUpdate: [],
certificate: codec.encode(certificateSchema, certificate),
inboxUpdate: {
crossChainMessages: [utils.getRandomBytes(100)],
messageWitnessHashes: [],
outboxRootWitness: {
bitmap: Buffer.alloc(0),
siblingHashes: [],
},
},
newCertificateThreshold: BigInt(99),
sendingChainID: utils.getRandomBytes(4),
};

it('should not throw if certificate is empty', () => {
expect(
verifyLivenessConditionForRegisteredChains(
{
...ccuParams,
certificate: Buffer.alloc(0),
},
10000,
),
).toBeUndefined();
});

it('should not throw if inbox update is empty', () => {
expect(
verifyLivenessConditionForRegisteredChains(
{
...ccuParams,
inboxUpdate: {
crossChainMessages: [],
messageWitnessHashes: [],
outboxRootWitness: {
bitmap: Buffer.alloc(0),
siblingHashes: [],
},
},
},
10000,
),
).toBeUndefined();
});

it('should throw if certificate timestamp is older than half of liveness limit', () => {
expect(() =>
verifyLivenessConditionForRegisteredChains(
{
...ccuParams,
},
certificate.timestamp + LIVENESS_LIMIT / 2 + 1,
),
).toThrow('The first CCU with a non-empty inbox update cannot contain a certificate older');
});

it('should not throw if inbox update is not older than half of liveness limit', () => {
expect(
verifyLivenessConditionForRegisteredChains(
{
...ccuParams,
},
certificate.timestamp + LIVENESS_LIMIT / 2,
),
).toBeUndefined();
});
});
});

0 comments on commit 28a78ac

Please sign in to comment.