-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Invariant helpers and mockVRF (#26)
* feat: Invariant helpers and mockVRF * fix: Lockfile * fix: Update prettier command --------- Co-authored-by: Hiroshi <[email protected]>
- Loading branch information
Showing
4 changed files
with
1,269 additions
and
430 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,40 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.20; | ||
|
||
import {CommonBase} from "forge-std/Base.sol"; | ||
import {StdCheats} from "forge-std/StdCheats.sol"; | ||
import {StdUtils} from "forge-std/StdUtils.sol"; | ||
|
||
contract InvariantTestHelpers is CommonBase, StdCheats, StdUtils { | ||
address[100] internal actors; | ||
address internal currentActor; | ||
bool internal callsMustBeValid; | ||
mapping(bytes => uint256) internal calls; | ||
|
||
constructor() { | ||
_populateActors(); | ||
_setCallsMustBeValid(); | ||
} | ||
|
||
modifier useActor(uint256 actorIndexSeed) { | ||
currentActor = actors[bound(actorIndexSeed, 0, 99)]; | ||
vm.startPrank(currentActor); | ||
_; | ||
vm.stopPrank(); | ||
} | ||
|
||
modifier countCall(bytes memory key) { | ||
calls[key]++; | ||
_; | ||
} | ||
|
||
function _populateActors() internal { | ||
for (uint256 i; i < 100; i++) { | ||
actors[i] = address(uint160(uint256(keccak256(abi.encodePacked(i))))); | ||
} | ||
} | ||
|
||
function _setCallsMustBeValid() internal { | ||
callsMustBeValid = vm.envBool("FOUNDRY_INVARIANT_FAIL_ON_REVERT"); | ||
} | ||
} |
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,45 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.20; | ||
|
||
import {VRFConsumerBaseV2} from "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol"; | ||
|
||
contract MockVRFCoordinatorV2 { | ||
VRFConsumerBaseV2 public vrfConsumerBaseV2; | ||
uint256 public requestCount; | ||
uint256[] public requestIds; | ||
|
||
function setVRFConsumerBaseV2(address _vrfConsumerBaseV2) external { | ||
vrfConsumerBaseV2 = VRFConsumerBaseV2(_vrfConsumerBaseV2); | ||
} | ||
|
||
function requestRandomWords( | ||
bytes32 keyHash, | ||
uint64 subId, | ||
uint16 requestConfirmations, | ||
uint32 callbackGasLimit, | ||
uint32 numWords | ||
) external returns (uint256 requestId) { | ||
requestId = uint256( | ||
keccak256(abi.encodePacked(keyHash, subId, requestConfirmations, callbackGasLimit, numWords, requestCount)) | ||
); | ||
requestIds.push(requestId); | ||
requestCount++; | ||
} | ||
|
||
function fulfillRandomWords(uint256 randomWord) external returns (uint256 requestId) { | ||
uint256 requestsInQueue = requestIds.length; | ||
if (requestsInQueue == 0) { | ||
return 0; | ||
} | ||
|
||
requestId = requestIds[requestsInQueue - 1]; | ||
if (requestId == 0) return requestId; | ||
|
||
uint256[] memory randomWords = new uint256[](1); | ||
randomWords[0] = randomWord; | ||
|
||
vrfConsumerBaseV2.rawFulfillRandomWords(requestId, randomWords); | ||
|
||
requestIds.pop(); | ||
} | ||
} |
Oops, something went wrong.