Jayant Krishnamurthy 8 сар өмнө
parent
commit
85c39218bf

+ 11 - 19
target_chains/ethereum/contracts/contracts/pulse/Pulse.sol

@@ -44,11 +44,9 @@ abstract contract Pulse is IPulse, PulseState {
                 req.publishTime = 1;
                 req.callbackGasLimit = 1;
                 req.requester = address(1);
-                req.numPriceIds = 0;
-                // Pre-warm the priceIds array storage
-                for (uint8 j = 0; j < MAX_PRICE_IDS; j++) {
-                    req.priceIds[j] = bytes32(0);
-                }
+                req.priceIdsHash = bytes32(uint256(1));
+                req.fee = 1;
+                req.provider = address(1);
             }
         }
     }
@@ -83,16 +81,12 @@ abstract contract Pulse is IPulse, PulseState {
         Request storage req = allocRequest(requestSequenceNumber);
         req.sequenceNumber = requestSequenceNumber;
         req.publishTime = publishTime;
-        req.callbackGasLimit = callbackGasLimit;
+        req.callbackGasLimit = SafeCast.toUint128(callbackGasLimit);
         req.requester = msg.sender;
-        req.numPriceIds = uint8(priceIds.length);
         req.provider = provider;
         req.fee = SafeCast.toUint128(msg.value - _state.pythFeeInWei);
+        req.priceIdsHash = keccak256(abi.encodePacked(priceIds));
 
-        // Copy price IDs to storage
-        for (uint8 i = 0; i < priceIds.length; i++) {
-            req.priceIds[i] = priceIds[i];
-        }
         _state.accruedFeesInWei += _state.pythFeeInWei;
 
         emit PriceUpdateRequested(req, priceIds);
@@ -119,14 +113,9 @@ abstract contract Pulse is IPulse, PulseState {
 
         // Verify priceIds match
         require(
-            priceIds.length == req.numPriceIds,
-            "Price IDs length mismatch"
+            req.priceIdsHash == keccak256(abi.encodePacked(priceIds)),
+            "Price IDs mismatch"
         );
-        for (uint8 i = 0; i < req.numPriceIds; i++) {
-            if (priceIds[i] != req.priceIds[i]) {
-                revert InvalidPriceIds(priceIds[i], req.priceIds[i]);
-            }
-        }
 
         // TODO: should this use parsePriceFeedUpdatesUnique? also, do we need to add 1 to maxPublishTime?
         IPyth pyth = IPyth(_state.pyth);
@@ -154,12 +143,14 @@ abstract contract Pulse is IPulse, PulseState {
         // TODO: I'm pretty sure this is going to use a lot of gas because it's doing a storage lookup for each sequence number.
         // a better solution would be a doubly-linked list of active requests.
         // After successful callback, update firstUnfulfilledSeq if needed
+        /*
         while (
             _state.firstUnfulfilledSeq < _state.currentSequenceNumber &&
             !isActive(findRequest(_state.firstUnfulfilledSeq))
         ) {
             _state.firstUnfulfilledSeq++;
         }
+        */
 
         try
             IPulseConsumer(req.requester)._pulseCallback{
@@ -463,7 +454,8 @@ abstract contract Pulse is IPulse, PulseState {
         actualCount = 0;
 
         // Start from the first unfulfilled sequence and work forwards
-        uint64 currentSeq = _state.firstUnfulfilledSeq;
+        // uint64 currentSeq = _state.firstUnfulfilledSeq;
+        uint64 currentSeq = 0;
 
         // Continue until we find enough active requests or reach current sequence
         while (

+ 6 - 11
target_chains/ethereum/contracts/contracts/pulse/PulseState.sol

@@ -7,19 +7,15 @@ contract PulseState {
     bytes1 public constant NUM_REQUESTS_MASK = 0x1f;
     // Maximum number of price feeds per request. This limit keeps gas costs predictable and reasonable. 10 is a reasonable number for most use cases.
     // Requests with more than 10 price feeds should be split into multiple requests
-    uint8 public constant MAX_PRICE_IDS = 10;
+    uint8 public constant MAX_PRICE_IDS = 2;
 
     struct Request {
-        uint64 sequenceNumber;
-        uint64 publishTime;
-        // TODO: this is going to absolutely explode gas costs. Need to do something smarter here.
-        // possible solution is to hash the price ids and store the hash instead.
-        // The ids themselves can be retrieved from the event.
-        bytes32[MAX_PRICE_IDS] priceIds;
-        uint8 numPriceIds; // Actual number of price IDs used
-        uint256 callbackGasLimit;
         address requester;
+        uint64 sequenceNumber;
         address provider;
+        uint64 publishTime;
+        bytes32 priceIdsHash;
+        uint128 callbackGasLimit;
         uint128 fee;
     }
 
@@ -33,17 +29,16 @@ contract PulseState {
     }
 
     struct State {
-        address admin;
         uint128 pythFeeInWei;
         uint128 accruedFeesInWei;
         address pyth;
         uint64 currentSequenceNumber;
         address defaultProvider;
         uint256 exclusivityPeriodSeconds;
+        address admin;
         Request[NUM_REQUESTS] requests;
         mapping(bytes32 => Request) requestsOverflow;
         mapping(address => ProviderInfo) providers;
-        uint64 firstUnfulfilledSeq; // All sequences before this are fulfilled
     }
 
     State internal _state;

+ 2 - 17
target_chains/ethereum/contracts/forge-test/Pulse.t.sol

@@ -148,19 +148,7 @@ contract PulseTest is Test, PulseEvents, IPulseConsumer, PulseTestUtils {
         PulseState.Request memory expectedRequest = PulseState.Request({
             sequenceNumber: 1,
             publishTime: publishTime,
-            priceIds: [
-                priceIds[0],
-                priceIds[1],
-                bytes32(0), // Fill remaining slots with zero
-                bytes32(0),
-                bytes32(0),
-                bytes32(0),
-                bytes32(0),
-                bytes32(0),
-                bytes32(0),
-                bytes32(0)
-            ],
-            numPriceIds: 2,
+            priceIdsHash: keccak256(abi.encode(priceIds)),
             callbackGasLimit: CALLBACK_GAS_LIMIT,
             requester: address(consumer),
             provider: defaultProvider,
@@ -182,10 +170,7 @@ contract PulseTest is Test, PulseEvents, IPulseConsumer, PulseTestUtils {
         PulseState.Request memory lastRequest = pulse.getRequest(1);
         assertEq(lastRequest.sequenceNumber, expectedRequest.sequenceNumber);
         assertEq(lastRequest.publishTime, expectedRequest.publishTime);
-        assertEq(lastRequest.numPriceIds, expectedRequest.numPriceIds);
-        for (uint8 i = 0; i < lastRequest.numPriceIds; i++) {
-            assertEq(lastRequest.priceIds[i], expectedRequest.priceIds[i]);
-        }
+        assertEq(lastRequest.priceIdsHash, expectedRequest.priceIdsHash);
         assertEq(
             lastRequest.callbackGasLimit,
             expectedRequest.callbackGasLimit