Skip to content

Commit

Permalink
Non breaking in parallel + remove test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Woody4618 committed Nov 5, 2024
1 parent 9b802c5 commit ff32102
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 46 deletions.
7 changes: 7 additions & 0 deletions cargo-registry/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ impl Client {
App::new(name)
.about(about)
.version(version)
.arg(
Arg::with_name("skip_preflight")
.long("skip-preflight")
.global(true)
.takes_value(false)
.help("Skip the preflight check when sending transactions"),
)
.arg(
Arg::with_name("config_file")
.short("C")
Expand Down
2 changes: 1 addition & 1 deletion cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn get_clap_app<'ab, 'v>(name: &str, about: &'ab str, version: &'v str) -> A
.long("skip-preflight")
.global(true)
.takes_value(false)
.help("Skip the preflight check when sending transactions (default: false)"),
.help("Skip the preflight check when sending transactions"),
)
.arg({
let arg = Arg::with_name("config_file")
Expand Down
14 changes: 9 additions & 5 deletions cli/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use {
solana_client::{
connection_cache::ConnectionCache,
send_and_confirm_transactions_in_parallel::{
send_and_confirm_transactions_in_parallel_blocking, SendAndConfirmConfig,
send_and_confirm_transactions_in_parallel_blocking_v2,
SendAndConfirmConfigV2,
},
tpu_client::{TpuClient, TpuClientConfig},
},
Expand Down Expand Up @@ -1575,7 +1576,10 @@ fn process_program_upgrade(
let signers = &[fee_payer_signer, upgrade_authority_signer];
tx.try_sign(signers, blockhash)?;
let final_tx_sig = rpc_client
.send_and_confirm_transaction_with_spinner(&tx)
.send_and_confirm_transaction_with_spinner_and_config(&tx,
config.commitment,
config.send_transaction_config
)
.map_err(|e| format!("Upgrading program failed: {e}"))?;
let program_id = CliProgramId {
program_id: program_id.to_string(),
Expand Down Expand Up @@ -2995,15 +2999,15 @@ fn send_deploy_messages(
.expect("Should return a valid tpu client")
);

send_and_confirm_transactions_in_parallel_blocking(
send_and_confirm_transactions_in_parallel_blocking_v2(
rpc_client.clone(),
tpu_client,
&write_messages,
&[fee_payer_signer, write_signer],
SendAndConfirmConfig {
SendAndConfirmConfigV2 {
resign_txs_count: Some(max_sign_attempts),
with_spinner: true,
skip_preflight: config.send_transaction_config.skip_preflight,
rpc_send_transaction_config: config.send_transaction_config,
},
)
},
Expand Down
9 changes: 5 additions & 4 deletions cli/src/program_v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use {
solana_client::{
connection_cache::ConnectionCache,
send_and_confirm_transactions_in_parallel::{
send_and_confirm_transactions_in_parallel_blocking, SendAndConfirmConfig,
send_and_confirm_transactions_in_parallel_blocking_v2,
SendAndConfirmConfigV2,
},
tpu_client::{TpuClient, TpuClientConfig},
},
Expand Down Expand Up @@ -1083,15 +1084,15 @@ fn send_messages(
.block_on(tpu_client_fut)
.expect("Should return a valid tpu client");

send_and_confirm_transactions_in_parallel_blocking(
send_and_confirm_transactions_in_parallel_blocking_v2(
rpc_client.clone(),
Some(tpu_client),
write_messages,
&[config.payer, config.authority],
SendAndConfirmConfig {
SendAndConfirmConfigV2 {
resign_txs_count: Some(5),
with_spinner: true,
skip_preflight: config.rpc_send_transaction_config.skip_preflight,
rpc_send_transaction_config: config.rpc_send_transaction_config,
},
)
}
Expand Down
19 changes: 6 additions & 13 deletions cli/tests/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ fn test_cli_program_upgrade_with_feature(enable_feature: bool) {

let mut config = CliConfig::recent_for_tests();
config.json_rpc_url = test_validator.rpc_url();
config.send_transaction_config.skip_preflight = false;

let online_signer = Keypair::new();
let offline_signer = Keypair::new();
Expand Down Expand Up @@ -1369,9 +1370,8 @@ fn test_cli_program_close_program() {
assert_eq!(programdata_lamports, recipient_account.lamports);
}

#[test_case(true; "Skip Preflight")]
#[test_case(false; "Dont skip Preflight")]
fn test_cli_program_extend_program_skip_preflight(skip_preflight: bool) {
#[test]
fn test_cli_program_extend_program() {
solana_logger::setup();

let mut noop_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Expand Down Expand Up @@ -1421,7 +1421,7 @@ fn test_cli_program_extend_program_skip_preflight(skip_preflight: bool) {
lamports: 100 * minimum_balance_for_programdata + minimum_balance_for_program,
};
config.send_transaction_config = RpcSendTransactionConfig {
skip_preflight: skip_preflight,
skip_preflight: false,
preflight_commitment: Some(CommitmentConfig::processed().commitment),
..RpcSendTransactionConfig::default()
};
Expand Down Expand Up @@ -1498,14 +1498,8 @@ fn test_cli_program_extend_program_skip_preflight(skip_preflight: bool) {
use_rpc: false,
skip_feature_verification: true,
});
if skip_preflight {
expect_command_failure(
&config,
"Program upgrade must fail, as the buffer is 1 byte too short",
"Deploying program failed: Error processing Instruction 0: account data too small for instruction",
);
} else {
expect_command_failure(

expect_command_failure(
&config,
"Program upgrade must fail, as the buffer is 1 byte too short",
"Deploying program failed: \
Expand All @@ -1517,7 +1511,6 @@ fn test_cli_program_extend_program_skip_preflight(skip_preflight: bool) {
ProgramData account not large enough\n \
Program BPFLoaderUpgradeab1e11111111111111111111111 failed: account data too small for instruction\n",
);
}

// Wait one slot to avoid "Program was deployed in this block already" error
wait_n_slots(&rpc_client, 1);
Expand Down
7 changes: 3 additions & 4 deletions cli/tests/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,8 @@ fn test_transfer_all(compute_unit_price: Option<u64>) {
check_balance!(500_000 - fee, &rpc_client, &recipient_pubkey);
}

#[test_case(true; "Skip Preflight")]
#[test_case(false; "Don`t skip Preflight")]
fn test_transfer_unfunded_recipient(skip_preflight: bool) {
#[test]
fn test_transfer_unfunded_recipient() {
solana_logger::setup();
let mint_keypair = Keypair::new();
let mint_pubkey = mint_keypair.pubkey();
Expand All @@ -580,7 +579,7 @@ fn test_transfer_unfunded_recipient(skip_preflight: bool) {
let mut config = CliConfig::recent_for_tests();
config.json_rpc_url = test_validator.rpc_url();
config.signers = vec![&default_signer];
config.send_transaction_config.skip_preflight = skip_preflight;
config.send_transaction_config.skip_preflight = false;

let sender_pubkey = config.signers[0].pubkey();
let recipient_pubkey = Pubkey::from([1u8; 32]);
Expand Down
27 changes: 20 additions & 7 deletions client-test/tests/send_and_confirm_transactions_in_parallel.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use {
solana_client::{
nonblocking::tpu_client::TpuClient,
rpc_config::RpcSendTransactionConfig,
send_and_confirm_transactions_in_parallel::{
send_and_confirm_transactions_in_parallel_blocking, SendAndConfirmConfig,
send_and_confirm_transactions_in_parallel_blocking_v2, SendAndConfirmConfigV2,
},
},
solana_rpc_client::rpc_client::RpcClient,
Expand Down Expand Up @@ -51,15 +52,21 @@ fn test_send_and_confirm_transactions_in_parallel_without_tpu_client() {
let original_alice_balance = rpc_client.get_balance(&alice.pubkey()).unwrap();
let (messages, sum) = create_messages(alice_pubkey, bob_pubkey);

let txs_errors = send_and_confirm_transactions_in_parallel_blocking(
let txs_errors = send_and_confirm_transactions_in_parallel_blocking_v2(
rpc_client.clone(),
None,
&messages,
&[&alice],
SendAndConfirmConfig {
SendAndConfirmConfigV2 {
with_spinner: false,
resign_txs_count: Some(5),
skip_preflight: false,
rpc_send_transaction_config: RpcSendTransactionConfig {
skip_preflight: false,
preflight_commitment: Some(CommitmentConfig::confirmed().commitment),
encoding: None,
max_retries: None,
min_context_slot: None,
},
},
);
assert!(txs_errors.is_ok());
Expand Down Expand Up @@ -110,15 +117,21 @@ fn test_send_and_confirm_transactions_in_parallel_with_tpu_client() {
);
let tpu_client = rpc_client.runtime().block_on(tpu_client_fut).unwrap();

let txs_errors = send_and_confirm_transactions_in_parallel_blocking(
let txs_errors = send_and_confirm_transactions_in_parallel_blocking_v2(
rpc_client.clone(),
Some(tpu_client),
&messages,
&[&alice],
SendAndConfirmConfig {
SendAndConfirmConfigV2 {
with_spinner: false,
resign_txs_count: Some(5),
skip_preflight: false,
rpc_send_transaction_config: RpcSendTransactionConfig {
skip_preflight: false,
preflight_commitment: Some(CommitmentConfig::confirmed().commitment),
encoding: None,
max_retries: None,
min_context_slot: None,
},
},
);
assert!(txs_errors.is_ok());
Expand Down
73 changes: 62 additions & 11 deletions client/src/send_and_confirm_transactions_in_parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,74 @@ struct BlockHashData {
pub last_valid_block_height: u64,
}

// Deprecated struct to maintain backward compatibility
#[deprecated(note = "Use SendAndConfirmConfigV2 with send_and_confirm_transactions_in_parallel_v2")]
#[derive(Clone, Debug, Copy)]
pub struct SendAndConfirmConfig {
pub with_spinner: bool,
pub resign_txs_count: Option<usize>,
pub skip_preflight: bool,
}

/// Sends and confirms transactions concurrently in a sync context
// New struct with RpcSendTransactionConfig for non-breaking change
#[derive(Clone, Debug, Copy)]
pub struct SendAndConfirmConfigV2 {
pub with_spinner: bool,
pub resign_txs_count: Option<usize>,
pub rpc_send_transaction_config: RpcSendTransactionConfig,
}

#[deprecated(note = "Use send_and_confirm_transactions_in_parallel_v2")]
pub async fn send_and_confirm_transactions_in_parallel<T: Signers + ?Sized>(
rpc_client: Arc<RpcClient>,
tpu_client: Option<QuicTpuClient>,
messages: &[Message],
signers: &T,
config: SendAndConfirmConfig,
) -> Result<Vec<Option<TransactionError>>> {
let config_v2 = SendAndConfirmConfigV2 {
with_spinner: config.with_spinner,
resign_txs_count: config.resign_txs_count,
rpc_send_transaction_config: RpcSendTransactionConfig {
skip_preflight: false,
..RpcSendTransactionConfig::default()
},
};
send_and_confirm_transactions_in_parallel_v2(
rpc_client, tpu_client, messages, signers, config_v2,
)
.await
}

#[deprecated(note = "Use send_and_confirm_transactions_in_parallel_blocking_v2")]
pub fn send_and_confirm_transactions_in_parallel_blocking<T: Signers + ?Sized>(
rpc_client: Arc<BlockingRpcClient>,
tpu_client: Option<QuicTpuClient>,
messages: &[Message],
signers: &T,
config: SendAndConfirmConfig,
) -> Result<Vec<Option<TransactionError>>> {
let fut = send_and_confirm_transactions_in_parallel(
let config_v2 = SendAndConfirmConfigV2 {
with_spinner: config.with_spinner,
resign_txs_count: config.resign_txs_count,
rpc_send_transaction_config: RpcSendTransactionConfig {
skip_preflight: false,
..RpcSendTransactionConfig::default()
},
};
send_and_confirm_transactions_in_parallel_blocking_v2(
rpc_client, tpu_client, messages, signers, config_v2,
)
}

/// Sends and confirms transactions concurrently in a sync context
pub fn send_and_confirm_transactions_in_parallel_blocking_v2<T: Signers + ?Sized>(
rpc_client: Arc<BlockingRpcClient>,
tpu_client: Option<QuicTpuClient>,
messages: &[Message],
signers: &T,
config: SendAndConfirmConfigV2,
) -> Result<Vec<Option<TransactionError>>> {
let fut = send_and_confirm_transactions_in_parallel_v2(
rpc_client.get_inner_client().clone(),
tpu_client,
messages,
Expand Down Expand Up @@ -197,7 +249,7 @@ async fn send_transaction_with_rpc_fallback(
serialized_transaction: Vec<u8>,
context: &SendingContext,
index: usize,
skip_preflight: bool,
rpc_send_transaction_config: RpcSendTransactionConfig,
) -> Result<()> {
let send_over_rpc = if let Some(tpu_client) = tpu_client {
!tokio::time::timeout(
Expand All @@ -214,9 +266,8 @@ async fn send_transaction_with_rpc_fallback(
.send_transaction_with_config(
&transaction,
RpcSendTransactionConfig {
skip_preflight,
preflight_commitment: Some(rpc_client.commitment().commitment),
..RpcSendTransactionConfig::default()
..rpc_send_transaction_config
},
)
.await
Expand Down Expand Up @@ -268,7 +319,7 @@ async fn sign_all_messages_and_send<T: Signers + ?Sized>(
messages_with_index: Vec<(usize, Message)>,
signers: &T,
context: &SendingContext,
skip_preflight: bool,
rpc_send_transaction_config: RpcSendTransactionConfig,
) -> Result<()> {
let current_transaction_count = messages_with_index.len();
let mut futures = vec![];
Expand Down Expand Up @@ -318,7 +369,7 @@ async fn sign_all_messages_and_send<T: Signers + ?Sized>(
serialized_transaction,
context,
*index,
skip_preflight,
rpc_send_transaction_config,
)
.await
});
Expand Down Expand Up @@ -433,12 +484,12 @@ async fn send_staggered_transactions(
/// The sending and confirmation of transactions is done in parallel tasks
/// The method signs transactions just before sending so that blockhash does not
/// expire.
pub async fn send_and_confirm_transactions_in_parallel<T: Signers + ?Sized>(
pub async fn send_and_confirm_transactions_in_parallel_v2<T: Signers + ?Sized>(
rpc_client: Arc<RpcClient>,
tpu_client: Option<QuicTpuClient>,
messages: &[Message],
signers: &T,
config: SendAndConfirmConfig,
config: SendAndConfirmConfigV2,
) -> Result<Vec<Option<TransactionError>>> {
// get current blockhash and corresponding last valid block height
let (blockhash, last_valid_block_height) = rpc_client
Expand Down Expand Up @@ -527,7 +578,7 @@ pub async fn send_and_confirm_transactions_in_parallel<T: Signers + ?Sized>(
messages_with_index,
signers,
&context,
config.skip_preflight,
config.rpc_send_transaction_config,
)
.await?;
confirm_transactions_till_block_height_and_resend_unexpired_transaction_over_tpu(
Expand Down
1 change: 0 additions & 1 deletion rpc-client/src/nonblocking/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,7 +868,6 @@ impl RpcClient {
};
let config = RpcSendTransactionConfig {
encoding: Some(encoding),
skip_preflight: config.skip_preflight,
preflight_commitment: Some(preflight_commitment.commitment),
..config
};
Expand Down

0 comments on commit ff32102

Please sign in to comment.