Skip to content

Commit e82944d

Browse files
committed
More renames
1 parent 36bae1a commit e82944d

15 files changed

+138
-114
lines changed

program/rust/src/c_oracle_header.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,11 @@ impl PythAccount for PriceAccount {
5757
#[repr(C)]
5858
#[derive(Copy, Clone, Pod, Zeroable)]
5959
pub struct PriceAccount {
60-
pub magic_: u32,
61-
pub ver_: u32,
62-
pub type_: u32,
63-
pub size_: u32,
60+
pub header: AccountHeader,
6461
/// Type of the price account
65-
pub ptype_: u32,
62+
pub price_type: u32,
6663
/// Exponent for the published prices
67-
pub expo_: i32,
64+
pub exponent: i32,
6865
/// Current number of authorized publishers
6966
pub num_: u32,
7067
/// Number of valid quotes for the last aggregation
@@ -131,33 +128,27 @@ pub struct PriceEma {
131128
#[repr(C)]
132129
#[derive(Copy, Clone, Zeroable, Pod)]
133130
pub struct AccountHeader {
134-
pub magic_: u32,
135-
pub ver_: u32,
136-
pub type_: u32,
137-
pub size_: u32,
131+
pub magic_number: u32,
132+
pub version: u32,
133+
pub account_type: u32,
134+
pub size: u32,
138135
}
139136

140137
#[repr(C)]
141138
#[derive(Copy, Clone, Pod, Zeroable)]
142139
pub struct ProductAccount {
143-
pub magic_: u32,
144-
pub ver_: u32,
145-
pub type_: u32,
146-
pub size_: u32,
147-
pub px_acc_: CPubkey,
140+
pub header: AccountHeader,
141+
pub first_price_account: CPubkey,
148142
}
149143

150144
#[repr(C)]
151145
#[derive(Copy, Clone)]
152146
pub struct MappingAccount {
153-
pub magic_: u32,
154-
pub ver_: u32,
155-
pub type_: u32,
156-
pub size_: u32,
157-
pub num_: u32,
158-
pub unused_: u32,
159-
pub next_: CPubkey,
160-
pub prod_: [CPubkey; PC_MAP_TABLE_SIZE as usize],
147+
pub header: AccountHeader,
148+
pub number_of_products: u32,
149+
pub unused_: u32,
150+
pub next_mapping_account: CPubkey,
151+
pub products_list: [CPubkey; PC_MAP_TABLE_SIZE as usize],
161152
}
162153

163154
// Unsafe impl because CPubkey is a union

program/rust/src/deserialize.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ pub fn load_checked<'a, T: PythAccount>(
7373
{
7474
let account_header = load_account_as::<AccountHeader>(account)?;
7575
pyth_assert(
76-
account_header.magic_ == PC_MAGIC
77-
&& account_header.ver_ == version
78-
&& account_header.type_ == T::ACCOUNT_TYPE,
76+
account_header.magic_number == PC_MAGIC
77+
&& account_header.version == version
78+
&& account_header.account_type == T::ACCOUNT_TYPE,
7979
ProgramError::InvalidArgument,
8080
)?;
8181
}
@@ -91,10 +91,10 @@ pub fn initialize_pyth_account_checked<'a, T: PythAccount>(
9191

9292
{
9393
let mut account_header = load_account_as_mut::<AccountHeader>(account)?;
94-
account_header.magic_ = PC_MAGIC;
95-
account_header.ver_ = version;
96-
account_header.type_ = T::ACCOUNT_TYPE;
97-
account_header.size_ = T::INITIAL_SIZE;
94+
account_header.magic_number = PC_MAGIC;
95+
account_header.version = version;
96+
account_header.account_type = T::ACCOUNT_TYPE;
97+
account_header.size = T::INITIAL_SIZE;
9898
}
9999

100100
load_account_as_mut::<T>(account)

program/rust/src/rust_oracle.rs

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,16 @@ pub fn add_mapping(
208208
let hdr = load::<CommandHeader>(instruction_data)?;
209209
let mut cur_mapping = load_checked::<MappingAccount>(cur_mapping, hdr.version)?;
210210
pyth_assert(
211-
cur_mapping.num_ == PC_MAP_TABLE_SIZE && pubkey_is_zero(&cur_mapping.next_),
211+
cur_mapping.number_of_products == PC_MAP_TABLE_SIZE
212+
&& pubkey_is_zero(&cur_mapping.next_mapping_account),
212213
ProgramError::InvalidArgument,
213214
)?;
214215

215216
initialize_pyth_account_checked::<MappingAccount>(next_mapping, hdr.version)?;
216-
pubkey_assign(&mut cur_mapping.next_, &next_mapping.key.to_bytes());
217+
pubkey_assign(
218+
&mut cur_mapping.next_mapping_account,
219+
&next_mapping.key.to_bytes(),
220+
);
217221

218222
Ok(())
219223
}
@@ -363,11 +367,17 @@ pub fn add_price(
363367

364368
let mut price_data =
365369
initialize_pyth_account_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
366-
price_data.expo_ = cmd_args.exponent;
367-
price_data.ptype_ = cmd_args.price_type;
370+
price_data.exponent = cmd_args.exponent;
371+
price_data.price_type = cmd_args.price_type;
368372
pubkey_assign(&mut price_data.prod_, &product_account.key.to_bytes());
369-
pubkey_assign(&mut price_data.next_, bytes_of(&product_data.px_acc_));
370-
pubkey_assign(&mut product_data.px_acc_, &price_account.key.to_bytes());
373+
pubkey_assign(
374+
&mut price_data.next_,
375+
bytes_of(&product_data.first_price_account),
376+
);
377+
pubkey_assign(
378+
&mut product_data.first_price_account,
379+
&price_account.key.to_bytes(),
380+
);
371381

372382
Ok(())
373383
}
@@ -398,7 +408,10 @@ pub fn del_price(
398408
let mut product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;
399409
let price_data = load_checked::<PriceAccount>(price_account, cmd_args.version)?;
400410
pyth_assert(
401-
pubkey_equal(&product_data.px_acc_, &price_account.key.to_bytes()),
411+
pubkey_equal(
412+
&product_data.first_price_account,
413+
&price_account.key.to_bytes(),
414+
),
402415
ProgramError::InvalidArgument,
403416
)?;
404417

@@ -407,7 +420,10 @@ pub fn del_price(
407420
ProgramError::InvalidArgument,
408421
)?;
409422

410-
pubkey_assign(&mut product_data.px_acc_, bytes_of(&price_data.next_));
423+
pubkey_assign(
424+
&mut product_data.first_price_account,
425+
bytes_of(&price_data.next_),
426+
);
411427
}
412428

413429
// Zero out the balance of the price account to delete it.
@@ -439,11 +455,11 @@ pub fn init_price(
439455

440456
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
441457
pyth_assert(
442-
price_data.ptype_ == cmd_args.price_type,
458+
price_data.price_type == cmd_args.price_type,
443459
ProgramError::InvalidArgument,
444460
)?;
445461

446-
price_data.expo_ = cmd_args.exponent;
462+
price_data.exponent = cmd_args.exponent;
447463

448464
price_data.last_slot_ = 0;
449465
price_data.valid_slot_ = 0;
@@ -530,7 +546,7 @@ pub fn add_publisher(
530546
bytes_of(&cmd_args.publisher),
531547
);
532548
price_data.num_ += 1;
533-
price_data.size_ =
549+
price_data.header.size =
534550
try_convert::<_, u32>(size_of::<PriceAccount>() - size_of_val(&price_data.comp_))?
535551
+ price_data.num_ * try_convert::<_, u32>(size_of::<PriceComponent>())?;
536552
Ok(())
@@ -574,7 +590,7 @@ pub fn del_publisher(
574590
0,
575591
size_of::<PriceComponent>(),
576592
);
577-
price_data.size_ =
593+
price_data.header.size =
578594
try_convert::<_, u32>(size_of::<PriceAccount>() - size_of_val(&price_data.comp_))?
579595
+ price_data.num_ * try_convert::<_, u32>(size_of::<PriceComponent>())?;
580596
return Ok(());
@@ -606,21 +622,22 @@ pub fn add_product(
606622
let mut mapping_data = load_checked::<MappingAccount>(tail_mapping_account, hdr.version)?;
607623
// The mapping account must have free space to add the product account
608624
pyth_assert(
609-
mapping_data.num_ < PC_MAP_TABLE_SIZE,
625+
mapping_data.number_of_products < PC_MAP_TABLE_SIZE,
610626
ProgramError::InvalidArgument,
611627
)?;
612628

613629
initialize_pyth_account_checked::<ProductAccount>(new_product_account, hdr.version)?;
614630

615-
let current_index: usize = try_convert(mapping_data.num_)?;
631+
let current_index: usize = try_convert(mapping_data.number_of_products)?;
616632
pubkey_assign(
617-
&mut mapping_data.prod_[current_index],
633+
&mut mapping_data.products_list[current_index],
618634
bytes_of(&new_product_account.key.to_bytes()),
619635
);
620-
mapping_data.num_ += 1;
621-
mapping_data.size_ =
622-
try_convert::<_, u32>(size_of::<MappingAccount>() - size_of_val(&mapping_data.prod_))?
623-
+ mapping_data.num_ * try_convert::<_, u32>(size_of::<CPubkey>())?;
636+
mapping_data.number_of_products += 1;
637+
mapping_data.header.size = try_convert::<_, u32>(
638+
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
639+
)? + mapping_data.number_of_products
640+
* try_convert::<_, u32>(size_of::<CPubkey>())?;
624641

625642
Ok(())
626643
}
@@ -680,7 +697,7 @@ pub fn upd_product(
680697
}
681698

682699
let mut product_data = load_checked::<ProductAccount>(product_account, hdr.version)?;
683-
product_data.size_ = try_convert(size_of::<ProductAccount>() + new_data.len())?;
700+
product_data.header.size = try_convert(size_of::<ProductAccount>() + new_data.len())?;
684701

685702
Ok(())
686703
}
@@ -738,36 +755,40 @@ pub fn del_product(
738755
let product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;
739756

740757
// This assertion is just to make the subtractions below simpler
741-
pyth_assert(mapping_data.num_ >= 1, ProgramError::InvalidArgument)?;
742758
pyth_assert(
743-
pubkey_is_zero(&product_data.px_acc_),
759+
mapping_data.number_of_products >= 1,
760+
ProgramError::InvalidArgument,
761+
)?;
762+
pyth_assert(
763+
pubkey_is_zero(&product_data.first_price_account),
744764
ProgramError::InvalidArgument,
745765
)?;
746766

747767
let product_key = product_account.key.to_bytes();
748768
let product_index = mapping_data
749-
.prod_
769+
.products_list
750770
.iter()
751771
.position(|x| pubkey_equal(x, &product_key))
752772
.ok_or(ProgramError::InvalidArgument)?;
753773

754774
let num_after_removal: usize = try_convert(
755775
mapping_data
756-
.num_
776+
.number_of_products
757777
.checked_sub(1)
758778
.ok_or(ProgramError::InvalidArgument)?,
759779
)?;
760780

761-
let last_key_bytes = mapping_data.prod_[num_after_removal];
781+
let last_key_bytes = mapping_data.products_list[num_after_removal];
762782
pubkey_assign(
763-
&mut mapping_data.prod_[product_index],
783+
&mut mapping_data.products_list[product_index],
764784
bytes_of(&last_key_bytes),
765785
);
766-
pubkey_clear(&mut mapping_data.prod_[num_after_removal]);
767-
mapping_data.num_ = try_convert::<_, u32>(num_after_removal)?;
768-
mapping_data.size_ =
769-
try_convert::<_, u32>(size_of::<MappingAccount>() - size_of_val(&mapping_data.prod_))?
770-
+ mapping_data.num_ * try_convert::<_, u32>(size_of::<CPubkey>())?;
786+
pubkey_clear(&mut mapping_data.products_list[num_after_removal]);
787+
mapping_data.number_of_products = try_convert::<_, u32>(num_after_removal)?;
788+
mapping_data.header.size = try_convert::<_, u32>(
789+
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
790+
)? + mapping_data.number_of_products
791+
* try_convert::<_, u32>(size_of::<CPubkey>())?;
771792
}
772793

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

program/rust/src/tests/test_add_mapping.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn test_add_mapping() {
4545
{
4646
let mut cur_mapping_data =
4747
load_checked::<MappingAccount>(&cur_mapping, PC_VERSION).unwrap();
48-
cur_mapping_data.num_ = PC_MAP_TABLE_SIZE;
48+
cur_mapping_data.number_of_products = PC_MAP_TABLE_SIZE;
4949
}
5050

5151
assert!(process_instruction(
@@ -65,12 +65,15 @@ fn test_add_mapping() {
6565
load_checked::<MappingAccount>(&cur_mapping, PC_VERSION).unwrap();
6666

6767
assert!(pubkey_equal(
68-
&cur_mapping_data.next_,
68+
&cur_mapping_data.next_mapping_account,
6969
&next_mapping.key.to_bytes()
7070
));
71-
assert!(pubkey_is_zero(&next_mapping_data.next_));
72-
pubkey_assign(&mut cur_mapping_data.next_, &Pubkey::default().to_bytes());
73-
cur_mapping_data.num_ = 0;
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+
);
76+
cur_mapping_data.number_of_products = 0;
7477
}
7578

7679
clear_account(&next_mapping).unwrap();
@@ -91,9 +94,9 @@ fn test_add_mapping() {
9194
{
9295
let mut cur_mapping_data =
9396
load_checked::<MappingAccount>(&cur_mapping, PC_VERSION).unwrap();
94-
assert!(pubkey_is_zero(&cur_mapping_data.next_));
95-
cur_mapping_data.num_ = PC_MAP_TABLE_SIZE;
96-
cur_mapping_data.magic_ = 0;
97+
assert!(pubkey_is_zero(&cur_mapping_data.next_mapping_account));
98+
cur_mapping_data.number_of_products = PC_MAP_TABLE_SIZE;
99+
cur_mapping_data.header.magic_number = 0;
97100
}
98101

99102
assert_eq!(
@@ -111,7 +114,7 @@ fn test_add_mapping() {
111114

112115
{
113116
let mut cur_mapping_data = load_account_as_mut::<MappingAccount>(&cur_mapping).unwrap();
114-
cur_mapping_data.magic_ = PC_MAGIC;
117+
cur_mapping_data.header.magic_number = PC_MAGIC;
115118
}
116119

117120
assert!(process_instruction(

program/rust/src/tests/test_add_price.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ fn test_add_price() {
8080
{
8181
let price_data = load_checked::<PriceAccount>(&price_account, PC_VERSION).unwrap();
8282
let product_data = load_checked::<ProductAccount>(&product_account, PC_VERSION).unwrap();
83-
assert_eq!(price_data.expo_, 1);
84-
assert_eq!(price_data.ptype_, 1);
83+
assert_eq!(price_data.exponent, 1);
84+
assert_eq!(price_data.price_type, 1);
8585
assert!(pubkey_equal(
8686
&price_data.prod_,
8787
&product_account.key.to_bytes()
8888
));
8989
assert!(pubkey_is_zero(&price_data.next_));
9090
assert!(pubkey_equal(
91-
&product_data.px_acc_,
91+
&product_data.first_price_account,
9292
&price_account.key.to_bytes()
9393
));
9494
}
@@ -107,8 +107,8 @@ fn test_add_price() {
107107
{
108108
let price_data_2 = load_checked::<PriceAccount>(&price_account_2, PC_VERSION).unwrap();
109109
let product_data = load_checked::<ProductAccount>(&product_account, PC_VERSION).unwrap();
110-
assert_eq!(price_data_2.expo_, 1);
111-
assert_eq!(price_data_2.ptype_, 1);
110+
assert_eq!(price_data_2.exponent, 1);
111+
assert_eq!(price_data_2.price_type, 1);
112112
assert!(pubkey_equal(
113113
&price_data_2.prod_,
114114
&product_account.key.to_bytes()
@@ -118,7 +118,7 @@ fn test_add_price() {
118118
&price_account.key.to_bytes()
119119
));
120120
assert!(pubkey_equal(
121-
&product_data.px_acc_,
121+
&product_data.first_price_account,
122122
&price_account_2.key.to_bytes()
123123
));
124124
}

0 commit comments

Comments
 (0)