Skip to content

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

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions target_chains/ethereum/entropy_sdk/solidity/MockEntropy.sol
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 {
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

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

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

}

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");
Copy link
Contributor

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

callbackRequests[currentSequenceNumber] = msg.sender;
return currentSequenceNumber++;
}

function triggerCallback(
uint256 _randomNumber,
uint64 _sequenceNumber
Copy link
Contributor

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 "_"

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

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

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

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

randomNumberBytes
);
callbackRequests[_sequenceNumber] = address(0);
}

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

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

_;
}
}