Skip to content

Commit

Permalink
fix(refactor): dataId -> feedId
Browse files Browse the repository at this point in the history
  • Loading branch information
JordyRo1 committed Sep 12, 2024
1 parent f9cfb10 commit 2e440ea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 51 deletions.
66 changes: 32 additions & 34 deletions solidity/src/PragmaDecoder.sol
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ contract PragmaDecoder {

function extractDataInfoFromUpdate(bytes calldata encoded, uint256 offset, bytes32 checkpointRoot)
internal
returns (uint256 endOffset, ParsedData memory parsedData, bytes32 dataId, uint64 publishTime)
returns (uint256 endOffset, ParsedData memory parsedData, bytes32 feedId, uint64 publishTime)
{
unchecked {
bytes calldata encodedUpdate;
Expand All @@ -181,21 +181,21 @@ contract PragmaDecoder {
bool valid;
(valid, endOffset) = _isProofValid(encodedProof, offset, checkpointRoot, encodedUpdate);
if (!valid) revert ErrorsLib.InvalidHyperlaneCheckpointRoot();
(parsedData, dataId, publishTime) = parseDataFeed(fulldataFeed);
(parsedData, feedId, publishTime) = parseDataFeed(fulldataFeed);
endOffset += 40;
}
}

function parseDataFeed(bytes calldata encodedDataFeed)
private
pure
returns (ParsedData memory parsedData, bytes32 dataId, uint64 publishTime)
returns (ParsedData memory parsedData, bytes32 feedId, uint64 publishTime)
{
parsedData = DataParser.parse(encodedDataFeed);

// Assuming dataId and publishTime are appended at the end of encodedDataFeed
uint256 offset = encodedDataFeed.length - 40; // 32 bytes for dataId, 8 bytes for publishTime
dataId = bytes32(UnsafeCalldataBytesLib.toUint256(encodedDataFeed, offset));
// Assuming feedId and publishTime are appended at the end of encodedDataFeed
uint256 offset = encodedDataFeed.length - 40; // 32 bytes for feedId, 8 bytes for publishTime
feedId = bytes32(UnsafeCalldataBytesLib.toUint256(encodedDataFeed, offset));
offset += 32;
publishTime = UnsafeBytesLib.toUint64(encodedDataFeed, offset);
}
Expand All @@ -213,51 +213,49 @@ contract PragmaDecoder {
unchecked {
for (uint256 i = 0; i < numUpdates; i++) {
ParsedData memory parsedData;
bytes32 dataId;
bytes32 feedId;
uint64 publishTime;
(offset, parsedData, dataId, publishTime) = extractDataInfoFromUpdate(encoded, offset, checkpointRoot);
updateLatestDataInfoIfNecessary(dataId, parsedData, publishTime);
(offset, parsedData, feedId, publishTime) = extractDataInfoFromUpdate(encoded, offset, checkpointRoot);
updateLatestDataInfoIfNecessary(feedId, parsedData, publishTime);
}
}
// We check that the offset is at the end of the encoded data.
// If not it means the data is not encoded correctly.
if (offset != encoded.length) revert ErrorsLib.InvalidUpdateData();
}

function updateLatestDataInfoIfNecessary(bytes32 dataId, ParsedData memory parsedData, uint64 publishTime)
function updateLatestDataInfoIfNecessary(bytes32 feedId, ParsedData memory parsedData, uint64 publishTime)
internal
{
if (publishTime > latestPublishTimes[dataId]) {
latestPublishTimes[dataId] = publishTime;
if (publishTime > latestPublishTimes[feedId]) {
latestPublishTimes[feedId] = publishTime;

if (parsedData.dataType == FeedType.SpotMedian) {
spotMedianFeeds[dataId] = parsedData.spot;
_latestPriceInfo[dataId] =
DataFeed(dataId, publishTime, parsedData.spot.metadata.numberOfSources, parsedData.spot.price);
emit EventsLib.SpotMedianUpdate(dataId, publishTime, parsedData.spot);
spotMedianFeeds[feedId] = parsedData.spot;
_latestPriceInfo[feedId] =
DataFeed(feedId, publishTime, parsedData.spot.metadata.numberOfSources, parsedData.spot.price);
emit EventsLib.SpotMedianUpdate(feedId, publishTime, parsedData.spot);
} else if (parsedData.dataType == FeedType.Twap) {
twapFeeds[dataId] = parsedData.twap;
_latestPriceInfo[dataId] = DataFeed(
dataId, publishTime, parsedData.twap.metadata.numberOfSources, parsedData.twap.twapPrice
);
emit EventsLib.TWAPUpdate(dataId, publishTime, parsedData.twap);
twapFeeds[feedId] = parsedData.twap;
_latestPriceInfo[feedId] =
DataFeed(feedId, publishTime, parsedData.twap.metadata.numberOfSources, parsedData.twap.twapPrice);
emit EventsLib.TWAPUpdate(feedId, publishTime, parsedData.twap);
} else if (parsedData.dataType == FeedType.RealizedVolatility) {
rvFeeds[dataId] = parsedData.rv;
_latestPriceInfo[dataId] =
DataFeed(dataId, publishTime, parsedData.rv.metadata.numberOfSources, parsedData.rv.endPrice);
emit EventsLib.RealizedVolatilityUpdate(dataId, publishTime, parsedData.rv);
rvFeeds[feedId] = parsedData.rv;
_latestPriceInfo[feedId] =
DataFeed(feedId, publishTime, parsedData.rv.metadata.numberOfSources, parsedData.rv.endPrice);
emit EventsLib.RealizedVolatilityUpdate(feedId, publishTime, parsedData.rv);
} else if (parsedData.dataType == FeedType.Options) {
optionsFeeds[dataId] = parsedData.options;
_latestPriceInfo[dataId] = DataFeed(
dataId, publishTime, parsedData.options.metadata.numberOfSources, parsedData.options.optionPrice
optionsFeeds[feedId] = parsedData.options;
_latestPriceInfo[feedId] = DataFeed(
feedId, publishTime, parsedData.options.metadata.numberOfSources, parsedData.options.optionPrice
);
emit EventsLib.OptionsUpdate(dataId, publishTime, parsedData.options);
emit EventsLib.OptionsUpdate(feedId, publishTime, parsedData.options);
} else if (parsedData.dataType == FeedType.Perpetuals) {
perpFeeds[dataId] = parsedData.perp;
_latestPriceInfo[dataId] = DataFeed(
dataId, publishTime, parsedData.perp.metadata.numberOfSources, parsedData.perp.markPrice
);
emit EventsLib.PerpUpdate(dataId, publishTime, parsedData.perp);
perpFeeds[feedId] = parsedData.perp;
_latestPriceInfo[feedId] =
DataFeed(feedId, publishTime, parsedData.perp.metadata.numberOfSources, parsedData.perp.markPrice);
emit EventsLib.PerpUpdate(feedId, publishTime, parsedData.perp);
} else {
revert ErrorsLib.InvalidDataFeedType();
}
Expand Down
28 changes: 14 additions & 14 deletions solidity/test/PragmaDecoder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ contract PragmaDecoderHarness is PragmaDecoder {
return updateDataInfoFromUpdate(updateData);
}

function exposed_spotMedianFeeds(bytes32 dataId) external view returns (SpotMedian memory) {
return spotMedianFeeds[dataId];
function exposed_spotMedianFeeds(bytes32 feedId) external view returns (SpotMedian memory) {
return spotMedianFeeds[feedId];
}

function exposed_twapFeeds(bytes32 dataId) external view returns (TWAP memory) {
return twapFeeds[dataId];
function exposed_twapFeeds(bytes32 feedId) external view returns (TWAP memory) {
return twapFeeds[feedId];
}

function exposed_rvFeeds(bytes32 dataId) external view returns (RealizedVolatility memory) {
return rvFeeds[dataId];
function exposed_rvFeeds(bytes32 feedId) external view returns (RealizedVolatility memory) {
return rvFeeds[feedId];
}

function exposed_optionsFeeds(bytes32 dataId) external view returns (Options memory) {
return optionsFeeds[dataId];
function exposed_optionsFeeds(bytes32 feedId) external view returns (Options memory) {
return optionsFeeds[feedId];
}

function exposed_latestPriceInfo(bytes32 dataId) external view returns (DataFeed memory) {
return _latestPriceInfo[dataId];
function exposed_latestPriceInfo(bytes32 feedId) external view returns (DataFeed memory) {
return _latestPriceInfo[feedId];
}

function exposed_latestPublishTimes(bytes32 dataId) external view returns (uint64) {
return latestPublishTimes[dataId];
function exposed_latestPublishTimes(bytes32 feedId) external view returns (uint64) {
return latestPublishTimes[feedId];
}

function exposed_perpFeeds(bytes32 dataId) external view returns (Perp memory) {
return perpFeeds[dataId];
function exposed_perpFeeds(bytes32 feedId) external view returns (Perp memory) {
return perpFeeds[feedId];
}

function _isProofValid(bytes calldata encodedProof, uint256 offset, bytes32 root, bytes calldata leafData)
Expand Down
2 changes: 1 addition & 1 deletion solidity/test/TestUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ library TestUtils {
uint16(proof.length),
proof,
updateData,
feedId, // dataId
feedId, // feedId
uint64(block.timestamp) // publishTime
);

Expand Down
4 changes: 2 additions & 2 deletions solidity/test/utils/PragmaTestUtils.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
// Utilities to help generating data feed messages and Hyperlane Checkpoints for them

struct DataFeedMessage {
bytes32 dataId;
bytes32 feedId;
int64 value;
int32 expo;
uint64 publishTime;
Expand All @@ -62,7 +62,7 @@ abstract contract PragmaTestUtils is Test, RandTestUtils, HyperlaneTestUtils {
for (uint256 i = 0; i < dataFeedMessages.length; i++) {
encodedDataFeedMessages[i] = abi.encodePacked(
uint8(DataFeedType.SpotMedian),
dataFeedMessages[i].dataId,
dataFeedMessages[i].feedId,
dataFeedMessages[i].value,
dataFeedMessages[i].expo,
dataFeedMessages[i].publishTime,
Expand Down

0 comments on commit 2e440ea

Please sign in to comment.