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

Commit

Permalink
✅ Add tests for block event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ishantiw committed Mar 5, 2024
1 parent d1488cd commit b9fbad1
Show file tree
Hide file tree
Showing 5 changed files with 658 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export class BlockEventHandler {
const nodeInfo = await this._sendingChainAPIClient.getNodeInfo();

if (nodeInfo.syncing) {
this._logger.debug('No CCU generation is possible as the node is syncing.');
return;
}
let chainAccount: ChainAccount | undefined;
Expand Down Expand Up @@ -355,19 +356,22 @@ export class BlockEventHandler {
const { finalizedHeight, syncing } = await this._receivingChainAPIClient.getNodeInfo();
// If receiving node is syncing then return
if (syncing) {
this._logger.debug('Receiving chain is syncing.');
return;
}
this._receivingChainFinalizedHeight = finalizedHeight;
const { inbox } = await this._receivingChainAPIClient.getChannelAccount(this._ownChainID);
if (!inbox) {
const channelDataOnReceivingChain = await this._receivingChainAPIClient.getChannelAccount(
this._ownChainID,
);
if (!channelDataOnReceivingChain) {
throw new Error('No channel data available on receiving chain.');
}
const chainAccount = await this._receivingChainAPIClient.getChainAccount(this._ownChainID);
if (!chainAccount?.lastCertificate) {
if (!chainAccount) {
throw new Error('No chain data available on receiving chain.');
}
this._heightToDeleteIndex.set(finalizedHeight, {
inboxSize: inbox.size,
inboxSize: channelDataOnReceivingChain.inbox.size,
lastCertificateHeight: chainAccount.lastCertificate?.height,
});
if (this._lastSentCCUTxID !== '') {
Expand Down Expand Up @@ -401,7 +405,7 @@ export class BlockEventHandler {
const { list: listOfCCUs, total } = await this._db.getListOfCCUs();
if (total > this._ccuSaveLimit) {
// listOfCCUs is a descending list of CCUs by nonce
for (let i = total; i > this._ccuSaveLimit; i -= 1) {
for (let i = total - 1; i >= this._ccuSaveLimit; i -= 1) {
await this._db.deleteCCUTransaction(Buffer.from(listOfCCUs[i]?.id as string, 'hex'));
}
}
Expand Down Expand Up @@ -448,11 +452,12 @@ export class BlockEventHandler {
);

this._lastDeletionHeight = endDeletionHeightByLastCertificate;

this._logger.debug(
`Deleted data on cleanup between heights 1 and ${endDeletionHeightByLastCertificate}`,
);
}

this._logger.debug(
`Deleted data on cleanup between heights 1 and ${endDeletionHeightByLastCertificate}`,
);
// Delete info less than finalized height
this._heightToDeleteIndex.forEach((_, key) => {
if (key < this._receivingChainFinalizedHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@ export class CCUHandler {
lastSentCCM.height,
newCertificate ? newCertificate.height : this._lastCertificate.height,
);
const { inbox: inboxOnReceivingChain } = await this._receivingChainAPIClient.getChannelAccount(
const channelDataOnReceivingChain = await this._receivingChainAPIClient.getChannelAccount(
this._ownChainID,
);
const { outbox: outboxOnSendingChain } = await this._sendingChainAPIClient.getChannelAccount(
if (!channelDataOnReceivingChain) {
return undefined;
}
const channelDataOnSendingChain = await this._sendingChainAPIClient.getChannelAccount(
this._receivingChainID,
);

if (!channelDataOnSendingChain) {
return undefined;
}
const { crossChainMessages, lastCCMToBeSent, messageWitnessHashes } = calculateMessageWitnesses(
inboxOnReceivingChain.size,
outboxOnSendingChain.size,
channelDataOnReceivingChain.inbox.size,
channelDataOnSendingChain?.outbox.size,
lastSentCCM,
ccmsRange,
this._maxCCUSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,18 @@ export class ChainAPIClient {
return this._client.node.getNodeInfo();
}

public async getChannelAccount(chainID: Buffer): Promise<ChannelData> {
return channelDataJSONToObj(
await this._client.invoke<ChannelDataJSON>('interoperability_getChannel', {
public async getChannelAccount(chainID: Buffer): Promise<ChannelData | undefined> {
const channelAccount = await this._client.invoke<ChannelDataJSON>(
'interoperability_getChannel',
{
chainID: chainID.toString('hex'),
}),
},
);
if (!channelAccount || channelAccount?.inbox === undefined) {
return undefined;
}

return channelDataJSONToObj(channelAccount);
}

public async getChainAccount(chainID: Buffer): Promise<ChainAccount | undefined> {
Expand Down
Loading

0 comments on commit b9fbad1

Please sign in to comment.