EntropyGasBenchmark.t.sol 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "forge-std/Test.sol";
  4. import "@pythnetwork/entropy-sdk-solidity/EntropyStructs.sol";
  5. import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
  6. import "./utils/EntropyTestUtils.t.sol";
  7. import "../contracts/entropy/EntropyUpgradable.sol";
  8. // TODO
  9. // - what's the impact of # of in-flight requests on gas usage? More requests => more hashes to
  10. // verify the provider's value.
  11. contract EntropyGasBenchmark is Test, EntropyTestUtils {
  12. ERC1967Proxy public proxy;
  13. EntropyUpgradable public random;
  14. uint128 pythFeeInWei = 7;
  15. address public provider1 = address(1);
  16. bytes32[] provider1Proofs;
  17. uint128 provider1FeeInWei = 8;
  18. uint64 provider1ChainLength = 100;
  19. address public user1 = address(3);
  20. function setUp() public {
  21. EntropyUpgradable _random = new EntropyUpgradable();
  22. // deploy proxy contract and point it to implementation
  23. proxy = new ERC1967Proxy(address(_random), "");
  24. // wrap in ABI to support easier calls
  25. random = EntropyUpgradable(address(proxy));
  26. random.initialize(
  27. address(4),
  28. address(5),
  29. pythFeeInWei,
  30. provider1,
  31. false
  32. );
  33. bytes32[] memory hashChain1 = generateHashChain(
  34. provider1,
  35. 0,
  36. provider1ChainLength
  37. );
  38. provider1Proofs = hashChain1;
  39. vm.prank(provider1);
  40. random.register(
  41. provider1FeeInWei,
  42. provider1Proofs[0],
  43. hex"0100",
  44. provider1ChainLength,
  45. ""
  46. );
  47. // Register twice so the commitment sequence number is nonzero. Zero values can be misleading
  48. // when gas benchmarking.
  49. vm.prank(provider1);
  50. random.register(
  51. provider1FeeInWei,
  52. provider1Proofs[0],
  53. hex"0100",
  54. provider1ChainLength,
  55. ""
  56. );
  57. assert(
  58. random.getProviderInfo(provider1).currentCommitmentSequenceNumber !=
  59. 0
  60. );
  61. }
  62. // Test helper method for requesting a random value as user from provider.
  63. function requestHelper(
  64. address user,
  65. uint randomNumber,
  66. bool useBlockhash
  67. ) public returns (uint64 sequenceNumber) {
  68. uint fee = random.getFee(provider1);
  69. vm.deal(user, fee);
  70. vm.prank(user);
  71. sequenceNumber = random.request{value: fee}(
  72. provider1,
  73. random.constructUserCommitment(bytes32(randomNumber)),
  74. useBlockhash
  75. );
  76. }
  77. function revealHelper(
  78. uint64 sequenceNumber,
  79. uint userRandom
  80. ) public returns (bytes32 randomNumber) {
  81. randomNumber = random.reveal(
  82. provider1,
  83. sequenceNumber,
  84. bytes32(userRandom),
  85. provider1Proofs[
  86. sequenceNumber -
  87. random
  88. .getProviderInfo(provider1)
  89. .originalCommitmentSequenceNumber
  90. ]
  91. );
  92. }
  93. function testBasicFlow() public {
  94. uint userRandom = 42;
  95. vm.roll(10);
  96. uint64 sequenceNumber = requestHelper(user1, userRandom, true);
  97. vm.roll(12);
  98. revealHelper(sequenceNumber, userRandom);
  99. }
  100. }