Skip to content

Commit

Permalink
storage layout checker scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramarti committed Mar 23, 2024
1 parent c7f1882 commit 9330a08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 44 deletions.
7 changes: 5 additions & 2 deletions script/foundry/deployment/Main.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ import { IHookModule } from "contracts/interfaces/modules/base/IHookModule.sol";
import { StringUtil } from "../../../script/foundry/utils/StringUtil.sol";
import { BroadcastManager } from "../../../script/foundry/utils/BroadcastManager.s.sol";
import { JsonDeploymentHandler } from "../../../script/foundry/utils/JsonDeploymentHandler.s.sol";
import { StorageLayoutChecker } from "../../../script/foundry/utils/upgrades/StorageLayoutCheck.s.sol";

// test
import { MockERC20 } from "test/foundry/mocks/token/MockERC20.sol";
import { MockERC721 } from "test/foundry/mocks/token/MockERC721.sol";
import { MockTokenGatedHook } from "test/foundry/mocks/MockTokenGatedHook.sol";

contract Main is Script, BroadcastManager, JsonDeploymentHandler {
contract Main is Script, BroadcastManager, JsonDeploymentHandler, StorageLayoutChecker {
using StringUtil for uint256;
using stdJson for string;

Expand Down Expand Up @@ -112,7 +113,9 @@ contract Main is Script, BroadcastManager, JsonDeploymentHandler {
/// @dev To use, run the following command (e.g. for Sepolia):
/// forge script script/foundry/deployment/Main.s.sol:Main --rpc-url $RPC_URL --broadcast --verify -vvvv

function run() public {
function run() virtual override public {
// This will run OZ storage layout check for all contracts. Requires --ffi flag.
super.run();
_beginBroadcast(); // BroadcastManager.s.sol

bool configByMultisig;
Expand Down
49 changes: 7 additions & 42 deletions script/foundry/utils/upgrades/StorageLayoutCheck.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pragma solidity ^0.8.23;
import { Script } from "forge-std/Script.sol";
import { Vm } from "forge-std/Vm.sol";
import { console } from "forge-std/console.sol";
import { strings } from "solidity-stringutils/src/strings.sol";

import { Utils } from "@openzeppelin-foundry-upgrades/src/internal/Utils.sol";
import { StringUtil } from "../StringUtil.sol";

/// @title StorageLayoutChecker
Expand All @@ -15,7 +17,10 @@ import { StringUtil } from "../StringUtil.sol";
/// MUST be called in scripts that deploy or upgrade contracts
/// MUST be called with `--ffi` flag
contract StorageLayoutChecker is Script {
function run() public {

using strings for *;

function run() virtual public {
_validate();
}

Expand All @@ -24,7 +29,7 @@ contract StorageLayoutChecker is Script {
/// instead of going 1 by 1 using ffi.
function _validate() private {
string[] memory inputs = _buildValidateCommand();
Vm.FfiResult memory result = _runAsBashCommand(inputs);
Vm.FfiResult memory result = Utils.runAsBashCommand(inputs);
string memory stdout = string(result.stdout);

// CLI validate command uses exit code to indicate if the validation passed or failed.
Expand All @@ -40,44 +45,6 @@ contract StorageLayoutChecker is Script {
}
}

/**
* @dev Runs an arbitrary command using bash.
* @param inputs Inputs for a command, e.g. ["grep", "-rl", "0x1234", "out/build-info"]
* @return The result of the corresponding bash command as a Vm.FfiResult struct
*/
function _runAsBashCommand(string[] memory inputs) internal returns (Vm.FfiResult memory) {
string[] memory bashCommand = _toBashCommand(inputs, "bash");
Vm.FfiResult memory result = vm.tryFfi(bashCommand);
if (result.exitCode != 0 && result.stdout.length == 0 && result.stderr.length == 0) {
// On Windows, using the bash executable from WSL leads to a non-zero exit code and no output
revert(StringUtil.concat('Failed to run bash command with "', bashCommand[0]));
} else {
return result;
}
}

/**
* @dev Converts an array of inputs to a bash command.
* @param inputs Inputs for a command, e.g. ["grep", "-rl", "0x1234", "out/build-info"]
* @param bashPath Path to the bash executable or just "bash" if it is in the PATH
* @return A bash command that runs the given inputs, e.g. ["bash", "-c", "grep -rl 0x1234 out/build-info"]
*/
function _toBashCommand(string[] memory inputs, string memory bashPath) internal pure returns (string[] memory) {
string memory commandString;
for (uint i = 0; i < inputs.length; i++) {
commandString = string.concat(commandString, inputs[i]);
if (i != inputs.length - 1) {
commandString = string.concat(commandString, " ");
}
}

string[] memory result = new string[](3);
result[0] = bashPath;
result[1] = "-c";
result[2] = commandString;
return result;
}

function _buildValidateCommand() private view returns (string[] memory) {
string memory outDir = "out";

Expand All @@ -90,8 +57,6 @@ contract StorageLayoutChecker is Script {
inputBuilder[i++] = "validate";
inputBuilder[i++] = string.concat(outDir, "/build-info");

inputBuilder[i++] = "--requireReference";

// Create a copy of inputs but with the correct length
string[] memory inputs = new string[](i);
for (uint8 j = 0; j < i; j++) {
Expand Down

0 comments on commit 9330a08

Please sign in to comment.