Skip to content

Commit 85c3921

Browse files
committed
with optimizations
1 parent 74ca371 commit 85c3921

File tree

3 files changed

+19
-47
lines changed

3 files changed

+19
-47
lines changed

target_chains/ethereum/contracts/contracts/pulse/Pulse.sol

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ abstract contract Pulse is IPulse, PulseState {
4444
req.publishTime = 1;
4545
req.callbackGasLimit = 1;
4646
req.requester = address(1);
47-
req.numPriceIds = 0;
48-
// Pre-warm the priceIds array storage
49-
for (uint8 j = 0; j < MAX_PRICE_IDS; j++) {
50-
req.priceIds[j] = bytes32(0);
51-
}
47+
req.priceIdsHash = bytes32(uint256(1));
48+
req.fee = 1;
49+
req.provider = address(1);
5250
}
5351
}
5452
}
@@ -83,16 +81,12 @@ abstract contract Pulse is IPulse, PulseState {
8381
Request storage req = allocRequest(requestSequenceNumber);
8482
req.sequenceNumber = requestSequenceNumber;
8583
req.publishTime = publishTime;
86-
req.callbackGasLimit = callbackGasLimit;
84+
req.callbackGasLimit = SafeCast.toUint128(callbackGasLimit);
8785
req.requester = msg.sender;
88-
req.numPriceIds = uint8(priceIds.length);
8986
req.provider = provider;
9087
req.fee = SafeCast.toUint128(msg.value - _state.pythFeeInWei);
88+
req.priceIdsHash = keccak256(abi.encodePacked(priceIds));
9189

92-
// Copy price IDs to storage
93-
for (uint8 i = 0; i < priceIds.length; i++) {
94-
req.priceIds[i] = priceIds[i];
95-
}
9690
_state.accruedFeesInWei += _state.pythFeeInWei;
9791

9892
emit PriceUpdateRequested(req, priceIds);
@@ -119,14 +113,9 @@ abstract contract Pulse is IPulse, PulseState {
119113

120114
// Verify priceIds match
121115
require(
122-
priceIds.length == req.numPriceIds,
123-
"Price IDs length mismatch"
116+
req.priceIdsHash == keccak256(abi.encodePacked(priceIds)),
117+
"Price IDs mismatch"
124118
);
125-
for (uint8 i = 0; i < req.numPriceIds; i++) {
126-
if (priceIds[i] != req.priceIds[i]) {
127-
revert InvalidPriceIds(priceIds[i], req.priceIds[i]);
128-
}
129-
}
130119

131120
// TODO: should this use parsePriceFeedUpdatesUnique? also, do we need to add 1 to maxPublishTime?
132121
IPyth pyth = IPyth(_state.pyth);
@@ -154,12 +143,14 @@ abstract contract Pulse is IPulse, PulseState {
154143
// 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.
155144
// a better solution would be a doubly-linked list of active requests.
156145
// After successful callback, update firstUnfulfilledSeq if needed
146+
/*
157147
while (
158148
_state.firstUnfulfilledSeq < _state.currentSequenceNumber &&
159149
!isActive(findRequest(_state.firstUnfulfilledSeq))
160150
) {
161151
_state.firstUnfulfilledSeq++;
162152
}
153+
*/
163154

164155
try
165156
IPulseConsumer(req.requester)._pulseCallback{
@@ -463,7 +454,8 @@ abstract contract Pulse is IPulse, PulseState {
463454
actualCount = 0;
464455

465456
// Start from the first unfulfilled sequence and work forwards
466-
uint64 currentSeq = _state.firstUnfulfilledSeq;
457+
// uint64 currentSeq = _state.firstUnfulfilledSeq;
458+
uint64 currentSeq = 0;
467459

468460
// Continue until we find enough active requests or reach current sequence
469461
while (

target_chains/ethereum/contracts/contracts/pulse/PulseState.sol

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,15 @@ contract PulseState {
77
bytes1 public constant NUM_REQUESTS_MASK = 0x1f;
88
// Maximum number of price feeds per request. This limit keeps gas costs predictable and reasonable. 10 is a reasonable number for most use cases.
99
// Requests with more than 10 price feeds should be split into multiple requests
10-
uint8 public constant MAX_PRICE_IDS = 10;
10+
uint8 public constant MAX_PRICE_IDS = 2;
1111

1212
struct Request {
13-
uint64 sequenceNumber;
14-
uint64 publishTime;
15-
// TODO: this is going to absolutely explode gas costs. Need to do something smarter here.
16-
// possible solution is to hash the price ids and store the hash instead.
17-
// The ids themselves can be retrieved from the event.
18-
bytes32[MAX_PRICE_IDS] priceIds;
19-
uint8 numPriceIds; // Actual number of price IDs used
20-
uint256 callbackGasLimit;
2113
address requester;
14+
uint64 sequenceNumber;
2215
address provider;
16+
uint64 publishTime;
17+
bytes32 priceIdsHash;
18+
uint128 callbackGasLimit;
2319
uint128 fee;
2420
}
2521

@@ -33,17 +29,16 @@ contract PulseState {
3329
}
3430

3531
struct State {
36-
address admin;
3732
uint128 pythFeeInWei;
3833
uint128 accruedFeesInWei;
3934
address pyth;
4035
uint64 currentSequenceNumber;
4136
address defaultProvider;
4237
uint256 exclusivityPeriodSeconds;
38+
address admin;
4339
Request[NUM_REQUESTS] requests;
4440
mapping(bytes32 => Request) requestsOverflow;
4541
mapping(address => ProviderInfo) providers;
46-
uint64 firstUnfulfilledSeq; // All sequences before this are fulfilled
4742
}
4843

4944
State internal _state;

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

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,7 @@ contract PulseTest is Test, PulseEvents, IPulseConsumer, PulseTestUtils {
148148
PulseState.Request memory expectedRequest = PulseState.Request({
149149
sequenceNumber: 1,
150150
publishTime: publishTime,
151-
priceIds: [
152-
priceIds[0],
153-
priceIds[1],
154-
bytes32(0), // Fill remaining slots with zero
155-
bytes32(0),
156-
bytes32(0),
157-
bytes32(0),
158-
bytes32(0),
159-
bytes32(0),
160-
bytes32(0),
161-
bytes32(0)
162-
],
163-
numPriceIds: 2,
151+
priceIdsHash: keccak256(abi.encode(priceIds)),
164152
callbackGasLimit: CALLBACK_GAS_LIMIT,
165153
requester: address(consumer),
166154
provider: defaultProvider,
@@ -182,10 +170,7 @@ contract PulseTest is Test, PulseEvents, IPulseConsumer, PulseTestUtils {
182170
PulseState.Request memory lastRequest = pulse.getRequest(1);
183171
assertEq(lastRequest.sequenceNumber, expectedRequest.sequenceNumber);
184172
assertEq(lastRequest.publishTime, expectedRequest.publishTime);
185-
assertEq(lastRequest.numPriceIds, expectedRequest.numPriceIds);
186-
for (uint8 i = 0; i < lastRequest.numPriceIds; i++) {
187-
assertEq(lastRequest.priceIds[i], expectedRequest.priceIds[i]);
188-
}
173+
assertEq(lastRequest.priceIdsHash, expectedRequest.priceIdsHash);
189174
assertEq(
190175
lastRequest.callbackGasLimit,
191176
expectedRequest.callbackGasLimit

0 commit comments

Comments
 (0)