Skip to content

Commit

Permalink
Merge pull request #1154 from ProjectOpenSea/horsefacts/vm-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
0age authored Apr 14, 2023
2 parents 3caa72c + 832df19 commit 00d9290
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 77 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ test/utils/eip712/gen.sol

# fuzz metrics
metrics.txt
call-metrics.txt
mutation-metrics.txt
*-metrics.txt

fuzz_debug.json
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fs_permissions = [
{ access = "read", path = "./reference-out" },
{ access = "write", path = "./call-metrics.txt" },
{ access = "write", path = "./mutation-metrics.txt" },
{ access = "write", path = "./assume-metrics.txt" },
{ access = "write", path = "./fuzz_debug.json" }
]

Expand Down
7 changes: 1 addition & 6 deletions test/foundry/new/ExpectedBalanceSerializer.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

// import { Vm } from "forge-std/Vm.sol";
// import { vm } from "./VmUtils.sol"";

// import { ExpectedBalances } from "./helpers/ExpectedBalances.sol";

// address constant VM_ADDRESS = address(
// uint160(uint256(keccak256("hevm cheat code")))
// );
// Vm constant vm = Vm(VM_ADDRESS);

// function tojsonAddress(
// string memory objectKey,
// string memory valueKey,
Expand Down
25 changes: 15 additions & 10 deletions test/foundry/new/helpers/FuzzDerivers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity ^0.8.17;

import { Test } from "forge-std/Test.sol";

import { Vm } from "forge-std/Vm.sol";
import { vm, assume } from "./VmUtils.sol";

