Skip to content

Commit

Permalink
fixup! pyth: introduce pyth accumulator library
Browse files Browse the repository at this point in the history
  • Loading branch information
swimricky committed Jun 7, 2023
1 parent 00cb6f4 commit 831aa26
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
12 changes: 12 additions & 0 deletions core/src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,18 @@ impl Validator {
info!("Hard forks: {:?}", hard_forks);
}
}
let pyth_keys = bank.get_accumulator_keys();
for (key_name, pk_res) in pyth_keys {
match pk_res {
Ok(pk) => {
info!("Accumulator {}: {}", key_name, pk);
}
Err(err) => {
error!("Failed to get Accumulator {}: {:?}", key_name, err);
abort();
}
}
}

node.info.wallclock = timestamp();
node.info.shred_version = compute_shred_version(
Expand Down
59 changes: 57 additions & 2 deletions runtime/src/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ pub struct CommitTransactionCounts {
/// Accumulator specific error type. It would be nice to use `transaction::Error` but it does
/// not include any `Custom` style variant we can leverage, so we introduce our own.
#[derive(Debug, thiserror::Error)]
enum AccumulatorUpdateError {
pub enum AccumulatorUpdateError {
#[error("get_program_accounts failed to return accounts")]
GetProgramAccounts,

Expand Down Expand Up @@ -2705,7 +2705,7 @@ impl Bank {
account
};

// Serialize into (and create if necesary) the message account.
// Serialize into (and create if necessary) the message account.
let message = message
.try_to_vec()
.map_err(|_| AccumulatorUpdateError::FailedMessageSerialization)?;
Expand Down Expand Up @@ -2747,6 +2747,40 @@ impl Bank {
.unwrap_or(default))
}

/// Get all accumulator related pubkeys from environment variables
/// or return default if the variable is not set.
pub fn get_accumulator_keys(
&self,
) -> Vec<(&str, std::result::Result<Pubkey, AccumulatorUpdateError>)> {
use pythnet_sdk::{pythnet, ACCUMULATOR_EMITTER_ADDRESS, MESSAGE_BUFFER_PID};
let accumulator_keys = vec![
(
"MESSAGE_BUFFER_PID",
Pubkey::new_from_array(MESSAGE_BUFFER_PID),
),
// accumulator emitter address should always be the same regardless
// of environment but checking here for completeness
(
"ACCUMULATOR_EMITTER_ADDR",
Pubkey::new_from_array(ACCUMULATOR_EMITTER_ADDRESS),
),
(
"ACCUMULATOR_SEQUENCE_ADDR",
Pubkey::new_from_array(pythnet::ACCUMULATOR_SEQUENCE_ADDR),
),
(
"WORMHOLE_PID",
Pubkey::new_from_array(pythnet::WORMHOLE_PID),
),
];
let accumulator_pubkeys: Vec<(&str, std::result::Result<Pubkey, AccumulatorUpdateError>)> =
accumulator_keys
.iter()
.map(|(k, d)| (*k, self.env_pubkey_or(k, *d)))
.collect();
accumulator_pubkeys
}

pub fn epoch_duration_in_years(&self, prev_epoch: Epoch) -> f64 {
// period: time that has passed as a fraction of a year, basically the length of
// an epoch as a fraction of a year
Expand Down Expand Up @@ -15337,6 +15371,27 @@ pub(crate) mod tests {
// 3. Check if message offset is > message size to prevent validator crash.
}

#[test]
fn test_get_accumulator_keys() {
use pythnet_sdk::{pythnet, ACCUMULATOR_EMITTER_ADDRESS, MESSAGE_BUFFER_PID};
let leader_pubkey = solana_sdk::pubkey::new_rand();
let GenesisConfigInfo { genesis_config, .. } =
create_genesis_config_with_leader(5, &leader_pubkey, 3);
let bank = Bank::new_for_tests(&genesis_config);
let accumulator_keys: Vec<Pubkey> = bank
.get_accumulator_keys()
.iter()
.map(|(_, pk_res)| *pk_res.as_ref().unwrap())
.collect();
let expected_pyth_keys = vec![
Pubkey::new_from_array(MESSAGE_BUFFER_PID),
Pubkey::new_from_array(ACCUMULATOR_EMITTER_ADDRESS),
Pubkey::new_from_array(pythnet::ACCUMULATOR_SEQUENCE_ADDR),
Pubkey::new_from_array(pythnet::WORMHOLE_PID),
];
assert_eq!(accumulator_keys, expected_pyth_keys);
}

fn poh_estimate_offset(bank: &Bank) -> Duration {
let mut epoch_start_slot = bank.epoch_schedule.get_first_slot_in_epoch(bank.epoch());
if epoch_start_slot == bank.slot() {
Expand Down

0 comments on commit 831aa26

Please sign in to comment.