Skip to content

Commit

Permalink
test new abi
Browse files Browse the repository at this point in the history
  • Loading branch information
jayantk committed Nov 27, 2023
1 parent a7cc2c9 commit 254eca0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
12 changes: 3 additions & 9 deletions target_chains/ethereum/contracts/contracts/entropy/Entropy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ import "./EntropyState.sol";
// protocol prior to the random number being revealed (i.e., prior to step (6) above). Integrators should ensure that
// the user is always incentivized to reveal their random number, and that the protocol has an escape hatch for
// cases where the user chooses not to reveal.
//
// TODOs:
// - governance??
// - correct method access modifiers (public vs external)
// - gas optimizations
// - 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 Entropy is IEntropy, EntropyState {
// TODO: Use an upgradeable proxy
constructor(uint pythFeeInWei, address defaultProvider) {
Expand All @@ -97,7 +89,8 @@ contract Entropy is IEntropy, EntropyState {
uint feeInWei,
bytes32 commitment,
bytes calldata commitmentMetadata,
uint64 chainLength
uint64 chainLength,
bytes calldata uri
) public override {
if (chainLength == 0) revert EntropyErrors.AssertionFailure();

Expand All @@ -118,6 +111,7 @@ contract Entropy is IEntropy, EntropyState {
provider.currentCommitmentSequenceNumber = provider.sequenceNumber;
provider.commitmentMetadata = commitmentMetadata;
provider.endSequenceNumber = provider.sequenceNumber + chainLength;
provider.uri = uri;

provider.sequenceNumber += 1;

Expand Down
49 changes: 43 additions & 6 deletions target_chains/ethereum/contracts/forge-test/Entropy.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ contract EntropyTest is Test {
bytes32[] provider1Proofs;
uint provider1FeeInWei = 8;
uint64 provider1ChainLength = 100;
bytes provider1Uri = bytes("https://foo.com");
bytes provider1CommitmentMetadata = hex"0100";

address public provider2 = address(2);
bytes32[] provider2Proofs;
uint provider2FeeInWei = 20;
bytes provider2Uri = bytes("https://bar.com");

address public user1 = address(3);
address public user2 = address(4);
Expand All @@ -44,14 +47,21 @@ contract EntropyTest is Test {
random.register(
provider1FeeInWei,
provider1Proofs[0],
hex"0100",
provider1ChainLength
provider1CommitmentMetadata,
provider1ChainLength,
provider1Uri
);

bytes32[] memory hashChain2 = generateHashChain(provider2, 0, 100);
provider2Proofs = hashChain2;
vm.prank(provider2);
random.register(provider2FeeInWei, provider2Proofs[0], hex"0200", 100);
random.register(
provider2FeeInWei,
provider2Proofs[0],
hex"0200",
100,
provider2Uri
);
}

function generateHashChain(
Expand Down Expand Up @@ -346,7 +356,13 @@ contract EntropyTest is Test {
10
);
vm.prank(provider1);
random.register(provider1FeeInWei, newHashChain[0], hex"0100", 10);
random.register(
provider1FeeInWei,
newHashChain[0],
hex"0100",
10,
provider1Uri
);
assertInvariants();
EntropyStructs.ProviderInfo memory info1 = random.getProviderInfo(
provider1
Expand Down Expand Up @@ -415,7 +431,13 @@ contract EntropyTest is Test {

// Check that overflowing the fee arithmetic causes the transaction to revert.
vm.prank(provider1);
random.register(MAX_UINT256, provider1Proofs[0], hex"0100", 100);
random.register(
MAX_UINT256,
provider1Proofs[0],
hex"0100",
100,
provider1Uri
);
vm.expectRevert();
random.getFee(provider1);
}
Expand Down Expand Up @@ -465,7 +487,13 @@ contract EntropyTest is Test {

// Reregistering updates the required fees
vm.prank(provider1);
random.register(12345, provider1Proofs[0], hex"0100", 100);
random.register(
12345,
provider1Proofs[0],
hex"0100",
100,
provider1Uri
);

assertRequestReverts(pythFeeInWei + 12345 - 1, provider1, 42, false);
requestWithFee(user2, pythFeeInWei + 12345, provider1, 42, false);
Expand Down Expand Up @@ -494,4 +522,13 @@ contract EntropyTest is Test {
vm.expectRevert();
random.withdraw(providerOneBalance);
}

function testGetProviderInfo() public {
EntropyStructs.ProviderInfo memory providerInfo1 = random
.getProviderInfo(provider1);
// These two fields aren't used by the Entropy contract itself -- they're just convenient info to store
// on-chain -- so they aren't tested in the other tests.
assertEq(providerInfo1.uri, provider1Uri);
assertEq(providerInfo1.commitmentMetadata, provider1CommitmentMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ contract EntropyStructs {
// Metadata for the current commitment. Providers may optionally use this field to to help
// manage rotations (i.e., to pick the sequence number from the correct hash chain).
bytes commitmentMetadata;
// Optional URI where clients can retrieve revelations for the provider.
// Client SDKs can use this field to automatically determine how to retrieve random values for each provider.
// TODO: specify the API that must be implemented at this URI
bytes uri;
// The first sequence number that is *not* included in the current commitment (i.e., an exclusive end index).
// The contract maintains the invariant that sequenceNumber <= endSequenceNumber.
// If sequenceNumber == endSequenceNumber, the provider must rotate their commitment to add additional random values.
Expand Down
3 changes: 2 additions & 1 deletion target_chains/ethereum/entropy_sdk/solidity/IEntropy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ interface IEntropy is EntropyEvents {
uint feeInWei,
bytes32 commitment,
bytes calldata commitmentMetadata,
uint64 chainLength
uint64 chainLength,
bytes calldata uri
) external;

// Withdraw a portion of the accumulated fees for the provider msg.sender.
Expand Down

0 comments on commit 254eca0

Please sign in to comment.