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

Fix message recovery logic and tests #8778

Merged
merged 1 commit into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,8 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
idxs,
siblingHashes,
};

// Convert each CCM byte to sha256 hash
const hashedCCMs = crossChainMessages.map(ccm => utils.hash(ccm));
const isVerified = regularMerkleTree.verifyDataBlock(
hashedCCMs,
crossChainMessages,
proof,
terminatedOutboxAccount.outboxRoot,
);
Expand Down Expand Up @@ -186,20 +183,20 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
ccm,
eventQueue: context.eventQueue.getChildQueue(ccmID),
};
let recoveredCCM: CCMsg;
// If the sending chain is the mainchain, recover the CCM.
// This function never raises an error.
if (ccm.sendingChainID.equals(getMainchainID(context.chainID))) {
await this._applyRecovery(ctx);
recoveredCCM = await this._applyRecovery(ctx);
} else {
// If the sending chain is not the mainchain, forward the CCM.
// This function never raises an error.
await this._forwardRecovery(ctx);

// Append the recovered CCM to the list of recovered CCMs.
// Notice that the ccm has been mutated in the applyRecovery and forwardRecovery functions
// as the status is set to CCM_STATUS_CODE_RECOVERED (so that it cannot be recovered again).
recoveredCCMs.push(crossChainMessage);
recoveredCCM = await this._forwardRecovery(ctx);
shuse2 marked this conversation as resolved.
Show resolved Hide resolved
}
// Append the recovered CCM to the list of recovered CCMs.
// Notice that the ccm has been updated in the applyRecovery and forwardRecovery functions
// as the status is set to CCM_STATUS_CODE_RECOVERED (so that it cannot be recovered again).
recoveredCCMs.push(codec.encode(ccmSchema, recoveredCCM));
}

const terminatedOutboxSubstore = this.stores.get(TerminatedOutboxStore);
Expand All @@ -224,7 +221,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
private async _applyRecovery(context: CrossChainMessageContext): Promise<void> {
private async _applyRecovery(context: CrossChainMessageContext): Promise<CCMsg> {
const { logger } = context;
const { ccmID } = getEncodedCCMAndID(context.ccm);
const recoveredCCM: CCMsg = {
Expand All @@ -233,7 +230,6 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
sendingChainID: context.ccm.receivingChainID,
receivingChainID: context.ccm.sendingChainID,
};

try {
for (const [module, method] of this.interoperableCCMethods.entries()) {
if (method.verifyCrossChainMessage) {
Expand All @@ -256,7 +252,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}

const commands = this.ccCommands.get(recoveredCCM.module);
Expand All @@ -268,7 +264,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}
const command = commands.find(com => com.name === recoveredCCM.crossChainCommand);
if (!command) {
Expand All @@ -279,7 +275,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}
if (command.verify) {
try {
Expand All @@ -300,7 +296,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}
}
const baseEventSnapshotID = context.eventQueue.createSnapshot();
Expand Down Expand Up @@ -338,7 +334,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}

const execEventSnapshotID = context.eventQueue.createSnapshot();
Expand Down Expand Up @@ -395,10 +391,12 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
ccm: recoveredCCM,
});
}

return recoveredCCM;
}

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
private async _forwardRecovery(context: CrossChainMessageContext): Promise<void> {
private async _forwardRecovery(context: CrossChainMessageContext): Promise<CCMsg> {
const { logger } = context;
const { ccmID } = getEncodedCCMAndID(context.ccm);
const recoveredCCM: CCMsg = {
Expand Down Expand Up @@ -438,7 +436,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
},
'Fail to execute verifyCrossChainMessage.',
);
return;
return recoveredCCM;
}
const baseEventSnapshotID = context.eventQueue.createSnapshot();
const baseStateSnapshotID = context.stateStore.createSnapshot();
Expand Down Expand Up @@ -475,7 +473,7 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.DISCARDED,
ccm: recoveredCCM,
});
return;
return recoveredCCM;
}

await this.internalMethod.addToOutbox(context, recoveredCCM.receivingChainID, recoveredCCM);
Expand All @@ -487,5 +485,6 @@ export class RecoverMessageCommand extends BaseInteroperabilityCommand<Mainchain
result: CCMProcessedResult.FORWARDED,
ccm: recoveredCCM,
});
return recoveredCCM;
}
}
Loading