Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Apr 8, 2024
1 parent 87ddfde commit c976989
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
35 changes: 17 additions & 18 deletions program/rust/src/processor/upd_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub fn upd_price(
price_data.prev_slot_ = price_data.agg_.pub_slot_;
price_data.prev_price_ = price_data.agg_.price_;
price_data.prev_conf_ = price_data.agg_.conf_;
price_data.prev_timestamp_ = clock.unix_timestamp;
price_data.prev_timestamp_ = price_data.timestamp_;
}
}

Expand All @@ -221,35 +221,34 @@ pub fn upd_price(

// If the aggregate was successfully updated, calculate the difference and update TWAP.
if updated {
let agg_diff = (clock.slot as i64)
- load_checked::<PriceAccount>(price_account, cmd_args.header.version)?.prev_slot_
as i64;
{
let mut price_data =
load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;

if !(noninitial_price_update_after_program_upgrade) {
// Multiple price updates may occur within the same slot. Updates within the same slot will
// use the previously calculated values (prev_twap, prev_twac, and prev_price_cumulative)
// from the last successful aggregated price update as their basis for recalculation. This
// ensures that each update within a slot builds upon the last and not the twap/twac/price_cumulative
// that is calculated right after the publishers' individual price updates.
if slots_since_last_update > 0 {
price_data.prev_twap_ = price_data.twap_;
price_data.prev_twac_ = price_data.twac_;
price_data.prev_price_cumulative = price_data.price_cumulative;
}
price_data.twap_ = price_data.prev_twap_;
price_data.twac_ = price_data.prev_twac_;
price_data.price_cumulative = price_data.prev_price_cumulative;
// Multiple price updates may occur within the same slot. Updates within the same slot will
// use the previously calculated values (prev_twap, prev_twac, and prev_price_cumulative)
// from the last successful aggregated price update as their basis for recalculation. This
// ensures that each update within a slot builds upon the last and not the twap/twac/price_cumulative
// that is calculated right after the publishers' individual price updates.
if slots_since_last_update > 0 {
price_data.prev_twap_ = price_data.twap_;
price_data.prev_twac_ = price_data.twac_;
price_data.prev_price_cumulative = price_data.price_cumulative;
}
price_data.twap_ = price_data.prev_twap_;
price_data.twac_ = price_data.prev_twac_;
price_data.price_cumulative = price_data.prev_price_cumulative;

price_data.update_price_cumulative()?;
// We want to send a message every time the aggregate price updates. However, during the migration,
// not every publisher will necessarily provide the accumulator accounts. The message_sent_ flag
// ensures that after every aggregate update, the next publisher who provides the accumulator accounts
// will send the message.
price_data.message_sent_ = 0;
}
let agg_diff = (clock.slot as i64)
- load_checked::<PriceAccount>(price_account, cmd_args.header.version)?.prev_slot_
as i64;
unsafe {
c_upd_twap(price_account.try_borrow_mut_data()?.as_mut_ptr(), agg_diff);
}
Expand Down
19 changes: 12 additions & 7 deletions program/rust/src/tests/test_upd_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use {
processor::process_instruction,
tests::test_utils::{
update_clock_slot,
update_clock_timestamp,
AccountSetup,
},
},
Expand Down Expand Up @@ -58,6 +59,7 @@ fn test_upd_price() {
clock_account.is_writable = false;

update_clock_slot(&mut clock_account, 1);
update_clock_timestamp(&mut clock_account, 1);

assert!(process_instruction(
&program_id,
Expand Down Expand Up @@ -146,12 +148,14 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 1);
assert_eq!(price_data.prev_price_, 42);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.prev_timestamp_, 1);
}

// next price doesn't change but slot does
println!("test");
// next price doesn't change but slot and timestamp does
populate_instruction(&mut instruction_data, 81, 2, 3);
update_clock_slot(&mut clock_account, 4);
update_clock_timestamp(&mut clock_account, 4);
assert!(process_instruction(
&program_id,
&[
Expand All @@ -176,7 +180,8 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 3);
assert_eq!(price_data.prev_price_, 81);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.timestamp_, 4); // We only check for timestamp_ here because test_upd_price doesn't directly update timestamp_, this is updated through c_upd_aggregate which is tested in test_upd_aggregate, but we assert here to show that in subsequent asserts for prev_tim
assert_eq!(price_data.prev_timestamp_, 1);
}

// next price doesn't change and neither does aggregate but slot does
Expand Down Expand Up @@ -206,7 +211,7 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 4);
assert_eq!(price_data.prev_price_, 81);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.prev_timestamp_, 4);
}

// try to publish back-in-time
Expand Down Expand Up @@ -238,7 +243,7 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 4);
assert_eq!(price_data.prev_price_, 81);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.prev_timestamp_, 4);
}

populate_instruction(&mut instruction_data, 50, 20, 5);
Expand Down Expand Up @@ -276,7 +281,7 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 5);
assert_eq!(price_data.prev_price_, 81);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.prev_timestamp_, 4);
}

// Negative prices are accepted
Expand Down Expand Up @@ -306,7 +311,7 @@ fn test_upd_price() {
assert_eq!(price_data.prev_slot_, 5);
assert_eq!(price_data.prev_price_, 81);
assert_eq!(price_data.prev_conf_, 2);
assert_eq!(price_data.prev_timestamp_, 0);
assert_eq!(price_data.prev_timestamp_, 4);
}
}

Expand Down
6 changes: 6 additions & 0 deletions program/rust/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ pub fn update_clock_slot(clock_account: &mut AccountInfo, slot: u64) {
clock_data.to_account_info(clock_account);
}

pub fn update_clock_timestamp(clock_account: &mut AccountInfo, timestamp: i64) {
let mut clock_data = clock::Clock::from_account_info(clock_account).unwrap();
clock_data.unix_timestamp = timestamp;
clock_data.to_account_info(clock_account);
}

impl From<OracleCommand> for CommandHeader {
fn from(val: OracleCommand) -> Self {
CommandHeader {
Expand Down

0 comments on commit c976989

Please sign in to comment.