Omniscia Gnosis Guild Audit

Integrity Code Style Findings

Integrity Code Style Findings

IYT-01C: Inefficient Re-Calculation of Base Type Tree

TypeSeverityLocation
Gas OptimizationIntegrity.sol:L266, L267, L281, L291-L293

Description:

The Integrity::_compatibleSiblingTypes function will inefficiently re-calculate the Topology::typeTree of the start exactly end - start - 2 times.

Example:

packages/evm/contracts/Integrity.sol
256function _compatibleSiblingTypes(
257 ConditionFlat[] memory conditions,
258 uint256 index,
259 Topology.Bounds[] memory childrenBounds
260) private pure {
261 uint256 start = childrenBounds[index].start;
262 uint256 end = childrenBounds[index].end;
263
264 for (uint256 j = start + 1; j < end; ++j) {
265 if (
266 !_isTypeMatch(conditions, start, j, childrenBounds) &&
267 !_isTypeEquivalent(conditions, start, j, childrenBounds)
268 ) {
269 revert UnsuitableChildTypeTree(index);
270 }
271 }
272}
273
274function _isTypeMatch(
275 ConditionFlat[] memory conditions,
276 uint256 i,
277 uint256 j,
278 Topology.Bounds[] memory childrenBounds
279) private pure returns (bool) {
280 return
281 typeTreeId(Topology.typeTree(conditions, i, childrenBounds)) ==
282 typeTreeId(Topology.typeTree(conditions, j, childrenBounds));
283}
284
285function _isTypeEquivalent(
286 ConditionFlat[] memory conditions,
287 uint256 i,
288 uint256 j,
289 Topology.Bounds[] memory childrenBounds
290) private pure returns (bool) {
291 ParameterType leftParamType = Topology
292 .typeTree(conditions, i, childrenBounds)
293 .paramType;
294 return
295 (leftParamType == ParameterType.Calldata ||
296 leftParamType == ParameterType.AbiEncoded) &&
297 Topology.typeTree(conditions, j, childrenBounds).paramType ==
298 ParameterType.Dynamic;
299}

Recommendation:

We advise the Topology::typeTree result of the start to be calculated outside the for loop and passed in on each iteration, significantly reducing the function's execution cost.

Alleviation (e6d315f9170dcf4c622d504bd2fb6eafbdac9b75):

The Gnosis Guild team evaluated this exhibit but opted not to apply its optimization as it relates to a configurational function and not a run-time gas cost.

IYT-02C: Inefficient Re-Calculation of Iterated Type Tree

TypeSeverityLocation
Gas OptimizationIntegrity.sol:L266, L267

Description:

The Integrity::_compatibleSiblingTypes function will inefficiently re-calculate the Topology::typeTree of the j iterator twice on each for loop execution.

Example:

packages/evm/contracts/Integrity.sol
256function _compatibleSiblingTypes(
257 ConditionFlat[] memory conditions,
258 uint256 index,
259 Topology.Bounds[] memory childrenBounds
260) private pure {
261 uint256 start = childrenBounds[index].start;
262 uint256 end = childrenBounds[index].end;
263
264 for (uint256 j = start + 1; j < end; ++j) {
265 if (
266 !_isTypeMatch(conditions, start, j, childrenBounds) &&
267 !_isTypeEquivalent(conditions, start, j, childrenBounds)
268 ) {
269 revert UnsuitableChildTypeTree(index);
270 }
271 }
272}
273
274function _isTypeMatch(
275 ConditionFlat[] memory conditions,
276 uint256 i,
277 uint256 j,
278 Topology.Bounds[] memory childrenBounds
279) private pure returns (bool) {
280 return
281 typeTreeId(Topology.typeTree(conditions, i, childrenBounds)) ==
282 typeTreeId(Topology.typeTree(conditions, j, childrenBounds));
283}
284
285function _isTypeEquivalent(
286 ConditionFlat[] memory conditions,
287 uint256 i,
288 uint256 j,
289 Topology.Bounds[] memory childrenBounds
290) private pure returns (bool) {
291 ParameterType leftParamType = Topology
292 .typeTree(conditions, i, childrenBounds)
293 .paramType;
294 return
295 (leftParamType == ParameterType.Calldata ||
296 leftParamType == ParameterType.AbiEncoded) &&
297 Topology.typeTree(conditions, j, childrenBounds).paramType ==
298 ParameterType.Dynamic;
299}

Recommendation:

We advise the code to calculate the Topology::typeTree of the j iterator once and to pass it in to the Integrity::_isTypeMatch and Integrity::_isTypeEquivalent functions, optimizing the gas cost of the code significantly.

Alleviation (e6d315f9170dcf4c622d504bd2fb6eafbdac9b75):

The Gnosis Guild team evaluated this exhibit but opted not to apply its optimization as it relates to a configurational function and not a run-time gas cost.