Skip to content

Commit 2c12500

Browse files
authored
cli: skip no-op program buffer writes (pyth-network#277)
cli: skip no-op program deploy write txs
1 parent 7c49b9c commit 2c12500

File tree

1 file changed

+49
-30
lines changed

1 file changed

+49
-30
lines changed

cli/src/program.rs

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,11 +2213,12 @@ fn do_process_program_write_and_deploy(
22132213
let blockhash = rpc_client.get_latest_blockhash()?;
22142214

22152215
// Initialize buffer account or complete if already partially initialized
2216-
let (initial_instructions, balance_needed) = if let Some(account) = rpc_client
2217-
.get_account_with_commitment(buffer_pubkey, config.commitment)?
2218-
.value
2216+
let (initial_instructions, balance_needed, buffer_program_data) = if let Some(mut account) =
2217+
rpc_client
2218+
.get_account_with_commitment(buffer_pubkey, config.commitment)?
2219+
.value
22192220
{
2220-
complete_partial_program_init(
2221+
let (ixs, balance_needed) = complete_partial_program_init(
22212222
loader_id,
22222223
&fee_payer_signer.pubkey(),
22232224
buffer_pubkey,
@@ -2229,7 +2230,11 @@ fn do_process_program_write_and_deploy(
22292230
},
22302231
min_rent_exempt_program_data_balance,
22312232
allow_excessive_balance,
2232-
)?
2233+
)?;
2234+
let buffer_program_data = account
2235+
.data
2236+
.split_off(UpgradeableLoaderState::size_of_buffer_metadata());
2237+
(ixs, balance_needed, buffer_program_data)
22332238
} else if loader_id == &bpf_loader_upgradeable::id() {
22342239
(
22352240
bpf_loader_upgradeable::create_buffer(
@@ -2240,6 +2245,7 @@ fn do_process_program_write_and_deploy(
22402245
program_len,
22412246
)?,
22422247
min_rent_exempt_program_data_balance,
2248+
vec![0; program_len],
22432249
)
22442250
} else {
22452251
(
@@ -2251,6 +2257,7 @@ fn do_process_program_write_and_deploy(
22512257
loader_id,
22522258
)],
22532259
min_rent_exempt_program_data_balance,
2260+
vec![0; program_len],
22542261
)
22552262
};
22562263
let initial_message = if !initial_instructions.is_empty() {
@@ -2281,7 +2288,10 @@ fn do_process_program_write_and_deploy(
22812288
let mut write_messages = vec![];
22822289
let chunk_size = calculate_max_chunk_size(&create_msg);
22832290
for (chunk, i) in program_data.chunks(chunk_size).zip(0..) {
2284-
write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec()));
2291+
let offset = i * chunk_size;
2292+
if chunk != &buffer_program_data[offset..offset + chunk.len()] {
2293+
write_messages.push(create_msg(offset as u32, chunk.to_vec()));
2294+
}
22852295
}
22862296

22872297
// Create and add final message
@@ -2370,31 +2380,37 @@ fn do_process_program_upgrade(
23702380
let (initial_message, write_messages, balance_needed) =
23712381
if let Some(buffer_signer) = buffer_signer {
23722382
// Check Buffer account to see if partial initialization has occurred
2373-
let (initial_instructions, balance_needed) = if let Some(account) = rpc_client
2374-
.get_account_with_commitment(&buffer_signer.pubkey(), config.commitment)?
2375-
.value
2376-
{
2377-
complete_partial_program_init(
2378-
&bpf_loader_upgradeable::id(),
2379-
&fee_payer_signer.pubkey(),
2380-
&buffer_signer.pubkey(),
2381-
&account,
2382-
UpgradeableLoaderState::size_of_buffer(program_len),
2383-
min_rent_exempt_program_data_balance,
2384-
true,
2385-
)?
2386-
} else {
2387-
(
2388-
bpf_loader_upgradeable::create_buffer(
2383+
let (initial_instructions, balance_needed, buffer_program_data) =
2384+
if let Some(mut account) = rpc_client
2385+
.get_account_with_commitment(&buffer_signer.pubkey(), config.commitment)?
2386+
.value
2387+
{
2388+
let (ixs, balance_needed) = complete_partial_program_init(
2389+
&bpf_loader_upgradeable::id(),
23892390
&fee_payer_signer.pubkey(),
2390-
buffer_pubkey,
2391-
&upgrade_authority.pubkey(),
2391+
&buffer_signer.pubkey(),
2392+
&account,
2393+
UpgradeableLoaderState::size_of_buffer(program_len),
23922394
min_rent_exempt_program_data_balance,
2393-
program_len,
2394-
)?,
2395-
min_rent_exempt_program_data_balance,
2396-
)
2397-
};
2395+
true,
2396+
)?;
2397+
let buffer_program_data = account
2398+
.data
2399+
.split_off(UpgradeableLoaderState::size_of_buffer_metadata());
2400+
(ixs, balance_needed, buffer_program_data)
2401+
} else {
2402+
(
2403+
bpf_loader_upgradeable::create_buffer(
2404+
&fee_payer_signer.pubkey(),
2405+
buffer_pubkey,
2406+
&upgrade_authority.pubkey(),
2407+
min_rent_exempt_program_data_balance,
2408+
program_len,
2409+
)?,
2410+
min_rent_exempt_program_data_balance,
2411+
vec![0; program_len],
2412+
)
2413+
};
23982414

23992415
let initial_message = if !initial_instructions.is_empty() {
24002416
Some(Message::new_with_blockhash(
@@ -2426,7 +2442,10 @@ fn do_process_program_upgrade(
24262442
let mut write_messages = vec![];
24272443
let chunk_size = calculate_max_chunk_size(&create_msg);
24282444
for (chunk, i) in program_data.chunks(chunk_size).zip(0..) {
2429-
write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec()));
2445+
let offset = i * chunk_size;
2446+
if chunk != &buffer_program_data[offset..offset + chunk.len()] {
2447+
write_messages.push(create_msg(offset as u32, chunk.to_vec()));
2448+
}
24302449
}
24312450

24322451
(initial_message, write_messages, balance_needed)

0 commit comments

Comments
 (0)