Skip to content

Commit 38d6850

Browse files
committed
add initial code to support max latency
1 parent 8207528 commit 38d6850

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

program/c/src/oracle/oracle.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ typedef struct pc_price
195195
pc_ema_t twac_; // time-weighted average conf interval
196196
int64_t timestamp_; // unix timestamp of aggregate price
197197
uint8_t min_pub_; // min publishers for valid price
198-
int8_t drv2_; // space for future derived values
199-
int16_t drv3_; // space for future derived values
198+
int8_t message_sent_; // flag to indicate if the current aggregate price has been sent as a message to the message buffer, 0 if not sent, 1 if sent
199+
uint8_t max_latency_; // configurable max latency in slots between send and receive
200+
int8_t drv3_; // space for future derived values
200201
int32_t drv4_; // space for future derived values
201202
pc_pub_key_t prod_; // product id/ref-account
202203
pc_pub_key_t next_; // next price account in list

program/c/src/oracle/upd_aggregate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
175175
// No overflow for INT64_MIN+conf or INT64_MAX-conf as 0 < conf < INT64_MAX
176176
// These checks ensure that price - conf and price + conf do not overflow.
177177
(int64_t)0 < conf && (INT64_MIN + conf) <= price && price <= (INT64_MAX-conf) &&
178-
slot_diff >= 0 && slot_diff <= PC_MAX_SEND_LATENCY ) {
178+
slot_diff <= (ptr->max_latency_ ? ptr->max_latency_ : PC_MAX_SEND_LATENCY) ) {
179179
numv += 1;
180180
prcs[ nprcs++ ] = price - conf;
181181
prcs[ nprcs++ ] = price;

program/rust/src/accounts/price.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ mod price_pythnet {
6565
/// Minimum valid publisher quotes for a succesful aggregation
6666
pub min_pub_: u8,
6767
pub message_sent_: u8,
68-
pub unused_2_: i16,
68+
/// Configurable max latency in slots between send and receive
69+
pub max_latency_: u8,
70+
/// Unused placeholder for alignment
71+
pub unused_2_: i8,
6972
pub unused_3_: i32,
7073
/// Corresponding product account
7174
pub product_account: Pubkey,
@@ -116,6 +119,7 @@ mod price_pythnet {
116119
self.agg_.price_,
117120
self.agg_.conf_,
118121
self.agg_.pub_slot_.saturating_sub(self.prev_slot_),
122+
self.max_latency_,
119123
); // pub_slot should always be >= prev_slot, but we protect ourselves against underflow just in case
120124
Ok(())
121125
} else {
@@ -172,11 +176,13 @@ mod price_pythnet {
172176
}
173177

174178
impl PriceCumulative {
175-
pub fn update(&mut self, price: i64, conf: u64, slot_gap: u64) {
179+
pub fn update(&mut self, price: i64, conf: u64, slot_gap: u64, max_latency: i16) {
176180
self.price += i128::from(price) * i128::from(slot_gap);
177181
self.conf += u128::from(conf) * u128::from(slot_gap);
182+
// Use PC_MAX_SEND_LATENCY if max_latency is 0, otherwise use max_latency
183+
let latency = if max_latency == 0 { PC_MAX_SEND_LATENCY.into() } else { max_latency.into() };
178184
// This is expected to saturate at 0 most of the time (while the feed is up).
179-
self.num_down_slots += slot_gap.saturating_sub(PC_MAX_SEND_LATENCY.into());
185+
self.num_down_slots += slot_gap.saturating_sub(latency as u64);
180186
}
181187
}
182188
}
@@ -225,7 +231,10 @@ mod price_solana {
225231
/// Whether the current aggregate price has been sent as a message to the message buffer.
226232
/// 0 = false, 1 = true. (this is a u8 to make the Pod trait happy)
227233
pub message_sent_: u8,
228-
pub unused_2_: i16,
234+
/// Configurable max latency in slots between send and receive
235+
pub max_latency_: u8,
236+
/// Unused placeholder for alignment
237+
pub unused_2_: i8,
229238
pub unused_3_: i32,
230239
/// Corresponding product account
231240
pub product_account: Pubkey,

0 commit comments

Comments
 (0)