Skip to content

Commit d0c9075

Browse files
committed
Pubkey
1 parent 56d422d commit d0c9075

18 files changed

+96
-322
lines changed

program/rust/src/c_oracle_header.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ pub struct PriceAccount {
8282
pub unused_2_: i16,
8383
pub unused_3_: i32,
8484
/// Corresponding product account
85-
pub product_account: CPubkey,
85+
pub product_account: Pubkey,
8686
/// Next price account in the list
87-
pub next_price_account: CPubkey,
87+
pub next_price_account: Pubkey,
8888
/// Second to last slot where aggregation was succesful (i.e. status : TRADING)
8989
pub prev_slot_: u64,
9090
/// Aggregate price at prev_slot_
@@ -102,7 +102,7 @@ pub struct PriceAccount {
102102
#[repr(C)]
103103
#[derive(Copy, Clone, Pod, Zeroable)]
104104
pub struct PriceComponent {
105-
pub pub_: CPubkey,
105+
pub pub_: Pubkey,
106106
pub agg_: PriceInfo,
107107
pub latest_: PriceInfo,
108108
}
@@ -138,7 +138,7 @@ pub struct AccountHeader {
138138
#[derive(Copy, Clone, Pod, Zeroable)]
139139
pub struct ProductAccount {
140140
pub header: AccountHeader,
141-
pub first_price_account: CPubkey,
141+
pub first_price_account: Pubkey,
142142
}
143143

144144
#[repr(C)]
@@ -147,35 +147,12 @@ pub struct MappingAccount {
147147
pub header: AccountHeader,
148148
pub number_of_products: u32,
149149
pub unused_: u32,
150-
pub next_mapping_account: CPubkey,
151-
pub products_list: [CPubkey; PC_MAP_TABLE_SIZE as usize],
150+
pub next_mapping_account: Pubkey,
151+
pub products_list: [Pubkey; PC_MAP_TABLE_SIZE as usize],
152152
}
153153

154-
// Unsafe impl because CPubkey is a union
155-
unsafe impl Pod for CPubkey {
156-
}
157-
unsafe impl Zeroable for CPubkey {
158-
}
159-
160-
161154
// Unsafe impl because product_list is of size 640 and there's no derived trait for this size
162155
unsafe impl Pod for MappingAccount {
163156
}
164157
unsafe impl Zeroable for MappingAccount {
165158
}
166-
167-
#[repr(C)]
168-
#[derive(Copy, Clone)]
169-
pub union CPubkey {
170-
pub k1_: [u8; 32usize],
171-
pub k8_: [u64; 4usize],
172-
}
173-
174-
impl CPubkey {
175-
pub fn new_unique() -> CPubkey {
176-
let solana_unique = Pubkey::new_unique();
177-
CPubkey {
178-
k1_: solana_unique.to_bytes(),
179-
}
180-
}
181-
}

program/rust/src/instruction.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use crate::c_oracle_header::{
2-
CPubkey,
3-
PC_VERSION,
4-
};
1+
use crate::c_oracle_header::PC_VERSION;
52
use crate::deserialize::load;
63
use crate::error::OracleError;
74
use bytemuck::{
@@ -13,6 +10,7 @@ use num_derive::{
1310
ToPrimitive,
1411
};
1512
use num_traits::FromPrimitive;
13+
use solana_program::pubkey::Pubkey;
1614

1715
/// WARNING : NEW COMMANDS SHOULD BE ADDED AT THE END OF THE LIST
1816
#[repr(i32)]
@@ -122,7 +120,7 @@ pub type InitPriceArgs = AddPriceArgs;
122120
#[derive(Zeroable, Pod, Copy, Clone)]
123121
pub struct AddPublisherArgs {
124122
pub header: CommandHeader,
125-
pub publisher: CPubkey,
123+
pub publisher: Pubkey,
126124
}
127125

128126
pub type DelPublisherArgs = AddPublisherArgs;

program/rust/src/rust_oracle.rs

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::mem::{
44
};
55

66
use crate::c_oracle_header::{
7-
CPubkey,
87
MappingAccount,
98
PriceAccount,
109
PriceComponent,
@@ -42,18 +41,14 @@ use crate::utils::{
4241
check_valid_signable_account,
4342
check_valid_writable_account,
4443
is_component_update,
45-
pubkey_assign,
46-
pubkey_clear,
47-
pubkey_equal,
48-
pubkey_is_zero,
4944
pyth_assert,
5045
read_pc_str_t,
5146
try_convert,
5247
};
5348
use crate::OracleError;
5449
use bytemuck::{
55-
bytes_of,
5650
bytes_of_mut,
51+
Zeroable,
5752
};
5853
use solana_program::account_info::AccountInfo;
5954
use solana_program::clock::Clock;
@@ -209,15 +204,12 @@ pub fn add_mapping(
209204
let mut cur_mapping = load_checked::<MappingAccount>(cur_mapping, hdr.version)?;
210205
pyth_assert(
211206
cur_mapping.number_of_products == PC_MAP_TABLE_SIZE
212-
&& pubkey_is_zero(&cur_mapping.next_mapping_account),
207+
&& cur_mapping.next_mapping_account == Pubkey::zeroed(),
213208
ProgramError::InvalidArgument,
214209
)?;
215210

216211
initialize_pyth_account_checked::<MappingAccount>(next_mapping, hdr.version)?;
217-
pubkey_assign(
218-
&mut cur_mapping.next_mapping_account,
219-
&next_mapping.key.to_bytes(),
220-
);
212+
cur_mapping.next_mapping_account = *next_mapping.key;
221213

222214
Ok(())
223215
}
@@ -252,10 +244,7 @@ pub fn upd_price(
252244

253245
// Verify that publisher is authorized
254246
while publisher_index < price_data.num_ as usize {
255-
if pubkey_equal(
256-
&price_data.comp_[publisher_index].pub_,
257-
&funding_account.key.to_bytes(),
258-
) {
247+
if price_data.comp_[publisher_index].pub_ == *funding_account.key {
259248
break;
260249
}
261250
publisher_index += 1;
@@ -369,18 +358,9 @@ pub fn add_price(
369358
initialize_pyth_account_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
370359
price_data.exponent = cmd_args.exponent;
371360
price_data.price_type = cmd_args.price_type;
372-
pubkey_assign(
373-
&mut price_data.product_account,
374-
&product_account.key.to_bytes(),
375-
);
376-
pubkey_assign(
377-
&mut price_data.next_price_account,
378-
bytes_of(&product_data.first_price_account),
379-
);
380-
pubkey_assign(
381-
&mut product_data.first_price_account,
382-
&price_account.key.to_bytes(),
383-
);
361+
price_data.product_account = *product_account.key;
362+
price_data.next_price_account = product_data.first_price_account;
363+
product_data.first_price_account = *price_account.key;
384364

385365
Ok(())
386366
}
@@ -411,22 +391,16 @@ pub fn del_price(
411391
let mut product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;
412392
let price_data = load_checked::<PriceAccount>(price_account, cmd_args.version)?;
413393
pyth_assert(
414-
pubkey_equal(
415-
&product_data.first_price_account,
416-
&price_account.key.to_bytes(),
417-
),
394+
product_data.first_price_account == *price_account.key,
418395
ProgramError::InvalidArgument,
419396
)?;
420397

421398
pyth_assert(
422-
pubkey_equal(&price_data.product_account, &product_account.key.to_bytes()),
399+
price_data.product_account == *product_account.key,
423400
ProgramError::InvalidArgument,
424401
)?;
425402

426-
pubkey_assign(
427-
&mut product_data.first_price_account,
428-
bytes_of(&price_data.next_price_account),
429-
);
403+
product_data.first_price_account = price_data.next_price_account;
430404
}
431405

432406
// Zero out the balance of the price account to delete it.
@@ -514,7 +488,7 @@ pub fn add_publisher(
514488

515489
pyth_assert(
516490
instruction_data.len() == size_of::<AddPublisherArgs>()
517-
&& !pubkey_is_zero(&cmd_args.publisher),
491+
&& cmd_args.publisher != Pubkey::zeroed(),
518492
ProgramError::InvalidArgument,
519493
)?;
520494

@@ -533,7 +507,7 @@ pub fn add_publisher(
533507
}
534508

535509
for i in 0..(price_data.num_ as usize) {
536-
if pubkey_equal(&cmd_args.publisher, bytes_of(&price_data.comp_[i].pub_)) {
510+
if cmd_args.publisher == price_data.comp_[i].pub_ {
537511
return Err(ProgramError::InvalidArgument);
538512
}
539513
}
@@ -544,10 +518,7 @@ pub fn add_publisher(
544518
0,
545519
size_of::<PriceComponent>(),
546520
);
547-
pubkey_assign(
548-
&mut price_data.comp_[current_index].pub_,
549-
bytes_of(&cmd_args.publisher),
550-
);
521+
price_data.comp_[current_index].pub_ = cmd_args.publisher;
551522
price_data.num_ += 1;
552523
price_data.header.size =
553524
try_convert::<_, u32>(size_of::<PriceAccount>() - size_of_val(&price_data.comp_))?
@@ -567,7 +538,7 @@ pub fn del_publisher(
567538

568539
pyth_assert(
569540
instruction_data.len() == size_of::<DelPublisherArgs>()
570-
&& !pubkey_is_zero(&cmd_args.publisher),
541+
&& cmd_args.publisher != Pubkey::zeroed(),
571542
ProgramError::InvalidArgument,
572543
)?;
573544

@@ -582,7 +553,7 @@ pub fn del_publisher(
582553
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
583554

584555
for i in 0..(price_data.num_ as usize) {
585-
if pubkey_equal(&cmd_args.publisher, bytes_of(&price_data.comp_[i].pub_)) {
556+
if cmd_args.publisher == price_data.comp_[i].pub_ {
586557
for j in i + 1..(price_data.num_ as usize) {
587558
price_data.comp_[j - 1] = price_data.comp_[j];
588559
}
@@ -632,15 +603,12 @@ pub fn add_product(
632603
initialize_pyth_account_checked::<ProductAccount>(new_product_account, hdr.version)?;
633604

634605
let current_index: usize = try_convert(mapping_data.number_of_products)?;
635-
pubkey_assign(
636-
&mut mapping_data.products_list[current_index],
637-
bytes_of(&new_product_account.key.to_bytes()),
638-
);
606+
mapping_data.products_list[current_index] = *new_product_account.key;
639607
mapping_data.number_of_products += 1;
640608
mapping_data.header.size = try_convert::<_, u32>(
641609
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
642610
)? + mapping_data.number_of_products
643-
* try_convert::<_, u32>(size_of::<CPubkey>())?;
611+
* try_convert::<_, u32>(size_of::<Pubkey>())?;
644612

645613
Ok(())
646614
}
@@ -763,15 +731,15 @@ pub fn del_product(
763731
ProgramError::InvalidArgument,
764732
)?;
765733
pyth_assert(
766-
pubkey_is_zero(&product_data.first_price_account),
734+
product_data.first_price_account == Pubkey::zeroed(),
767735
ProgramError::InvalidArgument,
768736
)?;
769737

770-
let product_key = product_account.key.to_bytes();
738+
let product_key = product_account.key;
771739
let product_index = mapping_data
772740
.products_list
773741
.iter()
774-
.position(|x| pubkey_equal(x, &product_key))
742+
.position(|x| *x == *product_key)
775743
.ok_or(ProgramError::InvalidArgument)?;
776744

777745
let num_after_removal: usize = try_convert(
@@ -782,16 +750,13 @@ pub fn del_product(
782750
)?;
783751

784752
let last_key_bytes = mapping_data.products_list[num_after_removal];
785-
pubkey_assign(
786-
&mut mapping_data.products_list[product_index],
787-
bytes_of(&last_key_bytes),
788-
);
789-
pubkey_clear(&mut mapping_data.products_list[num_after_removal]);
753+
mapping_data.products_list[product_index] = last_key_bytes;
754+
mapping_data.products_list[num_after_removal] = Pubkey::zeroed();
790755
mapping_data.number_of_products = try_convert::<_, u32>(num_after_removal)?;
791756
mapping_data.header.size = try_convert::<_, u32>(
792757
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
793758
)? + mapping_data.number_of_products
794-
* try_convert::<_, u32>(size_of::<CPubkey>())?;
759+
* try_convert::<_, u32>(size_of::<Pubkey>())?;
795760
}
796761

797762
// Zero out the balance of the price account to delete it.

program/rust/src/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ mod test_del_product;
88
mod test_del_publisher;
99
mod test_init_mapping;
1010
mod test_init_price;
11-
mod test_resize_account;
1211
mod test_set_min_pub;
1312
mod test_sizes;
1413
mod test_upd_aggregate;

program/rust/src/tests/pyth_simulator.rs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ use solana_program::instruction::{
1111
};
1212
use solana_program::pubkey::Pubkey;
1313
use solana_program::rent::Rent;
14-
use solana_program::{
15-
system_instruction,
16-
system_program,
17-
};
14+
use solana_program::system_instruction;
1815
use solana_program_test::{
1916
processor,
2017
BanksClient,
@@ -227,27 +224,6 @@ impl PythSimulator {
227224
.await
228225
}
229226

230-
/// Resize a price account (using the resize_price_account
231-
/// instruction).
232-
pub async fn resize_price_account(
233-
&mut self,
234-
price_keypair: &Keypair,
235-
) -> Result<(), BanksClientError> {
236-
let cmd: CommandHeader = OracleCommand::ResizePriceAccount.into();
237-
let instruction = Instruction::new_with_bytes(
238-
self.program_id,
239-
bytes_of(&cmd),
240-
vec![
241-
AccountMeta::new(self.payer.pubkey(), true),
242-
AccountMeta::new(price_keypair.pubkey(), true),
243-
AccountMeta::new(system_program::id(), false),
244-
],
245-
);
246-
247-
self.process_ix(instruction, &vec![&price_keypair]).await
248-
}
249-
250-
251227
/// Get the account at `key`. Returns `None` if no such account exists.
252228
pub async fn get_account(&mut self, key: Pubkey) -> Option<Account> {
253229
self.banks_client.get_account(key).await.unwrap()

program/rust/src/tests/test_add_mapping.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ use crate::instruction::{
1515
};
1616
use crate::processor::process_instruction;
1717
use crate::tests::test_utils::AccountSetup;
18-
use crate::utils::{
19-
clear_account,
20-
pubkey_assign,
21-
pubkey_equal,
22-
pubkey_is_zero,
18+
use crate::utils::clear_account;
19+
use bytemuck::{
20+
bytes_of,
21+
Zeroable,
2322
};
24-
use bytemuck::bytes_of;
2523
use solana_program::program_error::ProgramError;
2624
use solana_program::pubkey::Pubkey;
2725

@@ -64,15 +62,9 @@ fn test_add_mapping() {
6462
let mut cur_mapping_data =
6563
load_checked::<MappingAccount>(&cur_mapping, PC_VERSION).unwrap();
6664

67-
assert!(pubkey_equal(
68-
&cur_mapping_data.next_mapping_account,
69-
&next_mapping.key.to_bytes()
70-
));
71-
assert!(pubkey_is_zero(&next_mapping_data.next_mapping_account));
72-
pubkey_assign(
73-
&mut cur_mapping_data.next_mapping_account,
74-
&Pubkey::default().to_bytes(),
75-
);
65+
assert!(cur_mapping_data.next_mapping_account == *next_mapping.key);
66+
assert!(next_mapping_data.next_mapping_account == Pubkey::zeroed());
67+
cur_mapping_data.next_mapping_account = Pubkey::zeroed();
7668
cur_mapping_data.number_of_products = 0;
7769
}
7870

@@ -94,7 +86,7 @@ fn test_add_mapping() {
9486
{
9587
let mut cur_mapping_data =
9688
load_checked::<MappingAccount>(&cur_mapping, PC_VERSION).unwrap();
97-
assert!(pubkey_is_zero(&cur_mapping_data.next_mapping_account));
89+
assert!(cur_mapping_data.next_mapping_account == Pubkey::zeroed());
9890
cur_mapping_data.number_of_products = PC_MAP_TABLE_SIZE;
9991
cur_mapping_data.header.magic_number = 0;
10092
}

0 commit comments

Comments
 (0)