forked from storyprotocol/protocol-core
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added storage layout safety check on deployment and GHA (#16)
* storage layout check * forge install: openzeppelin-foundry-upgrades v0.1.0 * storage layout checker scripts * try gha without explicitly installing forge upgrades * adding missing forge build * full build for upgrade safety
- Loading branch information
Showing
7 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,3 @@ typechain | |
# converage | ||
lcov.info | ||
|
||
# forge lib folder | ||
/lib/ |
Submodule openzeppelin-foundry-upgrades
added at
c50a79
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// SPDX-License-Identifier: MIT | ||
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 | ||
/// @notice Helper contract to check if the storage layout of the contracts is compatible with upgrades | ||
/// @dev Picked relevant functionality from OpenZeppelin's `@openzeppelin/upgrades-core` package. | ||
/// NOTE: Replace this contract for their library, if these issues are resolved | ||
|
||
/// MUST be called in scripts that deploy or upgrade contracts | ||
/// MUST be called with `--ffi` flag | ||
contract StorageLayoutChecker is Script { | ||
|
||
using strings for *; | ||
|
||
function run() virtual public { | ||
_validate(); | ||
} | ||
|
||
/// @notice Runs the storage layout check | ||
/// @dev For simplicity and efficiency, we check all the upgradeablecontracts in the project | ||
/// instead of going 1 by 1 using ffi. | ||
function _validate() private { | ||
string[] memory inputs = _buildValidateCommand(); | ||
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. | ||
// As an extra precaution, we also check stdout for "SUCCESS" to ensure it actually ran. | ||
if (result.exitCode == 0 && stdout.toSlice().contains("SUCCESS".toSlice())) { | ||
return; | ||
} else if (result.stderr.length > 0) { | ||
// Validations failed to run | ||
revert(StringUtil.concat("Failed to run upgrade safety validation: ", string(result.stderr))); | ||
} else { | ||
// Validations ran but some contracts were not upgrade safe | ||
revert(StringUtil.concat("Upgrade safety validation failed:\n", stdout)); | ||
} | ||
} | ||
|
||
function _buildValidateCommand() private view returns (string[] memory) { | ||
string memory outDir = "out"; | ||
|
||
string[] memory inputBuilder = new string[](255); | ||
|
||
uint8 i = 0; | ||
// npx @openzeppelin/upgrades-core validate <build-info-dir> --requireReference | ||
inputBuilder[i++] = "npx"; | ||
inputBuilder[i++] = string.concat("@openzeppelin/upgrades-core"); | ||
inputBuilder[i++] = "validate"; | ||
inputBuilder[i++] = string.concat(outDir, "/build-info"); | ||
|
||
// Create a copy of inputs but with the correct length | ||
string[] memory inputs = new string[](i); | ||
for (uint8 j = 0; j < i; j++) { | ||
inputs[j] = inputBuilder[j]; | ||
} | ||
|
||
return inputs; | ||
} | ||
} |