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

[entropy] Rename files #1127

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

pragma solidity ^0.8.0;

import "@pythnetwork/entropy-sdk-solidity/PythRandomState.sol";
import "@pythnetwork/entropy-sdk-solidity/PythRandomErrors.sol";
import "@pythnetwork/entropy-sdk-solidity/PythRandomEvents.sol";
import "@pythnetwork/entropy-sdk-solidity/EntropyStructs.sol";
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah this is a much better name in retrospect.

import "@pythnetwork/entropy-sdk-solidity/EntropyErrors.sol";
import "@pythnetwork/entropy-sdk-solidity/EntropyEvents.sol";
import "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
import "./EntropyState.sol";

// PythRandom implements a secure 2-party random number generation procedure. The protocol
// Entropy implements a secure 2-party random number generation procedure. The protocol
// is an extension of a simple commit/reveal protocol. The original version has the following steps:
//
// 1. Two parties A and B each draw a random number x_{A,B}
Expand All @@ -20,7 +21,7 @@ import "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
// Thus, neither party needs to trust the other -- as long as they are themselves honest, they can
// ensure that the result r is random.
//
// PythRandom implements a version of this protocol that is optimized for on-chain usage. The
// Entropy implements a version of this protocol that is optimized for on-chain usage. The
// key difference is that one of the participants (the provider) commits to a sequence of random numbers
// up-front using a hash chain. Users of the protocol then simply grab the next random number in the sequence.
//
Expand Down Expand Up @@ -51,9 +52,9 @@ import "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
// be careful to ensure their off-chain service isn't compromised to reveal the random numbers -- if this occurs,
// then users will be able to influence the random number r.
//
// The PythRandom implementation of the above protocol allows anyone to permissionlessly register to be a
// The Entropy implementation of the above protocol allows anyone to permissionlessly register to be a
// randomness provider. Users then choose which provider to request randomness from. Each provider can set
// their own fee for the service. In addition, the PythRandom contract charges a flat fee that goes to the
// their own fee for the service. In addition, the Entropy contract charges a flat fee that goes to the
// Pyth protocol for each requested random number. Fees are paid in the native token of the network.
//
// This implementation has two intricacies that merit further explanation. First, the implementation supports
Expand All @@ -79,7 +80,7 @@ import "@pythnetwork/entropy-sdk-solidity/IEntropy.sol";
// - function to check invariants??
// - need to increment pyth fees if someone transfers funds to the contract via another method
// - off-chain data ERC support?
contract PythRandom is IEntropy, PythRandomState {
contract Entropy is IEntropy, EntropyState {
// TODO: Use an upgradeable proxy
constructor(uint pythFeeInWei) {
_state.accruedPythFeesInWei = 0;
Expand All @@ -97,9 +98,9 @@ contract PythRandom is IEntropy, PythRandomState {
bytes32 commitmentMetadata,
uint64 chainLength
) public override {
if (chainLength == 0) revert PythRandomErrors.AssertionFailure();
if (chainLength == 0) revert EntropyErrors.AssertionFailure();

PythRandomStructs.ProviderInfo storage provider = _state.providers[
EntropyStructs.ProviderInfo storage provider = _state.providers[
msg.sender
];

Expand All @@ -126,7 +127,7 @@ contract PythRandom is IEntropy, PythRandomState {
// Calling this function will transfer `amount` wei to the caller (provided that they have accrued a sufficient
// balance of fees in the contract).
function withdraw(uint256 amount) public override {
PythRandomStructs.ProviderInfo storage providerInfo = _state.providers[
EntropyStructs.ProviderInfo storage providerInfo = _state.providers[
msg.sender
];

Expand Down Expand Up @@ -157,26 +158,26 @@ contract PythRandom is IEntropy, PythRandomState {
bytes32 userCommitment,
bool useBlockHash
) public payable override returns (uint64 assignedSequenceNumber) {
PythRandomStructs.ProviderInfo storage providerInfo = _state.providers[
EntropyStructs.ProviderInfo storage providerInfo = _state.providers[
provider
];
if (_state.providers[provider].sequenceNumber == 0)
revert PythRandomErrors.NoSuchProvider();
revert EntropyErrors.NoSuchProvider();

// Assign a sequence number to the request
assignedSequenceNumber = providerInfo.sequenceNumber;
if (assignedSequenceNumber >= providerInfo.endSequenceNumber)
revert PythRandomErrors.OutOfRandomness();
revert EntropyErrors.OutOfRandomness();
providerInfo.sequenceNumber += 1;

// Check that fees were paid and increment the pyth / provider balances.
uint requiredFee = getFee(provider);
if (msg.value < requiredFee) revert PythRandomErrors.InsufficientFee();
if (msg.value < requiredFee) revert EntropyErrors.InsufficientFee();
providerInfo.accruedFeesInWei += providerInfo.feeInWei;
_state.accruedPythFeesInWei += (msg.value - providerInfo.feeInWei);

// Store the user's commitment so that we can fulfill the request later.
PythRandomStructs.Request storage req = _state.requests[
EntropyStructs.Request storage req = _state.requests[
requestKey(provider, assignedSequenceNumber)
];
req.provider = provider;
Expand Down Expand Up @@ -210,21 +211,21 @@ contract PythRandom is IEntropy, PythRandomState {
// TODO: do we need to check that this request exists?
// TODO: this method may need to be authenticated to prevent griefing
bytes32 key = requestKey(provider, sequenceNumber);
PythRandomStructs.Request storage req = _state.requests[key];
EntropyStructs.Request storage req = _state.requests[key];
// This invariant should be guaranteed to hold by the key construction procedure above, but check it
// explicitly to be extra cautious.
if (req.sequenceNumber != sequenceNumber)
revert PythRandomErrors.AssertionFailure();
revert EntropyErrors.AssertionFailure();

bool valid = isProofValid(
req.providerCommitmentSequenceNumber,
req.providerCommitment,
sequenceNumber,
providerRevelation
);
if (!valid) revert PythRandomErrors.IncorrectProviderRevelation();
if (!valid) revert EntropyErrors.IncorrectProviderRevelation();
if (constructUserCommitment(userRandomness) != req.userCommitment)
revert PythRandomErrors.IncorrectUserRevelation();
revert EntropyErrors.IncorrectUserRevelation();

bytes32 blockHash = bytes32(uint256(0));
if (req.blockNumber != 0) {
Expand All @@ -247,7 +248,7 @@ contract PythRandom is IEntropy, PythRandomState {

delete _state.requests[key];

PythRandomStructs.ProviderInfo storage providerInfo = _state.providers[
EntropyStructs.ProviderInfo storage providerInfo = _state.providers[
provider
];
if (providerInfo.currentCommitmentSequenceNumber < sequenceNumber) {
Expand All @@ -258,19 +259,14 @@ contract PythRandom is IEntropy, PythRandomState {

function getProviderInfo(
address provider
)
public
view
override
returns (PythRandomStructs.ProviderInfo memory info)
{
) public view override returns (EntropyStructs.ProviderInfo memory info) {
info = _state.providers[provider];
}

function getRequest(
address provider,
uint64 sequenceNumber
) public view override returns (PythRandomStructs.Request memory req) {
) public view override returns (EntropyStructs.Request memory req) {
bytes32 key = requestKey(provider, sequenceNumber);
req = _state.requests[key];
}
Expand Down Expand Up @@ -323,7 +319,7 @@ contract PythRandom is IEntropy, PythRandomState {
bytes32 revelation
) internal pure returns (bool valid) {
if (sequenceNumber <= lastSequenceNumber)
revert PythRandomErrors.AssertionFailure();
revert EntropyErrors.AssertionFailure();

bytes32 currentHash = revelation;
while (sequenceNumber > lastSequenceNumber) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache 2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

very minor change here: we were exposing the state object in the public SDK. I moved that definition into the contract directory instead.


pragma solidity ^0.8.0;

import "@pythnetwork/entropy-sdk-solidity/EntropyStructs.sol";

contract EntropyInternalStructs {
struct State {
uint pythFeeInWei;
uint accruedPythFeesInWei;
mapping(address => EntropyStructs.ProviderInfo) providers;
mapping(bytes32 => EntropyStructs.Request) requests;
}
}

contract EntropyState {
EntropyInternalStructs.State _state;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,16 @@

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "forge-std/Test.sol";

import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
import "@pythnetwork/pyth-sdk-solidity/PythErrors.sol";
import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
import "./utils/WormholeTestUtils.t.sol";
import "./utils/PythTestUtils.t.sol";
import "./utils/RandTestUtils.t.sol";
import "../contracts/random/PythRandom.sol";
import "@pythnetwork/entropy-sdk-solidity/EntropyStructs.sol";
import "../contracts/entropy/Entropy.sol";

// TODO
// - what's the impact of # of in-flight requests on gas usage? More requests => more hashes to
// verify the provider's value.
// - fuzz test?
contract PythRandomTest is Test, RandTestUtils {
PythRandom public random;
contract EntropyTest is Test {
Entropy public random;

uint pythFeeInWei = 7;

Expand All @@ -39,7 +32,7 @@ contract PythRandomTest is Test, RandTestUtils {
bytes32 ALL_ZEROS = bytes32(uint256(0));

function setUp() public {
random = new PythRandom(pythFeeInWei);
random = new Entropy(pythFeeInWei);

bytes32[] memory hashChain1 = generateHashChain(
provider1,
Expand Down Expand Up @@ -182,7 +175,7 @@ contract PythRandomTest is Test, RandTestUtils {
random.getAccruedPythFees();
assertEq(address(random).balance, expectedBalance);

PythRandomStructs.ProviderInfo memory info1 = random.getProviderInfo(
EntropyStructs.ProviderInfo memory info1 = random.getProviderInfo(
provider1
);
assert(
Expand All @@ -191,7 +184,7 @@ contract PythRandomTest is Test, RandTestUtils {
);
assert(info1.currentCommitmentSequenceNumber < info1.sequenceNumber);
assert(info1.sequenceNumber <= info1.endSequenceNumber);
PythRandomStructs.ProviderInfo memory info2 = random.getProviderInfo(
EntropyStructs.ProviderInfo memory info2 = random.getProviderInfo(
provider2
);
assert(
Expand Down Expand Up @@ -337,7 +330,7 @@ contract PythRandomTest is Test, RandTestUtils {
10
);
assertInvariants();
PythRandomStructs.ProviderInfo memory info1 = random.getProviderInfo(
EntropyStructs.ProviderInfo memory info1 = random.getProviderInfo(
provider1
);
assertEq(info1.endSequenceNumber, newHashChainOffset + 10);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

library PythRandomErrors {
library EntropyErrors {
// An invariant of the contract failed to hold. This error indicates a software logic bug.
error AssertionFailure();
// The provider being registered has already registered
Expand Down
18 changes: 18 additions & 0 deletions target_chains/ethereum/entropy_sdk/solidity/EntropyEvents.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "./EntropyStructs.sol";

interface EntropyEvents {
event Registered(EntropyStructs.ProviderInfo provider);

event Requested(EntropyStructs.Request request);

event Revealed(
EntropyStructs.Request request,
bytes32 userRevelation,
bytes32 providerRevelation,
bytes32 blockHash,
bytes32 randomNumber
);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// contracts/State.sol
// SPDX-License-Identifier: Apache 2

pragma solidity ^0.8.0;

contract PythRandomStructs {
contract EntropyStructs {
struct State {
uint pythFeeInWei;
uint accruedPythFeesInWei;
Expand Down Expand Up @@ -49,7 +48,3 @@ contract PythRandomStructs {
uint256 blockNumber;
}
}

contract PythRandomState {
PythRandomStructs.State _state;
}
8 changes: 4 additions & 4 deletions target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: Apache 2
pragma solidity ^0.8.0;

import "./PythRandomEvents.sol";
import "./EntropyEvents.sol";

interface IEntropy is PythRandomEvents {
interface IEntropy is EntropyEvents {
// Register msg.sender as a randomness provider. The arguments are the provider's configuration parameters
// and initial commitment. Re-registering the same provider rotates the provider's commitment (and updates
// the feeInWei).
Expand Down Expand Up @@ -53,12 +53,12 @@ interface IEntropy is PythRandomEvents {

function getProviderInfo(
address provider
) external view returns (PythRandomStructs.ProviderInfo memory info);
) external view returns (EntropyStructs.ProviderInfo memory info);

function getRequest(
address provider,
uint64 sequenceNumber
) external view returns (PythRandomStructs.Request memory req);
) external view returns (EntropyStructs.Request memory req);

function getFee(address provider) external view returns (uint feeAmount);

Expand Down
18 changes: 0 additions & 18 deletions target_chains/ethereum/entropy_sdk/solidity/PythRandomEvents.sol

This file was deleted.

Loading