Skip to content

Commit 9d5cc53

Browse files
authored
Change internal storage layout and order (#379)
1 parent 81325a5 commit 9d5cc53

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

ethereum/contracts/pyth/Pyth.sol

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,19 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
137137
priceId = encoded.toBytes32(index + attestationIndex);
138138
attestationIndex += 32;
139139

140-
info.price.price = int64(encoded.toUint64(index + attestationIndex));
140+
info.price = int64(encoded.toUint64(index + attestationIndex));
141141
attestationIndex += 8;
142142

143-
info.price.conf = encoded.toUint64(index + attestationIndex);
143+
info.conf = encoded.toUint64(index + attestationIndex);
144144
attestationIndex += 8;
145145

146-
info.price.expo = int32(encoded.toUint32(index + attestationIndex));
147-
info.emaPrice.expo = info.price.expo;
146+
info.expo = int32(encoded.toUint32(index + attestationIndex));
148147
attestationIndex += 4;
149148

150-
info.emaPrice.price = int64(encoded.toUint64(index + attestationIndex));
149+
info.emaPrice = int64(encoded.toUint64(index + attestationIndex));
151150
attestationIndex += 8;
152151

153-
info.emaPrice.conf = encoded.toUint64(index + attestationIndex);
152+
info.emaConf = encoded.toUint64(index + attestationIndex);
154153
attestationIndex += 8;
155154

156155
{
@@ -171,8 +170,7 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
171170
// Unused uint64 attestationTime
172171
attestationIndex += 8;
173172

174-
info.price.publishTime = encoded.toUint64(index + attestationIndex);
175-
info.emaPrice.publishTime = info.price.publishTime;
173+
info.publishTime = encoded.toUint64(index + attestationIndex);
176174
attestationIndex += 8;
177175

178176
if (status == 1) { // status == TRADING
@@ -182,20 +180,16 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
182180
// the previous price info that are passed here.
183181

184182
// Previous publish time
185-
info.price.publishTime = encoded.toUint64(index + attestationIndex);
183+
info.publishTime = encoded.toUint64(index + attestationIndex);
186184
attestationIndex += 8;
187185

188186
// Previous price
189-
info.price.price = int64(encoded.toUint64(index + attestationIndex));
187+
info.price = int64(encoded.toUint64(index + attestationIndex));
190188
attestationIndex += 8;
191189

192190
// Previous confidence
193-
info.price.conf = encoded.toUint64(index + attestationIndex);
191+
info.conf = encoded.toUint64(index + attestationIndex);
194192
attestationIndex += 8;
195-
196-
// The EMA is last updated when the aggregate had trading status,
197-
// so, we use previous publish time here too.
198-
info.emaPrice.publishTime = info.price.publishTime;
199193
}
200194
}
201195

@@ -209,14 +203,14 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
209203
uint64 latestPublishTime = latestPriceInfoPublishTime(priceId);
210204

211205
bool fresh = false;
212-
if(info.price.publishTime > latestPublishTime) {
206+
if(info.publishTime > latestPublishTime) {
213207
freshPrices += 1;
214208
fresh = true;
215209
setLatestPriceInfo(priceId, info);
216210
}
217211

218212
emit PriceFeedUpdate(priceId, fresh, vm.emitterChainId, vm.sequence, latestPublishTime,
219-
info.price.publishTime, info.price.price, info.price.conf);
213+
info.publishTime, info.price, info.conf);
220214
}
221215

222216

@@ -227,18 +221,18 @@ abstract contract Pyth is PythGetters, PythSetters, AbstractPyth {
227221
function queryPriceFeed(bytes32 id) public view override returns (PythStructs.PriceFeed memory priceFeed){
228222
// Look up the latest price info for the given ID
229223
PythInternalStructs.PriceInfo memory info = latestPriceInfo(id);
230-
require(info.price.publishTime != 0, "price feed for the given id is not pushed or does not exist");
224+
require(info.publishTime != 0, "price feed for the given id is not pushed or does not exist");
231225

232226
priceFeed.id = id;
233-
priceFeed.price.price = info.price.price;
234-
priceFeed.price.conf = info.price.conf;
235-
priceFeed.price.expo = info.price.expo;
236-
priceFeed.price.publishTime = uint(info.price.publishTime);
237-
238-
priceFeed.emaPrice.price = info.emaPrice.price;
239-
priceFeed.emaPrice.conf = info.emaPrice.conf;
240-
priceFeed.emaPrice.expo = info.emaPrice.expo;
241-
priceFeed.emaPrice.publishTime = uint(info.emaPrice.publishTime);
227+
priceFeed.price.price = info.price;
228+
priceFeed.price.conf = info.conf;
229+
priceFeed.price.expo = info.expo;
230+
priceFeed.price.publishTime = uint(info.publishTime);
231+
232+
priceFeed.emaPrice.price = info.emaPrice;
233+
priceFeed.emaPrice.conf = info.emaConf;
234+
priceFeed.emaPrice.expo = info.expo;
235+
priceFeed.emaPrice.publishTime = uint(info.publishTime);
242236
}
243237

244238
function priceFeedExists(bytes32 id) public override view returns (bool) {

ethereum/contracts/pyth/PythGetters.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ contract PythGetters is PythState {
2828
}
2929

3030
function latestPriceInfoPublishTime(bytes32 priceId) public view returns (uint64) {
31-
return _state.latestPriceInfo[priceId].price.publishTime;
31+
return _state.latestPriceInfo[priceId].publishTime;
3232
}
3333

3434
function hashDataSource(PythInternalStructs.DataSource memory ds) public pure returns (bytes32) {

ethereum/contracts/pyth/PythInternalStructs.sol

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@ import "@pythnetwork/pyth-sdk-solidity/PythStructs.sol";
99
contract PythInternalStructs {
1010
using BytesLib for bytes;
1111

12-
struct InternalPrice {
13-
int64 price;
14-
uint64 conf;
15-
uint64 publishTime;
16-
int32 expo;
17-
}
18-
1912
struct PriceInfo {
2013
// slot 1
21-
InternalPrice price;
14+
uint64 publishTime;
15+
int32 expo;
16+
int64 price;
17+
uint64 conf;
2218

2319
// slot 2
24-
InternalPrice emaPrice;
20+
int64 emaPrice;
21+
uint64 emaConf;
2522
}
2623

2724
struct DataSource {

0 commit comments

Comments
 (0)