GasBenchmark.t.sol 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: Apache 2
  2. pragma solidity ^0.8.0;
  3. import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
  4. import "forge-std/Test.sol";
  5. import "@pythnetwork/pyth-sdk-solidity/IPyth.sol";
  6. import "@pythnetwork/pyth-sdk-solidity/PythErrors.sol";
  7. import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
  8. import "./utils/WormholeTestUtils.t.sol";
  9. import "./utils/PythTestUtils.t.sol";
  10. contract GasBenchmark is Test, WormholeTestUtils, PythTestUtils {
  11. // 19, current mainnet number of guardians, is used to have gas estimates
  12. // close to our mainnet transactions.
  13. uint8 constant NUM_GUARDIANS = 19;
  14. // 2/3 of the guardians should sign a message for a VAA which is 13 out of 19 guardians.
  15. // It is possible to have more signers but the median seems to be 13.
  16. uint8 constant NUM_GUARDIAN_SIGNERS = 13;
  17. // Our mainnet transactions use 5 prices to form a batch of 5 prices,
  18. // but Pulse allows parsing up to 10 requests in a single requests, so we set up 10 prices.
  19. uint8 constant NUM_PRICES = 10;
  20. // We will have less than 2^11=2048 price for a foreseeable future.
  21. uint8 constant MERKLE_TREE_DEPTH = 11;
  22. IWormhole public wormhole;
  23. IPyth public pyth;
  24. bytes32[] priceIds;
  25. // Cached prices are populated in the setUp
  26. PythStructs.Price[] cachedPrices;
  27. uint64[] cachedPricesPublishTimes;
  28. bytes[][] cachedPricesUpdateData; // i th element contains the update data for the first i prices
  29. bytes[] allCachedPricesUpdateData; // the update data for all prices
  30. uint[] cachedPricesUpdateFee; // i th element contains the update fee for the first i prices
  31. uint allCachedPricesUpdateFee; // the update fee for all prices
  32. // Fresh prices are different prices that can be used
  33. // as a fresh price to update the prices
  34. PythStructs.Price[] freshPrices;
  35. uint64[] freshPricesPublishTimes;
  36. bytes[][] freshPricesUpdateData; // i th element contains the update data for the first i prices
  37. bytes[] allFreshPricesUpdateData; // the update data for all prices
  38. uint[] freshPricesUpdateFee; // i th element contains the update fee for the first i prices
  39. uint allFreshPricesUpdateFee; // the update fee for all prices
  40. uint64 sequence;
  41. uint randomSeed;
  42. function setUp() public {
  43. address wormholeAddr = setUpWormholeReceiver(NUM_GUARDIANS);
  44. wormhole = IWormhole(wormholeAddr);
  45. pyth = IPyth(setUpPyth(wormholeAddr));
  46. priceIds = new bytes32[](NUM_PRICES);
  47. priceIds[0] = bytes32(
  48. 0x1000000000000000000000000000000000000000000000000000000000000f00
  49. );
  50. for (uint i = 1; i < NUM_PRICES; ++i) {
  51. priceIds[i] = bytes32(uint256(priceIds[i - 1]) + 1);
  52. }
  53. for (uint i = 0; i < NUM_PRICES; ++i) {
  54. uint64 publishTime = uint64(getRand() % 10) + 1; // to make sure prevPublishTime is >= 0
  55. cachedPrices.push(
  56. PythStructs.Price(
  57. int64(uint64(getRand() % 1000)), // Price
  58. uint64(getRand() % 100), // Confidence
  59. -5, // Expo
  60. publishTime
  61. )
  62. );
  63. cachedPricesPublishTimes.push(publishTime);
  64. publishTime += uint64(getRand() % 10);
  65. freshPrices.push(
  66. PythStructs.Price(
  67. int64(uint64(getRand() % 1000)), // Price
  68. uint64(getRand() % 100), // Confidence
  69. -5, // Expo
  70. publishTime
  71. )
  72. );
  73. freshPricesPublishTimes.push(publishTime);
  74. // Generate Wormhole Merkle update data and fee for the first i th prices
  75. (
  76. bytes[] memory updateData,
  77. uint updateFee
  78. ) = generateUpdateDataAndFee(cachedPrices);
  79. cachedPricesUpdateData.push(updateData);
  80. cachedPricesUpdateFee.push(updateFee);
  81. (updateData, updateFee) = generateUpdateDataAndFee(freshPrices);
  82. freshPricesUpdateData.push(updateData);
  83. freshPricesUpdateFee.push(updateFee);
  84. }
  85. allCachedPricesUpdateData = cachedPricesUpdateData[NUM_PRICES - 1];
  86. allCachedPricesUpdateFee = cachedPricesUpdateFee[NUM_PRICES - 1];
  87. allFreshPricesUpdateData = freshPricesUpdateData[NUM_PRICES - 1];
  88. allFreshPricesUpdateFee = freshPricesUpdateFee[NUM_PRICES - 1];
  89. // Populate the contract with the initial prices
  90. pyth.updatePriceFeeds{value: allCachedPricesUpdateFee}(
  91. allCachedPricesUpdateData
  92. );
  93. }
  94. function getRand() internal returns (uint val) {
  95. ++randomSeed;
  96. val = uint(keccak256(abi.encode(randomSeed)));
  97. }
  98. function generateUpdateDataAndFee(
  99. PythStructs.Price[] memory prices
  100. ) internal returns (bytes[] memory updateData, uint updateFee) {
  101. updateData = new bytes[](1);
  102. updateData[0] = generateWhMerkleUpdate(
  103. pricesToPriceFeedMessages(priceIds, prices),
  104. MERKLE_TREE_DEPTH,
  105. NUM_GUARDIAN_SIGNERS
  106. );
  107. updateFee = pyth.getUpdateFee(updateData);
  108. }
  109. function testBenchmarkUpdatePriceFeeds1FeedFresh() public {
  110. pyth.updatePriceFeeds{value: freshPricesUpdateFee[0]}(
  111. freshPricesUpdateData[0]
  112. );
  113. }
  114. function testBenchmarkUpdatePriceFeeds2FeedsFresh() public {
  115. pyth.updatePriceFeeds{value: freshPricesUpdateFee[1]}(
  116. freshPricesUpdateData[1]
  117. );
  118. }
  119. function testBenchmarkUpdatePriceFeeds3FeedsFresh() public {
  120. pyth.updatePriceFeeds{value: freshPricesUpdateFee[2]}(
  121. freshPricesUpdateData[2]
  122. );
  123. }
  124. function testBenchmarkUpdatePriceFeeds4FeedsFresh() public {
  125. pyth.updatePriceFeeds{value: freshPricesUpdateFee[3]}(
  126. freshPricesUpdateData[3]
  127. );
  128. }
  129. function testBenchmarkUpdatePriceFeeds5FeedsFresh() public {
  130. pyth.updatePriceFeeds{value: freshPricesUpdateFee[4]}(
  131. freshPricesUpdateData[4]
  132. );
  133. }
  134. function testBenchmarkUpdatePriceFeeds1FeedNotFresh() public {
  135. pyth.updatePriceFeeds{value: cachedPricesUpdateFee[0]}(
  136. cachedPricesUpdateData[0]
  137. );
  138. }
  139. function testBenchmarkUpdatePriceFeeds2FeedsNotFresh() public {
  140. pyth.updatePriceFeeds{value: cachedPricesUpdateFee[1]}(
  141. cachedPricesUpdateData[1]
  142. );
  143. }
  144. function testBenchmarkUpdatePriceFeeds3FeedsNotFresh() public {
  145. pyth.updatePriceFeeds{value: cachedPricesUpdateFee[2]}(
  146. cachedPricesUpdateData[2]
  147. );
  148. }
  149. function testBenchmarkUpdatePriceFeeds4FeedsNotFresh() public {
  150. pyth.updatePriceFeeds{value: cachedPricesUpdateFee[3]}(
  151. cachedPricesUpdateData[3]
  152. );
  153. }
  154. function testBenchmarkUpdatePriceFeeds5FeedsNotFresh() public {
  155. pyth.updatePriceFeeds{value: cachedPricesUpdateFee[4]}(
  156. cachedPricesUpdateData[4]
  157. );
  158. }
  159. function testBenchmarkUpdatePriceFeedsIfNecessaryFresh() public {
  160. // Since the prices have advanced, the publishTimes are newer than one in
  161. // the contract and hence, the call should succeed.
  162. pyth.updatePriceFeedsIfNecessary{value: allFreshPricesUpdateFee}(
  163. allFreshPricesUpdateData,
  164. priceIds,
  165. freshPricesPublishTimes
  166. );
  167. }
  168. function testBenchmarkUpdatePriceFeedsIfNecessaryNotFresh() public {
  169. // Since the price is not advanced, the publishTimes are the same as the
  170. // ones in the contract.
  171. vm.expectRevert(PythErrors.NoFreshUpdate.selector);
  172. pyth.updatePriceFeedsIfNecessary{value: allCachedPricesUpdateFee}(
  173. allCachedPricesUpdateData,
  174. priceIds,
  175. cachedPricesPublishTimes
  176. );
  177. }
  178. function testBenchmarkParsePriceFeedUpdatesForOnePriceFeed() public {
  179. bytes32[] memory ids = new bytes32[](1);
  180. ids[0] = priceIds[0];
  181. pyth.parsePriceFeedUpdates{value: allFreshPricesUpdateFee}(
  182. allFreshPricesUpdateData,
  183. ids,
  184. 0,
  185. 50
  186. );
  187. }
  188. function testBenchmarkParsePriceFeedUpdatesForTwoPriceFeed() public {
  189. bytes32[] memory ids = new bytes32[](2);
  190. ids[0] = priceIds[0];
  191. ids[1] = priceIds[1];
  192. pyth.parsePriceFeedUpdates{value: allFreshPricesUpdateFee}(
  193. allFreshPricesUpdateData,
  194. ids,
  195. 0,
  196. 50
  197. );
  198. }
  199. function testBenchmarkParsePriceFeedUpdatesWithConfigTrue() public {
  200. bytes32[] memory ids = new bytes32[](1);
  201. ids[0] = priceIds[0];
  202. pyth.parsePriceFeedUpdatesWithConfig{value: freshPricesUpdateFee[0]}(
  203. freshPricesUpdateData[0],
  204. ids,
  205. 0,
  206. 50,
  207. false,
  208. true, // check minimal
  209. false
  210. );
  211. }
  212. function testBenchmarkParsePriceFeedUpdatesWithConfigFalse() public {
  213. bytes32[] memory ids = new bytes32[](1);
  214. ids[0] = priceIds[0];
  215. pyth.parsePriceFeedUpdatesWithConfig{value: freshPricesUpdateFee[0]}(
  216. freshPricesUpdateData[0], // contains only priceIds[0]
  217. ids,
  218. 0,
  219. 50,
  220. false,
  221. true, // check minimal
  222. false
  223. );
  224. }
  225. function testBenchmarkParsePriceFeedUpdatesUniqueFor() public {
  226. bytes32[] memory ids = new bytes32[](1);
  227. ids[0] = priceIds[0];
  228. pyth.parsePriceFeedUpdatesUnique{value: freshPricesUpdateFee[0]}(
  229. freshPricesUpdateData[0],
  230. ids,
  231. uint64(freshPrices[0].publishTime),
  232. 100
  233. );
  234. }
  235. function testBenchmarkParsePriceFeedUpdatesUniqueForOnePriceFeedNotWithinRange()
  236. public
  237. {
  238. bytes32[] memory ids = new bytes32[](1);
  239. ids[0] = priceIds[0];
  240. vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);
  241. pyth.parsePriceFeedUpdatesUnique{value: freshPricesUpdateFee[0]}(
  242. freshPricesUpdateData[0],
  243. ids,
  244. uint64(freshPrices[0].publishTime) - 1,
  245. 100
  246. );
  247. }
  248. // Helper function to run price feed update benchmark with a specified number of feeds
  249. function _runParsePriceFeedUpdatesBenchmark(uint256 numIds) internal {
  250. bytes32[] memory ids = new bytes32[](numIds);
  251. for (uint i = 0; i < numIds; i++) {
  252. ids[i] = priceIds[i];
  253. }
  254. pyth.parsePriceFeedUpdates{value: freshPricesUpdateFee[numIds - 1]}(
  255. freshPricesUpdateData[numIds - 1],
  256. ids,
  257. 0,
  258. 50
  259. );
  260. }
  261. function testBenchmarkParsePriceFeedUpdates1() public {
  262. _runParsePriceFeedUpdatesBenchmark(1);
  263. }
  264. function testBenchmarkParsePriceFeedUpdates2() public {
  265. _runParsePriceFeedUpdatesBenchmark(2);
  266. }
  267. function testBenchmarkParsePriceFeedUpdates3() public {
  268. _runParsePriceFeedUpdatesBenchmark(3);
  269. }
  270. function testBenchmarkParsePriceFeedUpdates4() public {
  271. _runParsePriceFeedUpdatesBenchmark(4);
  272. }
  273. function testBenchmarkParsePriceFeedUpdates5() public {
  274. _runParsePriceFeedUpdatesBenchmark(5);
  275. }
  276. function testBenchmarkParsePriceFeedUpdates6() public {
  277. _runParsePriceFeedUpdatesBenchmark(6);
  278. }
  279. function testBenchmarkParsePriceFeedUpdates7() public {
  280. _runParsePriceFeedUpdatesBenchmark(7);
  281. }
  282. function testBenchmarkParsePriceFeedUpdates8() public {
  283. _runParsePriceFeedUpdatesBenchmark(8);
  284. }
  285. function testBenchmarkParsePriceFeedUpdates9() public {
  286. _runParsePriceFeedUpdatesBenchmark(9);
  287. }
  288. function testBenchmarkParsePriceFeedUpdates10() public {
  289. _runParsePriceFeedUpdatesBenchmark(10);
  290. }
  291. function testBenchmarkParsePriceFeedUpdatesForAllPriceFeedsShuffledSubsetPriceIds()
  292. public
  293. {
  294. uint numIds = 3;
  295. bytes32[] memory ids = new bytes32[](numIds);
  296. ids[0] = priceIds[4];
  297. ids[1] = priceIds[2];
  298. ids[2] = priceIds[0];
  299. pyth.parsePriceFeedUpdates{value: freshPricesUpdateFee[4]}( // updateFee based on number of priceFeeds in updateData
  300. freshPricesUpdateData[4],
  301. ids,
  302. 0,
  303. 50
  304. );
  305. }
  306. function testBenchmarkParsePriceFeedUpdatesForOnePriceFeedNotWithinRange()
  307. public
  308. {
  309. bytes32[] memory ids = new bytes32[](1);
  310. ids[0] = priceIds[0];
  311. vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);
  312. pyth.parsePriceFeedUpdates{value: freshPricesUpdateFee[0]}(
  313. freshPricesUpdateData[0],
  314. ids,
  315. 50,
  316. 100
  317. );
  318. }
  319. function testBenchmarkParsePriceFeedUpdatesForAllPriceFeedsNotWithinRange()
  320. public
  321. {
  322. bytes32[] memory ids = new bytes32[](1);
  323. ids[0] = priceIds[0];
  324. vm.expectRevert(PythErrors.PriceFeedNotFoundWithinRange.selector);
  325. pyth.parsePriceFeedUpdates{value: allFreshPricesUpdateFee}(
  326. allFreshPricesUpdateData,
  327. ids,
  328. 50,
  329. 100
  330. );
  331. }
  332. function testBenchmarkGetPrice() public {
  333. // Set the block timestamp to 0. As prices have < 10 timestamp and staleness
  334. // below is set to 60 seconds, the getPriceNoOlderThan should work as expected.
  335. vm.warp(0);
  336. pyth.getPriceNoOlderThan(priceIds[0], 60);
  337. }
  338. function testBenchmarkGetEmaPrice() public {
  339. // Set the block timestamp to 0. As prices have < 10 timestamp and staleness
  340. // below is set to 60 seconds, the getEmaPriceNoOlderThan should work as expected.
  341. vm.warp(0);
  342. pyth.getEmaPriceNoOlderThan(priceIds[0], 60);
  343. }
  344. function testBenchmarkGetUpdateFee1() public view {
  345. pyth.getUpdateFee(freshPricesUpdateData[0]);
  346. }
  347. function testBenchmarkGetUpdateFee2() public view {
  348. pyth.getUpdateFee(freshPricesUpdateData[1]);
  349. }
  350. function testBenchmarkGetUpdateFee3() public view {
  351. pyth.getUpdateFee(freshPricesUpdateData[2]);
  352. }
  353. function testBenchmarkGetUpdateFee4() public view {
  354. pyth.getUpdateFee(freshPricesUpdateData[3]);
  355. }
  356. function testBenchmarkGetUpdateFee5() public view {
  357. pyth.getUpdateFee(freshPricesUpdateData[4]);
  358. }
  359. }