Skip to content

Commit eab08d4

Browse files
authored
Renames (#276)
* More renames * Another rename
1 parent 36bae1a commit eab08d4

15 files changed

+176
-143
lines changed

program/rust/src/c_oracle_header.rs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,49 +57,46 @@ 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
69-
pub num_: u32,
66+
pub num_: u32,
7067
/// Number of valid quotes for the last aggregation
71-
pub num_qt_: u32,
68+
pub num_qt_: u32,
7269
/// Last slot with a succesful aggregation (status : TRADING)
73-
pub last_slot_: u64,
70+
pub last_slot_: u64,
7471
/// Second to last slot where aggregation was attempted
75-
pub valid_slot_: u64,
72+
pub valid_slot_: u64,
7673
/// Ema for price
77-
pub twap_: PriceEma,
74+
pub twap_: PriceEma,
7875
/// Ema for confidence
79-
pub twac_: PriceEma,
76+
pub twac_: PriceEma,
8077
/// Last time aggregation was attempted
81-
pub timestamp_: i64,
78+
pub timestamp_: i64,
8279
/// Minimum valid publisher quotes for a succesful aggregation
83-
pub min_pub_: u8,
84-
pub unused_1_: i8,
85-
pub unused_2_: i16,
86-
pub unused_3_: i32,
80+
pub min_pub_: u8,
81+
pub unused_1_: i8,
82+
pub unused_2_: i16,
83+
pub unused_3_: i32,
8784
/// Corresponding product account
88-
pub prod_: CPubkey,
85+
pub product_account: CPubkey,
8986
/// Next price account in the list
90-
pub next_: CPubkey,
87+
pub next_price_account: CPubkey,
9188
/// Second to last slot where aggregation was succesful (i.e. status : TRADING)
92-
pub prev_slot_: u64,
89+
pub prev_slot_: u64,
9390
/// Aggregate price at prev_slot_
94-
pub prev_price_: i64,
91+
pub prev_price_: i64,
9592
/// Confidence interval at prev_slot_
96-
pub prev_conf_: u64,
93+
pub prev_conf_: u64,
9794
/// Timestamp of prev_slot_
98-
pub prev_timestamp_: i64,
95+
pub prev_timestamp_: i64,
9996
/// Last attempted aggregate results
100-
pub agg_: PriceInfo,
97+
pub agg_: PriceInfo,
10198
/// Publishers' price components
102-
pub comp_: [PriceComponent; PC_COMP_SIZE as usize],
99+
pub comp_: [PriceComponent; PC_COMP_SIZE as usize],
103100
}
104101

105102
#[repr(C)]
@@ -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: 57 additions & 33 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,20 @@ 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;
368-
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());
370+
price_data.exponent = cmd_args.exponent;
371+
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+
);
371384

372385
Ok(())
373386
}
@@ -398,16 +411,22 @@ pub fn del_price(
398411
let mut product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;
399412
let price_data = load_checked::<PriceAccount>(price_account, cmd_args.version)?;
400413
pyth_assert(
401-
pubkey_equal(&product_data.px_acc_, &price_account.key.to_bytes()),
414+
pubkey_equal(
415+
&product_data.first_price_account,
416+
&price_account.key.to_bytes(),
417+
),
402418
ProgramError::InvalidArgument,
403419
)?;
404420

405421
pyth_assert(
406-
pubkey_equal(&price_data.prod_, &product_account.key.to_bytes()),
422+
pubkey_equal(&price_data.product_account, &product_account.key.to_bytes()),
407423
ProgramError::InvalidArgument,
408424
)?;
409425

410-
pubkey_assign(&mut product_data.px_acc_, bytes_of(&price_data.next_));
426+
pubkey_assign(
427+
&mut product_data.first_price_account,
428+
bytes_of(&price_data.next_price_account),
429+
);
411430
}
412431

