Skip to content

Commit 75f4edc

Browse files
committed
refactor: move structs to PythInternalStructs, rename
1 parent 4bdb33d commit 75f4edc

File tree

6 files changed

+36
-45
lines changed

6 files changed

+36
-45
lines changed

target_chains/ethereum/contracts/contracts/pyth/Pyth.sol

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -179,34 +179,13 @@ abstract contract Pyth is
179179
if (price.publishTime == 0) revert PythErrors.PriceFeedNotFound();
180180
}
181181

182-
/// Internal struct to hold parameters for update processing
183-
/// @dev Storing these variable in a struct rather than local variables
184-
/// helps reduce stack depth.
185-
struct UpdateProcessingContext {
186-
bytes32[] priceIds;
187-
uint64 minPublishTime;
188-
uint64 maxPublishTime;
189-
bool checkUniqueness;
190-
PythStructs.PriceFeed[] priceFeeds;
191-
uint64[] slots;
192-
}
193-
194-
/// The initial Merkle header data in an updateData. The encoded bytes
195-
/// are kept in calldata for gas efficiency.
196-
/// @dev Storing these variable in a struct rather than local variables
197-
/// helps reduce stack depth.
198-
struct MerkleData {
199-
bytes20 digest;
200-
uint8 numUpdates;
201-
uint64 slot;
202-
}
203-
204-
/// @dev Helper function to process a single price update within a Merkle proof.
205-
function _processSingleMerkleUpdate(
206-
MerkleData memory merkleData,
182+
/// @dev Helper function to parse a single price update within a Merkle proof.
183+
/// Parsed price feeds will be stored in the context.
184+
function _parseSingleMerkleUpdate(
185+
PythInternalStructs.MerkleData memory merkleData,
207186
bytes calldata encoded,
208187
uint offset,
209-
UpdateProcessingContext memory context
188+
PythInternalStructs.UpdateParseContext memory context
210189
) internal pure returns (uint newOffset) {
211190
PythInternalStructs.PriceInfo memory priceInfo;
212191
bytes32 priceId;
@@ -230,10 +209,10 @@ abstract contract Pyth is
230209
if (k < context.priceIds.length && context.priceFeeds[k].id == 0) {
231210
uint publishTime = uint(priceInfo.publishTime);
232211
if (
233-
publishTime >= context.minPublishTime &&
234-
publishTime <= context.maxPublishTime &&
235-
(!context.checkUniqueness ||
236-
context.minPublishTime > prevPublishTime)
212+
publishTime >= context.config.minPublishTime &&
213+
publishTime <= context.config.maxPublishTime &&
214+
(!context.config.checkUniqueness ||
215+
context.config.minPublishTime > prevPublishTime)
237216
) {
238217
context.priceFeeds[k].id = priceId;
239218
context.priceFeeds[k].price.price = priceInfo.price;
@@ -252,7 +231,7 @@ abstract contract Pyth is
252231
/// @dev Processes a single entry from the updateData array.
253232
function _processSingleUpdateDataBlob(
254233
bytes calldata singleUpdateData,
255-
UpdateProcessingContext memory context
234+
PythInternalStructs.UpdateParseContext memory context
256235
) internal view {
257236
// Check magic number and length first
258237
if (
@@ -276,7 +255,7 @@ abstract contract Pyth is
276255
}
277256

278257
// Extract Merkle data
279-
MerkleData memory merkleData;
258+
PythInternalStructs.MerkleData memory merkleData;
280259
bytes calldata encoded;
281260
(
282261
offset,
@@ -291,7 +270,7 @@ abstract contract Pyth is
291270

292271
// Process each update within the Merkle proof
293272
for (uint j = 0; j < merkleData.numUpdates; j++) {
294-
offset = _processSingleMerkleUpdate(
273+
offset = _parseSingleMerkleUpdate(
295274
merkleData,
296275
encoded,
297276
offset,
@@ -322,16 +301,15 @@ abstract contract Pyth is
322301
}
323302

324303
// Create the context struct that holds all shared parameters
325-
UpdateProcessingContext memory context;
304+
PythInternalStructs.UpdateParseContext memory context;
326305
context.priceIds = priceIds;
327-
context.minPublishTime = config.minPublishTime;
328-
context.maxPublishTime = config.maxPublishTime;
329-
context.checkUniqueness = config.checkUniqueness;
306+
context.config = config;
330307
context.priceFeeds = new PythStructs.PriceFeed[](priceIds.length);
331308
context.slots = new uint64[](priceIds.length);
332309

333310
unchecked {
334311
// Process each update, passing the context struct
312+
// Parsed results will be filled in context.priceFeeds and context.slots
335313
for (uint i = 0; i < updateData.length; i++) {
336314
_processSingleUpdateDataBlob(updateData[i], context);
337315
}
@@ -369,8 +347,6 @@ abstract contract Pyth is
369347
);
370348
}
371349

372-
/// @dev Same as `parsePriceFeedUpdates`, but also returns the Pythnet slot
373-
/// associated with each price update.
374350
function parsePriceFeedUpdatesWithSlots(
375351
bytes[] calldata updateData,
376352
bytes32[] calldata priceIds,

target_chains/ethereum/contracts/contracts/pyth/PythInternalStructs.sol

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,26 @@ contract PythInternalStructs {
1515
bool checkUniqueness;
1616
}
1717

18+
/// Internal struct to hold parameters for update processing
19+
/// @dev Storing these variable in a struct rather than local variables
20+
/// helps reduce stack depth when passing arguments to functions.
21+
struct UpdateParseContext {
22+
bytes32[] priceIds;
23+
ParseConfig config;
24+
PythStructs.PriceFeed[] priceFeeds;
25+
uint64[] slots;
26+
}
27+
28+
/// The initial Merkle header data in an AccumulatorUpdate. The encoded bytes
29+
/// are kept in calldata for gas efficiency.
30+
/// @dev Storing these variable in a struct rather than local variables
31+
/// helps reduce stack depth when passing arguments to functions.
32+
struct MerkleData {
33+
bytes20 digest;
34+
uint8 numUpdates;
35+
uint64 slot;
36+
}
37+
1838
struct PriceInfo {
1939
// slot 1
2040
uint64 publishTime;

target_chains/ethereum/contracts/forge-test/PulseScheduler.t.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ contract SchedulerTest is Test, SchedulerEvents, PulseTestUtils {
10441044
vm.prank(pusher);
10451045
scheduler.updatePriceFeeds(subscriptionId, updateData, priceIds);
10461046

1047-
// Try to access from the non-whitelisted address (should fail)
1047+
// Try to access from a non-whitelisted address (should fail)
10481048
vm.startPrank(address(0xdead));
10491049
bytes32[] memory emptyPriceIds = new bytes32[](0);
10501050
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));

target_chains/ethereum/contracts/forge-test/Pyth.t.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
204204
);
205205
}
206206

207-
/// Testing parsePriceFeedUpdates method.
208207
function testParsePriceFeedUpdatesWorks(uint seed) public {
209208
setRandSeed(seed);
210209
uint numMessages = 1 + (getRandUint() % 10);
@@ -237,7 +236,6 @@ contract PythTest is Test, WormholeTestUtils, PythTestUtils {
237236
}
238237
}
239238

240-
/// Testing parsePriceFeedUpdatesWithSlots method.
241239
function testParsePriceFeedUpdatesWithSlotsWorks(uint seed) public {
242240
setRandSeed(seed);
243241
uint numMessages = 1 + (getRandUint() % 10);

target_chains/ethereum/sdk/solidity/AbstractPyth.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ abstract contract AbstractPyth is IPyth {
136136
override
137137
returns (PythStructs.PriceFeed[] memory priceFeeds);
138138

139-
/// @dev Same as `parsePriceFeedUpdates`, but also returns the Pythnet slot
140-
/// associated with each price update.
141139
function parsePriceFeedUpdatesWithSlots(
142140
bytes[] calldata updateData,
143141
bytes32[] calldata priceIds,

target_chains/ethereum/sdk/solidity/IPyth.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ interface IPyth is IPythEvents {
173173
/// @param maxPublishTime maximum acceptable publishTime for the given `priceIds`.
174174
/// @return priceFeeds Array of the price feeds corresponding to the given `priceIds` (with the same order).
175175
/// @return slots Array of the Pythnet slot corresponding to the given `priceIds` (with the same order).
176-
/// @
177176
function parsePriceFeedUpdatesWithSlots(
178177
bytes[] calldata updateData,
179178
bytes32[] calldata priceIds,

0 commit comments

Comments
 (0)