@@ -134,8 +134,8 @@ static inline void upd_twap(
134134// update aggregate price
135135static inline bool upd_aggregate ( pc_price_t * ptr , uint64_t slot , int64_t timestamp )
136136{
137- // only re-compute aggregate in next slot
138- if ( slot <= ptr -> agg_ .pub_slot_ ) {
137+ // only re-compute aggregate in current or future slots
138+ if ( slot < ptr -> agg_ .pub_slot_ ) {
139139 return false;
140140 }
141141
@@ -152,6 +152,8 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
152152
153153 // update aggregate details ready for next slot
154154 ptr -> valid_slot_ = ptr -> agg_ .pub_slot_ ;// valid slot-time of agg. price
155+ // only update twap if the slot is in the future
156+ bool update_twap = slot > ptr -> agg_ .pub_slot_ ;
155157 ptr -> agg_ .pub_slot_ = slot ; // publish slot-time of agg. price
156158 ptr -> timestamp_ = timestamp ;
157159
@@ -174,8 +176,7 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
174176 if ( iptr -> agg_ .status_ == PC_STATUS_TRADING &&
175177 // No overflow for INT64_MIN+conf or INT64_MAX-conf as 0 < conf < INT64_MAX
176178 // These checks ensure that price - conf and price + conf do not overflow.
177- (int64_t )0 < conf && (INT64_MIN + conf ) <= price && price <= (INT64_MAX - conf ) &&
178- slot_diff >= 0 && slot_diff <= PC_MAX_SEND_LATENCY ) {
179+ (int64_t )0 < conf && (INT64_MIN + conf ) <= price && price <= (INT64_MAX - conf ) && slot_diff <= PC_MAX_SEND_LATENCY ) {
179180 numv += 1 ;
180181 prcs [ nprcs ++ ] = price - conf ;
181182 prcs [ nprcs ++ ] = price ;
@@ -222,7 +223,9 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
222223 ptr -> agg_ .price_ = agg_price ;
223224 ptr -> agg_ .conf_ = (uint64_t )agg_conf ;
224225
225- upd_twap ( ptr , agg_diff );
226+ if ( update_twap ) {
227+ upd_twap ( ptr , agg_diff );
228+ }
226229 return true;
227230}
228231
0 commit comments