Skip to content

Commit 796a762

Browse files
committed
fix precommit
1 parent 38d6850 commit 796a762

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

program/rust/src/accounts/price.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,17 @@ mod price_pythnet {
176176
}
177177

178178
impl PriceCumulative {
179-
pub fn update(&mut self, price: i64, conf: u64, slot_gap: u64, max_latency: i16) {
179+
pub fn update(&mut self, price: i64, conf: u64, slot_gap: u64, max_latency: u8) {
180180
self.price += i128::from(price) * i128::from(slot_gap);
181181
self.conf += u128::from(conf) * u128::from(slot_gap);
182182
// 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() };
183+
let latency = if max_latency == 0 {
184+
u64::from(PC_MAX_SEND_LATENCY)
185+
} else {
186+
u64::from(max_latency)
187+
};
184188
// This is expected to saturate at 0 most of the time (while the feed is up).
185-
self.num_down_slots += slot_gap.saturating_sub(latency as u64);
189+
self.num_down_slots += slot_gap.saturating_sub(latency);
186190
}
187191
}
188192
}

program/rust/src/tests/test_twap.rs

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ use {
2121

2222
#[derive(Clone, Debug, Copy)]
2323
pub struct DataEvent {
24-
price: i64,
25-
conf: u64,
26-
slot_gap: u64,
24+
price: i64,
25+
conf: u64,
26+
slot_gap: u64,
27+
max_latency: u8,
2728
}
2829

2930
impl Arbitrary for DataEvent {
3031
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
3132
DataEvent {
32-
slot_gap: u64::from(u8::arbitrary(g)) + 1, /* Slot gap is always > 1, because there
33-
* has been a succesful aggregation */
34-
price: i64::arbitrary(g),
35-
conf: u64::arbitrary(g),
33+
slot_gap: u64::from(u8::arbitrary(g)) + 1, /* Slot gap is always > 1, because there
34+
* has been a succesful aggregation */
35+
price: i64::arbitrary(g),
36+
conf: u64::arbitrary(g),
37+
max_latency: u8::arbitrary(g),
3638
}
3739
}
3840
}
@@ -56,7 +58,12 @@ fn test_twap(input: Vec<DataEvent>) -> bool {
5658
let mut data = Vec::<DataEvent>::new();
5759

5860
for data_event in input {
59-
price_cumulative.update(data_event.price, data_event.conf, data_event.slot_gap);
61+
price_cumulative.update(
62+
data_event.price,
63+
data_event.conf,
64+
data_event.slot_gap,
65+
data_event.max_latency,
66+
);
6067
data.push(data_event);
6168
price_cumulative.check_price(data.as_slice());
6269
price_cumulative.check_conf(data.as_slice());
@@ -112,35 +119,53 @@ fn test_twap_unit() {
112119

113120
let data = vec![
114121
DataEvent {
115-
price: 1,
116-
conf: 2,
117-
slot_gap: 4,
122+
price: 1,
123+
conf: 2,
124+
slot_gap: 4,
125+
max_latency: 1,
118126
},
119127
DataEvent {
120-
price: i64::MAX,
121-
conf: u64::MAX,
122-
slot_gap: 1,
128+
price: i64::MAX,
129+
conf: u64::MAX,
130+
slot_gap: 1,
131+
max_latency: 2,
123132
},
124133
DataEvent {
125-
price: -10,
126-
conf: 4,
127-
slot_gap: 30,
134+
price: -10,
135+
conf: 4,
136+
slot_gap: 30,
137+
max_latency: 3,
128138
},
129139
];
130140

131-
price_cumulative.update(data[0].price, data[0].conf, data[0].slot_gap);
141+
price_cumulative.update(
142+
data[0].price,
143+
data[0].conf,
144+
data[0].slot_gap,
145+
data[0].max_latency,
146+
);
132147
assert_eq!(price_cumulative.price, 5);
133148
assert_eq!(price_cumulative.conf, 10);
134149
assert_eq!(price_cumulative.num_down_slots, 3);
135150
assert_eq!(price_cumulative.unused, 0);
136151

137-
price_cumulative.update(data[1].price, data[1].conf, data[1].slot_gap);
152+
price_cumulative.update(
153+
data[1].price,
154+
data[1].conf,
155+
data[1].slot_gap,
156+
data[1].max_latency,
157+
);
138158
assert_eq!(price_cumulative.price, 9_223_372_036_854_775_812i128);
139159
assert_eq!(price_cumulative.conf, 18_446_744_073_709_551_625u128);
140160
assert_eq!(price_cumulative.num_down_slots, 3);
141161
assert_eq!(price_cumulative.unused, 0);
142162

143-
price_cumulative.update(data[2].price, data[2].conf, data[2].slot_gap);
163+
price_cumulative.update(
164+
data[2].price,
165+
data[2].conf,
166+
data[2].slot_gap,
167+
data[2].max_latency,
168+
);
144169
assert_eq!(price_cumulative.price, 9_223_372_036_854_775_512i128);
145170
assert_eq!(price_cumulative.conf, 18_446_744_073_709_551_745u128);
146171
assert_eq!(price_cumulative.num_down_slots, 8);
@@ -152,7 +177,7 @@ fn test_twap_unit() {
152177
num_down_slots: 0,
153178
unused: 0,
154179
};
155-
price_cumulative_overflow.update(i64::MIN, u64::MAX, u64::MAX);
180+
price_cumulative_overflow.update(i64::MIN, u64::MAX, u64::MAX, u8::MAX);
156181
assert_eq!(
157182
price_cumulative_overflow.price,
158183
i128::MIN - i128::from(i64::MIN)

0 commit comments

Comments
 (0)