Skip to content

Commit 063bf58

Browse files
committed
test: fix and expand accumulator-v2 tests
1 parent 7145ab7 commit 063bf58

File tree

4 files changed

+41
-18
lines changed

4 files changed

+41
-18
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ num-traits = { version = "0.2" }
3535
num_cpus = "1.13.1"
3636
once_cell = "1.12.0"
3737
ouroboros = "0.15.0"
38-
pyth-oracle = { path = "/code/pyth-network/pyth-client/program/rust", features = ["library"] }
38+
pyth-oracle = { git = "https://github.com/pyth-network/pyth-client", branch = "accumulator-v2", features = ["library"] }
3939
pythnet-sdk = { git = "https://github.com/pyth-network/pyth-crosschain", version = "1.13.6", rev = "e670f57f89b05398ca352e4accb1e32724a8e1b4" }
4040
rand = "0.7.0"
4141
rayon = "1.5.3"

runtime/src/bank/pyth_accumulator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use {
33
crate::accounts_index::{ScanConfig, ScanError},
44
byteorder::LittleEndian,
55
byteorder::ReadBytesExt,
6-
itertools::Itertools,
76
log::*,
87
pyth_oracle::validator::AggregationError,
98
pythnet_sdk::{
@@ -187,15 +186,15 @@ pub fn update_v1<'a>(
187186
.collect::<std::result::Result<Vec<_>, std::io::Error>>()?
188187
.into_iter()
189188
.flatten()
190-
.sorted_unstable()
191-
.dedup()
192189
.collect()
193190
} else {
194191
Vec::new()
195192
};
196193

197194
let mut messages = v1_messages;
198195
messages.extend(v2_messages.iter().map(|x| &**x));
196+
messages.sort_unstable();
197+
messages.dedup();
199198

200199
// We now generate a Proof PDA (Owned by the System Program) to store the resulting Proof
201200
// Set. The derivation includes the ring buffer index to simulate a ring buffer in order

runtime/src/bank/pyth_accumulator_tests.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -668,9 +668,22 @@ fn test_update_accumulator_end_of_block() {
668668
);
669669
}
670670

671-
// This test will
672671
#[test]
673-
fn test_accumulator_v2() {
672+
fn test_accumulator_v2_all_v2() {
673+
test_accumulator_v2([false, false, false, false]);
674+
}
675+
676+
#[test]
677+
fn test_accumulator_v2_all_v1() {
678+
test_accumulator_v2([true, true, true, true]);
679+
}
680+
681+
#[test]
682+
fn test_accumulator_v2_mixed() {
683+
test_accumulator_v2([true, true, false, false]);
684+
}
685+
686+
fn test_accumulator_v2(generate_buffers: [bool; 4]) {
674687
let leader_pubkey = solana_sdk::pubkey::new_rand();
675688
let GenesisConfigInfo {
676689
mut genesis_config, ..
@@ -682,9 +695,6 @@ fn test_accumulator_v2() {
682695
genesis_config.epoch_schedule = EpochSchedule::new(slots_in_epoch);
683696
let mut bank = Bank::new_for_tests(&genesis_config);
684697

685-
bank = new_from_parent(&Arc::new(bank)); // Advance slot 1.
686-
bank = new_from_parent(&Arc::new(bank)); // Advance slot 2.
687-
688698
let generate_price = |seeds, generate_buffers: bool| {
689699
let (price_feed_key, _bump) = Pubkey::find_program_address(&[seeds], &ORACLE_PID);
690700
let mut price_feed_account =
@@ -730,8 +740,10 @@ fn test_accumulator_v2() {
730740
// sorts first.
731741
let message_buffer_bytes = create_message_buffer_bytes(messages.clone());
732742

743+
let mut seed = vec![1; 32];
744+
seed[..seeds.len()].copy_from_slice(seeds);
733745
// Create a Message account.
734-
let price_message_key = keypair_from_seed(&[1u8; 32]).unwrap();
746+
let price_message_key = keypair_from_seed(&seed).unwrap();
735747
let mut price_message_account = bank
736748
.get_account(&price_message_key.pubkey())
737749
.unwrap_or_default();
@@ -774,17 +786,23 @@ fn test_accumulator_v2() {
774786
.is_active(&feature_set::redo_move_accumulator_to_end_of_block::id()));
775787

776788
let prices_with_messages = [
777-
generate_price(b"seeds_1", false),
778-
generate_price(b"seeds_2", false),
779-
generate_price(b"seeds_3", false),
780-
generate_price(b"seeds_4", false),
789+
generate_price(b"seeds_1", generate_buffers[0]),
790+
generate_price(b"seeds_2", generate_buffers[1]),
791+
generate_price(b"seeds_3", generate_buffers[2]),
792+
generate_price(b"seeds_4", generate_buffers[3]),
781793
];
782794

795+
bank = new_from_parent(&Arc::new(bank)); // Advance slot 1.
796+
bank = new_from_parent(&Arc::new(bank)); // Advance slot 2.
797+
783798
let messages = prices_with_messages
784799
.iter()
785800
.map(|(_, messages)| messages)
786801
.flatten()
787-
.map(|message| &message[..]);
802+
.map(|message| &message[..])
803+
.sorted_unstable()
804+
.dedup()
805+
.collect::<Vec<_>>();
788806

789807
// Trigger Aggregation. We freeze instead of new_from_parent so
790808
// we can keep access to the bank.
@@ -795,13 +813,13 @@ fn test_accumulator_v2() {
795813
// to offset the ring index as our test is always below 10K slots.
796814
let wormhole_message_account = get_wormhole_message_account(&bank, bank.slot() as u32);
797815
assert_ne!(wormhole_message_account.data().len(), 0);
798-
PostedMessageUnreliableData::deserialize(&mut wormhole_message_account.data()).unwrap();
816+
let deserialized_wormhole_message =
817+
PostedMessageUnreliableData::deserialize(&mut wormhole_message_account.data()).unwrap();
799818

800819
// Create MerkleAccumulator by hand to verify that the Wormhole message
801820
// contents are correctg.
802821
let expected_accumulator =
803-
MerkleAccumulator::<Keccak160>::from_set(messages.clone().sorted_unstable().dedup())
804-
.unwrap();
822+
MerkleAccumulator::<Keccak160>::from_set(messages.iter().copied()).unwrap();
805823

806824
let expected_wormhole_message_payload =
807825
expected_accumulator.serialize(bank.slot(), ACCUMULATOR_RING_SIZE);
@@ -818,6 +836,10 @@ fn test_accumulator_v2() {
818836
..Default::default()
819837
},
820838
};
839+
assert_eq!(
840+
deserialized_wormhole_message.payload,
841+
expected_wormhole_message.payload
842+
);
821843

822844
assert_eq!(
823845
wormhole_message_account.data().to_vec(),
@@ -876,6 +898,7 @@ fn test_get_accumulator_keys() {
876898
Pubkey::new_from_array(ACCUMULATOR_EMITTER_ADDRESS),
877899
Pubkey::new_from_array(pythnet::ACCUMULATOR_SEQUENCE_ADDR),
878900
Pubkey::new_from_array(pythnet::WORMHOLE_PID),
901+
*ORACLE_PID,
879902
];
880903
assert_eq!(accumulator_keys, expected_pyth_keys);
881904
}

0 commit comments

Comments
 (0)