-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Extract JSON stuff from base test into a separate contract
- Loading branch information
1 parent
38a6a50
commit 4f8290a
Showing
6 changed files
with
119 additions
and
44 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"global": { | ||
"disable": 0, | ||
"enable": 0, | ||
"fullDeployment": 0 | ||
}, | ||
"signature": { | ||
"ko_viaKernel": 0, | ||
"ko_viaValidator": 0, | ||
"viaKernel": 0, | ||
"viaValidator": 0 | ||
}, | ||
"userOp": { | ||
"viaEntryPoint": 0, | ||
"viaKernel": 0, | ||
"viaValidator": 0 | ||
} | ||
} |
File renamed without changes.
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,87 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {console} from "forge-std/Console.sol"; | ||
|
||
import {Kernel} from "src/Kernel.sol"; | ||
import {KernelFactory} from "src/factory/KernelFactory.sol"; | ||
import {IKernel} from "src/interfaces/IKernel.sol"; | ||
import {IKernelValidator} from "src/interfaces/IKernelValidator.sol"; | ||
import {ValidationData} from "src/common/Types.sol"; | ||
import {Operation} from "src/common/Enums.sol"; | ||
import {LibString} from "solady/utils/LibString.sol"; | ||
import {ERC4337Utils} from "src/utils/ERC4337Utils.sol"; | ||
|
||
import {IEntryPoint} from "I4337/interfaces/IEntryPoint.sol"; | ||
import {UserOperation} from "I4337/interfaces/UserOperation.sol"; | ||
import {ENTRYPOINT_0_6_ADDRESS, ENTRYPOINT_0_6_BYTECODE} from "I4337/artifacts/EntryPoint_0_6.sol"; | ||
|
||
import {MainnetMetering} from "gas-metering/MainnetMetering.sol"; | ||
|
||
using ERC4337Utils for IEntryPoint; | ||
using ERC4337Utils for Kernel; | ||
|
||
/// @dev File path for the base json file (that will be copy pasted for new tests) | ||
string constant baseJsonFilePath = "./gas/validator/base.json"; | ||
|
||
/// @dev Global test contract that will output benchmark result inside a json file | ||
/// @notice Build to output a json like that: {"network": {"testCase": "gasUsed"}} | ||
/// @author KONFeature | ||
abstract contract JsonBenchmarkerTest is Test { | ||
/// @dev Check if the json writer is enabled or not | ||
bool private _isWriteEnabled; | ||
|
||
/// @dev The key we will use to write stuff in our json, to parallelise a few things | ||
string private _writerKey; | ||
|
||
/// @dev The JSON output of the benchmark | ||
string private _outputFilePath; | ||
|
||
/// @dev Init the base stuff required to run the benchmark | ||
function _initJsonWriter() internal { | ||
_isWriteEnabled = vm.envOr("WRITE_BENCHMARK_RESULT", false); | ||
|
||
// Early exit if write not enable | ||
if (!_isWriteEnabled) { | ||
return; | ||
} | ||
|
||
// Check if the file exist | ||
_outputFilePath = string.concat("./gas/validator/", _getOutputFileName(), ".json"); | ||
if (!vm.exists(_outputFilePath)) { | ||
// If not, create the initial version of it | ||
vm.copyFile(baseJsonFilePath, _outputFilePath); | ||
} | ||
} | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Abstract methods */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
/// @dev Get the current output file name | ||
function _getOutputFileName() internal view virtual returns (string memory); | ||
|
||
/* -------------------------------------------------------------------------- */ | ||
/* Utility methods */ | ||
/* -------------------------------------------------------------------------- */ | ||
|
||
/// @dev Only execute the method if json write is enabled | ||
modifier _onlyIfJsonWriteEnabled() { | ||
if (_isWriteEnabled) { | ||
_; | ||
} | ||
} | ||
|
||
/// @dev Add benchmark result to the json and log it | ||
function _addResult(string memory _key, string memory _testCase, uint256 _gasUsed) | ||
internal | ||
_onlyIfJsonWriteEnabled | ||
{ | ||
// Build the json key path of the result | ||
string memory keyPath = string.concat(".", _key, ".", _testCase); | ||
|
||
// Add it to the json | ||
vm.writeJson(LibString.toString(_gasUsed), _outputFilePath, keyPath); | ||
} | ||
} |