413432
// Zero out the balance of the price account to delete it.
@@ -439,11 +458,11 @@ pub fn init_price(
439458

440459
let mut price_data = load_checked::<PriceAccount>(price_account, cmd_args.header.version)?;
441460
pyth_assert(
442-
price_data.ptype_ == cmd_args.price_type,
461+
price_data.price_type == cmd_args.price_type,
443462
ProgramError::InvalidArgument,
444463
)?;
445464

446-
price_data.expo_ = cmd_args.exponent;
465+
price_data.exponent = cmd_args.exponent;
447466

448467
price_data.last_slot_ = 0;
449468
price_data.valid_slot_ = 0;
@@ -530,7 +549,7 @@ pub fn add_publisher(
530549
bytes_of(&cmd_args.publisher),
531550
);
532551
price_data.num_ += 1;
533-
price_data.size_ =
552+
price_data.header.size =
534553
try_convert::<_, u32>(size_of::<PriceAccount>() - size_of_val(&price_data.comp_))?
535554
+ price_data.num_ * try_convert::<_, u32>(size_of::<PriceComponent>())?;
536555
Ok(())
@@ -574,7 +593,7 @@ pub fn del_publisher(
574593
0,
575594
size_of::<PriceComponent>(),
576595
);
577-
price_data.size_ =
596+
price_data.header.size =
578597
try_convert::<_, u32>(size_of::<PriceAccount>() - size_of_val(&price_data.comp_))?
579598
+ price_data.num_ * try_convert::<_, u32>(size_of::<PriceComponent>())?;
580599
return Ok(());
@@ -606,21 +625,22 @@ pub fn add_product(
606625
let mut mapping_data = load_checked::<MappingAccount>(tail_mapping_account, hdr.version)?;
607626
// The mapping account must have free space to add the product account
608627
pyth_assert(
609-
mapping_data.num_ < PC_MAP_TABLE_SIZE,
628+
mapping_data.number_of_products < PC_MAP_TABLE_SIZE,
610629
ProgramError::InvalidArgument,
611630
)?;
612631

613632
initialize_pyth_account_checked::<ProductAccount>(new_product_account, hdr.version)?;
614633

615-
let current_index: usize = try_convert(mapping_data.num_)?;
634+
let current_index: usize = try_convert(mapping_data.number_of_products)?;
616635
pubkey_assign(
617-
&mut mapping_data.prod_[current_index],
636+
&mut mapping_data.products_list[current_index],
618637
bytes_of(&new_product_account.key.to_bytes()),
619638
);
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>())?;
639+
mapping_data.number_of_products += 1;
640+
mapping_data.header.size = try_convert::<_, u32>(
641+
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
642+
)? + mapping_data.number_of_products
643+
* try_convert::<_, u32>(size_of::<CPubkey>())?;
624644

625645
Ok(())
626646
}
@@ -680,7 +700,7 @@ pub fn upd_product(
680700
}
681701

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

685705
Ok(())
686706
}
@@ -738,36 +758,40 @@ pub fn del_product(
738758
let product_data = load_checked::<ProductAccount>(product_account, cmd_args.version)?;
739759

740760
// This assertion is just to make the subtractions below simpler
741-
pyth_assert(mapping_data.num_ >= 1, ProgramError::InvalidArgument)?;
742761
pyth_assert(
743-
pubkey_is_zero(&product_data.px_acc_),
762+
mapping_data.number_of_products >= 1,
763+
ProgramError::InvalidArgument,
764+
)?;
765+
pyth_assert(
766+
pubkey_is_zero(&product_data.first_price_account),
744767
ProgramError::InvalidArgument,
745768
)?;
746769

747770
let product_key = product_account.key.to_bytes();
748771
let product_index = mapping_data
749-
.prod_
772+
.products_list
750773
.iter()
751774
.position(|x| pubkey_equal(x, &product_key))
752775
.ok_or(ProgramError::InvalidArgument)?;
753776

754777
let num_after_removal: usize = try_convert(
755778
mapping_data
756-
.num_
779+
.number_of_products
757780
.checked_sub(1)
758781
.ok_or(ProgramError::InvalidArgument)?,
759782
)?;
760783

761-
let last_key_bytes = mapping_data.prod_[num_after_removal];
784+
let last_key_bytes = mapping_data.products_list[num_after_removal];
762785
pubkey_assign(
763-
&mut mapping_data.prod_[product_index],
786+
&mut mapping_data.products_list[product_index],
764787
bytes_of(&last_key_bytes),
765788
);
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>())?;
789+
pubkey_clear(&mut mapping_data.products_list[num_after_removal]);
790+
mapping_data.number_of_products = try_convert::<_, u32>(num_after_removal)?;
791+
mapping_data.header.size = try_convert::<_, u32>(
792+
size_of::<MappingAccount>() - size_of_val(&mapping_data.products_list),
793+
)? + mapping_data.number_of_products
794+
* try_convert::<_, u32>(size_of::<CPubkey>())?;
771795
}
772796

773797
// 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(

0 commit comments

Comments
 (0)