|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + accounts::PriceAccount, |
| 4 | + c_oracle_header::{ |
| 5 | + PC_NUM_COMP, |
| 6 | + PC_STATUS_TRADING, |
| 7 | + }, |
| 8 | + tests::pyth_simulator::{ |
| 9 | + PythSimulator, |
| 10 | + Quote, |
| 11 | + }, |
| 12 | + }, |
| 13 | + solana_sdk::{ |
| 14 | + signature::Keypair, |
| 15 | + signer::Signer, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +// Verify that the whole publisher set participates in aggregate |
| 20 | +// calculation. This is important for verifying that extra |
| 21 | +// publisher slots on Pythnet are working. Here's how this works: |
| 22 | +// |
| 23 | +// * Fill all publisher slots on a price |
| 24 | +// * Divide the price component array into two even halves: first_half, second_half |
| 25 | +// * Publish two distinct price values to either half |
| 26 | +// * Verify that the aggregate averages out to an expected value in the middle |
| 27 | +#[tokio::test] |
| 28 | +async fn test_full_publisher_set() -> Result<(), Box<dyn std::error::Error>> { |
| 29 | + let mut sim = PythSimulator::new().await; |
| 30 | + let pub_keypairs: Vec<_> = (0..PC_NUM_COMP).map(|_idx| Keypair::new()).collect(); |
| 31 | + let pub_pubkeys: Vec<_> = pub_keypairs.iter().map(|kp| kp.pubkey()).collect(); |
| 32 | + |
| 33 | + let security_authority = Keypair::new(); |
| 34 | + let price_accounts = sim |
| 35 | + .setup_product_fixture(pub_pubkeys.as_slice(), security_authority.pubkey()) |
| 36 | + .await; |
| 37 | + let price = price_accounts["LTC"]; |
| 38 | + |
| 39 | + |
| 40 | + let n_pubs = pub_keypairs.len(); |
| 41 | + |
| 42 | + // Divide publishers into two even parts (assuming the max PC_NUM_COMP size is even) |
| 43 | + let (first_half, second_half) = pub_keypairs.split_at(n_pubs / 2); |
| 44 | + |
| 45 | + // Starting with the first publisher in each half, publish an update |
| 46 | + for (first_kp, second_kp) in first_half.iter().zip(second_half.iter()) { |
| 47 | + let first_quote = Quote { |
| 48 | + price: 100, |
| 49 | + confidence: 30, |
| 50 | + status: PC_STATUS_TRADING, |
| 51 | + }; |
| 52 | + |
| 53 | + sim.upd_price(first_kp, price, first_quote).await?; |
| 54 | + |
| 55 | + let second_quote = Quote { |
| 56 | + price: 120, |
| 57 | + confidence: 30, |
| 58 | + status: PC_STATUS_TRADING, |
| 59 | + }; |
| 60 | + |
| 61 | + sim.upd_price(second_kp, price, second_quote).await?; |
| 62 | + } |
| 63 | + |
| 64 | + // Advance slot once from 1 to 2 |
| 65 | + sim.warp_to_slot(2).await?; |
| 66 | + |
| 67 | + // Final price update to trigger aggregation |
| 68 | + let first_kp = pub_keypairs.first().unwrap(); |
| 69 | + let first_quote = Quote { |
| 70 | + price: 100, |
| 71 | + confidence: 30, |
| 72 | + status: PC_STATUS_TRADING, |
| 73 | + }; |
| 74 | + sim.upd_price(first_kp, price, first_quote).await?; |
| 75 | + |
| 76 | + { |
| 77 | + let price_data = sim |
| 78 | + .get_account_data_as::<PriceAccount>(price) |
| 79 | + .await |
| 80 | + .unwrap(); |
| 81 | + |
| 82 | + assert_eq!(price_data.agg_.price_, 110); |
| 83 | + assert_eq!(price_data.agg_.conf_, 20); |
| 84 | + } |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
0 commit comments