Skip to content

Commit 60f995a

Browse files
maurelianQuentinI
authored andcommitted
feat: Add ForgeArtifacts.getSlot() (ethereum-optimism#13787)
1 parent e0e7c13 commit 60f995a

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

packages/contracts-bedrock/scripts/libraries/ForgeArtifacts.sol

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ library ForgeArtifacts {
152152

153153
/// @notice Pulls the `_initialized` storage slot information from the Forge artifacts for a given contract.
154154
function getInitializedSlot(string memory _contractName) internal returns (StorageSlot memory slot_) {
155-
string memory storageLayout = getStorageLayout(_contractName);
156-
157155
// FaultDisputeGame and PermissionedDisputeGame use a different name for the initialized storage slot.
158156
string memory slotName = "_initialized";
159157
string memory slotType = "t_uint8";
@@ -162,6 +160,7 @@ library ForgeArtifacts {
162160
slotType = "t_bool";
163161
}
164162

163+
string memory storageLayout = getStorageLayout(_contractName);
165164
bytes memory rawSlot = vm.parseJson(
166165
Process.bash(
167166
string.concat(
@@ -186,6 +185,31 @@ library ForgeArtifacts {
186185
});
187186
}
188187

188+
/// @notice Returns the storage slot for a given contract and slot name
189+
function getSlot(
190+
string memory _contractName,
191+
string memory _slotName
192+
)
193+
internal
194+
returns (StorageSlot memory slot_)
195+
{
196+
string memory storageLayout = getStorageLayout(_contractName);
197+
bytes memory rawSlot = vm.parseJson(
198+
Process.bash(
199+
string.concat("echo '", storageLayout, "' | jq '.storage[] | select(.label == \"", _slotName, "\")'")
200+
)
201+
);
202+
ForgeStorageSlot memory slot = abi.decode(rawSlot, (ForgeStorageSlot));
203+
slot_ = StorageSlot({
204+
astId: slot.astId,
205+
_contract: slot._contract,
206+
label: slot.label,
207+
offset: slot.offset,
208+
slot: vm.parseUint(slot.slot),
209+
_type: slot._type
210+
});
211+
}
212+
189213
/// @notice Returns whether or not a contract is initialized.
190214
/// Needs the name to get the storage layout.
191215
function isInitialized(string memory _name, address _address) internal returns (bool initialized_) {

packages/contracts-bedrock/test/L1/L1CrossDomainMessenger.t.sol

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AddressAliasHelper } from "src/vendor/AddressAliasHelper.sol";
1111
import { Predeploys } from "src/libraries/Predeploys.sol";
1212
import { Hashing } from "src/libraries/Hashing.sol";
1313
import { Encoding } from "src/libraries/Encoding.sol";
14+
import { ForgeArtifacts } from "scripts/libraries/ForgeArtifacts.sol";
1415

1516
// Target contract dependencies
1617
import { IL1CrossDomainMessenger } from "interfaces/L1/IL1CrossDomainMessenger.sol";
@@ -22,7 +23,12 @@ contract L1CrossDomainMessenger_Test is CommonTest {
2223
address recipient = address(0xabbaacdc);
2324

2425
/// @dev The storage slot of the l2Sender
25-
uint256 constant senderSlotIndex = 50;
26+
uint256 senderSlotIndex;
27+
28+
function setUp() public override {
29+
super.setUp();
30+
senderSlotIndex = ForgeArtifacts.getSlot("OptimismPortal2", "l2Sender").slot;
31+
}
2632

2733
/// @dev Tests that the implementation is initialized correctly.
2834
/// @notice Marked virtual to be overridden in

packages/contracts-bedrock/test/invariants/CrossDomainMessenger.t.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { Predeploys } from "src/libraries/Predeploys.sol";
1010
import { Constants } from "src/libraries/Constants.sol";
1111
import { Encoding } from "src/libraries/Encoding.sol";
1212
import { Hashing } from "src/libraries/Hashing.sol";
13+
import { ForgeArtifacts } from "scripts/libraries/ForgeArtifacts.sol";
1314

1415
contract RelayActor is StdUtils {
1516
// Storage slot of the l2Sender
16-
uint256 constant senderSlotIndex = 50;
17+
uint256 senderSlotIndex;
1718

1819
uint256 public numHashes;
1920
bytes32[] public hashes;
@@ -29,6 +30,7 @@ contract RelayActor is StdUtils {
2930
xdm = _xdm;
3031
vm = _vm;
3132
doFail = _doFail;
33+
senderSlotIndex = ForgeArtifacts.getSlot("OptimismPortal2", "l2Sender").slot;
3234
}
3335

3436
/// @notice Relays a message to the `L1CrossDomainMessenger` with a random `version`,

0 commit comments

Comments
 (0)