From 40f3af6cba49fdfca6bd02491262b4953d41d4c2 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 2 Jan 2023 13:52:10 -0700 Subject: [PATCH 1/4] Remove unused messages Vecs --- cli/src/program.rs | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/cli/src/program.rs b/cli/src/program.rs index 63e8d56e234d0b..c9d0fa2cbb4eab 100644 --- a/cli/src/program.rs +++ b/cli/src/program.rs @@ -1770,8 +1770,6 @@ fn do_process_program_write_and_deploy( allow_excessive_balance: bool, skip_fee_check: bool, ) -> ProcessResult { - // Build messages to calculate fees - let mut messages: Vec<&Message> = Vec::new(); let blockhash = rpc_client.get_latest_blockhash()?; // Initialize buffer account or complete if already partially initialized @@ -1855,17 +1853,6 @@ fn do_process_program_write_and_deploy( (None, None, 0) }; - if let Some(ref initial_message) = initial_message { - messages.push(initial_message); - } - if let Some(ref write_messages) = write_messages { - let mut write_message_refs = vec![]; - for message in write_messages.iter() { - write_message_refs.push(message); - } - messages.append(&mut write_message_refs); - } - // Create and add final message let final_message = if let Some(program_signers) = program_signers { @@ -1895,9 +1882,6 @@ fn do_process_program_write_and_deploy( } else { None }; - if let Some(ref message) = final_message { - messages.push(message); - } if !skip_fee_check { check_payer( @@ -1951,7 +1935,6 @@ fn do_process_program_upgrade( )?; // Build messages to calculate fees - let mut messages: Vec<&Message> = Vec::new(); let blockhash = rpc_client.get_latest_blockhash()?; let (initial_message, write_messages, balance_needed) = @@ -2018,17 +2001,6 @@ fn do_process_program_upgrade( (None, None, 0) }; - if let Some(ref message) = initial_message { - messages.push(message); - } - if let Some(ref write_messages) = write_messages { - let mut write_message_refs = vec![]; - for message in write_messages.iter() { - write_message_refs.push(message); - } - messages.append(&mut write_message_refs); - } - // Create and add final message let final_message = Message::new_with_blockhash( &[bpf_loader_upgradeable::upgrade( @@ -2040,7 +2012,6 @@ fn do_process_program_upgrade( Some(&config.signers[0].pubkey()), &blockhash, ); - messages.push(&final_message); let final_message = Some(final_message); if !skip_fee_check { From 953d37f604b573a86bfe289eb45c9573460d8c54 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 2 Jan 2023 14:06:59 -0700 Subject: [PATCH 2/4] Remove superfluous Option in check_payer and send_deploy_messages --- cli/src/program.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/src/program.rs b/cli/src/program.rs index c9d0fa2cbb4eab..1419dab56c320e 100644 --- a/cli/src/program.rs +++ b/cli/src/program.rs @@ -1848,9 +1848,9 @@ fn do_process_program_write_and_deploy( write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); } - (initial_message, Some(write_messages), balance_needed) + (initial_message, write_messages, balance_needed) } else { - (None, None, 0) + (None, vec![], 0) }; // Create and add final message @@ -1996,9 +1996,9 @@ fn do_process_program_upgrade( write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); } - (initial_message, Some(write_messages), balance_needed) + (initial_message, write_messages, balance_needed) } else { - (None, None, 0) + (None, vec![], 0) }; // Create and add final message @@ -2126,14 +2126,14 @@ fn check_payer( config: &CliConfig, balance_needed: u64, initial_message: &Option, - write_messages: &Option>, + write_messages: &[Message], final_message: &Option, ) -> Result<(), Box> { let mut fee = 0; if let Some(message) = initial_message { fee += rpc_client.get_fee_for_message(message)?; } - if let Some(write_messages) = write_messages { + if !write_messages.is_empty() { // Assume all write messages cost the same if let Some(message) = write_messages.get(0) { fee += rpc_client.get_fee_for_message(message)? * (write_messages.len() as u64); @@ -2156,7 +2156,7 @@ fn send_deploy_messages( rpc_client: Arc, config: &CliConfig, initial_message: &Option, - write_messages: &Option>, + write_messages: &[Message], final_message: &Option, initial_signer: Option<&dyn Signer>, write_signer: Option<&dyn Signer>, @@ -2187,7 +2187,7 @@ fn send_deploy_messages( } } - if let Some(write_messages) = write_messages { + if !write_messages.is_empty() { if let Some(write_signer) = write_signer { trace!("Writing program data"); let connection_cache = if config.use_quic { From 5fe0fde5e0a442c7e642e6fa1b4fd4b92376c341 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 2 Jan 2023 14:10:23 -0700 Subject: [PATCH 3/4] Remove superfluous Option in do_process_program_write_and_deploy --- cli/src/program.rs | 145 ++++++++++++++++++++++----------------------- 1 file changed, 71 insertions(+), 74 deletions(-) diff --git a/cli/src/program.rs b/cli/src/program.rs index 1419dab56c320e..1c5842d65d88f7 100644 --- a/cli/src/program.rs +++ b/cli/src/program.rs @@ -1029,7 +1029,7 @@ fn process_program_deploy( Some(&[program_signer.unwrap(), upgrade_authority_signer]), buffer_signer, &buffer_pubkey, - Some(upgrade_authority_signer), + upgrade_authority_signer, allow_excessive_balance, skip_fee_check, ) @@ -1133,7 +1133,7 @@ fn process_write_buffer( None, buffer_signer, &buffer_pubkey, - Some(buffer_authority), + buffer_authority, true, skip_fee_check, ); @@ -1766,92 +1766,89 @@ fn do_process_program_write_and_deploy( program_signers: Option<&[&dyn Signer]>, buffer_signer: Option<&dyn Signer>, buffer_pubkey: &Pubkey, - buffer_authority_signer: Option<&dyn Signer>, + buffer_authority_signer: &dyn Signer, allow_excessive_balance: bool, skip_fee_check: bool, ) -> ProcessResult { let blockhash = rpc_client.get_latest_blockhash()?; // Initialize buffer account or complete if already partially initialized - let (initial_message, write_messages, balance_needed) = - if let Some(buffer_authority_signer) = buffer_authority_signer { - let (initial_instructions, balance_needed) = if let Some(account) = rpc_client - .get_account_with_commitment(buffer_pubkey, config.commitment)? - .value - { - complete_partial_program_init( - loader_id, + let (initial_message, write_messages, balance_needed) = { + let (initial_instructions, balance_needed) = if let Some(account) = rpc_client + .get_account_with_commitment(buffer_pubkey, config.commitment)? + .value + { + complete_partial_program_init( + loader_id, + &config.signers[0].pubkey(), + buffer_pubkey, + &account, + if loader_id == &bpf_loader_upgradeable::id() { + UpgradeableLoaderState::size_of_buffer(program_len) + } else { + program_len + }, + minimum_balance, + allow_excessive_balance, + )? + } else if loader_id == &bpf_loader_upgradeable::id() { + ( + bpf_loader_upgradeable::create_buffer( &config.signers[0].pubkey(), buffer_pubkey, - &account, - if loader_id == &bpf_loader_upgradeable::id() { - UpgradeableLoaderState::size_of_buffer(program_len) - } else { - program_len - }, - minimum_balance, - allow_excessive_balance, - )? - } else if loader_id == &bpf_loader_upgradeable::id() { - ( - bpf_loader_upgradeable::create_buffer( - &config.signers[0].pubkey(), - buffer_pubkey, - &buffer_authority_signer.pubkey(), - minimum_balance, - program_len, - )?, + &buffer_authority_signer.pubkey(), minimum_balance, - ) - } else { - ( - vec![system_instruction::create_account( - &config.signers[0].pubkey(), - buffer_pubkey, - minimum_balance, - program_len as u64, - loader_id, - )], + program_len, + )?, + minimum_balance, + ) + } else { + ( + vec![system_instruction::create_account( + &config.signers[0].pubkey(), + buffer_pubkey, minimum_balance, - ) - }; - let initial_message = if !initial_instructions.is_empty() { - Some(Message::new_with_blockhash( - &initial_instructions, - Some(&config.signers[0].pubkey()), - &blockhash, - )) - } else { - None - }; + program_len as u64, + loader_id, + )], + minimum_balance, + ) + }; + let initial_message = if !initial_instructions.is_empty() { + Some(Message::new_with_blockhash( + &initial_instructions, + Some(&config.signers[0].pubkey()), + &blockhash, + )) + } else { + None + }; - // Create and add write messages + // Create and add write messages - let payer_pubkey = config.signers[0].pubkey(); - let create_msg = |offset: u32, bytes: Vec| { - let instruction = if loader_id == &bpf_loader_upgradeable::id() { - bpf_loader_upgradeable::write( - buffer_pubkey, - &buffer_authority_signer.pubkey(), - offset, - bytes, - ) - } else { - loader_instruction::write(buffer_pubkey, loader_id, offset, bytes) - }; - Message::new_with_blockhash(&[instruction], Some(&payer_pubkey), &blockhash) + let payer_pubkey = config.signers[0].pubkey(); + let create_msg = |offset: u32, bytes: Vec| { + let instruction = if loader_id == &bpf_loader_upgradeable::id() { + bpf_loader_upgradeable::write( + buffer_pubkey, + &buffer_authority_signer.pubkey(), + offset, + bytes, + ) + } else { + loader_instruction::write(buffer_pubkey, loader_id, offset, bytes) }; + Message::new_with_blockhash(&[instruction], Some(&payer_pubkey), &blockhash) + }; - let mut write_messages = vec![]; - let chunk_size = calculate_max_chunk_size(&create_msg); - for (chunk, i) in program_data.chunks(chunk_size).zip(0..) { - write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); - } + let mut write_messages = vec![]; + let chunk_size = calculate_max_chunk_size(&create_msg); + for (chunk, i) in program_data.chunks(chunk_size).zip(0..) { + write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); + } - (initial_message, write_messages, balance_needed) - } else { - (None, vec![], 0) - }; + (initial_message, write_messages, balance_needed) + }; // Create and add final message @@ -1901,7 +1898,7 @@ fn do_process_program_write_and_deploy( &write_messages, &final_message, buffer_signer, - buffer_authority_signer, + Some(buffer_authority_signer), program_signers, )?; From 6498b2fbefbb1eca6eb43f69b09725e556981d89 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Mon, 2 Jan 2023 14:28:44 -0700 Subject: [PATCH 4/4] Remove unnecessary block-wrapping in do_process_program_write_and_deploy --- cli/src/program.rs | 132 ++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/cli/src/program.rs b/cli/src/program.rs index 1c5842d65d88f7..7291229e669927 100644 --- a/cli/src/program.rs +++ b/cli/src/program.rs @@ -1773,85 +1773,79 @@ fn do_process_program_write_and_deploy( let blockhash = rpc_client.get_latest_blockhash()?; // Initialize buffer account or complete if already partially initialized - let (initial_message, write_messages, balance_needed) = { - let (initial_instructions, balance_needed) = if let Some(account) = rpc_client - .get_account_with_commitment(buffer_pubkey, config.commitment)? - .value - { - complete_partial_program_init( - loader_id, + let (initial_instructions, balance_needed) = if let Some(account) = rpc_client + .get_account_with_commitment(buffer_pubkey, config.commitment)? + .value + { + complete_partial_program_init( + loader_id, + &config.signers[0].pubkey(), + buffer_pubkey, + &account, + if loader_id == &bpf_loader_upgradeable::id() { + UpgradeableLoaderState::size_of_buffer(program_len) + } else { + program_len + }, + minimum_balance, + allow_excessive_balance, + )? + } else if loader_id == &bpf_loader_upgradeable::id() { + ( + bpf_loader_upgradeable::create_buffer( &config.signers[0].pubkey(), buffer_pubkey, - &account, - if loader_id == &bpf_loader_upgradeable::id() { - UpgradeableLoaderState::size_of_buffer(program_len) - } else { - program_len - }, + &buffer_authority_signer.pubkey(), minimum_balance, - allow_excessive_balance, - )? - } else if loader_id == &bpf_loader_upgradeable::id() { - ( - bpf_loader_upgradeable::create_buffer( - &config.signers[0].pubkey(), - buffer_pubkey, - &buffer_authority_signer.pubkey(), - minimum_balance, - program_len, - )?, - minimum_balance, - ) - } else { - ( - vec![system_instruction::create_account( - &config.signers[0].pubkey(), - buffer_pubkey, - minimum_balance, - program_len as u64, - loader_id, - )], + program_len, + )?, + minimum_balance, + ) + } else { + ( + vec![system_instruction::create_account( + &config.signers[0].pubkey(), + buffer_pubkey, minimum_balance, + program_len as u64, + loader_id, + )], + minimum_balance, + ) + }; + let initial_message = if !initial_instructions.is_empty() { + Some(Message::new_with_blockhash( + &initial_instructions, + Some(&config.signers[0].pubkey()), + &blockhash, + )) + } else { + None + }; + + // Create and add write messages + let payer_pubkey = config.signers[0].pubkey(); + let create_msg = |offset: u32, bytes: Vec| { + let instruction = if loader_id == &bpf_loader_upgradeable::id() { + bpf_loader_upgradeable::write( + buffer_pubkey, + &buffer_authority_signer.pubkey(), + offset, + bytes, ) - }; - let initial_message = if !initial_instructions.is_empty() { - Some(Message::new_with_blockhash( - &initial_instructions, - Some(&config.signers[0].pubkey()), - &blockhash, - )) } else { - None + loader_instruction::write(buffer_pubkey, loader_id, offset, bytes) }; - - // Create and add write messages - - let payer_pubkey = config.signers[0].pubkey(); - let create_msg = |offset: u32, bytes: Vec| { - let instruction = if loader_id == &bpf_loader_upgradeable::id() { - bpf_loader_upgradeable::write( - buffer_pubkey, - &buffer_authority_signer.pubkey(), - offset, - bytes, - ) - } else { - loader_instruction::write(buffer_pubkey, loader_id, offset, bytes) - }; - Message::new_with_blockhash(&[instruction], Some(&payer_pubkey), &blockhash) - }; - - let mut write_messages = vec![]; - let chunk_size = calculate_max_chunk_size(&create_msg); - for (chunk, i) in program_data.chunks(chunk_size).zip(0..) { - write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); - } - - (initial_message, write_messages, balance_needed) + Message::new_with_blockhash(&[instruction], Some(&payer_pubkey), &blockhash) }; - // Create and add final message + let mut write_messages = vec![]; + let chunk_size = calculate_max_chunk_size(&create_msg); + for (chunk, i) in program_data.chunks(chunk_size).zip(0..) { + write_messages.push(create_msg((i * chunk_size) as u32, chunk.to_vec())); + } + // Create and add final message let final_message = if let Some(program_signers) = program_signers { let message = if loader_id == &bpf_loader_upgradeable::id() { Message::new_with_blockhash(