Skip to content

Commit

Permalink
chore: setup postcheck checklist
Browse files Browse the repository at this point in the history
  • Loading branch information
TuDo1403 committed Feb 8, 2024
1 parent 098c8bb commit 47b9f0e
Show file tree
Hide file tree
Showing 19 changed files with 349 additions and 17 deletions.
6 changes: 6 additions & 0 deletions script/GeneralConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ contract GeneralConfig is BaseGeneralConfig, Utils {
_contractNameMap[contractEnum.key()] = contractEnum.name();
}

function getCompanionNetwork(TNetwork network) external pure returns (Network) {
if (network == DefaultNetwork.RoninTestnet.key()) return Network.Goerli;
if (network == DefaultNetwork.RoninMainnet.key()) return Network.EthMainnet;
revert("Network: Unknown companion network");
}

function getSender() public view virtual override returns (address payable sender) {
sender = _option.trezor ? payable(_trezorSender) : payable(_envSender);
bool isLocalNetwork = getCurrentNetwork() == DefaultNetwork.Local.key();
Expand Down
4 changes: 2 additions & 2 deletions script/Migration.s.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import { BaseMigrationV2 } from "./BaseMigrationV2.sol";
import { BaseMigration } from "./BaseMigration.sol";
import { DefaultNetwork } from "foundry-deployment-kit/utils/DefaultNetwork.sol";
import { GeneralConfig } from "./GeneralConfig.sol";
import { ISharedArgument } from "./interfaces/ISharedArgument.sol";
Expand All @@ -12,7 +12,7 @@ import { GlobalProposal } from "@ronin/contracts/libraries/GlobalProposal.sol";
import { Token } from "@ronin/contracts/libraries/Token.sol";
import { LibArray } from "./libraries/LibArray.sol";

contract Migration is BaseMigrationV2, Utils {
contract Migration is BaseMigration, Utils {
ISharedArgument public constant config = ISharedArgument(address(CONFIG));

function _configByteCode() internal virtual override returns (bytes memory) {
Expand Down
6 changes: 6 additions & 0 deletions script/PostChecker.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

abstract contract PostChecker {

}
127 changes: 112 additions & 15 deletions script/libraries/LibArray.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,121 @@ library LibArray {
*/
error LengthMismatch();

function toSingletonArray(address self) internal pure returns (address[] memory arr) {
arr = new address[](1);
arr[0] = self;
}

function toSingletonArray(bytes32 self) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](1);
arr[0] = self;
}

function toSingletonArray(uint256 self) internal pure returns (uint256[] memory arr) {
arr = new uint256[](1);
arr[0] = self;
}

/**
* @dev Returns whether or not there's a duplicate. Runs in O(n^2).
* @param arr Array to search
* @return Returns true if duplicate, false otherwise
*/
function hasDuplicate(uint256[] memory arr) internal pure returns (bool) {
if (arr.length == 0) return false;

unchecked {
for (uint256 i; i < arr.length - 1; ++i) {
for (uint256 j = i + 1; j < arr.length; ++j) {
if (arr[i] == arr[j]) return true;
}
}
}

return false;
}

/**
* @dev Returns whether two arrays of addresses are equal or not.
*/
function isEqual(uint256[] memory self, uint256[] memory other) internal pure returns (bool yes) {
yes = hash(self) == hash(other);
}

/**
* @dev Return the concatenated array from a and b.
*/
function extend(address[] memory a, address[] memory b) internal pure returns (address[] memory c) {
unchecked {
uint256 lengthA = a.length;
uint256 lengthB = b.length;
c = new address[](lengthA + lengthB);

uint256 i;
for (; i < lengthA;) {
c[i] = a[i];
++i;
}
for (uint256 j; j < lengthB;) {
c[i] = b[j];
++i;
++j;
}
}
}

/**
* @dev Converts an array of uint8 to an array of uint256.
* @param self The array of uint8.
* @return uint256s The resulting array of uint256s.
*/
function toUint256s(uint8[] memory self) internal pure returns (uint256[] memory uint256s) {
assembly ("memory-safe") {
uint256s := self
}
}

/**
* @dev Converts an array of uint96 to an array of uint256.
* @param self The array of uint96.
* @return uint256s The resulting array of uint256s.
*/
function toUint256s(uint96[] memory self) internal pure returns (uint256[] memory uint256s) {
assembly ("memory-safe") {
uint256s := self
}
}

/**
* @dev Down cast an array of uint256 to an array of uint8.
* @param self The array of uint256.
* @return uint8s The resulting array of uint256s.
* This function will result in invalid data if value in array is greater than 255.
* Use it as caution.
*/
function toUint8sUnsafe(uint256[] memory self) internal pure returns (uint8[] memory uint8s) {
assembly ("memory-safe") {
uint8s := self
}
}

function toUint96sUnsafe(uint256[] memory self) internal pure returns (uint96[] memory uint96s) {
assembly ("memory-safe") {
uint96s := self
}
}

function toAddressesUnsafe(uint256[] memory self) internal pure returns (address[] memory addrs) {
assembly ("memory-safe") {
addrs := self
}
}

/**
* @dev Create an array of indices with provided range.
* @param length The array size
* @return data an array of indices
*/
function arange(uint256 length) internal pure returns (uint256[] memory data) {
data = new uint256[](length);
for (uint256 i; i < length; ++i) {
Expand All @@ -41,16 +144,21 @@ library LibArray {
}
}

function hash(uint256[] memory data) internal pure returns (bytes32 digest) {
/**
* @dev Hash dynamic size array
* @param self The array of uint256
* @return digest The hash result of the array
*/
function hash(uint256[] memory self) internal pure returns (bytes32 digest) {
assembly ("memory-safe") {
digest := keccak256(add(data, 0x20), mload(data))
digest := keccak256(add(self, 0x20), mul(mload(self), 0x20))
}
}

/**
* @dev Calculates the sum of an array of uint256 values.
* @param data The array of uint256 values for which the sum is calculated.
* @return result The sum of the provided array of uint256 values.
* @return result The sum of the provided array.
*/
function sum(uint256[] memory data) internal pure returns (uint256 result) {
assembly ("memory-safe") {
Expand Down Expand Up @@ -78,7 +186,7 @@ library LibArray {

/**
* @dev Converts an array of uint64 to an array of uint256.
* @param self The array of bytes32.
* @param self The array of uint64.
* @return uint256s The resulting array of uint256.
*/
function toUint256s(uint64[] memory self) internal pure returns (uint256[] memory uint256s) {
Expand All @@ -87,17 +195,6 @@ library LibArray {
}
}

/**
* @dev Converts an array of address to an array of uint256.
* @param self The array of address.
* @return uint256s The resulting array of uint256.
*/
function toUint256s(address[] memory self) internal pure returns (uint256[] memory uint256s) {
assembly ("memory-safe") {
uint256s := self
}
}

/**
* @dev Sorts an array of uint256 values based on a corresponding array of values using the specified sorting mode.
* @param self The array to be sorted.
Expand Down
35 changes: 35 additions & 0 deletions script/post-check/BasePostCheck.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { Vm } from "forge-std/Vm.sol";
import { StdStyle } from "forge-std/StdStyle.sol";
import { console2 as console } from "forge-std/console2.sol";
import { BaseMigration } from "foundry-deployment-kit/BaseMigration.s.sol";
import { IBridgeManager } from "@ronin/contracts/interfaces/bridge/IBridgeManager.sol";

abstract contract BasePostCheck is BaseMigration {
using StdStyle for *;

uint256 internal seed = vm.unixTime();
address internal _bridgeSlash;
address internal _bridgeReward;
address internal _bridgeTracking;
mapping(uint256 chainId => address manager) internal _manager;
mapping(uint256 chainId => address gateway) internal _gateway;

modifier onPostCheck(string memory postCheckLabel) {
uint256 snapshotId = _beforePostCheck(postCheckLabel);
_;
_afterPostCheck(postCheckLabel, snapshotId);
}

function _beforePostCheck(string memory postCheckLabel) private returns (uint256 snapshotId) {
snapshotId = vm.snapshot();
console.log("\n> ".cyan(), postCheckLabel.blue().italic(), "...");
}

function _afterPostCheck(string memory postCheckLabel, uint256 snapshotId) private {
console.log(string.concat("Postcheck", postCheckLabel.italic(), "successful!\n").green());
vm.revertTo(snapshotId);
}
}
14 changes: 14 additions & 0 deletions script/post-check/PostCheck_StorageSlots.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import { BasePostCheck } from "./BasePostCheck.s.sol";

abstract contract PostCheck_StorageSlot is BasePostCheck {
function validate_BridgeManager_StorageSlot() internal onPostCheck("validate_BridgeManager_StorageSlot") { }

function validate_BridgeReward_StorageSlot() internal onPostCheck("validate_BridgeReward_StorageSlot") { }

function validate_BridgeSlash_StorageSlot() internal onPostCheck("validateBridgeSlash_StorageSlot") { }

function validate_BridgeTracking_StorageSlot() internal onPostCheck("validate_BridgeTracking_StorageSlot") { }
}
91 changes: 91 additions & 0 deletions script/post-check/bridge-manager/BridgeManager.post_check.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
BridgeManager
CRUD
add
when not self-called
it should revert
when self-called
when the list has duplicate
it should revert
when the list has null value
it should revert
when three input array mismatch
it should revert
when vote weight is zero
it should revert
when the valid input
governor should be in governor list
it should increase the total weight
governor should have expected weight
operator should have expected weight
it should map governor => bridge operator
it should map bridge operator => governor
bridge operator should be in operator list
it should notify proxy contract with valid data
it should notify immutable contract with valid data
remove
when not self-called
it should revert
when self-called
when the list has duplicate
it should revert
when the list has null value
it should revert
when operator is not in the operator list
it should not remove
when operator is in the list
governor should have 0 weight
it should decrease the total weight
bridge operator should have 0 weight
orders of other governors must unchanged
orders of other bridge operators must unchanged
it should notify proxy contract with valid data
it should notify immutable contract with valid data
it should remove the operator from the operator list
it should remove corresponding governor from the list
it should remove the mapping governor => bridge operator
it should remove the mapping bridge operator => governor

update
when self-called
it should revert
when caller is not governor
it should revert
when caller is governor
when new bridge operator is null
it should revert
when new bridge operator mapped to another governor
it should revert
when current bridge operator == new bridge operator
it should revert
when new bridge operator is not in the list
total weight must not change
old bridge operator should have 0 weight
orders of other governors must unchanged
orders of other bridge operators must unchanged
it should notify proxy contract with valid data
it should notify immutable contract with valid data
it should add the mapping governor => new bridge operator
it should add the mapping new bridge operator => governor
new bridge operator should has old bridge operator weight
it should remove the mapping old bridge operator => governor
it should remove the mapping governor => old bridge operator

Proposal
GlobalProposal
when the nonce is invalid
it should revert
when the caller is not the governor
it should verify signature with correct governor
when the target is BridgeManager
it should self call
ProposalForCurrentNetwork
when the nonce is invalid
it should revert
when the caller is not the governor
when the calldata includes signature
it should verify correct signature
when the calldata not includes signature
it should revert
when the target is BridgeManager
it should self call

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

abstract contract PostCheck_BridgeManager {

}
Loading

0 comments on commit 47b9f0e

Please sign in to comment.