Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

danielstevenberger
Copy link

Add MockEntropy Contract for Local Testing

Description:

This PR adds the MockEntropy contract to facilitate local testing and development. The contract includes the following functionalities:

  • Default Provider: Returns the contract's address.
  • Fee Calculation: Provides a fixed fee for entropy requests.
  • Request Handling: Allows users to request entropy with a callback.
  • Flexible Callback Execution: The triggerCallback function lets users specify the sequence number, random number, and callback contract.

Key Feature:

triggerCallback enables flexible and customizable callback handling for testing purposes.

Copy link

vercel bot commented Jun 28, 2024

@danielstevenberger is attempting to deploy a commit to the pyth-web Team on Vercel.

A member of the Team first needs to authorize it.

bytes32 randomNumberBytes = bytes32(_randomNumber);
IEntropyConsumer(callbackAddress)._entropyCallback(
_sequenceNumber,
callbackAddress,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be the provider

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made to be provider


function triggerCallback(
uint256 _randomNumber,
uint64 _sequenceNumber
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is there a leading _ here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed all unnecessary "_"

uint64 _sequenceNumber
) external {
address callbackAddress = callbackRequests[_sequenceNumber];
require(callbackAddress != address(0), "No pending request");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 NoSuchRequest

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use EntropyErrors.sol

address _provider,
bytes32 _userRandomNumber
) external payable isProvider(_provider) returns (uint64) {
require(msg.value >= fee, "Not enough ether sent for fee");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use EntropyErrors.sol

}

modifier isProvider(address _provider) {
require(_provider == address(this), "Invalid provider address");
Copy link
Collaborator

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use EntropyErrors.sol

Removed unnecessary "_" in inputs
Utilized EntropyErrors.sol errors
import {IEntropyConsumer} from "./IEntropyConsumer.sol";
import "./EntropyErrors.sol";

contract MockEntropy {
Copy link
Contributor

@jayantk jayantk Jul 16, 2024

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.

Copy link
Contributor

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)

Copy link
Contributor

@jayantk jayantk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach generally makes sense. I have a couple asks in the comments here:

  1. Please implement all parts of the IEntropy interface that an end user could reasonably invoke. You can ignore the methods that only providers will call, but there are other getters that should be implemented on the mock
  2. Please write a unit test using the mock
  3. please document how to use the mock in a doc comment on the contract + a method comment on the triggerCallback method

}

function getDefaultProvider() external view returns (address) {
return address(this);
Copy link
Contributor

Choose a reason for hiding this comment

The 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).

import {IEntropyConsumer} from "./IEntropyConsumer.sol";
import "./EntropyErrors.sol";

contract MockEntropy {
Copy link
Contributor

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)

import {IEntropyConsumer} from "./IEntropyConsumer.sol";
import "./EntropyErrors.sol";

contract MockEntropy {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

@danielstevenberger
Copy link
Author

danielstevenberger commented Jul 18, 2024

@jayantk please first confirm this is what you're looking for. I've done testing in my local with hardhat for price and triggering the callback that I can add here using forge if this approach aligns.

The functions below are all not implemented

  • register
  • reveal
  • revealWithCallback

The default provider get's registered in the constructor
All the other functions that are dependent on provider now use getDefaultProvider() instead of msg.sender

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants