Skip to content

Commit b220ae7

Browse files
committed
feat: add checks to make sure indexes are set
1 parent de6ba44 commit b220ae7

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

runtime/src/bank/pyth_accumulator.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ pub fn update_v1(
139139
let v1_messages = if use_message_buffers {
140140
let mut measure = Measure::start("update_v1_load_program_accounts");
141141

142+
assert!(
143+
bank.account_indexes_include_key(&*MESSAGE_BUFFER_PID),
144+
"MessageBuffer program account index missing"
145+
);
142146
message_buffer_accounts = bank
143147
.get_filtered_indexed_accounts(
144148
&IndexKey::ProgramId(*MESSAGE_BUFFER_PID),
@@ -384,6 +388,11 @@ fn post_accumulator_attestation(
384388
pub fn update_v2(bank: &Bank) -> std::result::Result<(), AccumulatorUpdateErrorV1> {
385389
let mut measure = Measure::start("update_v2_load_program_accounts");
386390

391+
assert!(
392+
bank.account_indexes_include_key(&*ORACLE_PID),
393+
"Oracle program account index missing"
394+
);
395+
387396
let accounts = bank
388397
.get_filtered_indexed_accounts(
389398
&IndexKey::ProgramId(*ORACLE_PID),

runtime/src/bank/pyth_accumulator_tests.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use {
2+
super::pyth_accumulator::MESSAGE_BUFFER_PID,
23
crate::{
4+
accounts_db::AccountShrinkThreshold,
5+
accounts_index::{
6+
AccountIndex, AccountSecondaryIndexes, AccountSecondaryIndexesIncludeExclude,
7+
},
38
bank::{
49
pyth_accumulator::{get_accumulator_keys, ACCUMULATOR_RING_SIZE, ORACLE_PID},
510
Bank,
@@ -25,6 +30,7 @@ use {
2530
epoch_schedule::EpochSchedule,
2631
feature::{self, Feature},
2732
feature_set,
33+
genesis_config::GenesisConfig,
2834
hash::hashv,
2935
pubkey::Pubkey,
3036
signature::keypair_from_seed,
@@ -33,6 +39,21 @@ use {
3339
std::{io::Read, mem::size_of, sync::Arc},
3440
};
3541

42+
fn create_new_bank_for_tests_with_index(genesis_config: &GenesisConfig) -> Bank {
43+
Bank::new_with_config_for_tests(
44+
genesis_config,
45+
AccountSecondaryIndexes {
46+
keys: Some(AccountSecondaryIndexesIncludeExclude {
47+
exclude: false,
48+
keys: [*ORACLE_PID, *MESSAGE_BUFFER_PID].into_iter().collect(),
49+
}),
50+
indexes: [AccountIndex::ProgramId].into_iter().collect(),
51+
},
52+
false,
53+
AccountShrinkThreshold::default(),
54+
)
55+
}
56+
3657
// Create Message Account Bytes
3758
//
3859
// NOTE: This was serialized by hand, but should be replaced with the pythnet-sdk
@@ -107,7 +128,7 @@ fn test_update_accumulator_sysvar() {
107128
// due to slot 0 having special handling.
108129
let slots_in_epoch = 32;
109130
genesis_config.epoch_schedule = EpochSchedule::new(slots_in_epoch);
110-
let mut bank = Bank::new_for_tests(&genesis_config);
131+
let mut bank = create_new_bank_for_tests_with_index(&genesis_config);
111132
bank = new_from_parent(&Arc::new(bank));
112133
bank = new_from_parent(&Arc::new(bank));
113134

@@ -392,7 +413,7 @@ fn test_update_accumulator_end_of_block() {
392413
// due to slot 0 having special handling.
393414
let slots_in_epoch = 32;
394415
genesis_config.epoch_schedule = EpochSchedule::new(slots_in_epoch);
395-
let mut bank = Bank::new_for_tests(&genesis_config);
416+
let mut bank = create_new_bank_for_tests_with_index(&genesis_config);
396417
bank = new_from_parent(&Arc::new(bank));
397418
bank = new_from_parent(&Arc::new(bank));
398419

@@ -683,7 +704,7 @@ fn test_accumulator_v2(generate_buffers: [bool; 4]) {
683704
// due to slot 0 having special handling.
684705
let slots_in_epoch = 32;
685706
genesis_config.epoch_schedule = EpochSchedule::new(slots_in_epoch);
686-
let mut bank = Bank::new_for_tests(&genesis_config);
707+
let mut bank = create_new_bank_for_tests_with_index(&genesis_config);
687708

688709
let generate_price = |seeds, generate_buffers: bool| {
689710
let (price_feed_key, _bump) = Pubkey::find_program_address(&[seeds], &ORACLE_PID);

validator/src/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use {
5252
AccountIndex, AccountSecondaryIndexes, AccountSecondaryIndexesIncludeExclude,
5353
AccountsIndexConfig, IndexLimitMb,
5454
},
55+
bank::pyth_accumulator::{MESSAGE_BUFFER_PID, ORACLE_PID},
5556
hardened_unpack::MAX_GENESIS_ARCHIVE_UNPACKED_SIZE,
5657
runtime_config::RuntimeConfig,
5758
snapshot_config::SnapshotConfig,
@@ -3176,6 +3177,10 @@ fn process_account_indexes(matches: &ArgMatches) -> AccountSecondaryIndexes {
31763177
})
31773178
.collect();
31783179

3180+
assert(account_indexes.contains(&AccountIndex::ProgramId),
3181+
"The indexing should be enabled for program-id accounts. Add the following flag:\n\
3182+
--account-index program-id\n");
3183+
31793184
let account_indexes_include_keys: HashSet<Pubkey> =
31803185
values_t!(matches, "account_index_include_key", Pubkey)
31813186
.unwrap_or_default()
@@ -3193,6 +3198,23 @@ fn process_account_indexes(matches: &ArgMatches) -> AccountSecondaryIndexes {
31933198
let exclude_keys = !account_indexes_exclude_keys.is_empty();
31943199
let include_keys = !account_indexes_include_keys.is_empty();
31953200

3201+
if include_keys {
3202+
if !account_indexes_include_keys.contains(&*ORACLE_PID) || !account_indexes_include_keys.contains(&*MESSAGE_BUFFER_PID) {
3203+
panic!(
3204+
"The oracle program id and message buffer program id must be included in the account index. Add the following flags\n\
3205+
--account-index-include-key {}\n\
3206+
--account-index-include-key {}\n",
3207+
&*ORACLE_PID, &*MESSAGE_BUFFER_PID
3208+
);
3209+
}
3210+
}
3211+
3212+
if exclude_keys {
3213+
if account_indexes_exclude_keys.contains(&*ORACLE_PID) || account_indexes_exclude_keys.contains(&*MESSAGE_BUFFER_PID) {
3214+
panic!("The oracle program id and message buffer program id must *not* be excluded from the account index.");
3215+
}
3216+
}
3217+
31963218
let keys = if !account_indexes.is_empty() && (exclude_keys || include_keys) {
31973219
let account_indexes_keys = AccountSecondaryIndexesIncludeExclude {
31983220
exclude: exclude_keys,

0 commit comments

Comments
 (0)