Skip to content

Commit

Permalink
gas optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
doublesharp committed Feb 24, 2024
1 parent f2e456a commit 6336c5d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
15 changes: 9 additions & 6 deletions contracts/SamWitchVRF.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {ISamWitchVRF} from "./interfaces/ISamWitchVRF.sol";
/// @author Sam Witch (SamWitchVRF & Estfor Kingdom)
/// @notice This contract listens for requests for VRF, and allows the oracle to fulfill random numbers
contract SamWitchVRF is ISamWitchVRF, UUPSUpgradeable, OwnableUpgradeable {
mapping(address consumer => uint64 nonce) public consumers;
mapping(address consumer => uint256 nonce) public consumers;
mapping(address oracles => bool isOracle) public oracles;
mapping(bytes32 requestId => bytes32 commitment) private requestCommitments;

Expand Down Expand Up @@ -42,13 +42,16 @@ contract SamWitchVRF is ISamWitchVRF, UUPSUpgradeable, OwnableUpgradeable {
uint256 callbackGasLimit
) external override returns (bytes32 requestId) {
address consumer = _msgSender();
uint64 currentNonce = consumers[consumer];
if (currentNonce == 0) {
uint256 nonce = consumers[consumer];
if (nonce == 0) {
revert InvalidConsumer(consumer);
}

uint64 nonce = ++currentNonce;
consumers[consumer] = currentNonce;
unchecked {
nonce += 1;
}

consumers[consumer] = nonce;
requestId = _computeRequestId(consumer, nonce);

requestCommitments[requestId] = keccak256(
Expand Down Expand Up @@ -115,7 +118,7 @@ contract SamWitchVRF is ISamWitchVRF, UUPSUpgradeable, OwnableUpgradeable {
emit ConsumerRegistered(_consumer);
}

function _computeRequestId(address sender, uint64 nonce) private pure returns (bytes32) {
function _computeRequestId(address sender, uint256 nonce) private pure returns (bytes32) {
return keccak256(abi.encodePacked(sender, nonce));
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/ISamWitchVRF.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ISamWitchVRF {
uint256 callbackGasLimit,
uint256 numWords,
address consumer,
uint64 nonce
uint256 nonce
);
event RandomWordsFulfilled(bytes32 requestId, uint[] randomWords, address oracle);

Expand Down

0 comments on commit 6336c5d

Please sign in to comment.