Skip to content

Commit

Permalink
Invariant testing suite
Browse files Browse the repository at this point in the history
  • Loading branch information
matejos committed Apr 10, 2024
1 parent 4a92e43 commit fae582d
Show file tree
Hide file tree
Showing 6 changed files with 492 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract OrderbookDex is IOrderbookDex, ERC1155Holder, ReentrancyGuard {
error InsufficientEndAmount(uint256 expectedAmount, uint256 actualAmount);
error InvalidArrayLength();
error InvalidInput(uint256 input);
error Unauthorized();
error Unauthorized(address sender);

constructor(IInverseProjected1155 _asset) {
asset = _asset;
Expand Down Expand Up @@ -209,7 +209,7 @@ contract OrderbookDex is IOrderbookDex, ERC1155Holder, ReentrancyGuard {
function cancelSellOrder(uint256 orderId) public virtual {
Order storage order = orders[orderId];
if (msg.sender != order.seller) {
revert Unauthorized();
revert Unauthorized(msg.sender);
}
uint256 assetAmount = order.assetAmount;
delete order.assetAmount;
Expand Down
4 changes: 3 additions & 1 deletion packages/contracts/evm-contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
src = 'contracts' # the source directory
test = 'test' # the test directory
out = 'artifacts/forge' # the output directory (for artifacts)
libs = ['test-lib'] # a list of library directories
libs = ['test-lib'] # a list of library directories
remappings = [ # a list of remappings
'@openzeppelin/=../../../node_modules/@openzeppelin/',
]
Expand Down Expand Up @@ -31,3 +31,5 @@ evm_version = 'berlin' # the evm version (by hardfork name)
#block_coinbase = '0x0000000000000000000000000000000000000000' # the address of `block.coinbase` in tests
#block_timestamp = 0 # the value of `block.timestamp` in tests
#block_difficulty = 0 # the value of `block.difficulty` in tests
[invariant]
fail_on_revert = true
92 changes: 92 additions & 0 deletions packages/contracts/evm-contracts/test-lib/StdInvariant.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.2 <0.9.0;

pragma experimental ABIEncoderV2;

abstract contract StdInvariant {
struct FuzzSelector {
address addr;
bytes4[] selectors;
}

address[] private _excludedContracts;
address[] private _excludedSenders;
address[] private _targetedContracts;
address[] private _targetedSenders;

string[] private _excludedArtifacts;
string[] private _targetedArtifacts;

FuzzSelector[] private _targetedArtifactSelectors;
FuzzSelector[] private _targetedSelectors;

// Functions for users:
// These are intended to be called in tests.

function excludeContract(address newExcludedContract_) internal {
_excludedContracts.push(newExcludedContract_);
}

function excludeSender(address newExcludedSender_) internal {
_excludedSenders.push(newExcludedSender_);
}

function excludeArtifact(string memory newExcludedArtifact_) internal {
_excludedArtifacts.push(newExcludedArtifact_);
}

function targetArtifact(string memory newTargetedArtifact_) internal {
_targetedArtifacts.push(newTargetedArtifact_);
}

function targetArtifactSelector(FuzzSelector memory newTargetedArtifactSelector_) internal {
_targetedArtifactSelectors.push(newTargetedArtifactSelector_);
}

function targetContract(address newTargetedContract_) internal {
_targetedContracts.push(newTargetedContract_);
}

function targetSelector(FuzzSelector memory newTargetedSelector_) internal {
_targetedSelectors.push(newTargetedSelector_);
}

function targetSender(address newTargetedSender_) internal {
_targetedSenders.push(newTargetedSender_);
}

// Functions for forge:
// These are called by forge to run invariant tests and don't need to be called in tests.

function excludeArtifacts() public view returns (string[] memory excludedArtifacts_) {
excludedArtifacts_ = _excludedArtifacts;
}

function excludeContracts() public view returns (address[] memory excludedContracts_) {
excludedContracts_ = _excludedContracts;
}

function excludeSenders() public view returns (address[] memory excludedSenders_) {
excludedSenders_ = _excludedSenders;
}

function targetArtifacts() public view returns (string[] memory targetedArtifacts_) {
targetedArtifacts_ = _targetedArtifacts;
}

function targetArtifactSelectors() public view returns (FuzzSelector[] memory targetedArtifactSelectors_) {
targetedArtifactSelectors_ = _targetedArtifactSelectors;
}

function targetContracts() public view returns (address[] memory targetedContracts_) {
targetedContracts_ = _targetedContracts;
}

function targetSelectors() public view returns (FuzzSelector[] memory targetedSelectors_) {
targetedSelectors_ = _targetedSelectors;
}

function targetSenders() public view returns (address[] memory targetedSenders_) {
targetedSenders_ = _targetedSenders;
}
}
4 changes: 3 additions & 1 deletion packages/contracts/evm-contracts/test-lib/ctest.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0;

contract CTest {
import {StdInvariant} from "./StdInvariant.sol";

contract CTest is StdInvariant {
event log(string);
event logs(bytes);

Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/evm-contracts/test/OrderbookDex.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ contract OrderbookDexTest is CTest, ERC1155Holder {
dex.createSellOrder(assetId, 100, 200);

vm.startPrank(alice);
vm.expectRevert(OrderbookDex.Unauthorized.selector);
vm.expectRevert(abi.encodeWithSelector(OrderbookDex.Unauthorized.selector, alice));
dex.cancelSellOrder(orderId);
}

Expand Down Expand Up @@ -499,7 +499,7 @@ contract OrderbookDexTest is CTest, ERC1155Holder {
orderIds[0] = orderId;

vm.startPrank(alice);
vm.expectRevert(OrderbookDex.Unauthorized.selector);
vm.expectRevert(abi.encodeWithSelector(OrderbookDex.Unauthorized.selector, alice));
dex.cancelBatchSellOrder(orderIds);
}

Expand Down
Loading

0 comments on commit fae582d

Please sign in to comment.