[eth] Gas improvement: Optimize getPrice and getEmaPrice (#389)

* Optimize getPrice and getEmaPrice
This commit is contained in:
Ali Behjati 2022-11-18 14:42:06 +01:00 committed by GitHub
parent b1afaacabf
commit bb06fdb831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 0 deletions

View File

@ -235,6 +235,38 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
);
}
// This is an overwrite of the same method in AbstractPyth.sol
// to be more gas efficient. It cannot move to PythGetters as it
// is overwriting the interface. Even indirect calling of a similar
// method from PythGetter has some gas overhead.
function getPriceUnsafe(
bytes32 id
) public view override returns (PythStructs.Price memory price) {
PythInternalStructs.PriceInfo storage info = _state.latestPriceInfo[id];
price.publishTime = info.publishTime;
price.expo = info.expo;
price.price = info.price;
price.conf = info.conf;
require(price.publishTime != 0, "price feed for the given id is not pushed or does not exist");
}
// This is an overwrite of the same method in AbstractPyth.sol
// to be more gas efficient. It cannot move to PythGetters as it
// is overwriting the interface. Even indirect calling of a similar
// method from PythGetter has some gas overhead.
function getEmaPriceUnsafe(
bytes32 id
) public view override returns (PythStructs.Price memory price) {
PythInternalStructs.PriceInfo storage info = _state.latestPriceInfo[id];
price.publishTime = info.publishTime;
price.expo = info.expo;
price.price = info.emaPrice;
price.conf = info.emaConf;
require(price.publishTime != 0, "price feed for the given id is not pushed or does not exist");
}
function parsePriceFeedUpdates(
bytes[] calldata updateData,
bytes32[] calldata priceIds,

View File

@ -130,6 +130,15 @@ contract GasBenchmark is Test, WormholeTestUtils, PythTestUtils {
pyth.getPrice(priceIds[0]);
}
function testBenchmarkGetEmaPrice() public {
// Set the block timestamp to 0. As prices have < 10 timestamp and staleness
// is set to 60 seconds, the getPrice should work as expected.
vm.warp(0);
pyth.getEmaPrice(priceIds[0]);
}
function testBenchmarkGetUpdateFee() public view {
pyth.getUpdateFee(freshPricesUpdateData);
}