@@ -42,33 +42,6 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
4242 revert PythErrors.InvalidUpdateDataSource ();
4343 }
4444
45- function extractPriceInfosFromAccumulatorUpdate (
46- bytes memory accumulatorUpdate
47- )
48- internal
49- view
50- returns (
51- PythInternalStructs.PriceInfo[] memory priceInfos ,
52- bytes32 [] memory priceIds
53- )
54- {
55- (
56- uint offset ,
57- UpdateType updateType
58- ) = extractUpdateTypeFromAccumulatorHeader (accumulatorUpdate);
59-
60- if (updateType != UpdateType.WormholeMerkle) {
61- revert PythErrors.InvalidUpdateData ();
62- }
63- (priceInfos, priceIds) = extractPriceInfosFromWormholeMerkle (
64- UnsafeBytesLib.slice (
65- accumulatorUpdate,
66- offset,
67- accumulatorUpdate.length - offset
68- )
69- );
70- }
71-
7245 function extractUpdateTypeFromAccumulatorHeader (
7346 bytes memory accumulatorUpdate
7447 ) internal pure returns (uint offset , UpdateType updateType ) {
@@ -134,37 +107,6 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
134107 }
135108 }
136109
137- function extractPriceInfosFromWormholeMerkle (
138- bytes memory encoded
139- )
140- internal
141- view
142- returns (
143- PythInternalStructs.PriceInfo[] memory priceInfos ,
144- bytes32 [] memory priceIds
145- )
146- {
147- unchecked {
148- (
149- uint offset ,
150- bytes20 digest ,
151- uint8 numUpdates
152- ) = extractWormholeMerkleHeaderDigestAndNumUpdates (encoded);
153-
154- priceInfos = new PythInternalStructs.PriceInfo [](numUpdates);
155- priceIds = new bytes32 [](numUpdates);
156- for (uint i = 0 ; i < numUpdates; i++ ) {
157- (
158- offset,
159- priceInfos[i],
160- priceIds[i]
161- ) = extractPriceFeedFromMerkleProof (digest, encoded, offset);
162- }
163-
164- if (offset != encoded.length ) revert PythErrors.InvalidUpdateData ();
165- }
166- }
167-
168110 function extractWormholeMerkleHeaderDigestAndNumUpdates (
169111 bytes memory encoded
170112 ) internal view returns (uint offset , bytes20 digest , uint8 numUpdates ) {
@@ -228,12 +170,12 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
228170 }
229171 }
230172
231- function extractPriceFeedFromMerkleProof (
173+ function extractPriceInfoFromMerkleProof (
232174 bytes20 digest ,
233175 bytes memory encoded ,
234176 uint offset
235177 )
236- private
178+ internal
237179 pure
238180 returns (
239181 uint endOffset ,
@@ -249,7 +191,9 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
249191 digest
250192 );
251193
252- (priceInfo, priceId) = extractPriceFeedMessage (encodedMessage);
194+ (priceInfo, priceId) = extractPriceInfoAndIdFromPriceFeedMessage (
195+ encodedMessage
196+ );
253197
254198 return (endOffset, priceInfo, priceId);
255199 }
@@ -284,15 +228,17 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
284228 }
285229 }
286230
287- function extractPriceFeedMessage (
231+ function extractPriceInfoAndIdFromPriceFeedMessage (
288232 bytes memory encodedMessage
289233 )
290234 private
291235 pure
292236 returns (PythInternalStructs.PriceInfo memory info , bytes32 priceId )
293237 {
294238 unchecked {
295- MessageType messageType = getMessageType (encodedMessage);
239+ MessageType messageType = MessageType (
240+ UnsafeBytesLib.toUint8 (encodedMessage, 0 )
241+ );
296242 if (messageType == MessageType.PriceFeed) {
297243 (info, priceId) = parsePriceFeedMessage (
298244 UnsafeBytesLib.slice (
@@ -307,12 +253,6 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
307253 }
308254 }
309255
310- function getMessageType (
311- bytes memory encodedMessage
312- ) private pure returns (MessageType messageType ) {
313- return MessageType (UnsafeBytesLib.toUint8 (encodedMessage, 0 ));
314- }
315-
316256 function parsePriceFeedMessage (
317257 bytes memory encodedPriceFeed
318258 )
@@ -402,47 +342,25 @@ abstract contract PythAccumulator is PythGetters, PythSetters, AbstractPyth {
402342 ) = extractWormholeMerkleHeaderDigestAndNumUpdates (encoded);
403343
404344 for (uint i = 0 ; i < numUpdates; i++ ) {
405- offset = verifyAndUpdatePriceFeedFromMerkleProof (
345+ PythInternalStructs.PriceInfo memory priceInfo;
346+ bytes32 priceId;
347+ (offset, priceInfo, priceId) = extractPriceInfoFromMerkleProof (
406348 digest,
407349 encoded,
408350 offset
409351 );
352+ uint64 latestPublishTime = latestPriceInfoPublishTime (priceId);
353+ if (priceInfo.publishTime > latestPublishTime) {
354+ setLatestPriceInfo (priceId, priceInfo);
355+ emit PriceFeedUpdate (
356+ priceId,
357+ priceInfo.publishTime,
358+ priceInfo.price,
359+ priceInfo.conf
360+ );
361+ }
410362 }
411-
412363 if (offset != encoded.length ) revert PythErrors.InvalidUpdateData ();
413364 }
414365 }
415-
416- function verifyAndUpdatePriceFeedFromMerkleProof (
417- bytes20 digest ,
418- bytes memory encoded ,
419- uint offset
420- ) private returns (uint endOffset ) {
421- PythInternalStructs.PriceInfo memory priceInfo;
422- bytes32 priceId;
423- (offset, priceInfo, priceId) = extractPriceFeedFromMerkleProof (
424- digest,
425- encoded,
426- offset
427- );
428- processMessage (priceInfo, priceId);
429-
430- return offset;
431- }
432-
433- function processMessage (
434- PythInternalStructs.PriceInfo memory info ,
435- bytes32 priceId
436- ) private {
437- uint64 latestPublishTime = latestPriceInfoPublishTime (priceId);
438- if (info.publishTime > latestPublishTime) {
439- setLatestPriceInfo (priceId, info);
440- emit PriceFeedUpdate (
441- priceId,
442- info.publishTime,
443- info.price,
444- info.conf
445- );
446- }
447- }
448366}
0 commit comments