Skip to content

Commit 9616fc9

Browse files
authored
fix: common parent (#1332)
1 parent 3ffe199 commit 9616fc9

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

contracts/contracts/lib/SubnetIDHelper.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,16 @@ library SubnetIDHelper {
9797

9898
/// @notice Computes the common parent of the current subnet and the one given as argument
9999
function commonParent(SubnetID calldata subnet1, SubnetID calldata subnet2) public pure returns (SubnetID memory) {
100+
// If the roots are not identical, exit early
100101
if (subnet1.root != subnet2.root) {
101102
return SubnetID({root: 0, route: new address[](0)});
102103
}
103104

105+
// If both subnet addresses references the same subnet, return early
106+
if (equals(subnet1, subnet2)) {
107+
return subnet1;
108+
}
109+
104110
uint256 i;
105111
uint256 subnet1routeLength = subnet1.route.length;
106112
uint256 subnet2routeLength = subnet2.route.length;
@@ -113,6 +119,8 @@ library SubnetIDHelper {
113119
return SubnetID({root: subnet1.root, route: new address[](0)});
114120
}
115121

122+
// note: only works for subnets that differ at least in the leaf element,
123+
// since `i + 1 === min(subnet1routeLength, subnet2routeLength)`
116124
address[] memory route = new address[](i);
117125
for (uint256 j; j < i; ) {
118126
route[j] = subnet1.route[j];

0 commit comments

Comments
 (0)