Skip to content

Commit 000cbc5

Browse files
swimrickyReisen
authored andcommitted
fixup! pyth: introduce pyth accumulator library
1 parent 885ab95 commit 000cbc5

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

runtime/src/bank.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ use {
7171
dashmap::DashMap,
7272
itertools::Itertools,
7373
log::*,
74-
pythnet_sdk::pythnet,
7574
rand::Rng,
7675
rayon::{
7776
iter::{IntoParallelIterator, IntoParallelRefIterator, ParallelIterator},
@@ -2515,8 +2514,7 @@ impl Bank {
25152514
pythnet_sdk::{
25162515
accumulators::{merkle::MerkleAccumulator, Accumulator},
25172516
hashers::keccak256_160::Keccak160,
2518-
pythnet::PYTH_PID,
2519-
MESSAGE_BUFFER_PID,
2517+
pythnet, MESSAGE_BUFFER_PID,
25202518
},
25212519
solana_sdk::borsh::BorshSerialize,
25222520
};
@@ -2527,7 +2525,7 @@ impl Bank {
25272525
// Find all accounts owned by the Message Buffer program using get_program_accounts, and
25282526
// extract the account data.
25292527

2530-
let message_buffer_pid = Self::env_pubkey_or(
2528+
let message_buffer_pid = self.env_pubkey_or(
25312529
"MESSAGE_BUFFER_PID",
25322530
Pubkey::new_from_array(MESSAGE_BUFFER_PID),
25332531
)?;
@@ -2536,14 +2534,15 @@ impl Bank {
25362534
.get_program_accounts(&message_buffer_pid, &ScanConfig::new(true))
25372535
.map_err(|_| AccumulatorUpdateError::GetProgramAccounts)?;
25382536

2537+
let preimage = b"account:MessageBuffer";
2538+
let mut expected_sighash = [0u8; 8];
2539+
expected_sighash.copy_from_slice(&hashv(&[preimage]).to_bytes()[..8]);
2540+
25392541
// Filter accounts that don't match the Anchor sighash.
25402542
let accounts = accounts.iter().filter(|(_, account)| {
25412543
// Remove accounts that do not start with the expected Anchor sighash.
2542-
let preimage = b"account:MessageBuffer";
25432544
let mut sighash = [0u8; 8];
2544-
let mut expected_sighash = [0u8; 8];
25452545
sighash.copy_from_slice(&account.data()[..8]);
2546-
expected_sighash.copy_from_slice(&hashv(&[preimage]).to_bytes()[..8]);
25472546
sighash == expected_sighash
25482547
});
25492548

@@ -2559,15 +2558,17 @@ impl Bank {
25592558
let header_len = cursor.read_u16::<LittleEndian>()?;
25602559
let mut header_begin = header_len;
25612560
let mut inputs = Vec::new();
2561+
let mut cur_end_offsets_idx: usize = 0;
25622562
while let Some(end) = cursor.read_u16::<LittleEndian>().ok() {
2563-
if end == 0 {
2563+
if end == 0 || cur_end_offsets_idx == (u8::MAX as usize) {
25642564
break;
25652565
}
25662566

25672567
let end_offset = header_len + end;
25682568
let accumulator_input_data = &data[header_begin as usize..end_offset as usize];
25692569
inputs.push(accumulator_input_data);
25702570
header_begin = end_offset;
2571+
cur_end_offsets_idx += 1;
25712572
}
25722573

25732574
Ok(inputs)
@@ -2578,7 +2579,7 @@ impl Bank {
25782579
.sorted_unstable()
25792580
.dedup();
25802581

2581-
let pyth_pid = Self::env_pubkey_or("PYTH_PID", Pubkey::new_from_array(pythnet::PYTH_PID))?;
2582+
let pyth_pid = self.env_pubkey_or("PYTH_PID", Pubkey::new_from_array(pythnet::PYTH_PID))?;
25822583

25832584
// We now generate a Proof PDA (Owned by the System Program) to store the resulting Proof
25842585
// Set. The derivation includes the ring buffer index to simulate a ring buffer in order
@@ -2593,7 +2594,11 @@ impl Bank {
25932594
);
25942595

25952596
let accumulator_data = {
2596-
let data = accounts.clone().collect::<Vec<_>>().try_to_vec()?;
2597+
let mut data = vec![];
2598+
let acc_state_magic = &mut b"PAS1".to_vec();
2599+
let accounts_data = &mut accounts.clone().collect::<Vec<_>>().try_to_vec()?;
2600+
data.append(acc_state_magic);
2601+
data.append(accounts_data);
25972602
let owner = solana_sdk::system_program::id();
25982603
let balance = self.get_minimum_balance_for_rent_exemption(data.len());
25992604
let mut account = AccountSharedData::new(balance, data.len(), &owner);
@@ -2625,16 +2630,16 @@ impl Bank {
26252630
) -> std::result::Result<(), AccumulatorUpdateError> {
26262631
use {
26272632
pythnet_sdk::{
2628-
pythnet::{ACCUMULATOR_SEQUENCE_ADDR, WORMHOLE_PID},
2633+
pythnet,
26292634
wormhole::{AccumulatorSequenceTracker, MessageData, PostedMessageUnreliableData},
26302635
ACCUMULATOR_EMITTER_ADDRESS,
26312636
},
26322637
solana_sdk::borsh::BorshSerialize,
26332638
};
26342639

2635-
let accumulator_sequence_addr = Self::env_pubkey_or(
2640+
let accumulator_sequence_addr = self.env_pubkey_or(
26362641
"ACCUMULATOR_SEQUENCE_ADDR",
2637-
Pubkey::new_from_array(ACCUMULATOR_SEQUENCE_ADDR),
2642+
Pubkey::new_from_array(pythnet::ACCUMULATOR_SEQUENCE_ADDR),
26382643
)?;
26392644

26402645
// Wormhole uses a Sequence account that is incremented each time a message is posted. As
@@ -2668,8 +2673,10 @@ impl Bank {
26682673
};
26692674

26702675
info!("msg_data.message: {:?}", message.message);
2671-
let wormhole_pid =
2672-
Self::env_pubkey_or("WORMHOLE_PID", Pubkey::new_from_array(WORMHOLE_PID))?;
2676+
let wormhole_pid = self.env_pubkey_or(
2677+
"WORMHOLE_PID",
2678+
Pubkey::new_from_array(pythnet::WORMHOLE_PID),
2679+
)?;
26732680

26742681
// Now we can bump and write the Sequence account.
26752682
sequence.sequence += 1;
@@ -2710,7 +2717,10 @@ impl Bank {
27102717
Ok(())
27112718
}
27122719

2720+
/// Read the pubkey from the environment variable `var` or return `default`
2721+
/// if the variable is not set.
27132722
fn env_pubkey_or(
2723+
&self,
27142724
var: &str,
27152725
default: Pubkey,
27162726
) -> std::result::Result<Pubkey, AccumulatorUpdateError> {

0 commit comments

Comments
 (0)