Rename Client methods to match proposed BanksClient (#10793)

This commit is contained in:
Greg Fitzgerald
2020-06-24 21:35:38 -06:00
committed by GitHub
parent ea708b0d84
commit 7ade330b23
23 changed files with 210 additions and 158 deletions

View File

@ -48,7 +48,7 @@ pub fn create_builtin_transactions(
// Seed the signer account
let rando0 = Keypair::new();
bank_client
.transfer(10_000, &mint_keypair, &rando0.pubkey())
.transfer_and_confirm(10_000, &mint_keypair, &rando0.pubkey())
.unwrap_or_else(|_| panic!("{}:{}", line!(), file!()));
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);
@ -70,7 +70,7 @@ pub fn create_native_loader_transactions(
// Seed the signer account©41
let rando0 = Keypair::new();
bank_client
.transfer(10_000, &mint_keypair, &rando0.pubkey())
.transfer_and_confirm(10_000, &mint_keypair, &rando0.pubkey())
.unwrap_or_else(|_| panic!("{}:{}", line!(), file!()));
let instruction = create_invoke_instruction(rando0.pubkey(), program_id, &1u8);

View File

@ -79,7 +79,11 @@ impl AsyncClient for BankClient {
}
impl SyncClient for BankClient {
fn send_message<T: Signers>(&self, keypairs: &T, message: Message) -> Result<Signature> {
fn send_and_confirm_message<T: Signers>(
&self,
keypairs: &T,
message: Message,
) -> Result<Signature> {
let blockhash = self.bank.last_blockhash();
let transaction = Transaction::new(keypairs, message, blockhash);
self.bank.process_transaction(&transaction)?;
@ -87,16 +91,25 @@ impl SyncClient for BankClient {
}
/// Create and process a transaction from a single instruction.
fn send_instruction(&self, keypair: &Keypair, instruction: Instruction) -> Result<Signature> {
fn send_and_confirm_instruction(
&self,
keypair: &Keypair,
instruction: Instruction,
) -> Result<Signature> {
let message = Message::new(&[instruction], Some(&keypair.pubkey()));
self.send_message(&[keypair], message)
self.send_and_confirm_message(&[keypair], message)
}
/// Transfer `lamports` from `keypair` to `pubkey`
fn transfer(&self, lamports: u64, keypair: &Keypair, pubkey: &Pubkey) -> Result<Signature> {
fn transfer_and_confirm(
&self,
lamports: u64,
keypair: &Keypair,
pubkey: &Pubkey,
) -> Result<Signature> {
let transfer_instruction =
system_instruction::transfer(&keypair.pubkey(), pubkey, lamports);
self.send_instruction(keypair, transfer_instruction)
self.send_and_confirm_instruction(keypair, transfer_instruction)
}
fn get_account_data(&self, pubkey: &Pubkey) -> Result<Option<Vec<u8>>> {
@ -307,7 +320,9 @@ mod tests {
.push(AccountMeta::new(jane_pubkey, true));
let message = Message::new(&[transfer_instruction], Some(&john_pubkey));
bank_client.send_message(&doe_keypairs, message).unwrap();
bank_client
.send_and_confirm_message(&doe_keypairs, message)
.unwrap();
assert_eq!(bank_client.get_balance(&bob_pubkey).unwrap(), 42);
}
}

View File

@ -903,7 +903,7 @@ mod tests {
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
bank_client
.transfer(50, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
.unwrap();
let allocate_with_seed = Message::new(
@ -918,13 +918,13 @@ mod tests {
);
assert!(bank_client
.send_message(&[&alice_keypair], allocate_with_seed)
.send_and_confirm_message(&[&alice_keypair], allocate_with_seed)
.is_ok());
let allocate = system_instruction::allocate(&alice_pubkey, 2);
assert!(bank_client
.send_instruction(&alice_keypair, allocate)
.send_and_confirm_instruction(&alice_keypair, allocate)
.is_ok());
}
@ -952,7 +952,7 @@ mod tests {
let bank = Arc::new(Bank::new(&genesis_config));
let bank_client = BankClient::new_shared(&bank);
bank_client
.transfer(mint_lamports, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(mint_lamports, &mint_keypair, &alice_pubkey)
.unwrap();
// create zero-lamports account to be cleaned
@ -960,14 +960,14 @@ mod tests {
let bank_client = BankClient::new_shared(&bank);
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 0, len1, &program);
let message = Message::new(&[ix], Some(&alice_pubkey));
let r = bank_client.send_message(&[&alice_keypair, &bob_keypair], message);
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
assert!(r.is_ok());
// transfer some to bogus pubkey just to make previous bank (=slot) really cleanable
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
let bank_client = BankClient::new_shared(&bank);
bank_client
.transfer(50, &alice_keypair, &Pubkey::new_rand())
.transfer_and_confirm(50, &alice_keypair, &Pubkey::new_rand())
.unwrap();
// super fun time; callback chooses to .clean_accounts() or not
@ -978,7 +978,7 @@ mod tests {
let bank_client = BankClient::new_shared(&bank);
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 1, len2, &program);
let message = Message::new(&[ix], Some(&alice_pubkey));
let r = bank_client.send_message(&[&alice_keypair, &bob_keypair], message);
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
assert!(r.is_ok());
}
@ -1013,7 +1013,7 @@ mod tests {
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
bank_client
.transfer(50, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
.unwrap();
let assign_with_seed = Message::new(
@ -1027,7 +1027,7 @@ mod tests {
);
assert!(bank_client
.send_message(&[&alice_keypair], assign_with_seed)
.send_and_confirm_message(&[&alice_keypair], assign_with_seed)
.is_ok());
}
@ -1042,7 +1042,7 @@ mod tests {
let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank);
bank_client
.transfer(50, &alice_keypair, &mallory_pubkey)
.transfer_and_confirm(50, &alice_keypair, &mallory_pubkey)
.unwrap();
// Erroneously sign transaction with recipient account key
@ -1058,7 +1058,7 @@ mod tests {
);
assert_eq!(
bank_client
.send_instruction(&mallory_keypair, malicious_instruction)
.send_and_confirm_instruction(&mallory_keypair, malicious_instruction)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature)

View File

@ -26,7 +26,7 @@ pub fn load_program<T: Client>(
loader_pubkey,
);
bank_client
.send_message(
.send_and_confirm_message(
&[from_keypair, &program_keypair],
Message::new(&[instruction], Some(&from_keypair.pubkey())),
)
@ -39,7 +39,7 @@ pub fn load_program<T: Client>(
loader_instruction::write(&program_pubkey, loader_pubkey, offset, chunk.to_vec());
let message = Message::new(&[instruction], Some(&from_keypair.pubkey()));
bank_client
.send_message(&[from_keypair, &program_keypair], message)
.send_and_confirm_message(&[from_keypair, &program_keypair], message)
.unwrap();
offset += chunk_size as u32;
}
@ -47,7 +47,7 @@ pub fn load_program<T: Client>(
let instruction = loader_instruction::finalize(&program_pubkey, loader_pubkey);
let message = Message::new(&[instruction], Some(&from_keypair.pubkey()));
bank_client
.send_message(&[from_keypair, &program_keypair], message)
.send_and_confirm_message(&[from_keypair, &program_keypair], message)
.unwrap();
program_pubkey

View File

@ -906,7 +906,7 @@ mod tests {
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
bank_client
.transfer(50, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
.unwrap();
let allocate_with_seed = Message::new(
@ -921,13 +921,13 @@ mod tests {
);
assert!(bank_client
.send_message(&[&alice_keypair], allocate_with_seed)
.send_and_confirm_message(&[&alice_keypair], allocate_with_seed)
.is_ok());
let allocate = system_instruction::allocate(&alice_pubkey, 2);
assert!(bank_client
.send_instruction(&alice_keypair, allocate)
.send_and_confirm_instruction(&alice_keypair, allocate)
.is_ok());
}
@ -955,7 +955,7 @@ mod tests {
let bank = Arc::new(Bank::new(&genesis_config));
let bank_client = BankClient::new_shared(&bank);
bank_client
.transfer(mint_lamports, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(mint_lamports, &mint_keypair, &alice_pubkey)
.unwrap();
// create zero-lamports account to be cleaned
@ -963,14 +963,14 @@ mod tests {
let bank_client = BankClient::new_shared(&bank);
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 0, len1, &program);
let message = Message::new(&[ix], Some(&alice_keypair.pubkey()));
let r = bank_client.send_message(&[&alice_keypair, &bob_keypair], message);
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
assert!(r.is_ok());
// transfer some to bogus pubkey just to make previous bank (=slot) really cleanable
let bank = Arc::new(Bank::new_from_parent(&bank, &collector, bank.slot() + 1));
let bank_client = BankClient::new_shared(&bank);
bank_client
.transfer(50, &alice_keypair, &Pubkey::new_rand())
.transfer_and_confirm(50, &alice_keypair, &Pubkey::new_rand())
.unwrap();
// super fun time; callback chooses to .clean_accounts() or not
@ -981,7 +981,7 @@ mod tests {
let bank_client = BankClient::new_shared(&bank);
let ix = system_instruction::create_account(&alice_pubkey, &bob_pubkey, 1, len2, &program);
let message = Message::new(&[ix], Some(&alice_pubkey));
let r = bank_client.send_message(&[&alice_keypair, &bob_keypair], message);
let r = bank_client.send_and_confirm_message(&[&alice_keypair, &bob_keypair], message);
assert!(r.is_ok());
}
@ -1016,7 +1016,7 @@ mod tests {
let alice_with_seed = Pubkey::create_with_seed(&alice_pubkey, seed, &owner).unwrap();
bank_client
.transfer(50, &mint_keypair, &alice_pubkey)
.transfer_and_confirm(50, &mint_keypair, &alice_pubkey)
.unwrap();
let assign_with_seed = Message::new(
@ -1030,7 +1030,7 @@ mod tests {
);
assert!(bank_client
.send_message(&[&alice_keypair], assign_with_seed)
.send_and_confirm_message(&[&alice_keypair], assign_with_seed)
.is_ok());
}
@ -1045,7 +1045,7 @@ mod tests {
let bank = Bank::new(&genesis_config);
let bank_client = BankClient::new(bank);
bank_client
.transfer(50, &alice_keypair, &mallory_pubkey)
.transfer_and_confirm(50, &alice_keypair, &mallory_pubkey)
.unwrap();
// Erroneously sign transaction with recipient account key
@ -1061,7 +1061,7 @@ mod tests {
);
assert_eq!(
bank_client
.send_instruction(&mallory_keypair, malicious_instruction)
.send_and_confirm_instruction(&mallory_keypair, malicious_instruction)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, InstructionError::MissingRequiredSignature)

View File

@ -18,6 +18,6 @@ fn test_program_native_noop() {
let instruction = create_invoke_instruction(alice_keypair.pubkey(), program_id, &1u8);
let bank_client = BankClient::new(bank);
bank_client
.send_instruction(&alice_keypair, instruction)
.send_and_confirm_instruction(&alice_keypair, instruction)
.unwrap();
}

View File

@ -62,7 +62,7 @@ fn fill_epoch_with_votes(
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(&[mint_keypair, vote_keypair], message)
.send_and_confirm_message(&[mint_keypair, vote_keypair], message)
.is_ok());
}
bank
@ -134,7 +134,7 @@ fn test_stake_create_and_split_single_signature() {
// only one signature required
bank_client
.send_message(&[&staker_keypair], message)
.send_and_confirm_message(&[&staker_keypair], message)
.expect("failed to create and delegate stake account");
// split the stake
@ -155,7 +155,7 @@ fn test_stake_create_and_split_single_signature() {
);
assert!(bank_client
.send_message(&[&staker_keypair], message)
.send_and_confirm_message(&[&staker_keypair], message)
.is_ok());
// w00t!
@ -200,7 +200,7 @@ fn test_stake_create_and_split_to_existing_system_account() {
);
bank_client
.send_message(&[&staker_keypair], message)
.send_and_confirm_message(&[&staker_keypair], message)
.expect("failed to create and delegate stake account");
let split_stake_address =
@ -210,7 +210,7 @@ fn test_stake_create_and_split_to_existing_system_account() {
// First, put a system account where we want the new stake account
let existing_lamports = 42;
bank_client
.transfer(existing_lamports, &staker_keypair, &split_stake_address)
.transfer_and_confirm(existing_lamports, &staker_keypair, &split_stake_address)
.unwrap();
assert_eq!(
bank_client.get_balance(&split_stake_address).unwrap(),
@ -231,7 +231,7 @@ fn test_stake_create_and_split_to_existing_system_account() {
);
assert_eq!(
bank_client
.send_message(&[&staker_keypair], message)
.send_and_confirm_message(&[&staker_keypair], message)
.unwrap_err()
.unwrap(),
TransactionError::InstructionError(0, SystemError::AccountAlreadyInUse.into())
@ -277,7 +277,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
bank_client
.send_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&stake_pubkey);
@ -294,7 +294,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.expect("failed to create and delegate stake account");
// Test that correct lamports are staked
@ -318,7 +318,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
// Test that lamports are still staked
@ -381,7 +381,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(
.send_and_confirm_message(
&[&mint_keypair, &stake_keypair, &split_stake_keypair],
message
)
@ -396,7 +396,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
let split_staked = get_staked(&bank, &split_stake_pubkey);
@ -413,7 +413,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
let mut bank = next_epoch(&bank);
@ -436,7 +436,7 @@ fn test_stake_account_lifetime() {
);
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_err());
// but we can withdraw unstaked
@ -452,7 +452,7 @@ fn test_stake_account_lifetime() {
);
// assert we can withdraw unstaked tokens
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
// finish cooldown
@ -476,7 +476,7 @@ fn test_stake_account_lifetime() {
Some(&mint_pubkey),
);
assert!(bank_client
.send_message(&[&mint_keypair, &stake_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &stake_keypair], message)
.is_ok());
// verify all the math sums to zero
@ -521,7 +521,7 @@ fn test_create_stake_account_from_seed() {
Some(&mint_pubkey),
);
bank_client
.send_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.send_and_confirm_message(&[&mint_keypair, &vote_keypair, &identity_keypair], message)
.expect("failed to create vote account");
let authorized = stake_state::Authorized::auto(&mint_pubkey);
@ -540,7 +540,7 @@ fn test_create_stake_account_from_seed() {
Some(&mint_pubkey),
);
bank_client
.send_message(&[&mint_keypair], message)
.send_and_confirm_message(&[&mint_keypair], message)
.expect("failed to create and delegate stake account");
// Test that correct lamports are staked