Skip to content

Commit 4a75b68

Browse files
committed
feat: accumulator v2 (wip 1)
1 parent 5563a7e commit 4a75b68

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

runtime/src/bank.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ mod builtin_programs;
199199
mod sysvar_cache;
200200
mod transaction_account_state_info;
201201

202+
mod pyth_accumulator;
203+
202204
pub const SECONDS_PER_YEAR: f64 = 365.25 * 24.0 * 60.0 * 60.0;
203205

204206
pub const MAX_LEADER_SCHEDULE_STAKES: Epoch = 5;
@@ -2242,9 +2244,31 @@ impl Bank {
22422244
}
22432245

22442246
info!("Accumulator: Updating accumulator. Slot: {}", self.slot());
2245-
if let Err(e) = self.update_accumulator_impl() {
2246-
error!("Error updating accumulator: {:?}", e);
2247+
2248+
lazy_static! {
2249+
static ref ACCUMULATOR_V2_SLOT: Option<Slot> =
2250+
match std::env::var("PYTH_ACCUMULATOR_V2_FROM_SLOT") {
2251+
Ok(value) => Some(
2252+
value
2253+
.parse()
2254+
.expect("invalid value of PYTH_ACCUMULATOR_V2_FROM_SLOT env var")
2255+
),
2256+
Err(std::env::VarError::NotPresent) => None,
2257+
Err(std::env::VarError::NotUnicode(err)) => {
2258+
panic!("invalid value of PYTH_ACCUMULATOR_V2_FROM_SLOT env var: {err:?}");
2259+
}
2260+
};
22472261
}
2262+
2263+
if ACCUMULATOR_V2_SLOT.map_or(false, |v2_slot| self.slot >= v2_slot) {
2264+
if let Err(e) = pyth_accumulator::update_v2(&self) {
2265+
error!("Error updating accumulator: {:?}", e);
2266+
}
2267+
} else {
2268+
if let Err(e) = self.update_accumulator_impl() {
2269+
error!("Error updating accumulator: {:?}", e);
2270+
}
2271+
};
22482272
}
22492273

22502274
fn update_accumulator_impl(&self) -> std::result::Result<(), AccumulatorUpdateError> {

runtime/src/bank/pyth_accumulator.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::env::{self, VarError};
2+
3+
use solana_sdk::pubkey::Pubkey;
4+
5+
use crate::accounts_index::{ScanConfig, ScanError};
6+
7+
use super::Bank;
8+
9+
#[derive(Debug, thiserror::Error)]
10+
pub enum AccumulatorUpdateV2Error {
11+
#[error("no oracle pubkey")]
12+
NoOraclePubkey,
13+
#[error("get_program_accounts failed to return accounts: {0}")]
14+
GetProgramAccounts(#[from] ScanError),
15+
}
16+
17+
lazy_static! {
18+
static ref ORACLE_PUBKEY: Option<Pubkey> = match env::var("PYTH_ORACLE_PUBKEY") {
19+
Ok(value) => Some(
20+
value
21+
.parse()
22+
.expect("invalid value of PYTH_ORACLE_PUBKEY env var")
23+
),
24+
Err(VarError::NotPresent) => None,
25+
Err(VarError::NotUnicode(err)) => {
26+
panic!("invalid value of PYTH_ORACLE_PUBKEY env var: {err:?}");
27+
}
28+
};
29+
}
30+
31+
pub fn update_v2(bank: &Bank) -> std::result::Result<(), AccumulatorUpdateV2Error> {
32+
let Some(oracle_pubkey) = &*ORACLE_PUBKEY else {
33+
return Err(AccumulatorUpdateV2Error::NoOraclePubkey);
34+
};
35+
36+
let accounts = bank
37+
.get_program_accounts(oracle_pubkey, &ScanConfig::new(true))
38+
.map_err(AccumulatorUpdateV2Error::GetProgramAccounts)?;
39+
40+
//...
41+
Ok(())
42+
}

sdk/program/src/account_info.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ impl<'a> AccountInfo<'a> {
184184
pub fn assign(&self, new_owner: &Pubkey) {
185185
// Set the non-mut owner field
186186
unsafe {
187+
#[allow(invalid_reference_casting)]
187188
std::ptr::write_volatile(
188189
self.owner as *const Pubkey as *mut [u8; 32],
189190
new_owner.to_bytes(),

0 commit comments

Comments
 (0)