import {
AdvancedOrderLib,
Expand Down Expand Up @@ -52,9 +52,6 @@ import { CriteriaResolverHelper } from "./CriteriaResolverHelper.sol";
* `FuzzTestContext`.
*/
library FuzzDerivers {
Vm private constant vm =
Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

using FuzzEngineLib for FuzzTestContext;
using AdvancedOrderLib for AdvancedOrder;
using AdvancedOrderLib for AdvancedOrder[];
Expand All @@ -73,7 +70,7 @@ library FuzzDerivers {

function withDerivedAvailableOrders(
FuzzTestContext memory context
) internal view returns (FuzzTestContext memory) {
) internal returns (FuzzTestContext memory) {
// TODO: handle skipped orders due to generateOrder reverts
bool[] memory expectedAvailableOrders = new bool[](
context.executionState.orders.length
Expand Down Expand Up @@ -124,7 +121,10 @@ library FuzzDerivers {
}

// TEMP (TODO: handle upstream)
vm.assume(!(order.startTime == 0 && order.endTime == 0));
assume(
!(order.startTime == 0 && order.endTime == 0),
"zero_start_end_time"
);

bool isAvailable = (block.timestamp < order.endTime && // not expired
block.timestamp >= order.startTime && // started
Expand Down Expand Up @@ -237,7 +237,6 @@ library FuzzDerivers {
FuzzTestContext memory context
)
internal
view
returns (
Execution[] memory implicitExecutions,
Execution[] memory explicitExecutions
Expand Down Expand Up @@ -277,7 +276,10 @@ library FuzzDerivers {
) = getFulfillAvailableExecutions(context);

// TEMP (TODO: handle upstream)
vm.assume(explicitExecutions.length > 0);
assume(
explicitExecutions.length > 0,
"no_explicit_executions_fulfillAvailable"
);

if (explicitExecutions.length == 0) {
revert(
Expand All @@ -295,7 +297,10 @@ library FuzzDerivers {
);

// TEMP (TODO: handle upstream)
vm.assume(explicitExecutions.length > 0);
assume(
explicitExecutions.length > 0,
"no_explicit_executions_match"
);

if (explicitExecutions.length == 0) {
revert("FuzzDerivers: no explicit executions derived - match");
Expand All @@ -311,7 +316,7 @@ library FuzzDerivers {
*/
function withDerivedExecutions(
FuzzTestContext memory context
) internal view returns (FuzzTestContext memory) {
) internal returns (FuzzTestContext memory) {
(
Execution[] memory implicitExecutions,
Execution[] memory explicitExecutions
Expand Down
9 changes: 2 additions & 7 deletions test/foundry/new/helpers/FuzzEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import { CheckHelpers, FuzzSetup } from "./FuzzSetup.sol";

import { ExpectedEventsUtil } from "./event-utils/ExpectedEventsUtil.sol";

import { logMutation } from "./Metrics.sol";

/**
* @notice Base test contract for FuzzEngine. Fuzz tests should inherit this.
* Includes the setup and helper functions from BaseOrderTest.
Expand Down Expand Up @@ -483,11 +485,4 @@ contract FuzzEngine is
check(context, selector);
}
}

function logMutation(string memory mutationName) internal {
if (vm.envOr("SEAPORT_COLLECT_FUZZ_METRICS", false)) {
string memory metric = string.concat(mutationName, ":1|c");
vm.writeLine("mutation-metrics.txt", metric);
}
}
}
8 changes: 1 addition & 7 deletions test/foundry/new/helpers/FuzzExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { FuzzTestContext } from "./FuzzTestContextLib.sol";
import { FuzzEngineLib } from "./FuzzEngineLib.sol";
import { FuzzHelpers } from "./FuzzHelpers.sol";

import { logCall } from "./Metrics.sol";
import { dumpExecutions } from "./DebugUtil.sol";

abstract contract FuzzExecutor is Test {
Expand Down Expand Up @@ -235,11 +236,4 @@ abstract contract FuzzExecutor is Test {
function exec(FuzzTestContext memory context) public {
exec(context, false);
}

function logCall(string memory callName, bool enabled) internal {
if (enabled && vm.envOr("SEAPORT_COLLECT_FUZZ_METRICS", false)) {
string memory metric = string.concat(callName, ":1|c");
vm.writeLine("call-metrics.txt", metric);
}
}
}
4 changes: 2 additions & 2 deletions test/foundry/new/helpers/FuzzGenerators.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ library SignatureGenerator {
bytes32 s,
Offerer offerer,
FuzzGeneratorContext memory context
) internal {
) internal pure {
address recovered = ecrecover(digest, v, r, s);
if (recovered != offerer.generate(context) || recovered == address(0)) {
revert("SignatureGenerator: Invalid signature");
Expand Down Expand Up @@ -2022,7 +2022,7 @@ library OffererGenerator {
function generate(
Offerer offerer,
FuzzGeneratorContext memory context
) internal returns (address) {
) internal pure returns (address) {
if (offerer == Offerer.TEST_CONTRACT) {
return context.self;
} else if (offerer == Offerer.ALICE) {
Expand Down
5 changes: 1 addition & 4 deletions test/foundry/new/helpers/FuzzInscribers.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import { Vm } from "forge-std/Vm.sol";
import { vm } from "./VmUtils.sol";

import { AdvancedOrder, OrderStatus } from "seaport-sol/SeaportStructs.sol";

Expand All @@ -15,9 +15,6 @@ import { AdvancedOrderLib } from "seaport-sol/SeaportSol.sol";
library FuzzInscribers {
using AdvancedOrderLib for AdvancedOrder;

Vm private constant vm =
Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

uint256 constant wipeDenominatorMask =
0x000000000000000000000000000000ffffffffffffffffffffffffffffffffff;

Expand Down
28 changes: 10 additions & 18 deletions test/foundry/new/helpers/FuzzMutationHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FuzzTestContext, MutationState } from "./FuzzTestContextLib.sol";

import { LibPRNG } from "solady/src/utils/LibPRNG.sol";

import { Vm } from "forge-std/Vm.sol";
import { vm } from "./VmUtils.sol";

import {
Failure,
Expand All @@ -16,10 +16,9 @@ import {
MutationContextDerivation
} from "./FuzzMutationSelectorLib.sol";

library FailureEligibilityLib {
Vm private constant vm =
Vm(address(uint160(uint256(keccak256("hevm cheat code")))));
import { assume } from "./VmUtils.sol";

library FailureEligibilityLib {
using LibPRNG for LibPRNG.PRNG;

function ensureFilterSetForEachFailure(
Expand Down Expand Up @@ -139,14 +138,14 @@ library FailureEligibilityLib {

function selectEligibleFailure(
FuzzTestContext memory context
) internal pure returns (Failure eligibleFailure) {
) internal returns (Failure eligibleFailure) {
LibPRNG.PRNG memory prng = LibPRNG.PRNG(context.fuzzParams.seed ^ 0xff);

Failure[] memory eligibleFailures = getEligibleFailures(context);

// TODO: remove this vm.assume as soon as at least one case is found
// for any permutation of orders.
vm.assume(eligibleFailures.length > 0);
assume(eligibleFailures.length > 0, "no_eligible_failures");

if (eligibleFailures.length == 0) {
revert("FailureEligibilityLib: no eligible failure found");
Expand Down Expand Up @@ -189,7 +188,6 @@ library OrderEligibilityLib {
Failure failure,
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibilityFilter
) internal pure returns (IneligibilityFilter memory) {
return IneligibilityFilter(failure.one(), fn(ineligibilityFilter));
Expand All @@ -199,7 +197,6 @@ library OrderEligibilityLib {
Failure[] memory failures,
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibilityFilter
) internal pure returns (IneligibilityFilter memory) {
return IneligibilityFilter(failures, fn(ineligibilityFilter));
Expand All @@ -208,7 +205,7 @@ library OrderEligibilityLib {
function setAllIneligibleFailures(
FuzzTestContext memory context,
IneligibilityFilter[] memory failuresAndFilters
) internal view {
) internal {
for (uint256 i = 0; i < failuresAndFilters.length; ++i) {
IneligibilityFilter memory failuresAndFilter = (
failuresAndFilters[i]
Expand All @@ -228,10 +225,9 @@ library OrderEligibilityLib {
FuzzTestContext memory context,
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibleMutationFilter,
Failure[] memory ineligibleFailures
) internal view {
) internal {
if (hasNoEligibleOrders(context, ineligibleMutationFilter)) {
context.setIneligibleFailures(ineligibleFailures);
}
Expand All @@ -241,9 +237,8 @@ library OrderEligibilityLib {
FuzzTestContext memory context,
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibleCondition
) internal view returns (bool) {
) internal returns (bool) {
for (uint256 i; i < context.executionState.orders.length; i++) {
// Once an eligible order is found, return false.
if (
Expand All @@ -264,9 +259,8 @@ library OrderEligibilityLib {
FuzzTestContext memory context,
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) condition
) internal view {
) internal {
for (uint256 i; i < context.executionState.orders.length; i++) {
if (condition(context.executionState.orders[i], i, context)) {
setIneligibleOrder(context, i);
Expand Down Expand Up @@ -333,7 +327,6 @@ library OrderEligibilityLib {
function fn(
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibleMutationFilter
) internal pure returns (bytes32 ptr) {
assembly {
Expand All @@ -349,7 +342,6 @@ library OrderEligibilityLib {
returns (
function(AdvancedOrder memory, uint256, FuzzTestContext memory)
internal
view
returns (bool) ineligibleMutationFilter
)
{
Expand All @@ -366,7 +358,7 @@ library MutationContextDeriverLib {
FuzzTestContext memory context,
MutationContextDerivation derivationMethod,
bytes32 ineligibilityFilter // use a function pointer
) internal view returns (MutationState memory mutationState) {
) internal returns (MutationState memory mutationState) {
if (derivationMethod == MutationContextDerivation.ORDER) {
context.setIneligibleOrders(
OrderEligibilityLib.asIneligibleMutationFilter(
Expand Down
4 changes: 1 addition & 3 deletions test/foundry/new/helpers/FuzzMutationSelectorLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ library FuzzMutationSelectorLib {
FuzzTestContext memory context
)
public
view
returns (
string memory name,
bytes4 mutationSelector,
Expand Down Expand Up @@ -397,7 +396,7 @@ library FailureDetailsLib {

function details_InvalidConduit(
FuzzTestContext memory context,
MutationState memory mutationState,
MutationState memory /* mutationState */,
bytes4 errorSelector
) internal view returns (bytes memory expectedRevertReason) {
bytes32 conduitKey = keccak256("invalid conduit");
Expand Down Expand Up @@ -442,7 +441,6 @@ library FailureDetailsLib {
IneligibilityFilter[] memory failuresAndFilters
)
internal
view
returns (
string memory name,
bytes4 mutationSelector,
Expand Down
8 changes: 5 additions & 3 deletions test/foundry/new/helpers/FuzzMutations.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ library MutationFilters {
AdvancedOrder memory order,
uint256 /* orderIndex */,
FuzzTestContext memory context
) internal view returns (bool) {
) internal returns (bool) {
bytes4 action = context.action();
if (
action == context.seaport.fulfillAvailableOrders.selector ||
Expand Down Expand Up @@ -289,7 +289,7 @@ library MutationFilters {
}

function ineligibleForCannotCancelOrder(
AdvancedOrder memory order,
AdvancedOrder memory /* order */,
uint256 /* orderIndex */,
FuzzTestContext memory context
) internal view returns (bool) {
Expand Down Expand Up @@ -324,7 +324,7 @@ library MutationFilters {
}

function ineligibleForOrderAlreadyFilled(
AdvancedOrder memory order,
AdvancedOrder memory /* order */,
uint256 /* orderIndex */,
FuzzTestContext memory context
) internal view returns (bool) {
Expand Down Expand Up @@ -373,6 +373,8 @@ library MutationFilters {
if (order.numerator == 1 && order.denominator == 1) {
return true;
}

return false;
}
}

Expand Down
7 changes: 1 addition & 6 deletions test/foundry/new/helpers/Labeler.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import { Vm } from "forge-std/Vm.sol";
import { vm } from "./VmUtils.sol";
import { LibString } from "solady/src/utils/LibString.sol";

address constant VM_ADDRESS = address(
uint160(uint256(keccak256("hevm cheat code")))
);
Vm constant vm = Vm(VM_ADDRESS);

address constant LABELER_ADDRESS = address(
uint160(uint256(keccak256(".labeler")))
);
Expand Down
Loading

0 comments on commit 00d9290

Please sign in to comment.