Skip to content

Commit 13cf30d

Browse files
committed
refactor: address pr comments
1 parent b0fd6a6 commit 13cf30d

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

target_chains/solana/cli/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ pub fn process_write_encoded_vaa_and_post_price_update(
394394
let encoded_vaa_keypair = Keypair::new();
395395

396396
// Transaction 1: Create and initialize VAA
397-
let init_instructions = create_and_init_encoded_vaa_instructions(
397+
let init_instructions = init_encoded_vaa_and_write_initial_data_ixs(
398398
&payer.pubkey(),
399399
vaa,
400400
&wormhole,
@@ -410,7 +410,7 @@ pub fn process_write_encoded_vaa_and_post_price_update(
410410
let price_update_keypair = Keypair::new();
411411
let mut update_instructions = vec![ComputeBudgetInstruction::set_compute_unit_limit(600_000)];
412412

413-
update_instructions.extend(write_and_verify_vaa_instructions(
413+
update_instructions.extend(write_remaining_data_and_verify_vaa_ixs(
414414
&payer.pubkey(),
415415
vaa,
416416
&encoded_vaa_keypair.pubkey(),
@@ -457,7 +457,7 @@ pub fn process_write_encoded_vaa_and_post_twap_update(
457457
let end_encoded_vaa_keypair = Keypair::new();
458458

459459
// Transaction 1: Create and initialize start VAA
460-
let start_init_instructions = create_and_init_encoded_vaa_instructions(
460+
let start_init_instructions = init_encoded_vaa_and_write_initial_data_ixs(
461461
&payer.pubkey(),
462462
start_vaa,
463463
&wormhole,
@@ -470,7 +470,7 @@ pub fn process_write_encoded_vaa_and_post_twap_update(
470470
)?;
471471

472472
// Transaction 2: Create and initialize end VAA
473-
let end_init_instructions = create_and_init_encoded_vaa_instructions(
473+
let end_init_instructions = init_encoded_vaa_and_write_initial_data_ixs(
474474
&payer.pubkey(),
475475
end_vaa,
476476
&wormhole,
@@ -484,13 +484,13 @@ pub fn process_write_encoded_vaa_and_post_twap_update(
484484

485485
// Transaction 3: Write remaining VAA data and verify both VAAs
486486
let mut verify_instructions = vec![ComputeBudgetInstruction::set_compute_unit_limit(400_000)];
487-
verify_instructions.extend(write_and_verify_vaa_instructions(
487+
verify_instructions.extend(write_remaining_data_and_verify_vaa_ixs(
488488
&payer.pubkey(),
489489
start_vaa,
490490
&start_encoded_vaa_keypair.pubkey(),
491491
wormhole,
492492
)?);
493-
verify_instructions.extend(write_and_verify_vaa_instructions(
493+
verify_instructions.extend(write_remaining_data_and_verify_vaa_ixs(
494494
&payer.pubkey(),
495495
end_vaa,
496496
&end_encoded_vaa_keypair.pubkey(),
@@ -523,7 +523,7 @@ pub fn process_write_encoded_vaa_and_post_twap_update(
523523
}
524524

525525
/// Creates instructions to initialize an encoded VAA account and write the first part of the VAA data
526-
pub fn create_and_init_encoded_vaa_instructions(
526+
pub fn init_encoded_vaa_and_write_initial_data_ixs(
527527
payer: &Pubkey,
528528
vaa: &[u8],
529529
wormhole: &Pubkey,
@@ -577,7 +577,7 @@ pub fn create_and_init_encoded_vaa_instructions(
577577
}
578578

579579
/// Creates instructions to write remaining VAA data and verify the VAA
580-
pub fn write_and_verify_vaa_instructions(
580+
pub fn write_remaining_data_and_verify_vaa_ixs(
581581
payer: &Pubkey,
582582
vaa: &[u8],
583583
encoded_vaa_keypair: &Pubkey,

target_chains/solana/programs/pyth-solana-receiver/src/lib.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ fn post_twap_update_from_vaas<'info>(
485485
// Calculate the TWAP and store it in the output account
486486
match (start_message, end_message) {
487487
(Message::TwapMessage(start_msg), Message::TwapMessage(end_msg)) => {
488+
// Verify that the feed ids and expos match, the start msg was published before the end msg,
489+
// and that they are the first messages within their slots
490+
validate_twap_messages(&start_msg, &end_msg)?;
488491
let (price, conf, down_slots_ratio) = calculate_twap(&start_msg, &end_msg)?;
489492

490493
twap_update_account.write_authority = write_authority.key();
@@ -504,7 +507,7 @@ fn post_twap_update_from_vaas<'info>(
504507
Ok(())
505508
}
506509

507-
fn calculate_twap(start_msg: &TwapMessage, end_msg: &TwapMessage) -> Result<(i64, u64, u32)> {
510+
fn validate_twap_messages(start_msg: &TwapMessage, end_msg: &TwapMessage) -> Result<()> {
508511
// Validate feed ids match
509512
require!(
510513
start_msg.feed_id == end_msg.feed_id,
@@ -532,6 +535,12 @@ fn calculate_twap(start_msg: &TwapMessage, end_msg: &TwapMessage) -> Result<(i64
532535
end_msg.prev_publish_time < end_msg.publish_time,
533536
ReceiverError::InvalidTwapEndMessage
534537
);
538+
Ok(())
539+
}
540+
541+
/// Calculate the TWAP for the window before start and end messages
542+
/// Warning: The parameters aren't checked for validity, call `validate_twap_messages` before using.
543+
fn calculate_twap(start_msg: &TwapMessage, end_msg: &TwapMessage) -> Result<(i64, u64, u32)> {
535544
let slot_diff = end_msg
536545
.publish_slot
537546
.checked_sub(start_msg.publish_slot)
@@ -669,8 +678,8 @@ fn verify_vaa_data_source(
669678
}
670679

671680
#[cfg(test)]
672-
/// Unit tests for the core TWAP calculation logic in `calculate_twap`
673-
/// This test module is here because `calculate_twap` is private and can't
681+
/// Unit tests for the core TWAP calculation logic in `calculate_twap` and `validate_twap_messages`
682+
/// This test module is here because these functions are private and can't
674683
/// be imported into `tests/test_post_twap_updates`.
675684
mod calculate_twap_unit_tests {
676685
use super::*;
@@ -698,6 +707,7 @@ mod calculate_twap_unit_tests {
698707
let start = create_basic_twap_message(100, 100, 90, 1000);
699708
let end = create_basic_twap_message(300, 200, 180, 1100);
700709

710+
validate_twap_messages(&start, &end).unwrap();
701711
let price = calculate_twap(&start, &end).unwrap();
702712
assert_eq!(price.0, 2); // (300-100)/(1100-1000) = 2
703713
}
@@ -707,7 +717,7 @@ mod calculate_twap_unit_tests {
707717
let start = create_basic_twap_message(100, 100, 90, 1100);
708718
let end = create_basic_twap_message(300, 200, 180, 1000);
709719

710-
let err = calculate_twap(&start, &end).unwrap_err();
720+
let err = validate_twap_messages(&start, &end).unwrap_err();
711721
assert_eq!(err, ReceiverError::InvalidTwapSlots.into());
712722
}
713723

@@ -716,13 +726,13 @@ mod calculate_twap_unit_tests {
716726
let start = create_basic_twap_message(100, 100, 110, 1000);
717727
let end = create_basic_twap_message(300, 200, 180, 1100);
718728

719-
let err = calculate_twap(&start, &end).unwrap_err();
729+
let err = validate_twap_messages(&start, &end).unwrap_err();
720730
assert_eq!(err, ReceiverError::InvalidTwapStartMessage.into());
721731

722732
let start = create_basic_twap_message(100, 100, 90, 1000);
723733
let end = create_basic_twap_message(300, 200, 200, 1100);
724734

725-
let err = calculate_twap(&start, &end).unwrap_err();
735+
let err = validate_twap_messages(&start, &end).unwrap_err();
726736
assert_eq!(err, ReceiverError::InvalidTwapEndMessage.into());
727737
}
728738

@@ -731,6 +741,7 @@ mod calculate_twap_unit_tests {
731741
let start = create_basic_twap_message(i128::MIN, 100, 90, 1000);
732742
let end = create_basic_twap_message(i128::MAX, 200, 180, 1100);
733743

744+
validate_twap_messages(&start, &end).unwrap();
734745
let err = calculate_twap(&start, &end).unwrap_err();
735746
assert_eq!(err, ReceiverError::TwapCalculationOverflow.into());
736747
}
@@ -741,7 +752,7 @@ mod calculate_twap_unit_tests {
741752
let mut end = create_basic_twap_message(300, 200, 180, 1100);
742753
end.feed_id = [1; 32];
743754

744-
let err = calculate_twap(&start, &end).unwrap_err();
755+
let err = validate_twap_messages(&start, &end).unwrap_err();
745756
assert_eq!(err, ReceiverError::FeedIdMismatch.into());
746757
}
747758

@@ -751,7 +762,7 @@ mod calculate_twap_unit_tests {
751762
let mut end = create_basic_twap_message(300, 200, 180, 1100);
752763
end.exponent = 9;
753764

754-
let err = calculate_twap(&start, &end).unwrap_err();
765+
let err = validate_twap_messages(&start, &end).unwrap_err();
755766
assert_eq!(err, ReceiverError::ExponentMismatch.into());
756767
}
757768
}

target_chains/solana/pyth_solana_receiver_sdk/src/price_update.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,8 @@ impl PriceUpdateV2 {
6161
}
6262
/// A time weighted average price account.
6363
/// This account is used by the Pyth Receiver program to store a TWAP update from a Pyth price feed.
64-
///
65-
/// ## Warning
66-
/// This function doesn't check whether the TwapUpdate is constructed from verified price updates.
67-
/// TwapUpdates should be only created and used after the client has already verified the VAAs via the Wormhole contract.
68-
/// Check out `target_chains/solana/cli/src/main.rs` for an example of how to do this.
64+
/// TwapUpdates can only be created after the client has verified the VAAs via the Wormhole contract.
65+
/// Check out `target_chains/solana/cli/src/main.rs` for an example of how to do this.
6966
///
7067
/// It contains:
7168
/// - `write_authority`: The write authority for this account. This authority can close this account to reclaim rent or update the account to contain a different TWAP update.

0 commit comments

Comments
 (0)