Skip to content

Commit 666fadd

Browse files
authored
Reduce size of Oracle binary (#309)
* Tweak compile parameters to reduce file size * Remove no-op time machine functions * Remove unused resize_price_account instruction * Fix clippy warning * Don't strip symbols in release (this is done by the build script)
1 parent 9024a24 commit 666fadd

File tree

4 files changed

+4
-113
lines changed

4 files changed

+4
-113
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[profile.release]
22
opt-level = 2
3-
lto = true
3+
lto = "fat"
4+
codegen-units = 1
45

56
[workspace]
67
members = [

program/rust/src/processor.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::c_oracle_header::{
1010
command_t_e_cmd_del_publisher,
1111
command_t_e_cmd_init_mapping,
1212
command_t_e_cmd_init_price,
13-
command_t_e_cmd_resize_price_account,
1413
command_t_e_cmd_set_min_pub,
1514
command_t_e_cmd_upd_price,
1615
command_t_e_cmd_upd_price_no_fail_on_error,
@@ -34,7 +33,6 @@ use crate::rust_oracle::{
3433
del_publisher,
3534
init_mapping,
3635
init_price,
37-
resize_price_account,
3836
set_min_pub,
3937
upd_price,
4038
upd_price_no_fail_on_error,
@@ -64,9 +62,6 @@ pub fn process_instruction(
6462
command_t_e_cmd_upd_price_no_fail_on_error => {
6563
upd_price_no_fail_on_error(program_id, accounts, instruction_data)
6664
}
67-
command_t_e_cmd_resize_price_account => {
68-
resize_price_account(program_id, accounts, instruction_data)
69-
}
7065
command_t_e_cmd_add_price => add_price(program_id, accounts, instruction_data),
7166
command_t_e_cmd_init_mapping => init_mapping(program_id, accounts, instruction_data),
7267
command_t_e_cmd_init_price => init_price(program_id, accounts, instruction_data),

program/rust/src/rust_oracle.rs

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ use bytemuck::{
1010
use solana_program::account_info::AccountInfo;
1111
use solana_program::clock::Clock;
1212
use solana_program::entrypoint::ProgramResult;
13-
use solana_program::program::invoke;
1413
use solana_program::program_error::ProgramError;
1514
use solana_program::program_memory::{
1615
sol_memcpy,
1716
sol_memset,
1817
};
1918
use solana_program::pubkey::Pubkey;
20-
use solana_program::rent::Rent;
21-
use solana_program::system_instruction::transfer;
22-
use solana_program::system_program::check_id;
2319
use solana_program::sysvar::Sysvar;
2420

2521
use crate::c_oracle_header::{
@@ -44,16 +40,13 @@ use crate::c_oracle_header::{
4440
PC_PROD_ACC_SIZE,
4541
PC_PTYPE_UNKNOWN,
4642
PC_STATUS_UNKNOWN,
47-
PC_VERSION,
4843
};
4944
use crate::deserialize::{
5045
initialize_pyth_account_checked, /* TODO: This has a confusingly similar name to a Solana
5146
* sdk function */
5247
load,
53-
load_account_as_mut,
5448
load_checked,
5549
};
56-
use crate::time_machine_types::PriceAccountWrapper;
5750
use crate::utils::{
5851
check_exponent_range,
5952
check_valid_fresh_account,
@@ -69,11 +62,6 @@ use crate::utils::{
6962
read_pc_str_t,
7063
try_convert,
7164
};
72-
use crate::OracleError;
73-
74-
const PRICE_T_SIZE: usize = size_of::<pc_price_t>();
75-
const PRICE_ACCOUNT_SIZE: usize = size_of::<PriceAccountWrapper>();
76-
7765

7866
#[cfg(target_arch = "bpf")]
7967
#[link(name = "cpyth-bpf")]
@@ -87,79 +75,6 @@ extern "C" {
8775
pub fn c_upd_aggregate(_input: *mut u8, clock_slot: u64, clock_timestamp: i64) -> bool;
8876
}
8977

90-
fn send_lamports<'a>(
91-
from: &AccountInfo<'a>,
92-
to: &AccountInfo<'a>,
93-
system_program: &AccountInfo<'a>,
94-
amount: u64,
95-
) -> Result<(), ProgramError> {
96-
let transfer_instruction = transfer(from.key, to.key, amount);
97-
invoke(
98-
&transfer_instruction,
99-
&[from.clone(), to.clone(), system_program.clone()],
100-
)?;
101-
Ok(())
102-
}
103-
104-
/// resizes a price account so that it fits the Time Machine
105-
/// key[0] funding account [signer writable]
106-
/// key[1] price account [Signer writable]
107-
/// key[2] system program [readable]
108-
pub fn resize_price_account(
109-
program_id: &Pubkey,
110-
accounts: &[AccountInfo],
111-
_instruction_data: &[u8],
112-
) -> ProgramResult {
113-
let [funding_account_info, price_account_info, system_program] = match accounts {
114-
[x, y, z] => Ok([x, y, z]),
115-
_ => Err(ProgramError::InvalidArgument),
116-
}?;
117-
118-
check_valid_funding_account(funding_account_info)?;
119-
check_valid_signable_account(program_id, price_account_info, size_of::<pc_price_t>())?;
120-
pyth_assert(
121-
check_id(system_program.key),
122-
OracleError::InvalidSystemAccount.into(),
123-
)?;
124-
//throw an error if not a price account
125-
//need to makre sure it goes out of scope immediatly to avoid mutable borrow errors
126-
{
127-
load_checked::<pc_price_t>(price_account_info, PC_VERSION)?;
128-
}
129-
let account_len = price_account_info.try_data_len()?;
130-
match account_len {
131-
PRICE_T_SIZE => {
132-
//ensure account is still rent exempt after resizing
133-
let rent: Rent = Default::default();
134-
let lamports_needed: u64 = rent
135-
.minimum_balance(size_of::<PriceAccountWrapper>())
136-
.saturating_sub(price_account_info.lamports());
137-
if lamports_needed > 0 {
138-
send_lamports(
139-
funding_account_info,
140-
price_account_info,
141-
system_program,
142-
lamports_needed,
143-
)?;
144-
}
145-
//resize
146-
//we do not need to zero initialize since this is the first time this memory
147-
//is allocated
148-
price_account_info.realloc(size_of::<PriceAccountWrapper>(), false)?;
149-
//The load below would fail if the account was not a price account, reverting the whole
150-
// transaction
151-
let mut price_account =
152-
load_checked::<PriceAccountWrapper>(price_account_info, PC_VERSION)?;
153-
//Initialize Time Machine
154-
price_account.initialize_time_machine()?;
155-
Ok(())
156-
}
157-
PRICE_ACCOUNT_SIZE => Ok(()),
158-
_ => Err(ProgramError::InvalidArgument),
159-
}
160-
}
161-
162-
16378
/// initialize the first mapping account in a new linked-list of mapping accounts
16479
/// accounts[0] funding account [signer writable]
16580
/// accounts[1] new mapping account [signer writable]
@@ -272,23 +187,16 @@ pub fn upd_price(
272187
}
273188

274189
// Try to update the aggregate
275-
let mut aggregate_updated = false;
276190
if clock.slot > latest_aggregate_price.pub_slot_ {
277191
unsafe {
278-
aggregate_updated = c_upd_aggregate(
192+
let _aggregate_updated = c_upd_aggregate(
279193
price_account.try_borrow_mut_data()?.as_mut_ptr(),
280194
clock.slot,
281195
clock.unix_timestamp,
282196
);
283197
}
284198
}
285199

286-
let account_len = price_account.try_data_len()?;
287-
if aggregate_updated && account_len == PRICE_ACCOUNT_SIZE {
288-
let mut price_account = load_account_as_mut::<PriceAccountWrapper>(price_account)?;
289-
price_account.add_price_to_time_machine()?;
290-
}
291-
292200
// Try to update the publisher's price
293201
if is_component_update(cmd_args)? {
294202
let mut status: u32 = cmd_args.status_;
@@ -463,7 +371,7 @@ pub fn init_price(
463371
0,
464372
size_of::<pc_price_info_t>(),
465373
);
466-
for i in 0..(price_data.comp_.len() as usize) {
374+
for i in 0..price_data.comp_.len() {
467375
sol_memset(
468376
bytes_of_mut(&mut price_data.comp_[i].agg_),
469377
0,

program/rust/src/time_machine_types.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use crate::c_oracle_header::{
55
PC_ACCTYPE_PRICE,
66
PC_PRICE_T_COMP_OFFSET,
77
};
8-
use crate::error::OracleError;
98
use bytemuck::{
109
Pod,
1110
Zeroable,
1211
};
13-
use solana_program::msg;
1412

1513

1614
#[derive(Debug, Clone, Copy)]
@@ -33,17 +31,6 @@ pub struct PriceAccountWrapper {
3331
//TimeMachine
3432
pub time_machine: TimeMachineWrapper,
3533
}
36-
impl PriceAccountWrapper {
37-
pub fn initialize_time_machine(&mut self) -> Result<(), OracleError> {
38-
msg!("implement me");
39-
Ok(())
40-
}
41-
42-
pub fn add_price_to_time_machine(&mut self) -> Result<(), OracleError> {
43-
msg!("implement me");
44-
Ok(())
45-
}
46-
}
4734

4835
#[cfg(target_endian = "little")]
4936
unsafe impl Zeroable for PriceAccountWrapper {

0 commit comments

Comments
 (0)