Skip to content

Commit 45cb98a

Browse files
committed
chore: use separate functions to compute slots
1 parent 0307c37 commit 45cb98a

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

contracts/l2/discovery/L2GNS.sol

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -163,23 +163,7 @@ contract L2GNS is GNS {
163163

164164
require(l1GNSAccount.exists, "!ACCOUNT");
165165

166-
// subgraphs mapping is stored at slot SUBGRAPH_MAPPING_SLOT.
167-
// So our subgraph is at slot keccak256(abi.encodePacked(uint256(subgraphID), uint256(SUBGRAPH_MAPPING_SLOT)))
168-
// The curatorNSignal mapping is at slot 2 within the SubgraphData struct,
169-
// So the mapping is at slot keccak256(abi.encodePacked(uint256(subgraphID), uint256(SUBGRAPH_MAPPING_SLOT))) + 2
170-
// Therefore the nSignal value for msg.sender should be at slot:
171-
uint256 curatorSlot = uint256(
172-
keccak256(
173-
abi.encodePacked(
174-
uint256(msg.sender),
175-
uint256(
176-
keccak256(
177-
abi.encodePacked(uint256(_subgraphID), uint256(SUBGRAPH_MAPPING_SLOT))
178-
)
179-
).add(2)
180-
)
181-
)
182-
);
166+
uint256 curatorSlot = _getCuratorSlot(msg.sender, _subgraphID);
183167

184168
Verifier.SlotValue memory curatorNSignalSlot = Verifier.extractSlotValueFromProof(
185169
keccak256(abi.encodePacked(curatorSlot)),
@@ -234,27 +218,7 @@ contract L2GNS is GNS {
234218

235219
require(l1GNSAccount.exists, "!ACCOUNT");
236220

237-
uint256 curatorSlot;
238-
{
239-
// legacy subgraphs mapping is stored at slot LEGACY_SUBGRAPH_MAPPING_SLOT.
240-
// So the subgraphs for the account are at slot keccak256(abi.encodePacked(uint256(_subgraphCreatorAccount), uint256(SUBGRAPH_MAPPING_SLOT)))
241-
uint256 accountSlot = uint256(
242-
keccak256(
243-
abi.encodePacked(
244-
uint256(_subgraphCreatorAccount),
245-
uint256(LEGACY_SUBGRAPH_MAPPING_SLOT)
246-
)
247-
)
248-
);
249-
// Then the subgraph for this _seqID should be at:
250-
uint256 subgraphSlot = uint256(keccak256(abi.encodePacked(_seqID, accountSlot)));
251-
// The curatorNSignal mapping is at slot 2 within the SubgraphData struct,
252-
// So the mapping is at slot subgraphSlot + 2
253-
// Therefore the nSignal value for msg.sender should be at slot:
254-
curatorSlot = uint256(
255-
keccak256(abi.encodePacked(uint256(msg.sender), uint256(subgraphSlot).add(2)))
256-
);
257-
}
221+
uint256 curatorSlot = _getLegacyCuratorSlot(msg.sender, _subgraphCreatorAccount, _seqID);
258222

259223
Verifier.SlotValue memory curatorNSignalSlot = Verifier.extractSlotValueFromProof(
260224
keccak256(abi.encodePacked(curatorSlot)),
@@ -299,4 +263,46 @@ contract L2GNS is GNS {
299263
);
300264
migratedData.curatorBalanceClaimed[_curator] = true;
301265
}
266+
267+
function _getCuratorSlot(address _curator, uint256 _subgraphID)
268+
internal
269+
pure
270+
returns (uint256)
271+
{
272+
// subgraphs mapping is stored at slot SUBGRAPH_MAPPING_SLOT.
273+
// So our subgraph is at slot keccak256(abi.encodePacked(uint256(subgraphID), uint256(SUBGRAPH_MAPPING_SLOT)))
274+
// The curatorNSignal mapping is at slot 2 within the SubgraphData struct,
275+
// So the mapping is at slot keccak256(abi.encodePacked(uint256(subgraphID), uint256(SUBGRAPH_MAPPING_SLOT))) + 2
276+
// Therefore the nSignal value for msg.sender should be at slot:
277+
return
278+
uint256(
279+
keccak256(
280+
abi.encodePacked(
281+
uint256(_curator),
282+
uint256(keccak256(abi.encodePacked(_subgraphID, SUBGRAPH_MAPPING_SLOT)))
283+
.add(2)
284+
)
285+
)
286+
);
287+
}
288+
289+
function _getLegacyCuratorSlot(
290+
address _curator,
291+
address _subgraphCreatorAccount,
292+
uint256 _seqID
293+
) internal pure returns (uint256) {
294+
// legacy subgraphs mapping is stored at slot LEGACY_SUBGRAPH_MAPPING_SLOT.
295+
// So the subgraphs for the account are at slot keccak256(abi.encodePacked(uint256(_subgraphCreatorAccount), uint256(SUBGRAPH_MAPPING_SLOT)))
296+
uint256 accountSlot = uint256(
297+
keccak256(
298+
abi.encodePacked(uint256(_subgraphCreatorAccount), LEGACY_SUBGRAPH_MAPPING_SLOT)
299+
)
300+
);
301+
// Then the subgraph for this _seqID should be at:
302+
uint256 subgraphSlot = uint256(keccak256(abi.encodePacked(_seqID, accountSlot)));
303+
// The curatorNSignal mapping is at slot 2 within the SubgraphData struct,
304+
// So the mapping is at slot subgraphSlot + 2
305+
// Therefore the nSignal value for msg.sender should be at slot:
306+
return uint256(keccak256(abi.encodePacked(uint256(_curator), subgraphSlot.add(2))));
307+
}
302308
}

0 commit comments

Comments
 (0)