Improve program-test process_transaction() speed by reducing sleep duration in banks-server (#20508)

* banks_server: Reduce sleep duration for local server

This speeds up process_transaction_with_commitment_and_context()
and thus most program tests by a lot.

* Plumb tick duration through poh config and signature polling

Co-authored-by: Jon Cinque <jon.cinque@gmail.com>
This commit is contained in:
Christian Kamm 2021-10-15 18:55:41 +02:00 committed by GitHub
parent 1e064c32e2
commit bea181eba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 13 deletions

View File

@ -369,7 +369,9 @@ mod tests {
let message = Message::new(&[instruction], Some(&mint_pubkey)); let message = Message::new(&[instruction], Some(&mint_pubkey));
Runtime::new()?.block_on(async { Runtime::new()?.block_on(async {
let client_transport = start_local_server(bank_forks, block_commitment_cache).await; let client_transport =
start_local_server(bank_forks, block_commitment_cache, Duration::from_millis(1))
.await;
let mut banks_client = start_client(client_transport).await?; let mut banks_client = start_client(client_transport).await?;
let recent_blockhash = banks_client.get_recent_blockhash().await?; let recent_blockhash = banks_client.get_recent_blockhash().await?;
@ -400,7 +402,9 @@ mod tests {
let message = Message::new(&[instruction], Some(mint_pubkey)); let message = Message::new(&[instruction], Some(mint_pubkey));
Runtime::new()?.block_on(async { Runtime::new()?.block_on(async {
let client_transport = start_local_server(bank_forks, block_commitment_cache).await; let client_transport =
start_local_server(bank_forks, block_commitment_cache, Duration::from_millis(1))
.await;
let mut banks_client = start_client(client_transport).await?; let mut banks_client = start_client(client_transport).await?;
let (_, recent_blockhash, last_valid_block_height) = banks_client.get_fees().await?; let (_, recent_blockhash, last_valid_block_height) = banks_client.get_fees().await?;
let transaction = Transaction::new(&[&genesis.mint_keypair], message, recent_blockhash); let transaction = Transaction::new(&[&genesis.mint_keypair], message, recent_blockhash);

View File

@ -47,6 +47,7 @@ struct BanksServer {
bank_forks: Arc<RwLock<BankForks>>, bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>, block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
transaction_sender: Sender<TransactionInfo>, transaction_sender: Sender<TransactionInfo>,
poll_signature_status_sleep_duration: Duration,
} }
impl BanksServer { impl BanksServer {
@ -58,11 +59,13 @@ impl BanksServer {
bank_forks: Arc<RwLock<BankForks>>, bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>, block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
transaction_sender: Sender<TransactionInfo>, transaction_sender: Sender<TransactionInfo>,
poll_signature_status_sleep_duration: Duration,
) -> Self { ) -> Self {
Self { Self {
bank_forks, bank_forks,
block_commitment_cache, block_commitment_cache,
transaction_sender, transaction_sender,
poll_signature_status_sleep_duration,
} }
} }
@ -85,6 +88,7 @@ impl BanksServer {
fn new_loopback( fn new_loopback(
bank_forks: Arc<RwLock<BankForks>>, bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>, block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
poll_signature_status_sleep_duration: Duration,
) -> Self { ) -> Self {
let (transaction_sender, transaction_receiver) = channel(); let (transaction_sender, transaction_receiver) = channel();
let bank = bank_forks.read().unwrap().working_bank(); let bank = bank_forks.read().unwrap().working_bank();
@ -99,7 +103,12 @@ impl BanksServer {
.name("solana-bank-forks-client".to_string()) .name("solana-bank-forks-client".to_string())
.spawn(move || Self::run(server_bank_forks, transaction_receiver)) .spawn(move || Self::run(server_bank_forks, transaction_receiver))
.unwrap(); .unwrap();
Self::new(bank_forks, block_commitment_cache, transaction_sender) Self::new(
bank_forks,
block_commitment_cache,
transaction_sender,
poll_signature_status_sleep_duration,
)
} }
fn slot(&self, commitment: CommitmentLevel) -> Slot { fn slot(&self, commitment: CommitmentLevel) -> Slot {
@ -124,7 +133,7 @@ impl BanksServer {
.bank(commitment) .bank(commitment)
.get_signature_status_with_blockhash(signature, blockhash); .get_signature_status_with_blockhash(signature, blockhash);
while status.is_none() { while status.is_none() {
sleep(Duration::from_millis(200)).await; sleep(self.poll_signature_status_sleep_duration).await;
let bank = self.bank(commitment); let bank = self.bank(commitment);
if bank.block_height() > last_valid_block_height { if bank.block_height() > last_valid_block_height {
break; break;
@ -270,8 +279,13 @@ impl Banks for BanksServer {
pub async fn start_local_server( pub async fn start_local_server(
bank_forks: Arc<RwLock<BankForks>>, bank_forks: Arc<RwLock<BankForks>>,
block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>, block_commitment_cache: Arc<RwLock<BlockCommitmentCache>>,
poll_signature_status_sleep_duration: Duration,
) -> UnboundedChannel<Response<BanksResponse>, ClientMessage<BanksRequest>> { ) -> UnboundedChannel<Response<BanksResponse>, ClientMessage<BanksRequest>> {
let banks_server = BanksServer::new_loopback(bank_forks, block_commitment_cache); let banks_server = BanksServer::new_loopback(
bank_forks,
block_commitment_cache,
poll_signature_status_sleep_duration,
);
let (client_transport, server_transport) = transport::channel::unbounded(); let (client_transport, server_transport) = transport::channel::unbounded();
let server = server::BaseChannel::with_defaults(server_transport).execute(banks_server.serve()); let server = server::BaseChannel::with_defaults(server_transport).execute(banks_server.serve());
tokio::spawn(server); tokio::spawn(server);
@ -311,8 +325,12 @@ pub async fn start_tcp_server(
0, 0,
); );
let server = let server = BanksServer::new(
BanksServer::new(bank_forks.clone(), block_commitment_cache.clone(), sender); bank_forks.clone(),
block_commitment_cache.clone(),
sender,
Duration::from_millis(200),
);
chan.execute(server.serve()) chan.execute(server.serve())
}) })
// Max 10 channels. // Max 10 channels.

View File

@ -32,6 +32,7 @@ use {
instruction::InstructionError, instruction::InstructionError,
message::Message, message::Message,
native_token::sol_to_lamports, native_token::sol_to_lamports,
poh_config::PohConfig,
process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext}, process_instruction::{stable_log, InvokeContext, ProcessInstructionWithContext},
program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR}, program_error::{ProgramError, ACCOUNT_BORROW_FAILED, UNSUPPORTED_SYSVAR},
pubkey::Pubkey, pubkey::Pubkey,
@ -756,7 +757,7 @@ impl ProgramTest {
let mint_keypair = Keypair::new(); let mint_keypair = Keypair::new();
let voting_keypair = Keypair::new(); let voting_keypair = Keypair::new();
let genesis_config = create_genesis_config_with_leader_ex( let mut genesis_config = create_genesis_config_with_leader_ex(
sol_to_lamports(1_000_000.0), sol_to_lamports(1_000_000.0),
&mint_keypair.pubkey(), &mint_keypair.pubkey(),
&bootstrap_validator_pubkey, &bootstrap_validator_pubkey,
@ -769,6 +770,8 @@ impl ProgramTest {
ClusterType::Development, ClusterType::Development,
vec![], vec![],
); );
let target_tick_duration = Duration::from_micros(100);
genesis_config.poh_config = PohConfig::new_sleep(target_tick_duration);
debug!("Payer address: {}", mint_keypair.pubkey()); debug!("Payer address: {}", mint_keypair.pubkey());
debug!("Genesis config: {}", genesis_config); debug!("Genesis config: {}", genesis_config);
@ -838,8 +841,13 @@ impl ProgramTest {
pub async fn start(self) -> (BanksClient, Keypair, Hash) { pub async fn start(self) -> (BanksClient, Keypair, Hash) {
let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank(); let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank();
let transport = let target_tick_duration = gci.genesis_config.poh_config.target_tick_duration;
start_local_server(bank_forks.clone(), block_commitment_cache.clone()).await; let transport = start_local_server(
bank_forks.clone(),
block_commitment_cache.clone(),
target_tick_duration,
)
.await;
let banks_client = start_client(transport) let banks_client = start_client(transport)
.await .await
.unwrap_or_else(|err| panic!("Failed to start banks client: {}", err)); .unwrap_or_else(|err| panic!("Failed to start banks client: {}", err));
@ -847,7 +855,6 @@ impl ProgramTest {
// Run a simulated PohService to provide the client with new blockhashes. New blockhashes // Run a simulated PohService to provide the client with new blockhashes. New blockhashes
// are required when sending multiple otherwise identical transactions in series from a // are required when sending multiple otherwise identical transactions in series from a
// test // test
let target_tick_duration = gci.genesis_config.poh_config.target_tick_duration;
tokio::spawn(async move { tokio::spawn(async move {
loop { loop {
bank_forks bank_forks
@ -868,8 +875,13 @@ impl ProgramTest {
/// with SOL for sending transactions /// with SOL for sending transactions
pub async fn start_with_context(self) -> ProgramTestContext { pub async fn start_with_context(self) -> ProgramTestContext {
let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank(); let (bank_forks, block_commitment_cache, last_blockhash, gci) = self.setup_bank();
let transport = let target_tick_duration = gci.genesis_config.poh_config.target_tick_duration;
start_local_server(bank_forks.clone(), block_commitment_cache.clone()).await; let transport = start_local_server(
bank_forks.clone(),
block_commitment_cache.clone(),
target_tick_duration,
)
.await;
let banks_client = start_client(transport) let banks_client = start_client(transport)
.await .await
.unwrap_or_else(|err| panic!("Failed to start banks client: {}", err)); .unwrap_or_else(|err| panic!("Failed to start banks client: {}", err));