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

Unnecessary computing of message witness when no new certificate and last certified height #8893

Closed
Tracked by #7226
mitsuaki-u opened this issue Aug 23, 2023 · 0 comments

Comments

@mitsuaki-u
Copy link
Contributor

Expected behavior

In chain connector plugin => newBlockHandler => computeCCUParams method, when no new certificate and last certified height === 0, there is an early return.

Actual behavior

Currently on every new block received we execute the following logic to calculate message witness:

const lastSentCCM = (await this._chainConnectorStore.getLastSentCCM()) ?? {
			nonce: DEFAULT_LAST_CCM_SENT_NONCE,
			height: this._lastCertificate.height,
		};

		let activeValidatorsUpdate: ActiveValidatorsUpdate = {
			blsKeysUpdate: [],
			bftWeightsUpdate: [],
			bftWeightsUpdateBitmap: EMPTY_BYTES,
		};
		let certificate = EMPTY_BYTES;
		let certificateThreshold;
		let outboxRootWitness;

		// Take range from lastSentCCM height until new or last certificate height
		const ccmsToBeIncluded = ccmsFromEvents.filter(
			record =>
				record.height >= lastSentCCM.height &&
				// If no newCertificate then use lastCertificate height
				record.height <= (newCertificate ? newCertificate.height : this._lastCertificate.height),
		);
		// Calculate messageWitnessHashes for pending CCMs if any
		const channelDataOnReceivingChain = await this._receivingChainClient.invoke<ChannelDataJSON>(
			'interoperability_getChannel',
			{ chainID: this._ownChainID.toString('hex') },
		);
		if (!channelDataOnReceivingChain?.inbox) {
			this.logger.info('Receiving chain is not registered yet on the sending chain.');
			return;
		}
		const inboxSizeOnReceivingChain = channelDataJSONToObj(channelDataOnReceivingChain).inbox.size;

		const receivingChainChannelDataJSON = await this._sendingChainClient.invoke<ChannelDataJSON>(
			'interoperability_getChannel',
			{ chainID: this._receivingChainID.toString('hex') },
		);

		if (!receivingChainChannelDataJSON?.outbox) {
			this.logger.info('Sending chain is not registered yet on the receiving chain.');
			return;
		}
		const outboxSizeOnSendingChain = channelDataJSONToObj(receivingChainChannelDataJSON).outbox
			.size;
		const messageWitnessHashesForCCMs = calculateMessageWitnesses(
			inboxSizeOnReceivingChain,
			outboxSizeOnSendingChain,
			lastSentCCM,
			ccmsToBeIncluded,
			this._maxCCUSize,
		);

However, we can avoid unnecessarily executing this logic upon every new bloc received when there is no new certificate and last certified height === 0 since we have this information earlier on in the logic.

Steps to reproduce

Check chain connector newBlockHandler logic

Which version(s) does this affect? (Environment, OS, etc...)

v6.0

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants