Skip to content

Commit 7c14e9e

Browse files
authored
refactor: move upd_twap from upd_aggregate to rust upd_price (#398)
* move upd_twap from upd_aggregate to rust upd_price * remove comment * address comments * address comments
1 parent 46caea4 commit 7c14e9e

File tree

3 files changed

+22
-28
lines changed

3 files changed

+22
-28
lines changed

program/c/src/oracle/upd_aggregate.h

-9
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,6 @@ static inline void upd_twap(
134134
// update aggregate price
135135
static 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_ ) {
139-
return false;
140-
}
141-
142-
// get number of slots from last published valid price
143-
int64_t agg_diff = ( int64_t )slot - ( int64_t )( ptr->last_slot_ );
144-
145137
// Update the value of the previous price, if it had TRADING status.
146138
if ( ptr->agg_.status_ == PC_STATUS_TRADING ) {
147139
ptr->prev_slot_ = ptr->agg_.pub_slot_;
@@ -224,7 +216,6 @@ static inline bool upd_aggregate( pc_price_t *ptr, uint64_t slot, int64_t timest
224216
ptr->agg_.price_ = agg_price;
225217
ptr->agg_.conf_ = (uint64_t)agg_conf;
226218

227-
upd_twap( ptr, agg_diff );
228219
return true;
229220
}
230221

program/rust/src/processor/upd_price.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -177,21 +177,32 @@ pub fn upd_price(
177177

178178
// Try to update the aggregate
179179
#[allow(unused_variables)]
180-
let mut aggregate_updated = false;
181-
182-
// NOTE: c_upd_aggregate must use a raw pointer to price
183-
// data. Solana's `<account>.borrow_*` methods require exclusive
184-
// access, i.e. no other borrow can exist for the account.
185-
#[allow(unused_assignments)]
186-
if clock.slot > latest_aggregate_price.pub_slot_ {
187-
unsafe {
188-
aggregate_updated = c_upd_aggregate(
180+
let aggregate_updated = if clock.slot > latest_aggregate_price.pub_slot_ {
181+
let updated = unsafe {
182+
// NOTE: c_upd_aggregate must use a raw pointer to price
183+
// data. Solana's `<account>.borrow_*` methods require exclusive
184+
// access, i.e. no other borrow can exist for the account.
185+
c_upd_aggregate(
189186
price_account.try_borrow_mut_data()?.as_mut_ptr(),
190187
clock.slot,
191188
clock.unix_timestamp,
192-
);
189+
)
190+
};
191+
192+
// If the aggregate was successfully updated, calculate the difference and update TWAP.
193+
if updated {
194+
let agg_diff = (clock.slot as i64)
195+
- load_checked::<PriceAccount>(price_account, cmd_args.header.version)?.prev_slot_
196+
as i64;
197+
// Encapsulate TWAP update logic in a function to minimize unsafe block scope.
198+
unsafe {
199+
c_upd_twap(price_account.try_borrow_mut_data()?.as_mut_ptr(), agg_diff);
200+
}
193201
}
194-
}
202+
updated
203+
} else {
204+
false
205+
};
195206

196207
// Reload price data as a struct after c_upd_aggregate() borrow is dropped
197208
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;

program/rust/src/tests/test_upd_aggregate.rs

-8
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ fn test_upd_aggregate() {
100100

101101
assert_eq!(price_data.agg_.price_, 100);
102102
assert_eq!(price_data.agg_.conf_, 10);
103-
assert_eq!(price_data.twap_.val_, 100);
104-
assert_eq!(price_data.twac_.val_, 10);
105103
assert_eq!(price_data.num_qt_, 1);
106104
assert_eq!(price_data.timestamp_, 1);
107105
assert_eq!(price_data.prev_slot_, 0);
@@ -134,8 +132,6 @@ fn test_upd_aggregate() {
134132

135133
assert_eq!(price_data.agg_.price_, 145);
136134
assert_eq!(price_data.agg_.conf_, 55);
137-
assert_eq!(price_data.twap_.val_, 106);
138-
assert_eq!(price_data.twac_.val_, 16);
139135
assert_eq!(price_data.num_qt_, 2);
140136
assert_eq!(price_data.timestamp_, 2);
141137
assert_eq!(price_data.prev_slot_, 1000);
@@ -169,8 +165,6 @@ fn test_upd_aggregate() {
169165

170166
assert_eq!(price_data.agg_.price_, 200);
171167
assert_eq!(price_data.agg_.conf_, 90);
172-
assert_eq!(price_data.twap_.val_, 114);
173-
assert_eq!(price_data.twac_.val_, 23);
174168
assert_eq!(price_data.num_qt_, 3);
175169
assert_eq!(price_data.timestamp_, 3);
176170
assert_eq!(price_data.prev_slot_, 1000);
@@ -205,8 +199,6 @@ fn test_upd_aggregate() {
205199

206200
assert_eq!(price_data.agg_.price_, 245);
207201
assert_eq!(price_data.agg_.conf_, 85);
208-
assert_eq!(price_data.twap_.val_, 125);
209-
assert_eq!(price_data.twac_.val_, 28);
210202
assert_eq!(price_data.num_qt_, 4);
211203
assert_eq!(price_data.timestamp_, 4);
212204
assert_eq!(price_data.last_slot_, 1001);

0 commit comments

Comments
 (0)