-
Notifications
You must be signed in to change notification settings - Fork 267
Create MockEntropy.sol #1745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create MockEntropy.sol #1745
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: Apache 2 | ||
pragma solidity ^0.8.0; | ||
|
||
import {IEntropyConsumer} from "./IEntropyConsumer.sol"; | ||
|
||
contract MockEntropy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please document this mock contract and how to use it Please also add a unit test in https://github.com/pyth-network/pyth-crosschain/tree/main/target_chains/ethereum/contracts/forge-test to validate that this mock behaves as expected |
||
uint256 public fee; | ||
|
||
mapping(uint256 => address) public callbackRequests; | ||
uint64 public currentSequenceNumber; | ||
|
||
constructor(uint256 _fee) { | ||
fee = _fee; | ||
} | ||
|
||
function getDefaultProvider() external view returns (address) { | ||
return address(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest requiring the caller to pass a provider address as a constructor argument. Using the address of the deployed contract could cause mixups in the tested code (e.g., it won't catch a bug where someone passes the Entropy contract address as the provider address by mistake). |
||
} | ||
|
||
function getFee( | ||
address _provider | ||
) external view isProvider(_provider) returns (uint256) { | ||
return fee; | ||
} | ||
|
||
function setFee(uint256 _fee) external { | ||
fee = _fee; | ||
} | ||
|
||
function requestWithCallback( | ||
address _provider, | ||
bytes32 _userRandomNumber | ||
) external payable isProvider(_provider) returns (uint64) { | ||
require(msg.value >= fee, "Not enough ether sent for fee"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the same errors we have in EntropyErrors.sol. Here it should be InsufficientFee There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use EntropyErrors.sol |
||
callbackRequests[currentSequenceNumber] = msg.sender; | ||
return currentSequenceNumber++; | ||
} | ||
|
||
function triggerCallback( | ||
uint256 _randomNumber, | ||
uint64 _sequenceNumber | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is there a leading There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed all unnecessary "_" |
||
) external { | ||
address callbackAddress = callbackRequests[_sequenceNumber]; | ||
require(callbackAddress != address(0), "No pending request"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the same errors we have in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use EntropyErrors.sol |
||
bytes32 randomNumberBytes = bytes32(_randomNumber); | ||
hanbao-dev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
IEntropyConsumer(callbackAddress)._entropyCallback( | ||
_sequenceNumber, | ||
callbackAddress, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be the provider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made to be provider |
||
randomNumberBytes | ||
); | ||
callbackRequests[_sequenceNumber] = address(0); | ||
} | ||
|
||
modifier isProvider(address _provider) { | ||
require(_provider == address(this), "Invalid provider address"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use the same errors we have in EntropyErrors.sol. Here it should be NoSuchProvider There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use EntropyErrors.sol |
||
_; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should inherit from
IEntropy
and define all of the methods there, so it's a drop-in replacement for testing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(it's ok to throw an error saying "not implemented" for methods like
register
which are targeted toward providers though)